blob: a376d00a5142f322cbb61e1067e7c68ba03fc17c [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaard3d8fee2021-06-30 19:54:43 +020029 int tcd_in_catch; // in catch or finally block
30 int tcd_did_throw; // set did_throw in :endtry
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +010031 int tcd_catch_idx; // instruction of the first :catch or :finally
32 int tcd_finally_idx; // instruction of the :finally block or zero
33 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_caught; // catch block entered
Bram Moolenaar2e34c342021-03-14 12:13:33 +010035 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010036 int tcd_return; // when TRUE return from end of :finally
37} trycmd_T;
38
Bram Moolenaar4c137212021-04-19 16:48:48 +020039// Data local to a function.
40// On a function call, if not empty, is saved on the stack and restored when
41// returning.
42typedef struct {
43 int floc_restore_cmdmod;
44 cmdmod_T floc_save_cmdmod;
45 int floc_restore_cmdmod_stacklen;
46} funclocal_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010047
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020048// Structure to hold a reference to an outer_T, with information of whether it
49// was allocated.
50typedef struct {
51 outer_T *or_outer;
52 partial_T *or_partial; // decrement "or_partial->pt_refcount" later
53 int or_outer_allocated; // free "or_outer" later
54} outer_ref_T;
55
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010056// A stack is used to store:
57// - arguments passed to a :def function
58// - info about the calling function, to use when returning
59// - local variables
60// - temporary values
61//
62// In detail (FP == Frame Pointer):
63// arg1 first argument from caller (if present)
64// arg2 second argument from caller (if present)
65// extra_arg1 any missing optional argument default value
66// FP -> cur_func calling function
67// current previous instruction pointer
68// frame_ptr previous Frame Pointer
69// var1 space for local variable
70// var2 space for local variable
71// .... fixed space for max. number of local variables
72// temp temporary values
73// .... flexible space for temporary values (can grow big)
74
75/*
76 * Execution context.
77 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010078struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010079 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020080 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar4c137212021-04-19 16:48:48 +020081 int ec_initial_frame_idx; // frame index when called
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082
Bram Moolenaarc04f2a42021-06-09 19:30:03 +020083 outer_ref_T *ec_outer_ref; // outer scope used for closures, allocated
Bram Moolenaar4c137212021-04-19 16:48:48 +020084 funclocal_T ec_funclocal;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020085
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010086 garray_T ec_trystack; // stack of trycmd_T values
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010087
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010088 isn_T *ec_instr; // array with instructions
Dominique Pelle3276f582021-08-07 12:44:41 +020089 int ec_dfunc_idx; // current function index
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020091
92 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar4c137212021-04-19 16:48:48 +020093
94 int ec_did_emsg_before;
95 int ec_trylevel_at_start;
96 where_T ec_where;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010097};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010098
Bram Moolenaar12d26532021-02-19 19:13:21 +010099#ifdef FEAT_PROFILE
100// stack of profinfo_T used when profiling.
101static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
102#endif
103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100104// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +0200105#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100106
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200107 void
108to_string_error(vartype_T vartype)
109{
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200110 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +0200111}
112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100113/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100114 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100115 */
116 static int
117ufunc_argcount(ufunc_T *ufunc)
118{
119 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
120}
121
122/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200123 * Create a new list from "count" items at the bottom of the stack.
124 * When "count" is zero an empty list is added to the stack.
125 */
126 static int
127exe_newlist(int count, ectx_T *ectx)
128{
129 list_T *list = list_alloc_with_items(count);
130 int idx;
131 typval_T *tv;
132
133 if (list == NULL)
134 return FAIL;
135 for (idx = 0; idx < count; ++idx)
136 list_set_item(list, idx, STACK_TV_BOT(idx - count));
137
138 if (count > 0)
139 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200140 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarfe270812020-04-11 22:31:27 +0200141 return FAIL;
142 else
143 ++ectx->ec_stack.ga_len;
144 tv = STACK_TV_BOT(-1);
145 tv->v_type = VAR_LIST;
146 tv->vval.v_list = list;
147 ++list->lv_refcount;
148 return OK;
149}
150
151/*
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200152 * If debug_tick changed check if "ufunc" has a breakpoint and update
153 * "uf_has_breakpoint".
154 */
155 static void
156update_has_breakpoint(ufunc_T *ufunc)
157{
158 if (ufunc->uf_debug_tick != debug_tick)
159 {
160 linenr_T breakpoint;
161
162 ufunc->uf_debug_tick = debug_tick;
163 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name, 0);
164 ufunc->uf_has_breakpoint = breakpoint > 0;
165 }
166}
167
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200168static garray_T dict_stack = GA_EMPTY;
169
170/*
171 * Put a value on the dict stack. This consumes "tv".
172 */
173 static int
174dict_stack_save(typval_T *tv)
175{
176 if (dict_stack.ga_growsize == 0)
177 ga_init2(&dict_stack, (int)sizeof(typval_T), 10);
178 if (ga_grow(&dict_stack, 1) == FAIL)
179 return FAIL;
180 ((typval_T *)dict_stack.ga_data)[dict_stack.ga_len] = *tv;
181 ++dict_stack.ga_len;
182 return OK;
183}
184
185/*
186 * Get the typval at top of the dict stack.
187 */
188 static typval_T *
189dict_stack_get_tv(void)
190{
191 if (dict_stack.ga_len == 0)
192 return NULL;
193 return ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
194}
195
196/*
197 * Get the dict at top of the dict stack.
198 */
199 static dict_T *
200dict_stack_get_dict(void)
201{
202 typval_T *tv;
203
204 if (dict_stack.ga_len == 0)
205 return NULL;
206 tv = ((typval_T *)dict_stack.ga_data) + dict_stack.ga_len - 1;
207 if (tv->v_type == VAR_DICT)
208 return tv->vval.v_dict;
209 return NULL;
210}
211
212/*
213 * Drop an item from the dict stack.
214 */
215 static void
216dict_stack_drop(void)
217{
218 if (dict_stack.ga_len == 0)
219 {
220 iemsg("Dict stack underflow");
221 return;
222 }
223 --dict_stack.ga_len;
224 clear_tv(((typval_T *)dict_stack.ga_data) + dict_stack.ga_len);
225}
226
227/*
228 * Drop items from the dict stack until the length is equal to "len".
229 */
230 static void
231dict_stack_clear(int len)
232{
233 while (dict_stack.ga_len > len)
234 dict_stack_drop();
235}
236
Bram Moolenaar4f8f5422021-06-20 19:28:14 +0200237/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100239 * This adds a stack frame and sets the instruction pointer to the start of the
240 * called function.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200241 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 *
243 * Stack has:
244 * - current arguments (already there)
245 * - omitted optional argument (default values) added here
246 * - stack frame:
247 * - pointer to calling function
248 * - Index of next instruction in calling function
249 * - previous frame pointer
250 * - reserved space for local variables
251 */
252 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100253call_dfunc(
254 int cdf_idx,
255 partial_T *pt,
256 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100257 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258{
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100259 int argcount = argcount_arg;
260 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
261 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200262 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100263 int arg_to_add;
264 int vararg_count = 0;
265 int varcount;
266 int idx;
267 estack_T *entry;
268 funclocal_T *floc = NULL;
Bram Moolenaar648594e2021-07-11 17:55:01 +0200269 int res = OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270
271 if (dfunc->df_deleted)
272 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100273 // don't use ufunc->uf_name, it may have been freed
274 emsg_funcname(e_func_deleted,
275 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276 return FAIL;
277 }
278
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100279#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100280 if (do_profiling == PROF_YES)
281 {
Bram Moolenaar35578162021-08-02 19:10:38 +0200282 if (GA_GROW_OK(&profile_info_ga, 1))
Bram Moolenaar12d26532021-02-19 19:13:21 +0100283 {
284 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
285 + profile_info_ga.ga_len;
286 ++profile_info_ga.ga_len;
287 CLEAR_POINTER(info);
288 profile_may_start_func(info, ufunc,
289 (((dfunc_T *)def_functions.ga_data)
290 + ectx->ec_dfunc_idx)->df_ufunc);
291 }
Bram Moolenaar12d26532021-02-19 19:13:21 +0100292 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100293#endif
294
Bram Moolenaar2ac4b252021-06-20 20:09:42 +0200295 // Update uf_has_breakpoint if needed.
296 update_has_breakpoint(ufunc);
297
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200298 // When debugging and using "cont" switches to the not-debugged
299 // instructions, may need to still compile them.
Bram Moolenaar648594e2021-07-11 17:55:01 +0200300 if (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc)))
301 {
302 res = compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL);
303
304 // compile_def_function() may cause def_functions.ga_data to change
305 dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
306 }
307 if (res == FAIL || INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +0200308 {
309 if (did_emsg_cumul + did_emsg == did_emsg_before)
310 semsg(_(e_function_is_not_compiled_str),
311 printable_func_name(ufunc));
312 return FAIL;
313 }
314
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200315 if (ufunc->uf_va_name != NULL)
316 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200317 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200318 // Stack at time of call with 2 varargs:
319 // normal_arg
320 // optional_arg
321 // vararg_1
322 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200323 // After creating the list:
324 // normal_arg
325 // optional_arg
326 // vararg-list
327 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200328 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200329 // After creating the list
330 // normal_arg
331 // (space for optional_arg)
332 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200333 vararg_count = argcount - ufunc->uf_args.ga_len;
334 if (vararg_count < 0)
335 vararg_count = 0;
336 else
337 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200338 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200339 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200340
341 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200342 }
343
Bram Moolenaarfe270812020-04-11 22:31:27 +0200344 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200345 if (arg_to_add < 0)
346 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200347 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200348 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200349 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200350 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200351 return FAIL;
352 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200353
354 // Reserve space for:
355 // - missing arguments
356 // - stack frame
357 // - local variables
358 // - if needed: a counter for number of closures created in
359 // ectx->ec_funcrefs.
360 varcount = dfunc->df_varcount + dfunc->df_has_closure;
Bram Moolenaar35578162021-08-02 19:10:38 +0200361 if (GA_GROW_FAILS(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 return FAIL;
363
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100364 // If depth of calling is getting too high, don't execute the function.
365 if (funcdepth_increment() == FAIL)
366 return FAIL;
Bram Moolenaare99d4222021-06-13 14:01:26 +0200367 ++ex_nesting_level;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100368
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100369 // Only make a copy of funclocal if it contains something to restore.
Bram Moolenaar4c137212021-04-19 16:48:48 +0200370 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100371 {
372 floc = ALLOC_ONE(funclocal_T);
373 if (floc == NULL)
374 return FAIL;
Bram Moolenaar4c137212021-04-19 16:48:48 +0200375 *floc = ectx->ec_funclocal;
376 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100377 }
378
Bram Moolenaarfe270812020-04-11 22:31:27 +0200379 // Move the vararg-list to below the missing optional arguments.
380 if (vararg_count > 0 && arg_to_add > 0)
381 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100382
383 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200384 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200385 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200386 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387
388 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100389 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
390 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200391 STACK_TV_BOT(STACK_FRAME_INSTR_OFF)->vval.v_string = (void *)ectx->ec_instr;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200392 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string =
393 (void *)ectx->ec_outer_ref;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100394 STACK_TV_BOT(STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string = (void *)floc;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100395 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200396 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100397
398 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100400 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 if (dfunc->df_has_closure)
402 {
403 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
404
405 tv->v_type = VAR_NUMBER;
406 tv->vval.v_number = 0;
407 }
408 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100410 if (pt != NULL || ufunc->uf_partial != NULL
411 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100412 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200413 outer_ref_T *ref = ALLOC_CLEAR_ONE(outer_ref_T);
Bram Moolenaar0186e582021-01-10 18:33:11 +0100414
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200415 if (ref == NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100416 return FAIL;
417 if (pt != NULL)
418 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200419 ref->or_outer = &pt->pt_outer;
420 ++pt->pt_refcount;
421 ref->or_partial = pt;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100422 }
423 else if (ufunc->uf_partial != NULL)
424 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200425 ref->or_outer = &ufunc->uf_partial->pt_outer;
426 ++ufunc->uf_partial->pt_refcount;
427 ref->or_partial = ufunc->uf_partial;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100428 }
429 else
430 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200431 ref->or_outer = ALLOC_CLEAR_ONE(outer_T);
Dominique Pelle5a9e5842021-07-24 19:32:12 +0200432 if (unlikely(ref->or_outer == NULL))
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200433 {
434 vim_free(ref);
435 return FAIL;
436 }
437 ref->or_outer_allocated = TRUE;
438 ref->or_outer->out_stack = &ectx->ec_stack;
439 ref->or_outer->out_frame_idx = ectx->ec_frame_idx;
440 if (ectx->ec_outer_ref != NULL)
441 ref->or_outer->out_up = ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100442 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200443 ectx->ec_outer_ref = ref;
Bram Moolenaarab360522021-01-10 14:02:28 +0100444 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100445 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200446 ectx->ec_outer_ref = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100447
Bram Moolenaarc970e422021-03-17 15:03:04 +0100448 ++ufunc->uf_calls;
449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 // Set execution state to the start of the called function.
451 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100452 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100453 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200454 if (entry != NULL)
455 {
456 // Set the script context to the script where the function was defined.
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200457 // Save the current context so it can be restored on return.
458 entry->es_save_sctx = current_sctx;
459 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200460 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100461
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +0200462 // Start execution at the first instruction.
463 ectx->ec_iidx = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464
465 return OK;
466}
467
468// Get pointer to item in the stack.
469#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
470
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000471// Double linked list of funcstack_T in use.
472static funcstack_T *first_funcstack = NULL;
473
474 static void
475add_funcstack_to_list(funcstack_T *funcstack)
476{
477 // Link in list of funcstacks.
478 if (first_funcstack != NULL)
479 first_funcstack->fs_prev = funcstack;
480 funcstack->fs_next = first_funcstack;
481 funcstack->fs_prev = NULL;
482 first_funcstack = funcstack;
483}
484
485 static void
486remove_funcstack_from_list(funcstack_T *funcstack)
487{
488 if (funcstack->fs_prev == NULL)
489 first_funcstack = funcstack->fs_next;
490 else
491 funcstack->fs_prev->fs_next = funcstack->fs_next;
492 if (funcstack->fs_next != NULL)
493 funcstack->fs_next->fs_prev = funcstack->fs_prev;
494}
495
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 * Used when returning from a function: Check if any closure is still
498 * referenced. If so then move the arguments and variables to a separate piece
499 * of stack to be used when the closure is called.
500 * When "free_arguments" is TRUE the arguments are to be freed.
501 * Returns FAIL when out of memory.
502 */
503 static int
504handle_closure_in_use(ectx_T *ectx, int free_arguments)
505{
506 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
507 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200508 int argcount;
509 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200510 int idx;
511 typval_T *tv;
512 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200513 garray_T *gap = &ectx->ec_funcrefs;
514 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200516 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200517 return OK; // function was freed
518 if (dfunc->df_has_closure == 0)
519 return OK; // no closures
520 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
521 closure_count = tv->vval.v_number;
522 if (closure_count == 0)
523 return OK; // no funcrefs created
524
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200525 argcount = ufunc_argcount(dfunc->df_ufunc);
526 top = ectx->ec_frame_idx - argcount;
527
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200528 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200529 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200531 partial_T *pt;
532 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200533
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200534 if (off < 0)
535 continue; // count is off or already done
536 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200537 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200538 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200539 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200540 int i;
541
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200542 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200543 // unreferenced on return.
544 for (i = 0; i < dfunc->df_varcount; ++i)
545 {
546 typval_T *stv = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200548 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200549 --refcount;
550 }
551 if (refcount > 1)
552 {
553 closure_in_use = TRUE;
554 break;
555 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200556 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557 }
558
559 if (closure_in_use)
560 {
561 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
562 typval_T *stack;
563
564 // A closure is using the arguments and/or local variables.
565 // Move them to the called function.
566 if (funcstack == NULL)
567 return FAIL;
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000568
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200569 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
570 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200571 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
572 funcstack->fs_ga.ga_data = stack;
573 if (stack == NULL)
574 {
575 vim_free(funcstack);
576 return FAIL;
577 }
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000578 add_funcstack_to_list(funcstack);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200579
580 // Move or copy the arguments.
581 for (idx = 0; idx < argcount; ++idx)
582 {
583 tv = STACK_TV(top + idx);
584 if (free_arguments)
585 {
586 *(stack + idx) = *tv;
587 tv->v_type = VAR_UNKNOWN;
588 }
589 else
590 copy_tv(tv, stack + idx);
591 }
592 // Move the local variables.
593 for (idx = 0; idx < dfunc->df_varcount; ++idx)
594 {
595 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200596
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200597 // A partial created for a local function, that is also used as a
598 // local variable, has a reference count for the variable, thus
599 // will never go down to zero. When all these refcounts are one
600 // then the funcstack is unused. We need to count how many we have
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000601 // so we know when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200602 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
603 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200604 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200605
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200606 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200607 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
608 gap->ga_len - closure_count + i])
609 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200610 }
611
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200612 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200613 tv->v_type = VAR_UNKNOWN;
614 }
615
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200616 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200618 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
619 - closure_count + idx];
620 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200621 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200622 ++funcstack->fs_refcount;
623 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100624 pt->pt_outer.out_stack = &funcstack->fs_ga;
625 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200626 }
627 }
628 }
629
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200630 for (idx = 0; idx < closure_count; ++idx)
631 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
632 - closure_count + idx]);
633 gap->ga_len -= closure_count;
634 if (gap->ga_len == 0)
635 ga_clear(gap);
636
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637 return OK;
638}
639
640/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200641 * Called when a partial is freed or its reference count goes down to one. The
642 * funcstack may be the only reference to the partials in the local variables.
643 * Go over all of them, the funcref and can be freed if all partials
644 * referencing the funcstack have a reference count of one.
645 */
646 void
647funcstack_check_refcount(funcstack_T *funcstack)
648{
649 int i;
650 garray_T *gap = &funcstack->fs_ga;
651 int done = 0;
652
653 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
654 return;
655 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
656 {
657 typval_T *tv = ((typval_T *)gap->ga_data) + i;
658
659 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
660 && tv->vval.v_partial->pt_funcstack == funcstack
661 && tv->vval.v_partial->pt_refcount == 1)
662 ++done;
663 }
664 if (done == funcstack->fs_min_refcount)
665 {
666 typval_T *stack = gap->ga_data;
667
668 // All partials referencing the funcstack have a reference count of
669 // one, thus the funcstack is no longer of use.
670 for (i = 0; i < gap->ga_len; ++i)
671 clear_tv(stack + i);
672 vim_free(stack);
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000673 remove_funcstack_from_list(funcstack);
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200674 vim_free(funcstack);
675 }
676}
677
678/*
Bram Moolenaar7509ad82021-12-14 18:14:37 +0000679 * For garbage collecting: set references in all variables referenced by
680 * all funcstacks.
681 */
682 int
683set_ref_in_funcstacks(int copyID)
684{
685 funcstack_T *funcstack;
686
687 for (funcstack = first_funcstack; funcstack != NULL;
688 funcstack = funcstack->fs_next)
689 {
690 typval_T *stack = funcstack->fs_ga.ga_data;
691 int i;
692
693 for (i = 0; i < funcstack->fs_ga.ga_len; ++i)
694 if (set_ref_in_item(stack + i, copyID, NULL, NULL))
695 return TRUE; // abort
696 }
697 return FALSE;
698}
699
700/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 * Return from the current function.
702 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200703 static int
Bram Moolenaar4c137212021-04-19 16:48:48 +0200704func_return(ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100707 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200708 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
709 + ectx->ec_dfunc_idx;
710 int argcount = ufunc_argcount(dfunc->df_ufunc);
711 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200712 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100713 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
714 + STACK_FRAME_FUNC_OFF)->vval.v_number;
Bram Moolenaarb06b50d2021-04-26 21:39:25 +0200715 funclocal_T *floc;
716#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100717 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
718 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719
Bram Moolenaar12d26532021-02-19 19:13:21 +0100720 if (do_profiling == PROF_YES)
721 {
722 ufunc_T *caller = prev_dfunc->df_ufunc;
723
724 if (dfunc->df_ufunc->uf_profiling
725 || (caller != NULL && caller->uf_profiling))
726 {
727 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
728 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
729 --profile_info_ga.ga_len;
730 }
731 }
732#endif
Bram Moolenaar919c12c2021-12-14 14:29:16 +0000733
734 // No check for uf_refcount being zero, cannot think of a way that would
735 // happen.
Bram Moolenaarc970e422021-03-17 15:03:04 +0100736 --dfunc->df_ufunc->uf_calls;
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200739 entry = estack_pop();
740 if (entry != NULL)
Bram Moolenaarc70fe462021-04-17 17:59:19 +0200741 current_sctx = entry->es_save_sctx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100742
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (handle_closure_in_use(ectx, TRUE) == FAIL)
744 return FAIL;
745
746 // Clear the arguments.
747 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
748 clear_tv(STACK_TV(idx));
749
750 // Clear local variables and temp values, but not the return value.
751 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100752 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100754
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100755 // The return value should be on top of the stack. However, when aborting
756 // it may not be there and ec_frame_idx is the top of the stack.
757 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100758 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100759 ret_idx = 0;
760
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200761 if (ectx->ec_outer_ref != NULL)
762 {
763 if (ectx->ec_outer_ref->or_outer_allocated)
764 vim_free(ectx->ec_outer_ref->or_outer);
765 partial_unref(ectx->ec_outer_ref->or_partial);
766 vim_free(ectx->ec_outer_ref);
767 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100768
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100769 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100770 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100771 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
772 + STACK_FRAME_IIDX_OFF)->vval.v_number;
Bram Moolenaar5930ddc2021-04-26 20:32:59 +0200773 ectx->ec_instr = (void *)STACK_TV(ectx->ec_frame_idx
774 + STACK_FRAME_INSTR_OFF)->vval.v_string;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200775 ectx->ec_outer_ref = (void *)STACK_TV(ectx->ec_frame_idx
Bram Moolenaar0186e582021-01-10 18:33:11 +0100776 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100777 floc = (void *)STACK_TV(ectx->ec_frame_idx
778 + STACK_FRAME_FUNCLOCAL_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200779 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100780 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
781 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaard386e922021-04-25 14:48:49 +0200782
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100783 if (floc == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +0200784 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100785 else
786 {
Bram Moolenaar4c137212021-04-19 16:48:48 +0200787 ectx->ec_funclocal = *floc;
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100788 vim_free(floc);
789 }
790
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100791 if (ret_idx > 0)
792 {
793 // Reset the stack to the position before the call, with a spot for the
794 // return value, moved there from above the frame.
795 ectx->ec_stack.ga_len = top + 1;
796 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
797 }
798 else
799 // Reset the stack to the position before the call.
800 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100802 funcdepth_decrement();
Bram Moolenaare99d4222021-06-13 14:01:26 +0200803 --ex_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200804 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805}
806
807#undef STACK_TV
808
809/*
810 * Prepare arguments and rettv for calling a builtin or user function.
811 */
812 static int
813call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
814{
815 int idx;
816 typval_T *tv;
817
818 // Move arguments from bottom of the stack to argvars[] and add terminator.
819 for (idx = 0; idx < argcount; ++idx)
820 argvars[idx] = *STACK_TV_BOT(idx - argcount);
821 argvars[argcount].v_type = VAR_UNKNOWN;
822
823 // Result replaces the arguments on the stack.
824 if (argcount > 0)
825 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +0200826 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 return FAIL;
828 else
829 ++ectx->ec_stack.ga_len;
830
831 // Default return value is zero.
832 tv = STACK_TV_BOT(-1);
833 tv->v_type = VAR_NUMBER;
834 tv->vval.v_number = 0;
835
836 return OK;
837}
838
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200839// Ugly global to avoid passing the execution context around through many
840// layers.
841static ectx_T *current_ectx = NULL;
842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843/*
844 * Call a builtin function by index.
845 */
846 static int
847call_bfunc(int func_idx, int argcount, ectx_T *ectx)
848{
849 typval_T argvars[MAX_FUNC_ARGS];
850 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100851 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200852 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200853 char *save_func_name = ectx->ec_where.wt_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854
855 if (call_prepare(argcount, argvars, ectx) == FAIL)
856 return FAIL;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200857 ectx->ec_where.wt_func_name = internal_func_name(func_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200859 // Call the builtin function. Set "current_ectx" so that when it
860 // recursively invokes call_def_function() a closure context can be set.
861 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200863 current_ectx = prev_ectx;
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +0200864 ectx->ec_where.wt_func_name = save_func_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865
866 // Clear the arguments.
867 for (idx = 0; idx < argcount; ++idx)
868 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200869
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100870 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200871 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872 return OK;
873}
874
875/*
876 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100877 * If the function is compiled this will add a stack frame and set the
878 * instruction pointer at the start of the function.
879 * Otherwise the function is called here.
Bram Moolenaarc04f2a42021-06-09 19:30:03 +0200880 * If "pt" is not null use "pt->pt_outer" for ec_outer_ref->or_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100881 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 */
883 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100884call_ufunc(
885 ufunc_T *ufunc,
886 partial_T *pt,
887 int argcount,
888 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200889 isn_T *iptr,
890 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891{
892 typval_T argvars[MAX_FUNC_ARGS];
893 funcexe_T funcexe;
894 int error;
895 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100896 int did_emsg_before = did_emsg;
Bram Moolenaar968a5b62021-06-15 19:32:40 +0200897 compiletype_T compile_type = COMPILE_TYPE(ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898
Bram Moolenaare99d4222021-06-13 14:01:26 +0200899 if (func_needs_compiling(ufunc, compile_type)
900 && compile_def_function(ufunc, FALSE, compile_type, NULL)
901 == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200902 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200903 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100904 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100905 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100906 if (error != FCERR_UNKNOWN)
907 {
908 if (error == FCERR_TOOMANY)
Bram Moolenaare1242042021-12-16 20:56:57 +0000909 semsg(_(e_too_many_arguments_for_function_str), ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100910 else
Bram Moolenaare1242042021-12-16 20:56:57 +0000911 semsg(_(e_not_enough_arguments_for_function_str),
912 ufunc->uf_name);
Bram Moolenaar50824712020-12-20 21:10:17 +0100913 return FAIL;
914 }
915
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100916 // The function has been compiled, can call it quickly. For a function
917 // that was defined later: we can call it directly next time.
918 if (iptr != NULL)
919 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100920 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100921 iptr->isn_type = ISN_DCALL;
922 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
923 iptr->isn_arg.dfunc.cdf_argcount = argcount;
924 }
Bram Moolenaar4c137212021-04-19 16:48:48 +0200925 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927
928 if (call_prepare(argcount, argvars, ectx) == FAIL)
929 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200930 CLEAR_FIELD(funcexe);
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000931 funcexe.fe_evaluate = TRUE;
932 funcexe.fe_selfdict = selfdict != NULL ? selfdict : dict_stack_get_dict();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
934 // Call the user function. Result goes in last position on the stack.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 error = call_user_func_check(ufunc, argcount, argvars,
Bram Moolenaar851f86b2021-12-13 14:26:44 +0000936 STACK_TV_BOT(-1), &funcexe, funcexe.fe_selfdict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937
938 // Clear the arguments.
939 for (idx = 0; idx < argcount; ++idx)
940 clear_tv(&argvars[idx]);
941
942 if (error != FCERR_NONE)
943 {
Bram Moolenaar2ef91562021-12-11 16:14:07 +0000944 user_func_error(error, ufunc->uf_name, &funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100945 return FAIL;
946 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100947 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200948 // Error other than from calling the function itself.
949 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 return OK;
951}
952
953/*
Bram Moolenaara91a7132021-03-25 21:12:15 +0100954 * If command modifiers were applied restore them.
955 */
956 static void
957may_restore_cmdmod(funclocal_T *funclocal)
958{
959 if (funclocal->floc_restore_cmdmod)
960 {
961 cmdmod.cmod_filter_regmatch.regprog = NULL;
962 undo_cmdmod(&cmdmod);
963 cmdmod = funclocal->floc_save_cmdmod;
964 funclocal->floc_restore_cmdmod = FALSE;
965 }
966}
967
968/*
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200969 * Return TRUE if an error was given (not caught in try/catch) or CTRL-C was
970 * pressed.
Bram Moolenaara1773442020-08-12 15:21:22 +0200971 */
972 static int
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200973vim9_aborting(int prev_uncaught_emsg)
Bram Moolenaara1773442020-08-12 15:21:22 +0200974{
Bram Moolenaar88c89c72021-08-14 14:01:05 +0200975 return uncaught_emsg > prev_uncaught_emsg || got_int || did_throw;
Bram Moolenaara1773442020-08-12 15:21:22 +0200976}
977
978/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 * Execute a function by "name".
980 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100981 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982 * Returns FAIL if not found without an error message.
983 */
984 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100985call_by_name(
986 char_u *name,
987 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +0100988 ectx_T *ectx,
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +0200989 isn_T *iptr,
990 dict_T *selfdict)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991{
992 ufunc_T *ufunc;
993
994 if (builtin_function(name, -1))
995 {
996 int func_idx = find_internal_func(name);
997
998 if (func_idx < 0)
999 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +02001000 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 return FAIL;
1002 return call_bfunc(func_idx, argcount, ectx);
1003 }
1004
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001005 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +02001006
1007 if (ufunc == NULL)
1008 {
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001009 int prev_uncaught_emsg = uncaught_emsg;
Bram Moolenaara1773442020-08-12 15:21:22 +02001010
1011 if (script_autoload(name, TRUE))
1012 // loaded a package, search for the function again
1013 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar88c89c72021-08-14 14:01:05 +02001014
1015 if (vim9_aborting(prev_uncaught_emsg))
Bram Moolenaara1773442020-08-12 15:21:22 +02001016 return FAIL; // bail out if loading the script caused an error
1017 }
1018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001020 {
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001021 if (ufunc->uf_arg_types != NULL || ufunc->uf_va_type != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001022 {
1023 int i;
1024 typval_T *argv = STACK_TV_BOT(0) - argcount;
1025
1026 // The function can change at runtime, check that the argument
1027 // types are correct.
1028 for (i = 0; i < argcount; ++i)
1029 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001030 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001031
Bram Moolenaar6ce46b92021-08-07 15:35:36 +02001032 if (i < ufunc->uf_args.ga_len && ufunc->uf_arg_types != NULL)
Bram Moolenaare3ffcd92021-03-08 21:47:13 +01001033 type = ufunc->uf_arg_types[i];
1034 else if (ufunc->uf_va_type != NULL)
1035 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001036 if (type != NULL && check_typval_arg_type(type,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02001037 &argv[i], NULL, i + 1) == FAIL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001038 return FAIL;
1039 }
1040 }
1041
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001042 return call_ufunc(ufunc, NULL, argcount, ectx, iptr, selfdict);
Bram Moolenaar04947cc2021-03-06 19:26:46 +01001043 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
1045 return FAIL;
1046}
1047
1048 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001049call_partial(
1050 typval_T *tv,
1051 int argcount_arg,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001052 ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053{
Bram Moolenaara90afb92020-07-15 22:38:56 +02001054 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001055 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001057 int res = FAIL;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001058 dict_T *selfdict = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059
1060 if (tv->v_type == VAR_PARTIAL)
1061 {
Bram Moolenaara90afb92020-07-15 22:38:56 +02001062 partial_T *pt = tv->vval.v_partial;
1063 int i;
1064
1065 if (pt->pt_argc > 0)
1066 {
1067 // Make space for arguments from the partial, shift the "argcount"
1068 // arguments up.
Bram Moolenaar35578162021-08-02 19:10:38 +02001069 if (GA_GROW_FAILS(&ectx->ec_stack, pt->pt_argc))
Bram Moolenaara90afb92020-07-15 22:38:56 +02001070 return FAIL;
1071 for (i = 1; i <= argcount; ++i)
1072 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
1073 ectx->ec_stack.ga_len += pt->pt_argc;
1074 argcount += pt->pt_argc;
1075
1076 // copy the arguments from the partial onto the stack
1077 for (i = 0; i < pt->pt_argc; ++i)
1078 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
1079 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001080 selfdict = pt->pt_dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081
1082 if (pt->pt_func != NULL)
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001083 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL, selfdict);
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 name = pt->pt_name;
1086 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +02001089 if (name != NULL)
1090 {
1091 char_u fname_buf[FLEN_FIXED + 1];
1092 char_u *tofree = NULL;
1093 int error = FCERR_NONE;
1094 char_u *fname;
1095
1096 // May need to translate <SNR>123_ to K_SNR.
1097 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1098 if (error != FCERR_NONE)
1099 res = FAIL;
1100 else
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001101 res = call_by_name(fname, argcount, ectx, NULL, selfdict);
Bram Moolenaar95006e32020-08-29 17:47:08 +02001102 vim_free(tofree);
1103 }
1104
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +01001105 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 {
1107 if (called_emsg == called_emsg_before)
Bram Moolenaare1242042021-12-16 20:56:57 +00001108 semsg(_(e_unknown_function_str),
Bram Moolenaar015f4262020-05-05 21:25:22 +02001109 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 return FAIL;
1111 }
1112 return OK;
1113}
1114
1115/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001116 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
1117 * TRUE.
1118 */
1119 static int
1120error_if_locked(int lock, char *error)
1121{
1122 if (lock & (VAR_LOCKED | VAR_FIXED))
1123 {
1124 emsg(_(error));
1125 return TRUE;
1126 }
1127 return FALSE;
1128}
1129
1130/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01001131 * Give an error if "tv" is not a number and return FAIL.
1132 */
1133 static int
1134check_for_number(typval_T *tv)
1135{
1136 if (tv->v_type != VAR_NUMBER)
1137 {
1138 semsg(_(e_expected_str_but_got_str),
1139 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
1140 return FAIL;
1141 }
1142 return OK;
1143}
1144
1145/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001146 * Store "tv" in variable "name".
1147 * This is for s: and g: variables.
1148 */
1149 static void
1150store_var(char_u *name, typval_T *tv)
1151{
1152 funccal_entry_T entry;
Bram Moolenaard877a572021-04-01 19:42:48 +02001153 int flags = ASSIGN_DECL;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001154
Bram Moolenaard877a572021-04-01 19:42:48 +02001155 if (tv->v_lock)
1156 flags |= ASSIGN_CONST;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001157 save_funccal(&entry);
Bram Moolenaard877a572021-04-01 19:42:48 +02001158 set_var_const(name, NULL, tv, FALSE, flags, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001159 restore_funccal();
1160}
1161
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001162/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001163 * Convert "tv" to a string.
1164 * Return FAIL if not allowed.
1165 */
1166 static int
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001167do_2string(typval_T *tv, int is_2string_any, int tolerant)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001168{
1169 if (tv->v_type != VAR_STRING)
1170 {
1171 char_u *str;
1172
1173 if (is_2string_any)
1174 {
1175 switch (tv->v_type)
1176 {
1177 case VAR_SPECIAL:
1178 case VAR_BOOL:
1179 case VAR_NUMBER:
1180 case VAR_FLOAT:
1181 case VAR_BLOB: break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001182
1183 case VAR_LIST:
1184 if (tolerant)
1185 {
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001186 char_u *s, *e, *p;
1187 garray_T ga;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001188
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001189 ga_init2(&ga, sizeof(char_u *), 1);
1190
1191 // Convert to NL separated items, then
1192 // escape the items and replace the NL with
1193 // a space.
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001194 str = typval2string(tv, TRUE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001195 if (str == NULL)
1196 return FAIL;
1197 s = str;
1198 while ((e = vim_strchr(s, '\n')) != NULL)
1199 {
1200 *e = NUL;
Bram Moolenaar21c1a0c2021-10-17 17:20:23 +01001201 p = vim_strsave_fnameescape(s,
1202 VSE_NONE);
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001203 if (p != NULL)
1204 {
1205 ga_concat(&ga, p);
1206 ga_concat(&ga, (char_u *)" ");
1207 vim_free(p);
1208 }
1209 s = e + 1;
1210 }
1211 vim_free(str);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001212 clear_tv(tv);
1213 tv->v_type = VAR_STRING;
Bram Moolenaarb288ba92021-06-05 17:10:55 +02001214 tv->vval.v_string = ga.ga_data;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02001215 return OK;
1216 }
1217 // FALLTHROUGH
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001218 default: to_string_error(tv->v_type);
1219 return FAIL;
1220 }
1221 }
Bram Moolenaar34453202021-01-31 13:08:38 +01001222 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001223 clear_tv(tv);
1224 tv->v_type = VAR_STRING;
1225 tv->vval.v_string = str;
1226 }
1227 return OK;
1228}
1229
1230/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001231 * When the value of "sv" is a null list of dict, allocate it.
1232 */
1233 static void
1234allocate_if_null(typval_T *tv)
1235{
1236 switch (tv->v_type)
1237 {
1238 case VAR_LIST:
1239 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001240 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001241 break;
1242 case VAR_DICT:
1243 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +01001244 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001245 break;
Bram Moolenaarb7c21af2021-04-18 14:12:31 +02001246 case VAR_BLOB:
1247 if (tv->vval.v_blob == NULL)
1248 (void)rettv_blob_alloc(tv);
1249 break;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001250 default:
1251 break;
1252 }
1253}
Bram Moolenaard3aac292020-04-19 14:32:17 +02001254
Bram Moolenaare7525c52021-01-09 13:20:37 +01001255/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001256 * Return the character "str[index]" where "index" is the character index,
1257 * including composing characters.
1258 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001259 */
1260 char_u *
1261char_from_string(char_u *str, varnumber_T index)
1262{
1263 size_t nbyte = 0;
1264 varnumber_T nchar = index;
1265 size_t slen;
1266
1267 if (str == NULL)
1268 return NULL;
1269 slen = STRLEN(str);
1270
Bram Moolenaarff871402021-03-26 13:34:05 +01001271 // Do the same as for a list: a negative index counts from the end.
1272 // Optimization to check the first byte to be below 0x80 (and no composing
1273 // character follows) makes this a lot faster.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001274 if (index < 0)
1275 {
1276 int clen = 0;
1277
1278 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaarff871402021-03-26 13:34:05 +01001279 {
1280 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1281 ++nbyte;
1282 else if (enc_utf8)
1283 nbyte += utfc_ptr2len(str + nbyte);
1284 else
1285 nbyte += mb_ptr2len(str + nbyte);
1286 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001287 nchar = clen + index;
1288 if (nchar < 0)
1289 // unlike list: index out of range results in empty string
1290 return NULL;
1291 }
1292
1293 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaarff871402021-03-26 13:34:05 +01001294 {
1295 if (str[nbyte] < 0x80 && str[nbyte + 1] < 0x80)
1296 ++nbyte;
1297 else if (enc_utf8)
1298 nbyte += utfc_ptr2len(str + nbyte);
1299 else
1300 nbyte += mb_ptr2len(str + nbyte);
1301 }
Bram Moolenaare7525c52021-01-09 13:20:37 +01001302 if (nbyte >= slen)
1303 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001304 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001305}
1306
1307/*
1308 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001309 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001310 * If going over the end return "str_len".
1311 * If "idx" is negative count from the end, -1 is the last character.
1312 * When going over the start return -1.
1313 */
1314 static long
1315char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1316{
1317 varnumber_T nchar = idx;
1318 size_t nbyte = 0;
1319
1320 if (nchar >= 0)
1321 {
1322 while (nchar > 0 && nbyte < str_len)
1323 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001324 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001325 --nchar;
1326 }
1327 }
1328 else
1329 {
1330 nbyte = str_len;
1331 while (nchar < 0 && nbyte > 0)
1332 {
1333 --nbyte;
1334 nbyte -= mb_head_off(str, str + nbyte);
1335 ++nchar;
1336 }
1337 if (nchar < 0)
1338 return -1;
1339 }
1340 return (long)nbyte;
1341}
1342
1343/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001344 * Return the slice "str[first : last]" using character indexes. Composing
1345 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001346 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001347 * Return NULL when the result is empty.
1348 */
1349 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001350string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001351{
1352 long start_byte, end_byte;
1353 size_t slen;
1354
1355 if (str == NULL)
1356 return NULL;
1357 slen = STRLEN(str);
1358 start_byte = char_idx2byte(str, slen, first);
1359 if (start_byte < 0)
1360 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001361 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001362 end_byte = (long)slen;
1363 else
1364 {
1365 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001366 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001367 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001368 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001369 }
1370
1371 if (start_byte >= (long)slen || end_byte <= start_byte)
1372 return NULL;
1373 return vim_strnsave(str + start_byte, end_byte - start_byte);
1374}
1375
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001376/*
1377 * Get a script variable for ISN_STORESCRIPT and ISN_LOADSCRIPT.
1378 * When "dfunc_idx" is negative don't give an error.
1379 * Returns NULL for an error.
1380 */
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001381 static svar_T *
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001382get_script_svar(scriptref_T *sref, int dfunc_idx)
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001383{
1384 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001385 dfunc_T *dfunc = dfunc_idx < 0 ? NULL
1386 : ((dfunc_T *)def_functions.ga_data) + dfunc_idx;
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001387 svar_T *sv;
1388
1389 if (sref->sref_seq != si->sn_script_seq)
1390 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001391 // The script was reloaded after the function was compiled, the
1392 // script_idx may not be valid.
1393 if (dfunc != NULL)
1394 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1395 printable_func_name(dfunc->df_ufunc));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001396 return NULL;
1397 }
1398 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar60dc8272021-07-29 22:48:54 +02001399 if (!equal_type(sv->sv_type, sref->sref_type, 0))
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001400 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02001401 if (dfunc != NULL)
1402 emsg(_(e_script_variable_type_changed));
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001403 return NULL;
1404 }
1405 return sv;
1406}
1407
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001408/*
Bram Moolenaar20677332021-06-06 17:02:53 +02001409 * Function passed to do_cmdline() for splitting a script joined by NL
1410 * characters.
1411 */
1412 static char_u *
1413get_split_sourceline(
1414 int c UNUSED,
1415 void *cookie,
1416 int indent UNUSED,
1417 getline_opt_T options UNUSED)
1418{
1419 source_cookie_T *sp = (source_cookie_T *)cookie;
1420 char_u *p;
1421 char_u *line;
1422
1423 if (*sp->nextline == NUL)
1424 return NULL;
1425 p = vim_strchr(sp->nextline, '\n');
1426 if (p == NULL)
1427 {
1428 line = vim_strsave(sp->nextline);
1429 sp->nextline += STRLEN(sp->nextline);
1430 }
1431 else
1432 {
1433 line = vim_strnsave(sp->nextline, p - sp->nextline);
1434 sp->nextline = p + 1;
1435 }
1436 return line;
1437}
1438
1439/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 * Execute a function by "name".
1441 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001442 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 */
1444 static int
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001445call_eval_func(
1446 char_u *name,
1447 int argcount,
Bram Moolenaar2fecb532021-03-24 22:00:56 +01001448 ectx_T *ectx,
1449 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450{
Bram Moolenaared677f52020-08-12 16:38:10 +02001451 int called_emsg_before = called_emsg;
1452 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001454 res = call_by_name(name, argcount, ectx, iptr, NULL);
Bram Moolenaared677f52020-08-12 16:38:10 +02001455 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001457 dictitem_T *v;
1458
1459 v = find_var(name, NULL, FALSE);
1460 if (v == NULL)
1461 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001462 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001463 return FAIL;
1464 }
1465 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1466 {
Bram Moolenaare1242042021-12-16 20:56:57 +00001467 semsg(_(e_unknown_function_str), name);
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001468 return FAIL;
1469 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001470 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001472 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473}
1474
1475/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001476 * When a function reference is used, fill a partial with the information
1477 * needed, especially when it is used as a closure.
1478 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001479 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001480fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1481{
1482 pt->pt_func = ufunc;
1483 pt->pt_refcount = 1;
1484
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001485 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001486 {
1487 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1488 + ectx->ec_dfunc_idx;
1489
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001490 // The closure may need to find arguments and local variables in the
1491 // current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001492 pt->pt_outer.out_stack = &ectx->ec_stack;
1493 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001494 if (ectx->ec_outer_ref != NULL)
1495 {
1496 // The current context already has a context, link to that one.
1497 pt->pt_outer.out_up = ectx->ec_outer_ref->or_outer;
1498 if (ectx->ec_outer_ref->or_partial != NULL)
1499 {
1500 pt->pt_outer.out_up_partial = ectx->ec_outer_ref->or_partial;
1501 ++pt->pt_outer.out_up_partial->pt_refcount;
1502 }
1503 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001504
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001505 // If this function returns and the closure is still being used, we
1506 // need to make a copy of the context (arguments and local variables).
1507 // Store a reference to the partial so we can handle that.
Bram Moolenaar35578162021-08-02 19:10:38 +02001508 if (GA_GROW_FAILS(&ectx->ec_funcrefs, 1))
Bram Moolenaarf112f302020-12-20 17:47:52 +01001509 {
1510 vim_free(pt);
1511 return FAIL;
1512 }
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02001513 // Extra variable keeps the count of closures created in the current
1514 // function call.
Bram Moolenaarf112f302020-12-20 17:47:52 +01001515 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1516 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1517
1518 ((partial_T **)ectx->ec_funcrefs.ga_data)
1519 [ectx->ec_funcrefs.ga_len] = pt;
1520 ++pt->pt_refcount;
1521 ++ectx->ec_funcrefs.ga_len;
1522 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001523 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001524 return OK;
1525}
1526
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001527/*
1528 * Execute iptr->isn_arg.string as an Ex command.
1529 */
1530 static int
1531exec_command(isn_T *iptr)
1532{
1533 source_cookie_T cookie;
1534
1535 SOURCING_LNUM = iptr->isn_lnum;
1536 // Pass getsourceline to get an error for a missing ":end"
1537 // command.
1538 CLEAR_FIELD(cookie);
1539 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1540 if (do_cmdline(iptr->isn_arg.string,
1541 getsourceline, &cookie,
1542 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED) == FAIL
1543 || did_emsg)
1544 return FAIL;
1545 return OK;
1546}
1547
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001548// used for v_instr of typval of VAR_INSTR
1549struct instr_S {
1550 ectx_T *instr_ectx;
1551 isn_T *instr_instr;
1552};
1553
Bram Moolenaar4c137212021-04-19 16:48:48 +02001554// used for substitute_instr
1555typedef struct subs_expr_S {
1556 ectx_T *subs_ectx;
1557 isn_T *subs_instr;
1558 int subs_status;
1559} subs_expr_T;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560
1561// Get pointer to item in the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001562#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563
1564// Get pointer to item at the bottom of the stack, -1 is the bottom.
1565#undef STACK_TV_BOT
Bram Moolenaar4c137212021-04-19 16:48:48 +02001566#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001568// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001569#define STACK_TV_VAR(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001571// Set when calling do_debug().
1572static ectx_T *debug_context = NULL;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001573static int debug_var_count;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001574
1575/*
1576 * When debugging lookup "name" and return the typeval.
1577 * When not found return NULL.
1578 */
1579 typval_T *
1580lookup_debug_var(char_u *name)
1581{
1582 int idx;
1583 dfunc_T *dfunc;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001584 ufunc_T *ufunc;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001585 ectx_T *ectx = debug_context;
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001586 int varargs_off;
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001587
1588 if (ectx == NULL)
1589 return NULL;
1590 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
1591
1592 // Go through the local variable names, from last to first.
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001593 for (idx = debug_var_count - 1; idx >= 0; --idx)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001594 {
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001595 if (STRCMP(((char_u **)dfunc->df_var_names.ga_data)[idx], name) == 0)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001596 return STACK_TV_VAR(idx);
1597 }
1598
Bram Moolenaar6bc30b02021-06-16 19:19:55 +02001599 // Go through argument names.
1600 ufunc = dfunc->df_ufunc;
1601 varargs_off = ufunc->uf_va_name == NULL ? 0 : 1;
1602 for (idx = 0; idx < ufunc->uf_args.ga_len; ++idx)
1603 if (STRCMP(((char_u **)(ufunc->uf_args.ga_data))[idx], name) == 0)
1604 return STACK_TV(ectx->ec_frame_idx - ufunc->uf_args.ga_len
1605 - varargs_off + idx);
1606 if (ufunc->uf_va_name != NULL && STRCMP(ufunc->uf_va_name, name) == 0)
1607 return STACK_TV(ectx->ec_frame_idx - 1);
1608
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02001609 return NULL;
1610}
1611
Bram Moolenaar26a44842021-09-02 18:49:06 +02001612/*
1613 * Return TRUE if there might be a breakpoint in "ufunc", which is when a
1614 * breakpoint was set in that function or when there is any expression.
1615 */
1616 int
1617may_break_in_function(ufunc_T *ufunc)
1618{
1619 return ufunc->uf_has_breakpoint || debug_has_expr_breakpoint();
1620}
1621
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001622 static void
1623handle_debug(isn_T *iptr, ectx_T *ectx)
1624{
1625 char_u *line;
1626 ufunc_T *ufunc = (((dfunc_T *)def_functions.ga_data)
1627 + ectx->ec_dfunc_idx)->df_ufunc;
1628 isn_T *ni;
1629 int end_lnum = iptr->isn_lnum;
1630 garray_T ga;
1631 int lnum;
1632
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001633 if (ex_nesting_level > debug_break_level)
1634 {
1635 linenr_T breakpoint;
1636
Bram Moolenaar26a44842021-09-02 18:49:06 +02001637 if (!may_break_in_function(ufunc))
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001638 return;
1639
1640 // check for the next breakpoint if needed
1641 breakpoint = dbg_find_breakpoint(FALSE, ufunc->uf_name,
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001642 iptr->isn_arg.debug.dbg_break_lnum);
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02001643 if (breakpoint <= 0 || breakpoint > iptr->isn_lnum)
1644 return;
1645 }
1646
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001647 SOURCING_LNUM = iptr->isn_lnum;
1648 debug_context = ectx;
Bram Moolenaar8cec9272021-06-23 20:20:53 +02001649 debug_var_count = iptr->isn_arg.debug.dbg_var_names_len;
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001650
1651 for (ni = iptr + 1; ni->isn_type != ISN_FINISH; ++ni)
1652 if (ni->isn_type == ISN_DEBUG
1653 || ni->isn_type == ISN_RETURN
1654 || ni->isn_type == ISN_RETURN_VOID)
1655 {
Bram Moolenaar112bed02021-11-23 22:16:34 +00001656 end_lnum = ni->isn_lnum + (ni->isn_type == ISN_DEBUG ? 0 : 1);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001657 break;
1658 }
1659
1660 if (end_lnum > iptr->isn_lnum)
1661 {
1662 ga_init2(&ga, sizeof(char_u *), 10);
1663 for (lnum = iptr->isn_lnum; lnum < end_lnum; ++lnum)
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001664 {
Bram Moolenaar303215d2021-07-07 20:10:43 +02001665 char_u *p = ((char_u **)ufunc->uf_lines.ga_data)[lnum - 1];
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001666
Bram Moolenaar303215d2021-07-07 20:10:43 +02001667 if (p == NULL)
1668 continue; // left over from continuation line
1669 p = skipwhite(p);
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001670 if (*p == '#')
1671 break;
Bram Moolenaar35578162021-08-02 19:10:38 +02001672 if (GA_GROW_OK(&ga, 1))
Bram Moolenaar59b50c32021-06-17 22:27:48 +02001673 ((char_u **)(ga.ga_data))[ga.ga_len++] = p;
1674 if (STRNCMP(p, "def ", 4) == 0)
1675 break;
1676 }
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001677 line = ga_concat_strings(&ga, " ");
1678 vim_free(ga.ga_data);
1679 }
1680 else
1681 line = ((char_u **)ufunc->uf_lines.ga_data)[iptr->isn_lnum - 1];
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001682
Dominique Pelle6e969552021-06-17 13:53:41 +02001683 do_debug(line == NULL ? (char_u *)"[empty]" : line);
Bram Moolenaar4cea5362021-06-16 22:24:40 +02001684 debug_context = NULL;
1685
1686 if (end_lnum > iptr->isn_lnum)
1687 vim_free(line);
1688}
1689
Bram Moolenaar4c137212021-04-19 16:48:48 +02001690/*
1691 * Execute instructions in execution context "ectx".
1692 * Return OK or FAIL;
1693 */
1694 static int
1695exec_instructions(ectx_T *ectx)
1696{
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001697 int ret = FAIL;
1698 int save_trylevel_at_start = ectx->ec_trylevel_at_start;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02001699 int dict_stack_len_at_start = dict_stack.ga_len;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001700
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02001701 // Start execution at the first instruction.
Bram Moolenaar4c137212021-04-19 16:48:48 +02001702 ectx->ec_iidx = 0;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001703
Bram Moolenaarff652882021-05-16 15:24:49 +02001704 // Only catch exceptions in this instruction list.
1705 ectx->ec_trylevel_at_start = trylevel;
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 for (;;)
1708 {
Bram Moolenaara7490192021-07-22 12:26:14 +02001709 static int breakcheck_count = 0; // using "static" makes it faster
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 isn_T *iptr;
Bram Moolenaara7490192021-07-22 12:26:14 +02001711 typval_T *tv;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001712
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001713 if (unlikely(++breakcheck_count >= 100))
Bram Moolenaar270d0382020-05-15 21:42:53 +02001714 {
1715 line_breakcheck();
1716 breakcheck_count = 0;
1717 }
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001718 if (unlikely(got_int))
Bram Moolenaar20431c92020-03-20 18:39:46 +01001719 {
1720 // Turn CTRL-C into an exception.
1721 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001722 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001723 goto theend;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001724 did_throw = TRUE;
1725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001727 if (unlikely(did_emsg && msg_list != NULL && *msg_list != NULL))
Bram Moolenaara26b9702020-04-18 19:53:28 +02001728 {
1729 // Turn an error message into an exception.
1730 did_emsg = FALSE;
1731 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001732 goto theend;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001733 did_throw = TRUE;
1734 *msg_list = NULL;
1735 }
1736
Dominique Pelle5a9e5842021-07-24 19:32:12 +02001737 if (unlikely(did_throw))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02001739 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001740 trycmd_T *trycmd = NULL;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001741 int index = trystack->ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742
1743 // An exception jumps to the first catch, finally, or returns from
1744 // the current function.
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001745 while (index > 0)
1746 {
1747 trycmd = ((trycmd_T *)trystack->ga_data) + index - 1;
Bram Moolenaar834193a2021-06-30 20:39:15 +02001748 if (!trycmd->tcd_in_catch || trycmd->tcd_finally_idx != 0)
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001749 break;
1750 // In the catch and finally block of this try we have to go up
1751 // one level.
1752 --index;
1753 trycmd = NULL;
1754 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02001755 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 {
Bram Moolenaar834193a2021-06-30 20:39:15 +02001757 if (trycmd->tcd_in_catch)
1758 {
1759 // exception inside ":catch", jump to ":finally" once
1760 ectx->ec_iidx = trycmd->tcd_finally_idx;
1761 trycmd->tcd_finally_idx = 0;
1762 }
1763 else
1764 // jump to first ":catch"
1765 ectx->ec_iidx = trycmd->tcd_catch_idx;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001766 trycmd->tcd_in_catch = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02001767 did_throw = FALSE; // don't come back here until :endtry
1768 trycmd->tcd_did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 }
1770 else
1771 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001772 // Not inside try or need to return from current functions.
1773 // Push a dummy return value.
Bram Moolenaar35578162021-08-02 19:10:38 +02001774 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001775 goto theend;
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001776 tv = STACK_TV_BOT(0);
1777 tv->v_type = VAR_NUMBER;
1778 tv->vval.v_number = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001779 ++ectx->ec_stack.ga_len;
1780 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001782 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001783 need_rethrow = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02001784 if (handle_closure_in_use(ectx, FALSE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001785 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 goto done;
1787 }
1788
Bram Moolenaar4c137212021-04-19 16:48:48 +02001789 if (func_return(ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001790 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 }
1792 continue;
1793 }
1794
Bram Moolenaar4c137212021-04-19 16:48:48 +02001795 iptr = &ectx->ec_instr[ectx->ec_iidx++];
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 switch (iptr->isn_type)
1797 {
1798 // execute Ex command line
1799 case ISN_EXEC:
Bram Moolenaaraacc9662021-08-13 19:40:51 +02001800 if (exec_command(iptr) == FAIL)
1801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 break;
1803
Bram Moolenaar20677332021-06-06 17:02:53 +02001804 // execute Ex command line split at NL characters.
1805 case ISN_EXEC_SPLIT:
1806 {
1807 source_cookie_T cookie;
Bram Moolenaar518df272021-06-06 17:34:13 +02001808 char_u *line;
Bram Moolenaar20677332021-06-06 17:02:53 +02001809
1810 SOURCING_LNUM = iptr->isn_lnum;
1811 CLEAR_FIELD(cookie);
1812 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1813 cookie.nextline = iptr->isn_arg.string;
Bram Moolenaar518df272021-06-06 17:34:13 +02001814 line = get_split_sourceline(0, &cookie, 0, 0);
1815 if (do_cmdline(line,
Bram Moolenaar20677332021-06-06 17:02:53 +02001816 get_split_sourceline, &cookie,
1817 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1818 == FAIL
1819 || did_emsg)
Bram Moolenaar518df272021-06-06 17:34:13 +02001820 {
1821 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001822 goto on_error;
Bram Moolenaar518df272021-06-06 17:34:13 +02001823 }
1824 vim_free(line);
Bram Moolenaar20677332021-06-06 17:02:53 +02001825 }
1826 break;
1827
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001828 // execute Ex command line that is only a range
1829 case ISN_EXECRANGE:
1830 {
1831 exarg_T ea;
1832 char *error = NULL;
1833
1834 CLEAR_FIELD(ea);
1835 ea.cmdidx = CMD_SIZE;
1836 ea.addr_type = ADDR_LINES;
1837 ea.cmd = iptr->isn_arg.string;
1838 parse_cmd_address(&ea, &error, FALSE);
Bram Moolenaar01a4dcb2021-12-04 13:15:10 +00001839 if (ea.cmd == NULL)
1840 goto on_error;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00001841 if (error == NULL)
1842 error = ex_range_without_command(&ea);
1843 if (error != NULL)
1844 {
1845 SOURCING_LNUM = iptr->isn_lnum;
1846 emsg(error);
1847 goto on_error;
1848 }
1849 }
1850 break;
1851
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001852 // Evaluate an expression with legacy syntax, push it onto the
1853 // stack.
1854 case ISN_LEGACY_EVAL:
1855 {
1856 char_u *arg = iptr->isn_arg.string;
1857 int res;
1858 int save_flags = cmdmod.cmod_flags;
1859
Bram Moolenaar35578162021-08-02 19:10:38 +02001860 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001861 goto theend;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02001862 tv = STACK_TV_BOT(0);
1863 init_tv(tv);
1864 cmdmod.cmod_flags |= CMOD_LEGACY;
1865 res = eval0(arg, tv, NULL, &EVALARG_EVALUATE);
1866 cmdmod.cmod_flags = save_flags;
1867 if (res == FAIL)
1868 goto on_error;
1869 ++ectx->ec_stack.ga_len;
1870 }
1871 break;
1872
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001873 // push typeval VAR_INSTR with instructions to be executed
1874 case ISN_INSTR:
1875 {
Bram Moolenaar35578162021-08-02 19:10:38 +02001876 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001877 goto theend;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02001878 tv = STACK_TV_BOT(0);
1879 tv->vval.v_instr = ALLOC_ONE(instr_T);
1880 if (tv->vval.v_instr == NULL)
1881 goto on_error;
1882 ++ectx->ec_stack.ga_len;
1883
1884 tv->v_type = VAR_INSTR;
1885 tv->vval.v_instr->instr_ectx = ectx;
1886 tv->vval.v_instr->instr_instr = iptr->isn_arg.instr;
1887 }
1888 break;
1889
Bram Moolenaar4c137212021-04-19 16:48:48 +02001890 // execute :substitute with an expression
1891 case ISN_SUBSTITUTE:
1892 {
1893 subs_T *subs = &iptr->isn_arg.subs;
1894 source_cookie_T cookie;
1895 struct subs_expr_S *save_instr = substitute_instr;
1896 struct subs_expr_S subs_instr;
1897 int res;
1898
1899 subs_instr.subs_ectx = ectx;
1900 subs_instr.subs_instr = subs->subs_instr;
1901 subs_instr.subs_status = OK;
1902 substitute_instr = &subs_instr;
1903
1904 SOURCING_LNUM = iptr->isn_lnum;
1905 // This is very much like ISN_EXEC
1906 CLEAR_FIELD(cookie);
1907 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1908 res = do_cmdline(subs->subs_cmd,
1909 getsourceline, &cookie,
1910 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED);
1911 substitute_instr = save_instr;
1912
1913 if (res == FAIL || did_emsg
1914 || subs_instr.subs_status == FAIL)
1915 goto on_error;
1916 }
1917 break;
1918
1919 case ISN_FINISH:
1920 goto done;
1921
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001922 case ISN_REDIRSTART:
1923 // create a dummy entry for var_redir_str()
1924 if (alloc_redir_lval() == FAIL)
1925 goto on_error;
1926
1927 // The output is stored in growarray "redir_ga" until
1928 // redirection ends.
1929 init_redir_ga();
1930 redir_vname = 1;
1931 break;
1932
1933 case ISN_REDIREND:
1934 {
1935 char_u *res = get_clear_redir_ga();
1936
1937 // End redirection, put redirected text on the stack.
1938 clear_redir_lval();
1939 redir_vname = 0;
1940
Bram Moolenaar35578162021-08-02 19:10:38 +02001941 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001942 {
1943 vim_free(res);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02001944 goto theend;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02001945 }
1946 tv = STACK_TV_BOT(0);
1947 tv->v_type = VAR_STRING;
1948 tv->vval.v_string = res;
1949 ++ectx->ec_stack.ga_len;
1950 }
1951 break;
1952
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001953 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001954#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001955 if (trigger_cexpr_autocmd(iptr->isn_arg.number) == FAIL)
1956 goto on_error;
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001957#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001958 break;
1959
1960 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001961#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001962 {
1963 exarg_T ea;
1964 int res;
1965
1966 CLEAR_FIELD(ea);
1967 ea.cmdidx = iptr->isn_arg.cexpr.cexpr_ref->cer_cmdidx;
1968 ea.forceit = iptr->isn_arg.cexpr.cexpr_ref->cer_forceit;
1969 ea.cmdlinep = &iptr->isn_arg.cexpr.cexpr_ref->cer_cmdline;
1970 --ectx->ec_stack.ga_len;
1971 tv = STACK_TV_BOT(0);
1972 res = cexpr_core(&ea, tv);
1973 clear_tv(tv);
1974 if (res == FAIL)
1975 goto on_error;
1976 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02001977#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02001978 break;
1979
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001980 // execute Ex command from pieces on the stack
1981 case ISN_EXECCONCAT:
1982 {
1983 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001984 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001985 int pass;
1986 int i;
1987 char_u *cmd = NULL;
1988 char_u *str;
1989
1990 for (pass = 1; pass <= 2; ++pass)
1991 {
1992 for (i = 0; i < count; ++i)
1993 {
1994 tv = STACK_TV_BOT(i - count);
1995 str = tv->vval.v_string;
1996 if (str != NULL && *str != NUL)
1997 {
1998 if (pass == 2)
1999 STRCPY(cmd + len, str);
2000 len += STRLEN(str);
2001 }
2002 if (pass == 2)
2003 clear_tv(tv);
2004 }
2005 if (pass == 1)
2006 {
2007 cmd = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02002008 if (unlikely(cmd == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002009 goto theend;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002010 len = 0;
2011 }
2012 }
2013
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02002014 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002015 do_cmdline_cmd(cmd);
2016 vim_free(cmd);
2017 }
2018 break;
2019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 // execute :echo {string} ...
2021 case ISN_ECHO:
2022 {
2023 int count = iptr->isn_arg.echo.echo_count;
2024 int atstart = TRUE;
2025 int needclr = TRUE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002026 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027
2028 for (idx = 0; idx < count; ++idx)
2029 {
2030 tv = STACK_TV_BOT(idx - count);
2031 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
2032 &atstart, &needclr);
2033 clear_tv(tv);
2034 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01002035 if (needclr)
2036 msg_clr_eos();
Bram Moolenaar4c137212021-04-19 16:48:48 +02002037 ectx->ec_stack.ga_len -= count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 }
2039 break;
2040
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002041 // :execute {string} ...
2042 // :echomsg {string} ...
Bram Moolenaar7de62622021-08-07 15:05:47 +02002043 // :echoconsole {string} ...
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002044 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01002045 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002046 case ISN_ECHOMSG:
Bram Moolenaar7de62622021-08-07 15:05:47 +02002047 case ISN_ECHOCONSOLE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002048 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01002049 {
2050 int count = iptr->isn_arg.number;
2051 garray_T ga;
2052 char_u buf[NUMBUFLEN];
2053 char_u *p;
2054 int len;
2055 int failed = FALSE;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002056 int idx;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002057
2058 ga_init2(&ga, 1, 80);
2059 for (idx = 0; idx < count; ++idx)
2060 {
2061 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002062 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01002063 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002064 if (tv->v_type == VAR_CHANNEL
2065 || tv->v_type == VAR_JOB)
2066 {
2067 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar68db9962021-05-09 23:19:22 +02002068 semsg(_(e_using_invalid_value_as_string_str),
2069 vartype_name(tv->v_type));
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002070 break;
2071 }
2072 else
2073 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002074 }
2075 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002076 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01002077
2078 len = (int)STRLEN(p);
Bram Moolenaar35578162021-08-02 19:10:38 +02002079 if (GA_GROW_FAILS(&ga, len + 2))
Bram Moolenaarad39c092020-02-26 18:23:43 +01002080 failed = TRUE;
2081 else
2082 {
2083 if (ga.ga_len > 0)
2084 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
2085 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
2086 ga.ga_len += len;
2087 }
2088 clear_tv(tv);
2089 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02002090 ectx->ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002091 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002092 {
2093 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002094 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002095 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002096
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02002097 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002098 {
2099 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02002100 {
2101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002102 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002103 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002104 {
2105 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01002106 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01002107 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02002108 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002109 else
2110 {
2111 msg_sb_eol();
2112 if (iptr->isn_type == ISN_ECHOMSG)
2113 {
2114 msg_attr(ga.ga_data, echo_attr);
2115 out_flush();
2116 }
Bram Moolenaar7de62622021-08-07 15:05:47 +02002117 else if (iptr->isn_type == ISN_ECHOCONSOLE)
2118 {
2119 ui_write(ga.ga_data, (int)STRLEN(ga.ga_data),
2120 TRUE);
2121 ui_write((char_u *)"\r\n", 2, TRUE);
2122 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002123 else
2124 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002125 SOURCING_LNUM = iptr->isn_lnum;
2126 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002127 }
2128 }
2129 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01002130 ga_clear(&ga);
2131 }
2132 break;
2133
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 // load local variable or argument
2135 case ISN_LOAD:
Bram Moolenaar35578162021-08-02 19:10:38 +02002136 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002137 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002139 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 break;
2141
2142 // load v: variable
2143 case ISN_LOADV:
Bram Moolenaar35578162021-08-02 19:10:38 +02002144 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002145 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002147 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 break;
2149
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002150 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 case ISN_LOADSCRIPT:
2152 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01002153 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 svar_T *sv;
2155
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002156 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002157 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002158 goto theend;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01002159 allocate_if_null(sv->sv_tv);
Bram Moolenaar35578162021-08-02 19:10:38 +02002160 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002163 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 }
2165 break;
2166
2167 // load s: variable in old script
2168 case ISN_LOADS:
2169 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002170 hashtab_T *ht = &SCRIPT_VARS(
2171 iptr->isn_arg.loadstore.ls_sid);
2172 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 if (di == NULL)
2176 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002178 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181 else
2182 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002183 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002184 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002186 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 }
2188 }
2189 break;
2190
Bram Moolenaard3aac292020-04-19 14:32:17 +02002191 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002193 case ISN_LOADB:
2194 case ISN_LOADW:
2195 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02002197 dictitem_T *di = NULL;
2198 hashtab_T *ht = NULL;
2199 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002200
Bram Moolenaard3aac292020-04-19 14:32:17 +02002201 switch (iptr->isn_type)
2202 {
2203 case ISN_LOADG:
2204 ht = get_globvar_ht();
2205 namespace = 'g';
2206 break;
2207 case ISN_LOADB:
2208 ht = &curbuf->b_vars->dv_hashtab;
2209 namespace = 'b';
2210 break;
2211 case ISN_LOADW:
2212 ht = &curwin->w_vars->dv_hashtab;
2213 namespace = 'w';
2214 break;
2215 case ISN_LOADT:
2216 ht = &curtab->tp_vars->dv_hashtab;
2217 namespace = 't';
2218 break;
2219 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002220 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002221 }
2222 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 if (di == NULL)
2225 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002226 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002227 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02002228 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002229 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 }
2231 else
2232 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002233 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002234 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002235 copy_tv(&di->di_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002236 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 }
2238 }
2239 break;
2240
Bram Moolenaar03290b82020-12-19 16:30:44 +01002241 // load autoload variable
2242 case ISN_LOADAUTO:
2243 {
2244 char_u *name = iptr->isn_arg.string;
2245
Bram Moolenaar35578162021-08-02 19:10:38 +02002246 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002247 goto theend;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01002249 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01002250 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01002251 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002252 ++ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002253 }
2254 break;
2255
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002256 // load g:/b:/w:/t: namespace
2257 case ISN_LOADGDICT:
2258 case ISN_LOADBDICT:
2259 case ISN_LOADWDICT:
2260 case ISN_LOADTDICT:
2261 {
2262 dict_T *d = NULL;
2263
2264 switch (iptr->isn_type)
2265 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002266 case ISN_LOADGDICT: d = get_globvar_dict(); break;
2267 case ISN_LOADBDICT: d = curbuf->b_vars; break;
2268 case ISN_LOADWDICT: d = curwin->w_vars; break;
2269 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002270 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002271 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002272 }
Bram Moolenaar35578162021-08-02 19:10:38 +02002273 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002274 goto theend;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002275 tv = STACK_TV_BOT(0);
2276 tv->v_type = VAR_DICT;
2277 tv->v_lock = 0;
2278 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01002279 ++d->dv_refcount;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002280 ++ectx->ec_stack.ga_len;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002281 }
2282 break;
2283
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 // load &option
2285 case ISN_LOADOPT:
2286 {
2287 typval_T optval;
2288 char_u *name = iptr->isn_arg.string;
2289
Bram Moolenaara8c17702020-04-01 21:17:24 +02002290 // This is not expected to fail, name is checked during
2291 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar35578162021-08-02 19:10:38 +02002292 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002293 goto theend;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002294 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002295 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002297 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 }
2299 break;
2300
2301 // load $ENV
2302 case ISN_LOADENV:
2303 {
2304 typval_T optval;
2305 char_u *name = iptr->isn_arg.string;
2306
Bram Moolenaar35578162021-08-02 19:10:38 +02002307 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002308 goto theend;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002309 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02002310 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 *STACK_TV_BOT(0) = optval;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002312 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 }
2314 break;
2315
2316 // load @register
2317 case ISN_LOADREG:
Bram Moolenaar35578162021-08-02 19:10:38 +02002318 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002319 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 tv = STACK_TV_BOT(0);
2321 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002322 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002323 // This may result in NULL, which should be equivalent to an
2324 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 tv->vval.v_string = get_reg_contents(
2326 iptr->isn_arg.number, GREG_EXPR_SRC);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002327 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 break;
2329
2330 // store local variable
2331 case ISN_STORE:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002332 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 tv = STACK_TV_VAR(iptr->isn_arg.number);
2334 clear_tv(tv);
2335 *tv = *STACK_TV_BOT(0);
2336 break;
2337
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002338 // store s: variable in old script
2339 case ISN_STORES:
2340 {
2341 hashtab_T *ht = &SCRIPT_VARS(
2342 iptr->isn_arg.loadstore.ls_sid);
2343 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002344 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002345
Bram Moolenaar4c137212021-04-19 16:48:48 +02002346 --ectx->ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002347 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002348 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002349 else
2350 {
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002351 SOURCING_LNUM = iptr->isn_lnum;
2352 if (var_check_permission(di, name) == FAIL)
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002353 {
2354 clear_tv(STACK_TV_BOT(0));
Bram Moolenaardcf29ac2021-04-02 14:44:02 +02002355 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002356 }
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002357 clear_tv(&di->di_tv);
2358 di->di_tv = *STACK_TV_BOT(0);
2359 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002360 }
2361 break;
2362
2363 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 case ISN_STORESCRIPT:
2365 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002366 scriptref_T *sref = iptr->isn_arg.script.scriptref;
2367 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368
Bram Moolenaar6db660b2021-08-01 14:08:54 +02002369 sv = get_script_svar(sref, ectx->ec_dfunc_idx);
Bram Moolenaar07a65d22020-12-26 20:09:15 +01002370 if (sv == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002371 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02002372 --ectx->ec_stack.ga_len;
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002373
2374 // "const" and "final" are checked at compile time, locking
2375 // the value needs to be checked here.
2376 SOURCING_LNUM = iptr->isn_lnum;
2377 if (value_check_lock(sv->sv_tv->v_lock, sv->sv_name, FALSE))
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002378 {
2379 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002380 goto on_error;
Bram Moolenaar6e50ec22021-04-03 19:32:44 +02002381 }
Bram Moolenaarf5906aa2021-04-02 14:35:15 +02002382
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 clear_tv(sv->sv_tv);
2384 *sv->sv_tv = *STACK_TV_BOT(0);
2385 }
2386 break;
2387
2388 // store option
2389 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002390 case ISN_STOREFUNCOPT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 {
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002392 char_u *opt_name = iptr->isn_arg.storeopt.so_name;
2393 int opt_flags = iptr->isn_arg.storeopt.so_flags;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 long n = 0;
2395 char_u *s = NULL;
2396 char *msg;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002397 char_u numbuf[NUMBUFLEN];
2398 char_u *tofree = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002399
Bram Moolenaar4c137212021-04-19 16:48:48 +02002400 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 tv = STACK_TV_BOT(0);
2402 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002403 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01002405 if (s == NULL)
2406 s = (char_u *)"";
2407 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002408 else if (iptr->isn_type == ISN_STOREFUNCOPT)
2409 {
2410 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002411 // If the option can be set to a function reference or
2412 // a lambda and the passed value is a function
2413 // reference, then convert it to the name (string) of
2414 // the function reference.
2415 s = tv2string(tv, &tofree, numbuf, 0);
2416 if (s == NULL || *s == NUL)
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002417 {
2418 clear_tv(tv);
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002419 goto on_error;
2420 }
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002421 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02002423 // must be VAR_NUMBER, CHECKTYPE makes sure
2424 n = tv->vval.v_number;
Bram Moolenaardcb53be2021-12-09 14:23:43 +00002425 msg = set_option_value(opt_name, n, s, opt_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02002426 clear_tv(tv);
Bram Moolenaar2ef91562021-12-11 16:14:07 +00002427 vim_free(tofree);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 if (msg != NULL)
2429 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002430 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02002432 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 }
2435 break;
2436
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002437 // store $ENV
2438 case ISN_STOREENV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002439 --ectx->ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002440 tv = STACK_TV_BOT(0);
2441 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
2442 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002443 break;
2444
2445 // store @r
2446 case ISN_STOREREG:
2447 {
2448 int reg = iptr->isn_arg.number;
2449
Bram Moolenaar4c137212021-04-19 16:48:48 +02002450 --ectx->ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002451 tv = STACK_TV_BOT(0);
Bram Moolenaar74f4a962021-06-17 21:03:07 +02002452 write_reg_contents(reg, tv_get_string(tv), -1, FALSE);
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01002453 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002454 }
2455 break;
2456
2457 // store v: variable
2458 case ISN_STOREV:
Bram Moolenaar4c137212021-04-19 16:48:48 +02002459 --ectx->ec_stack.ga_len;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002460 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
2461 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002462 // should not happen, type is checked when compiling
2463 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002464 break;
2465
Bram Moolenaard3aac292020-04-19 14:32:17 +02002466 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02002468 case ISN_STOREB:
2469 case ISN_STOREW:
2470 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002472 dictitem_T *di;
2473 hashtab_T *ht;
2474 char_u *name = iptr->isn_arg.string + 2;
2475
Bram Moolenaard3aac292020-04-19 14:32:17 +02002476 switch (iptr->isn_type)
2477 {
2478 case ISN_STOREG:
2479 ht = get_globvar_ht();
2480 break;
2481 case ISN_STOREB:
2482 ht = &curbuf->b_vars->dv_hashtab;
2483 break;
2484 case ISN_STOREW:
2485 ht = &curwin->w_vars->dv_hashtab;
2486 break;
2487 case ISN_STORET:
2488 ht = &curtab->tp_vars->dv_hashtab;
2489 break;
2490 default: // Cannot reach here
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002491 goto theend;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002493
Bram Moolenaar4c137212021-04-19 16:48:48 +02002494 --ectx->ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002495 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002497 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 else
2499 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01002500 SOURCING_LNUM = iptr->isn_lnum;
2501 if (var_check_permission(di, name) == FAIL)
2502 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 clear_tv(&di->di_tv);
2504 di->di_tv = *STACK_TV_BOT(0);
2505 }
2506 }
2507 break;
2508
Bram Moolenaar03290b82020-12-19 16:30:44 +01002509 // store an autoload variable
2510 case ISN_STOREAUTO:
2511 SOURCING_LNUM = iptr->isn_lnum;
2512 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2513 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002514 --ectx->ec_stack.ga_len;
Bram Moolenaar03290b82020-12-19 16:30:44 +01002515 break;
2516
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 // store number in local variable
2518 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002519 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 clear_tv(tv);
2521 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002522 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 break;
2524
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002525 // store value in list or dict variable
2526 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002527 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002528 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002529 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002530 typval_T *tv_dest = STACK_TV_BOT(-1);
2531 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002532
Bram Moolenaar752fc692021-01-04 21:57:11 +01002533 // Stack contains:
2534 // -3 value to be stored
2535 // -2 index
2536 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002537 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002538 SOURCING_LNUM = iptr->isn_lnum;
2539 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002540 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002541 dest_type = tv_dest->v_type;
2542 if (dest_type == VAR_DICT)
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02002543 status = do_2string(tv_idx, TRUE, FALSE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002544 else if (dest_type == VAR_LIST
2545 && tv_idx->v_type != VAR_NUMBER)
2546 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002547 emsg(_(e_number_expected));
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002548 status = FAIL;
2549 }
2550 }
2551 else if (dest_type != tv_dest->v_type)
2552 {
2553 // just in case, should be OK
2554 semsg(_(e_expected_str_but_got_str),
2555 vartype_name(dest_type),
2556 vartype_name(tv_dest->v_type));
2557 status = FAIL;
2558 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002559
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002560 if (status == OK && dest_type == VAR_LIST)
2561 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002562 long lidx = (long)tv_idx->vval.v_number;
2563 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002564
2565 if (list == NULL)
2566 {
2567 emsg(_(e_list_not_set));
2568 goto on_error;
2569 }
2570 if (lidx < 0 && list->lv_len + lidx >= 0)
2571 // negative index is relative to the end
2572 lidx = list->lv_len + lidx;
2573 if (lidx < 0 || lidx > list->lv_len)
2574 {
2575 semsg(_(e_listidx), lidx);
2576 goto on_error;
2577 }
2578 if (lidx < list->lv_len)
2579 {
2580 listitem_T *li = list_find(list, lidx);
2581
2582 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002583 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002584 goto on_error;
2585 // overwrite existing list item
2586 clear_tv(&li->li_tv);
2587 li->li_tv = *tv;
2588 }
2589 else
2590 {
2591 if (error_if_locked(list->lv_lock,
2592 e_cannot_change_list))
2593 goto on_error;
2594 // append to list, only fails when out of memory
2595 if (list_append_tv(list, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002596 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002597 clear_tv(tv);
2598 }
2599 }
2600 else if (status == OK && dest_type == VAR_DICT)
2601 {
2602 char_u *key = tv_idx->vval.v_string;
2603 dict_T *dict = tv_dest->vval.v_dict;
2604 dictitem_T *di;
2605
2606 SOURCING_LNUM = iptr->isn_lnum;
2607 if (dict == NULL)
2608 {
2609 emsg(_(e_dictionary_not_set));
2610 goto on_error;
2611 }
2612 if (key == NULL)
2613 key = (char_u *)"";
2614 di = dict_find(dict, key, -1);
2615 if (di != NULL)
2616 {
2617 if (error_if_locked(di->di_tv.v_lock,
2618 e_cannot_change_dict_item))
2619 goto on_error;
2620 // overwrite existing value
2621 clear_tv(&di->di_tv);
2622 di->di_tv = *tv;
2623 }
2624 else
2625 {
2626 if (error_if_locked(dict->dv_lock,
2627 e_cannot_change_dict))
2628 goto on_error;
2629 // add to dict, only fails when out of memory
2630 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002631 goto theend;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002632 clear_tv(tv);
2633 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002634 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002635 else if (status == OK && dest_type == VAR_BLOB)
2636 {
Bram Moolenaar51e93322021-04-17 20:44:56 +02002637 long lidx = (long)tv_idx->vval.v_number;
2638 blob_T *blob = tv_dest->vval.v_blob;
2639 varnumber_T nr;
2640 int error = FALSE;
2641 int len;
2642
2643 if (blob == NULL)
2644 {
2645 emsg(_(e_blob_not_set));
2646 goto on_error;
2647 }
2648 len = blob_len(blob);
2649 if (lidx < 0 && len + lidx >= 0)
2650 // negative index is relative to the end
2651 lidx = len + lidx;
2652
2653 // Can add one byte at the end.
2654 if (lidx < 0 || lidx > len)
2655 {
2656 semsg(_(e_blobidx), lidx);
2657 goto on_error;
2658 }
2659 if (value_check_lock(blob->bv_lock,
2660 (char_u *)"blob", FALSE))
2661 goto on_error;
2662 nr = tv_get_number_chk(tv, &error);
2663 if (error)
2664 goto on_error;
2665 blob_set_append(blob, lidx, nr);
Bram Moolenaar68452172021-04-12 21:21:02 +02002666 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002667 else
2668 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002669 status = FAIL;
2670 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002671 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002672
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002673 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002674 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002675 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002676 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002677 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002678 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002679 goto on_error;
2680 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002681 }
2682 break;
2683
Bram Moolenaar68452172021-04-12 21:21:02 +02002684 // store value in blob range
2685 case ISN_STORERANGE:
2686 {
2687 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2688 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2689 typval_T *tv_dest = STACK_TV_BOT(-1);
2690 int status = OK;
2691
2692 // Stack contains:
2693 // -4 value to be stored
2694 // -3 first index or "none"
2695 // -2 second index or "none"
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002696 // -1 destination list or blob
Bram Moolenaar68452172021-04-12 21:21:02 +02002697 tv = STACK_TV_BOT(-4);
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002698 if (tv_dest->v_type == VAR_LIST)
Bram Moolenaar68452172021-04-12 21:21:02 +02002699 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002700 long n1;
2701 long n2;
2702 int error = FALSE;
2703
2704 SOURCING_LNUM = iptr->isn_lnum;
2705 n1 = (long)tv_get_number_chk(tv_idx1, &error);
2706 if (error)
2707 status = FAIL;
2708 else
2709 {
2710 if (tv_idx2->v_type == VAR_SPECIAL
2711 && tv_idx2->vval.v_number == VVAL_NONE)
2712 n2 = list_len(tv_dest->vval.v_list) - 1;
2713 else
2714 n2 = (long)tv_get_number_chk(tv_idx2, &error);
2715 if (error)
2716 status = FAIL;
2717 else
2718 {
2719 listitem_T *li1 = check_range_index_one(
2720 tv_dest->vval.v_list, &n1, FALSE);
2721
2722 if (li1 == NULL)
2723 status = FAIL;
2724 else
2725 {
2726 status = check_range_index_two(
2727 tv_dest->vval.v_list,
2728 &n1, li1, &n2, FALSE);
2729 if (status != FAIL)
2730 status = list_assign_range(
2731 tv_dest->vval.v_list,
2732 tv->vval.v_list,
2733 n1,
2734 n2,
2735 tv_idx2->v_type == VAR_SPECIAL,
2736 (char_u *)"=",
2737 (char_u *)"[unknown]");
2738 }
2739 }
2740 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002741 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002742 else if (tv_dest->v_type == VAR_BLOB)
Bram Moolenaar68452172021-04-12 21:21:02 +02002743 {
2744 varnumber_T n1;
2745 varnumber_T n2;
2746 int error = FALSE;
2747
2748 n1 = tv_get_number_chk(tv_idx1, &error);
2749 if (error)
2750 status = FAIL;
2751 else
2752 {
2753 if (tv_idx2->v_type == VAR_SPECIAL
2754 && tv_idx2->vval.v_number == VVAL_NONE)
2755 n2 = blob_len(tv_dest->vval.v_blob) - 1;
2756 else
2757 n2 = tv_get_number_chk(tv_idx2, &error);
2758 if (error)
2759 status = FAIL;
2760 else
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002761 {
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002762 long bloblen = blob_len(tv_dest->vval.v_blob);
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002763
2764 if (check_blob_index(bloblen,
Bram Moolenaarbd6406f2021-04-14 21:30:06 +02002765 n1, FALSE) == FAIL
Bram Moolenaar0e3ff192021-04-14 20:35:23 +02002766 || check_blob_range(bloblen,
2767 n1, n2, FALSE) == FAIL)
2768 status = FAIL;
2769 else
2770 status = blob_set_range(
2771 tv_dest->vval.v_blob, n1, n2, tv);
2772 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002773 }
2774 }
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02002775 else
2776 {
2777 status = FAIL;
2778 emsg(_(e_blob_required));
2779 }
Bram Moolenaar68452172021-04-12 21:21:02 +02002780
2781 clear_tv(tv_idx1);
2782 clear_tv(tv_idx2);
2783 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002784 ectx->ec_stack.ga_len -= 4;
Bram Moolenaar68452172021-04-12 21:21:02 +02002785 clear_tv(tv);
2786
2787 if (status == FAIL)
2788 goto on_error;
2789 }
2790 break;
2791
Bram Moolenaar0186e582021-01-10 18:33:11 +01002792 // load or store variable or argument from outer scope
2793 case ISN_LOADOUTER:
2794 case ISN_STOREOUTER:
2795 {
2796 int depth = iptr->isn_arg.outer.outer_depth;
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02002797 outer_T *outer = ectx->ec_outer_ref == NULL ? NULL
2798 : ectx->ec_outer_ref->or_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002799
2800 while (depth > 1 && outer != NULL)
2801 {
2802 outer = outer->out_up;
2803 --depth;
2804 }
2805 if (outer == NULL)
2806 {
2807 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar69c76172021-12-02 16:38:52 +00002808 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx
2809 || ectx->ec_outer_ref == NULL)
2810 // Possibly :def function called from legacy
2811 // context.
2812 emsg(_(e_closure_called_from_invalid_context));
2813 else
2814 iemsg("LOADOUTER depth more than scope levels");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002815 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002816 }
2817 tv = ((typval_T *)outer->out_stack->ga_data)
2818 + outer->out_frame_idx + STACK_FRAME_SIZE
2819 + iptr->isn_arg.outer.outer_idx;
2820 if (iptr->isn_type == ISN_LOADOUTER)
2821 {
Bram Moolenaar35578162021-08-02 19:10:38 +02002822 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02002823 goto theend;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002824 copy_tv(tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02002825 ++ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002826 }
2827 else
2828 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02002829 --ectx->ec_stack.ga_len;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002830 clear_tv(tv);
2831 *tv = *STACK_TV_BOT(0);
2832 }
2833 }
2834 break;
2835
Bram Moolenaar752fc692021-01-04 21:57:11 +01002836 // unlet item in list or dict variable
2837 case ISN_UNLETINDEX:
2838 {
2839 typval_T *tv_idx = STACK_TV_BOT(-2);
2840 typval_T *tv_dest = STACK_TV_BOT(-1);
2841 int status = OK;
2842
2843 // Stack contains:
2844 // -2 index
2845 // -1 dict or list
2846 if (tv_dest->v_type == VAR_DICT)
2847 {
2848 // unlet a dict item, index must be a string
2849 if (tv_idx->v_type != VAR_STRING)
2850 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002851 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002852 semsg(_(e_expected_str_but_got_str),
2853 vartype_name(VAR_STRING),
2854 vartype_name(tv_idx->v_type));
2855 status = FAIL;
2856 }
2857 else
2858 {
2859 dict_T *d = tv_dest->vval.v_dict;
2860 char_u *key = tv_idx->vval.v_string;
2861 dictitem_T *di = NULL;
2862
Bram Moolenaar71b76852021-12-17 20:15:38 +00002863 if (d != NULL && value_check_lock(
2864 d->dv_lock, NULL, FALSE))
Bram Moolenaar752fc692021-01-04 21:57:11 +01002865 status = FAIL;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002866 else
2867 {
Bram Moolenaar71b76852021-12-17 20:15:38 +00002868 SOURCING_LNUM = iptr->isn_lnum;
2869 if (key == NULL)
2870 key = (char_u *)"";
2871 if (d != NULL)
2872 di = dict_find(d, key, (int)STRLEN(key));
2873 if (di == NULL)
2874 {
2875 // NULL dict is equivalent to empty dict
2876 semsg(_(e_dictkey), key);
2877 status = FAIL;
2878 }
2879 else if (var_check_fixed(di->di_flags,
2880 NULL, FALSE)
2881 || var_check_ro(di->di_flags,
2882 NULL, FALSE))
2883 status = FAIL;
2884 else
2885 dictitem_remove(d, di);
Bram Moolenaar752fc692021-01-04 21:57:11 +01002886 }
2887 }
2888 }
2889 else if (tv_dest->v_type == VAR_LIST)
2890 {
2891 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002892 SOURCING_LNUM = iptr->isn_lnum;
2893 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002894 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002895 status = FAIL;
2896 }
2897 else
2898 {
2899 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002900 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002901 listitem_T *li = NULL;
2902
2903 li = list_find(l, n);
2904 if (li == NULL)
2905 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002906 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002907 semsg(_(e_listidx), n);
2908 status = FAIL;
2909 }
2910 else
2911 // TODO: check for list or item locked
2912 listitem_remove(l, li);
2913 }
2914 }
2915 else
2916 {
2917 status = FAIL;
2918 semsg(_(e_cannot_index_str),
2919 vartype_name(tv_dest->v_type));
2920 }
2921
2922 clear_tv(tv_idx);
2923 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02002924 ectx->ec_stack.ga_len -= 2;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002925 if (status == FAIL)
2926 goto on_error;
2927 }
2928 break;
2929
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002930 // unlet range of items in list variable
2931 case ISN_UNLETRANGE:
2932 {
2933 // Stack contains:
2934 // -3 index1
2935 // -2 index2
2936 // -1 dict or list
2937 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2938 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2939 typval_T *tv_dest = STACK_TV_BOT(-1);
2940 int status = OK;
2941
2942 if (tv_dest->v_type == VAR_LIST)
2943 {
2944 // indexes must be a number
2945 SOURCING_LNUM = iptr->isn_lnum;
2946 if (check_for_number(tv_idx1) == FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002947 || (tv_idx2->v_type != VAR_SPECIAL
2948 && check_for_number(tv_idx2) == FAIL))
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002949 {
2950 status = FAIL;
2951 }
2952 else
2953 {
2954 list_T *l = tv_dest->vval.v_list;
2955 long n1 = (long)tv_idx1->vval.v_number;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002956 long n2 = tv_idx2->v_type == VAR_SPECIAL
2957 ? 0 : (long)tv_idx2->vval.v_number;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002958 listitem_T *li;
2959
2960 li = list_find_index(l, &n1);
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002961 if (li == NULL)
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002962 status = FAIL;
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002963 else
2964 {
2965 if (n1 < 0)
2966 n1 = list_idx_of_item(l, li);
2967 if (n2 < 0)
2968 {
2969 listitem_T *li2 = list_find(l, n2);
2970
2971 if (li2 == NULL)
2972 status = FAIL;
2973 else
2974 n2 = list_idx_of_item(l, li2);
2975 }
2976 if (status != FAIL
Bram Moolenaar5dd839c2021-07-22 18:48:53 +02002977 && tv_idx2->v_type != VAR_SPECIAL
2978 && n2 < n1)
2979 {
2980 semsg(_(e_listidx), n2);
2981 status = FAIL;
2982 }
2983 if (status != FAIL
Bram Moolenaar63cb6562021-07-20 22:21:59 +02002984 && list_unlet_range(l, li, NULL, n1,
2985 tv_idx2->v_type != VAR_SPECIAL, n2)
2986 == FAIL)
2987 status = FAIL;
2988 }
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002989 }
2990 }
2991 else
2992 {
2993 status = FAIL;
2994 SOURCING_LNUM = iptr->isn_lnum;
2995 semsg(_(e_cannot_index_str),
2996 vartype_name(tv_dest->v_type));
2997 }
2998
2999 clear_tv(tv_idx1);
3000 clear_tv(tv_idx2);
3001 clear_tv(tv_dest);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003002 ectx->ec_stack.ga_len -= 3;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01003003 if (status == FAIL)
3004 goto on_error;
3005 }
3006 break;
3007
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 // push constant
3009 case ISN_PUSHNR:
3010 case ISN_PUSHBOOL:
3011 case ISN_PUSHSPEC:
3012 case ISN_PUSHF:
3013 case ISN_PUSHS:
3014 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003015 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003016 case ISN_PUSHCHANNEL:
3017 case ISN_PUSHJOB:
Bram Moolenaar35578162021-08-02 19:10:38 +02003018 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003019 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003021 tv->v_lock = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003022 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003023 switch (iptr->isn_type)
3024 {
3025 case ISN_PUSHNR:
3026 tv->v_type = VAR_NUMBER;
3027 tv->vval.v_number = iptr->isn_arg.number;
3028 break;
3029 case ISN_PUSHBOOL:
3030 tv->v_type = VAR_BOOL;
3031 tv->vval.v_number = iptr->isn_arg.number;
3032 break;
3033 case ISN_PUSHSPEC:
3034 tv->v_type = VAR_SPECIAL;
3035 tv->vval.v_number = iptr->isn_arg.number;
3036 break;
3037#ifdef FEAT_FLOAT
3038 case ISN_PUSHF:
3039 tv->v_type = VAR_FLOAT;
3040 tv->vval.v_float = iptr->isn_arg.fnumber;
3041 break;
3042#endif
3043 case ISN_PUSHBLOB:
3044 blob_copy(iptr->isn_arg.blob, tv);
3045 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003046 case ISN_PUSHFUNC:
3047 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003048 if (iptr->isn_arg.string == NULL)
3049 tv->vval.v_string = NULL;
3050 else
3051 tv->vval.v_string =
3052 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003053 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003054 case ISN_PUSHCHANNEL:
3055#ifdef FEAT_JOB_CHANNEL
3056 tv->v_type = VAR_CHANNEL;
3057 tv->vval.v_channel = iptr->isn_arg.channel;
3058 if (tv->vval.v_channel != NULL)
3059 ++tv->vval.v_channel->ch_refcount;
3060#endif
3061 break;
3062 case ISN_PUSHJOB:
3063#ifdef FEAT_JOB_CHANNEL
3064 tv->v_type = VAR_JOB;
3065 tv->vval.v_job = iptr->isn_arg.job;
3066 if (tv->vval.v_job != NULL)
3067 ++tv->vval.v_job->jv_refcount;
3068#endif
3069 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 default:
3071 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02003072 tv->vval.v_string = vim_strsave(
3073 iptr->isn_arg.string == NULL
3074 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003075 }
3076 break;
3077
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003078 case ISN_UNLET:
3079 if (do_unlet(iptr->isn_arg.unlet.ul_name,
3080 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003081 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003082 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003083 case ISN_UNLETENV:
3084 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
3085 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003086
Bram Moolenaaraacc9662021-08-13 19:40:51 +02003087 case ISN_LOCKUNLOCK:
3088 {
3089 typval_T *lval_root_save = lval_root;
3090 int res;
3091
3092 // Stack has the local variable, argument the whole :lock
3093 // or :unlock command, like ISN_EXEC.
3094 --ectx->ec_stack.ga_len;
3095 lval_root = STACK_TV_BOT(0);
3096 res = exec_command(iptr);
3097 clear_tv(lval_root);
3098 lval_root = lval_root_save;
3099 if (res == FAIL)
3100 goto on_error;
3101 }
3102 break;
3103
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003104 case ISN_LOCKCONST:
3105 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
3106 break;
3107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 // create a list from items on the stack; uses a single allocation
3109 // for the list header and the items
3110 case ISN_NEWLIST:
Bram Moolenaar4c137212021-04-19 16:48:48 +02003111 if (exe_newlist(iptr->isn_arg.number, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003112 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 break;
3114
3115 // create a dict from items on the stack
3116 case ISN_NEWDICT:
3117 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003118 int count = iptr->isn_arg.number;
3119 dict_T *dict = dict_alloc();
3120 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003121 char_u *key;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003122 int idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003123
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003124 if (unlikely(dict == NULL))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003125 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 for (idx = 0; idx < count; ++idx)
3127 {
Bram Moolenaare8593122020-07-18 15:17:02 +02003128 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02003130 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003131 key = tv->vval.v_string == NULL
3132 ? (char_u *)"" : tv->vval.v_string;
3133 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02003134 if (item != NULL)
3135 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003136 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003137 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02003138 dict_unref(dict);
3139 goto on_error;
3140 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01003141 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 clear_tv(tv);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02003143 if (unlikely(item == NULL))
Bram Moolenaare8593122020-07-18 15:17:02 +02003144 {
3145 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003146 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003147 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
3149 item->di_tv.v_lock = 0;
3150 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003151 {
Bram Moolenaard032f342020-07-18 18:13:02 +02003152 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02003153 dict_unref(dict);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003154 goto theend;
Bram Moolenaare8593122020-07-18 15:17:02 +02003155 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157
3158 if (count > 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003159 ectx->ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar35578162021-08-02 19:10:38 +02003160 else if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003161 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003163 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 tv = STACK_TV_BOT(-1);
3165 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003166 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003167 tv->vval.v_dict = dict;
3168 ++dict->dv_refcount;
3169 }
3170 break;
3171
3172 // call a :def function
3173 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02003174 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003175 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
3176 NULL,
3177 iptr->isn_arg.dfunc.cdf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003178 ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02003179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 break;
3181
3182 // call a builtin function
3183 case ISN_BCALL:
3184 SOURCING_LNUM = iptr->isn_lnum;
3185 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
3186 iptr->isn_arg.bfunc.cbf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003187 ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003188 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 break;
3190
3191 // call a funcref or partial
3192 case ISN_PCALL:
3193 {
3194 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
3195 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003196 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197
3198 SOURCING_LNUM = iptr->isn_lnum;
3199 if (pfunc->cpf_top)
3200 {
3201 // funcref is above the arguments
3202 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
3203 }
3204 else
3205 {
3206 // Get the funcref from the stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003207 --ectx->ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003208 partial_tv = *STACK_TV_BOT(0);
3209 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003211 r = call_partial(tv, pfunc->cpf_argcount, ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02003212 if (tv == &partial_tv)
3213 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003215 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 }
3217 break;
3218
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003219 case ISN_PCALL_END:
3220 // PCALL finished, arguments have been consumed and replaced by
3221 // the return value. Now clear the funcref from the stack,
3222 // and move the return value in its place.
Bram Moolenaar4c137212021-04-19 16:48:48 +02003223 --ectx->ec_stack.ga_len;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003224 clear_tv(STACK_TV_BOT(-1));
3225 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
3226 break;
3227
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 // call a user defined function or funcref/partial
3229 case ISN_UCALL:
3230 {
3231 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3232
3233 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar2fecb532021-03-24 22:00:56 +01003234 if (call_eval_func(cufunc->cuf_name, cufunc->cuf_argcount,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003235 ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02003236 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003237 }
3238 break;
3239
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003240 // return from a :def function call without a value
3241 case ISN_RETURN_VOID:
Bram Moolenaar35578162021-08-02 19:10:38 +02003242 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003243 goto theend;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003244 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003245 ++ectx->ec_stack.ga_len;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003246 tv->v_type = VAR_VOID;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003247 tv->vval.v_number = 0;
3248 tv->v_lock = 0;
3249 // FALLTHROUGH
3250
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02003251 // return from a :def function call with what is on the stack
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 case ISN_RETURN:
3253 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003254 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003255 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01003256
3257 if (trystack->ga_len > 0)
3258 trycmd = ((trycmd_T *)trystack->ga_data)
3259 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003260 if (trycmd != NULL
Bram Moolenaar4c137212021-04-19 16:48:48 +02003261 && trycmd->tcd_frame_idx == ectx->ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003263 // jump to ":finally" or ":endtry"
3264 if (trycmd->tcd_finally_idx != 0)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003265 ectx->ec_iidx = trycmd->tcd_finally_idx;
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01003266 else
Bram Moolenaar4c137212021-04-19 16:48:48 +02003267 ectx->ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 trycmd->tcd_return = TRUE;
3269 }
3270 else
Bram Moolenaard032f342020-07-18 18:13:02 +02003271 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 }
3273 break;
3274
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02003275 // push a partial, a reference to a compiled function
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 case ISN_FUNCREF:
3277 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01003278 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar38453522021-11-28 22:00:12 +00003279 ufunc_T *ufunc;
3280 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 if (pt == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003283 goto theend;
Bram Moolenaar35578162021-08-02 19:10:38 +02003284 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003285 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003286 vim_free(pt);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003287 goto theend;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003288 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003289 if (funcref->fr_func_name == NULL)
3290 {
3291 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
3292 + funcref->fr_dfunc_idx;
3293
3294 ufunc = pt_dfunc->df_ufunc;
3295 }
3296 else
3297 {
3298 ufunc = find_func(funcref->fr_func_name, FALSE, NULL);
3299 }
Bram Moolenaar293eb9b2021-11-29 10:36:19 +00003300 if (ufunc == NULL)
3301 {
3302 SOURCING_LNUM = iptr->isn_lnum;
3303 emsg(_(e_function_reference_invalid));
3304 goto theend;
3305 }
Bram Moolenaar38453522021-11-28 22:00:12 +00003306 if (fill_partial_and_closure(pt, ufunc, ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003307 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003309 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 tv->vval.v_partial = pt;
3311 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003312 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003313 }
3314 break;
3315
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003316 // Create a global function from a lambda.
3317 case ISN_NEWFUNC:
3318 {
3319 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3320
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01003321 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaar4c137212021-04-19 16:48:48 +02003322 ectx) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003323 goto theend;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003324 }
3325 break;
3326
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003327 // List functions
3328 case ISN_DEF:
3329 if (iptr->isn_arg.string == NULL)
3330 list_functions(NULL);
3331 else
3332 {
3333 exarg_T ea;
3334
3335 CLEAR_FIELD(ea);
3336 ea.cmd = ea.arg = iptr->isn_arg.string;
3337 define_function(&ea, NULL);
3338 }
3339 break;
3340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 // jump if a condition is met
3342 case ISN_JUMP:
3343 {
3344 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003345 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 int jump = TRUE;
3347
3348 if (when != JUMP_ALWAYS)
3349 {
3350 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003351 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02003352 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003353 || when == JUMP_IF_COND_TRUE)
3354 {
3355 SOURCING_LNUM = iptr->isn_lnum;
3356 jump = tv_get_bool_chk(tv, &error);
3357 if (error)
3358 goto on_error;
3359 }
3360 else
3361 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003363 || when == JUMP_AND_KEEP_IF_FALSE
3364 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003365 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01003366 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003367 {
3368 // drop the value from the stack
3369 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003370 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371 }
3372 }
3373 if (jump)
Bram Moolenaar4c137212021-04-19 16:48:48 +02003374 ectx->ec_iidx = iptr->isn_arg.jump.jump_where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 }
3376 break;
3377
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003378 // Jump if an argument with a default value was already set and not
3379 // v:none.
3380 case ISN_JUMP_IF_ARG_SET:
3381 tv = STACK_TV_VAR(iptr->isn_arg.jumparg.jump_arg_off);
3382 if (tv->v_type != VAR_UNKNOWN
3383 && !(tv->v_type == VAR_SPECIAL
3384 && tv->vval.v_number == VVAL_NONE))
Bram Moolenaar4c137212021-04-19 16:48:48 +02003385 ectx->ec_iidx = iptr->isn_arg.jumparg.jump_where;
Bram Moolenaar38a3bfa2021-03-29 22:14:55 +02003386 break;
3387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003388 // top of a for loop
3389 case ISN_FOR:
3390 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003391 typval_T *ltv = STACK_TV_BOT(-1);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003392 typval_T *idxtv =
3393 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
3394
Bram Moolenaar35578162021-08-02 19:10:38 +02003395 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003396 goto theend;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003397 if (ltv->v_type == VAR_LIST)
Bram Moolenaara91a7132021-03-25 21:12:15 +01003398 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003399 list_T *list = ltv->vval.v_list;
3400
3401 // push the next item from the list
3402 ++idxtv->vval.v_number;
3403 if (list == NULL
3404 || idxtv->vval.v_number >= list->lv_len)
3405 {
3406 // past the end of the list, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003407 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3408 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003409 }
3410 else if (list->lv_first == &range_list_item)
3411 {
3412 // non-materialized range() list
3413 tv = STACK_TV_BOT(0);
3414 tv->v_type = VAR_NUMBER;
3415 tv->v_lock = 0;
3416 tv->vval.v_number = list_find_nr(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 list, idxtv->vval.v_number, NULL);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003418 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003419 }
3420 else
3421 {
3422 listitem_T *li = list_find(list,
3423 idxtv->vval.v_number);
3424
3425 copy_tv(&li->li_tv, STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02003426 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003427 }
3428 }
3429 else if (ltv->v_type == VAR_STRING)
3430 {
3431 char_u *str = ltv->vval.v_string;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003432
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003433 // The index is for the last byte of the previous
3434 // character.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003435 ++idxtv->vval.v_number;
Bram Moolenaar175a41c2021-04-08 18:05:03 +02003436 if (str == NULL || str[idxtv->vval.v_number] == NUL)
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003437 {
3438 // past the end of the string, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003439 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3440 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003441 }
3442 else
3443 {
3444 int clen = mb_ptr2len(str + idxtv->vval.v_number);
3445
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003446 // Push the next character from the string.
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003447 tv = STACK_TV_BOT(0);
3448 tv->v_type = VAR_STRING;
3449 tv->vval.v_string = vim_strnsave(
3450 str + idxtv->vval.v_number, clen);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003451 ++ectx->ec_stack.ga_len;
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003452 idxtv->vval.v_number += clen - 1;
3453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003454 }
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003455 else if (ltv->v_type == VAR_BLOB)
3456 {
3457 blob_T *blob = ltv->vval.v_blob;
3458
3459 // When we get here the first time make a copy of the
3460 // blob, so that the iteration still works when it is
3461 // changed.
3462 if (idxtv->vval.v_number == -1 && blob != NULL)
3463 {
3464 blob_copy(blob, ltv);
3465 blob_unref(blob);
3466 blob = ltv->vval.v_blob;
3467 }
3468
3469 // The index is for the previous byte.
3470 ++idxtv->vval.v_number;
3471 if (blob == NULL
3472 || idxtv->vval.v_number >= blob_len(blob))
3473 {
3474 // past the end of the blob, jump to "endfor"
Bram Moolenaar4c137212021-04-19 16:48:48 +02003475 ectx->ec_iidx = iptr->isn_arg.forloop.for_end;
3476 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003477 }
3478 else
3479 {
3480 // Push the next byte from the blob.
3481 tv = STACK_TV_BOT(0);
3482 tv->v_type = VAR_NUMBER;
3483 tv->vval.v_number = blob_get(blob,
3484 idxtv->vval.v_number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003485 ++ectx->ec_stack.ga_len;
Bram Moolenaard551d6c2021-04-18 13:15:58 +02003486 }
3487 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 else
3489 {
Bram Moolenaar74e54fc2021-03-26 20:41:29 +01003490 semsg(_(e_for_loop_on_str_not_supported),
3491 vartype_name(ltv->v_type));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003492 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 }
3494 }
3495 break;
3496
3497 // start of ":try" block
3498 case ISN_TRY:
3499 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003500 trycmd_T *trycmd = NULL;
3501
Bram Moolenaar35578162021-08-02 19:10:38 +02003502 if (GA_GROW_FAILS(&ectx->ec_trystack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003503 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02003504 trycmd = ((trycmd_T *)ectx->ec_trystack.ga_data)
3505 + ectx->ec_trystack.ga_len;
3506 ++ectx->ec_trystack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003508 CLEAR_POINTER(trycmd);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003509 trycmd->tcd_frame_idx = ectx->ec_frame_idx;
3510 trycmd->tcd_stack_len = ectx->ec_stack.ga_len;
Bram Moolenaara91a7132021-03-25 21:12:15 +01003511 trycmd->tcd_catch_idx =
3512 iptr->isn_arg.try.try_ref->try_catch;
3513 trycmd->tcd_finally_idx =
3514 iptr->isn_arg.try.try_ref->try_finally;
3515 trycmd->tcd_endtry_idx =
3516 iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 }
3518 break;
3519
3520 case ISN_PUSHEXC:
3521 if (current_exception == NULL)
3522 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003523 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003524 iemsg("Evaluating catch while current_exception is NULL");
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003525 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003526 }
Bram Moolenaar35578162021-08-02 19:10:38 +02003527 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003528 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 tv = STACK_TV_BOT(0);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003530 ++ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003532 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 tv->vval.v_string = vim_strsave(
3534 (char_u *)current_exception->value);
3535 break;
3536
3537 case ISN_CATCH:
3538 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003539 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
Bram Moolenaar4c137212021-04-19 16:48:48 +02003541 may_restore_cmdmod(&ectx->ec_funclocal);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 if (trystack->ga_len > 0)
3543 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01003544 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 + trystack->ga_len - 1;
3546 trycmd->tcd_caught = TRUE;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003547 trycmd->tcd_did_throw = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548 }
3549 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01003550 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 catch_exception(current_exception);
3552 }
3553 break;
3554
Bram Moolenaarc150c092021-02-13 15:02:46 +01003555 case ISN_TRYCONT:
3556 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003557 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003558 trycont_T *trycont = &iptr->isn_arg.trycont;
3559 int i;
3560 trycmd_T *trycmd;
3561 int iidx = trycont->tct_where;
3562
3563 if (trystack->ga_len < trycont->tct_levels)
3564 {
3565 siemsg("TRYCONT: expected %d levels, found %d",
3566 trycont->tct_levels, trystack->ga_len);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003567 goto theend;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003568 }
3569 // Make :endtry jump to any outer try block and the last
3570 // :endtry inside the loop to the loop start.
3571 for (i = trycont->tct_levels; i > 0; --i)
3572 {
3573 trycmd = ((trycmd_T *)trystack->ga_data)
3574 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01003575 // Add one to tcd_cont to be able to jump to
3576 // instruction with index zero.
3577 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003578 iidx = trycmd->tcd_finally_idx == 0
3579 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003580 }
3581 // jump to :finally or :endtry of current try statement
Bram Moolenaar4c137212021-04-19 16:48:48 +02003582 ectx->ec_iidx = iidx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01003583 }
3584 break;
3585
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003586 case ISN_FINALLY:
3587 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003588 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01003589 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3590 + trystack->ga_len - 1;
3591
3592 // Reset the index to avoid a return statement jumps here
3593 // again.
3594 trycmd->tcd_finally_idx = 0;
3595 break;
3596 }
3597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 // end of ":try" block
3599 case ISN_ENDTRY:
3600 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003601 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003602
3603 if (trystack->ga_len > 0)
3604 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003605 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01003606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 --trystack->ga_len;
3608 --trylevel;
3609 trycmd = ((trycmd_T *)trystack->ga_data)
3610 + trystack->ga_len;
Bram Moolenaard3d8fee2021-06-30 19:54:43 +02003611 if (trycmd->tcd_did_throw)
3612 did_throw = TRUE;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01003613 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 {
3615 // discard the exception
3616 if (caught_stack == current_exception)
3617 caught_stack = caught_stack->caught;
3618 discard_current_exception();
3619 }
3620
3621 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02003622 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003623
Bram Moolenaar4c137212021-04-19 16:48:48 +02003624 while (ectx->ec_stack.ga_len > trycmd->tcd_stack_len)
Bram Moolenaard9d77892021-02-12 21:32:47 +01003625 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003626 --ectx->ec_stack.ga_len;
Bram Moolenaard9d77892021-02-12 21:32:47 +01003627 clear_tv(STACK_TV_BOT(0));
3628 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01003629 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01003630 // handling :continue: jump to outer try block or
3631 // start of the loop
Bram Moolenaar4c137212021-04-19 16:48:48 +02003632 ectx->ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 }
3634 }
3635 break;
3636
3637 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01003638 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02003639 garray_T *trystack = &ectx->ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02003640
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003641 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
3642 {
3643 // throwing an exception while using "silent!" causes
3644 // the function to abort but not display an error.
3645 tv = STACK_TV_BOT(-1);
3646 clear_tv(tv);
3647 tv->v_type = VAR_NUMBER;
3648 tv->vval.v_number = 0;
3649 goto done;
3650 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003651 --ectx->ec_stack.ga_len;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003652 tv = STACK_TV_BOT(0);
3653 if (tv->vval.v_string == NULL
3654 || *skipwhite(tv->vval.v_string) == NUL)
3655 {
3656 vim_free(tv->vval.v_string);
3657 SOURCING_LNUM = iptr->isn_lnum;
3658 emsg(_(e_throw_with_empty_string));
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003659 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003660 }
3661
3662 // Inside a "catch" we need to first discard the caught
3663 // exception.
3664 if (trystack->ga_len > 0)
3665 {
3666 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
3667 + trystack->ga_len - 1;
3668 if (trycmd->tcd_caught && current_exception != NULL)
3669 {
3670 // discard the exception
3671 if (caught_stack == current_exception)
3672 caught_stack = caught_stack->caught;
3673 discard_current_exception();
3674 trycmd->tcd_caught = FALSE;
3675 }
3676 }
3677
3678 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
3679 == FAIL)
3680 {
3681 vim_free(tv->vval.v_string);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003682 goto theend;
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01003683 }
3684 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 break;
3687
3688 // compare with special values
3689 case ISN_COMPAREBOOL:
3690 case ISN_COMPARESPECIAL:
3691 {
3692 typval_T *tv1 = STACK_TV_BOT(-2);
3693 typval_T *tv2 = STACK_TV_BOT(-1);
3694 varnumber_T arg1 = tv1->vval.v_number;
3695 varnumber_T arg2 = tv2->vval.v_number;
3696 int res;
3697
3698 switch (iptr->isn_arg.op.op_type)
3699 {
3700 case EXPR_EQUAL: res = arg1 == arg2; break;
3701 case EXPR_NEQUAL: res = arg1 != arg2; break;
3702 default: res = 0; break;
3703 }
3704
Bram Moolenaar4c137212021-04-19 16:48:48 +02003705 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 tv1->v_type = VAR_BOOL;
3707 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3708 }
3709 break;
3710
3711 // Operation with two number arguments
3712 case ISN_OPNR:
3713 case ISN_COMPARENR:
3714 {
3715 typval_T *tv1 = STACK_TV_BOT(-2);
3716 typval_T *tv2 = STACK_TV_BOT(-1);
3717 varnumber_T arg1 = tv1->vval.v_number;
3718 varnumber_T arg2 = tv2->vval.v_number;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003719 varnumber_T res = 0;
3720 int div_zero = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721
3722 switch (iptr->isn_arg.op.op_type)
3723 {
3724 case EXPR_MULT: res = arg1 * arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003725 case EXPR_DIV: if (arg2 == 0)
3726 div_zero = TRUE;
3727 else
3728 res = arg1 / arg2;
3729 break;
3730 case EXPR_REM: if (arg2 == 0)
3731 div_zero = TRUE;
3732 else
3733 res = arg1 % arg2;
3734 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 case EXPR_SUB: res = arg1 - arg2; break;
3736 case EXPR_ADD: res = arg1 + arg2; break;
3737
3738 case EXPR_EQUAL: res = arg1 == arg2; break;
3739 case EXPR_NEQUAL: res = arg1 != arg2; break;
3740 case EXPR_GREATER: res = arg1 > arg2; break;
3741 case EXPR_GEQUAL: res = arg1 >= arg2; break;
3742 case EXPR_SMALLER: res = arg1 < arg2; break;
3743 case EXPR_SEQUAL: res = arg1 <= arg2; break;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003744 default: break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003745 }
3746
Bram Moolenaar4c137212021-04-19 16:48:48 +02003747 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 if (iptr->isn_type == ISN_COMPARENR)
3749 {
3750 tv1->v_type = VAR_BOOL;
3751 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
3752 }
3753 else
3754 tv1->vval.v_number = res;
Bram Moolenaarfbeefb12021-08-07 15:50:23 +02003755 if (div_zero)
3756 {
3757 SOURCING_LNUM = iptr->isn_lnum;
3758 emsg(_(e_divide_by_zero));
3759 goto on_error;
3760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 }
3762 break;
3763
3764 // Computation with two float arguments
3765 case ISN_OPFLOAT:
3766 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003767#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 {
3769 typval_T *tv1 = STACK_TV_BOT(-2);
3770 typval_T *tv2 = STACK_TV_BOT(-1);
3771 float_T arg1 = tv1->vval.v_float;
3772 float_T arg2 = tv2->vval.v_float;
3773 float_T res = 0;
3774 int cmp = FALSE;
3775
3776 switch (iptr->isn_arg.op.op_type)
3777 {
3778 case EXPR_MULT: res = arg1 * arg2; break;
3779 case EXPR_DIV: res = arg1 / arg2; break;
3780 case EXPR_SUB: res = arg1 - arg2; break;
3781 case EXPR_ADD: res = arg1 + arg2; break;
3782
3783 case EXPR_EQUAL: cmp = arg1 == arg2; break;
3784 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
3785 case EXPR_GREATER: cmp = arg1 > arg2; break;
3786 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
3787 case EXPR_SMALLER: cmp = arg1 < arg2; break;
3788 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
3789 default: cmp = 0; break;
3790 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003791 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 if (iptr->isn_type == ISN_COMPAREFLOAT)
3793 {
3794 tv1->v_type = VAR_BOOL;
3795 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3796 }
3797 else
3798 tv1->vval.v_float = res;
3799 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01003800#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003801 break;
3802
3803 case ISN_COMPARELIST:
3804 {
3805 typval_T *tv1 = STACK_TV_BOT(-2);
3806 typval_T *tv2 = STACK_TV_BOT(-1);
3807 list_T *arg1 = tv1->vval.v_list;
3808 list_T *arg2 = tv2->vval.v_list;
3809 int cmp = FALSE;
3810 int ic = iptr->isn_arg.op.op_ic;
3811
3812 switch (iptr->isn_arg.op.op_type)
3813 {
3814 case EXPR_EQUAL: cmp =
3815 list_equal(arg1, arg2, ic, FALSE); break;
3816 case EXPR_NEQUAL: cmp =
3817 !list_equal(arg1, arg2, ic, FALSE); break;
3818 case EXPR_IS: cmp = arg1 == arg2; break;
3819 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3820 default: cmp = 0; break;
3821 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003822 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 clear_tv(tv1);
3824 clear_tv(tv2);
3825 tv1->v_type = VAR_BOOL;
3826 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3827 }
3828 break;
3829
3830 case ISN_COMPAREBLOB:
3831 {
3832 typval_T *tv1 = STACK_TV_BOT(-2);
3833 typval_T *tv2 = STACK_TV_BOT(-1);
3834 blob_T *arg1 = tv1->vval.v_blob;
3835 blob_T *arg2 = tv2->vval.v_blob;
3836 int cmp = FALSE;
3837
3838 switch (iptr->isn_arg.op.op_type)
3839 {
3840 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3841 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3842 case EXPR_IS: cmp = arg1 == arg2; break;
3843 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3844 default: cmp = 0; break;
3845 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02003846 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 clear_tv(tv1);
3848 clear_tv(tv2);
3849 tv1->v_type = VAR_BOOL;
3850 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3851 }
3852 break;
3853
3854 // TODO: handle separately
3855 case ISN_COMPARESTRING:
3856 case ISN_COMPAREDICT:
3857 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858 case ISN_COMPAREANY:
3859 {
3860 typval_T *tv1 = STACK_TV_BOT(-2);
3861 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003862 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 int ic = iptr->isn_arg.op.op_ic;
3864
Bram Moolenaareb26f432020-09-14 16:50:05 +02003865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003866 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003868 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 }
3870 break;
3871
3872 case ISN_ADDLIST:
3873 case ISN_ADDBLOB:
3874 {
3875 typval_T *tv1 = STACK_TV_BOT(-2);
3876 typval_T *tv2 = STACK_TV_BOT(-1);
3877
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003878 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 if (iptr->isn_type == ISN_ADDLIST)
Bram Moolenaar07802042021-09-09 23:01:14 +02003880 {
3881 if (iptr->isn_arg.op.op_type == EXPR_APPEND
3882 && tv1->vval.v_list != NULL)
3883 list_extend(tv1->vval.v_list, tv2->vval.v_list,
3884 NULL);
3885 else
3886 eval_addlist(tv1, tv2);
3887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 else
3889 eval_addblob(tv1, tv2);
3890 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003891 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 }
3893 break;
3894
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003895 case ISN_LISTAPPEND:
3896 {
3897 typval_T *tv1 = STACK_TV_BOT(-2);
3898 typval_T *tv2 = STACK_TV_BOT(-1);
3899 list_T *l = tv1->vval.v_list;
3900
3901 // add an item to a list
3902 if (l == NULL)
3903 {
3904 SOURCING_LNUM = iptr->isn_lnum;
3905 emsg(_(e_cannot_add_to_null_list));
3906 goto on_error;
3907 }
3908 if (list_append_tv(l, tv2) == FAIL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02003909 goto theend;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003910 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003911 --ectx->ec_stack.ga_len;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003912 }
3913 break;
3914
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003915 case ISN_BLOBAPPEND:
3916 {
3917 typval_T *tv1 = STACK_TV_BOT(-2);
3918 typval_T *tv2 = STACK_TV_BOT(-1);
3919 blob_T *b = tv1->vval.v_blob;
3920 int error = FALSE;
3921 varnumber_T n;
3922
3923 // add a number to a blob
3924 if (b == NULL)
3925 {
3926 SOURCING_LNUM = iptr->isn_lnum;
3927 emsg(_(e_cannot_add_to_null_blob));
3928 goto on_error;
3929 }
3930 n = tv_get_number_chk(tv2, &error);
3931 if (error)
3932 goto on_error;
3933 ga_append(&b->bv_ga, (int)n);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003934 --ectx->ec_stack.ga_len;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003935 }
3936 break;
3937
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 // Computation with two arguments of unknown type
3939 case ISN_OPANY:
3940 {
3941 typval_T *tv1 = STACK_TV_BOT(-2);
3942 typval_T *tv2 = STACK_TV_BOT(-1);
3943 varnumber_T n1, n2;
3944#ifdef FEAT_FLOAT
3945 float_T f1 = 0, f2 = 0;
3946#endif
3947 int error = FALSE;
3948
3949 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3950 {
3951 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3952 {
3953 eval_addlist(tv1, tv2);
3954 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003955 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 break;
3957 }
3958 else if (tv1->v_type == VAR_BLOB
3959 && tv2->v_type == VAR_BLOB)
3960 {
3961 eval_addblob(tv1, tv2);
3962 clear_tv(tv2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02003963 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965 }
3966 }
3967#ifdef FEAT_FLOAT
3968 if (tv1->v_type == VAR_FLOAT)
3969 {
3970 f1 = tv1->vval.v_float;
3971 n1 = 0;
3972 }
3973 else
3974#endif
3975 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003976 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 n1 = tv_get_number_chk(tv1, &error);
3978 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003979 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980#ifdef FEAT_FLOAT
3981 if (tv2->v_type == VAR_FLOAT)
3982 f1 = n1;
3983#endif
3984 }
3985#ifdef FEAT_FLOAT
3986 if (tv2->v_type == VAR_FLOAT)
3987 {
3988 f2 = tv2->vval.v_float;
3989 n2 = 0;
3990 }
3991 else
3992#endif
3993 {
3994 n2 = tv_get_number_chk(tv2, &error);
3995 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003996 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003997#ifdef FEAT_FLOAT
3998 if (tv1->v_type == VAR_FLOAT)
3999 f2 = n2;
4000#endif
4001 }
4002#ifdef FEAT_FLOAT
4003 // if there is a float on either side the result is a float
4004 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
4005 {
4006 switch (iptr->isn_arg.op.op_type)
4007 {
4008 case EXPR_MULT: f1 = f1 * f2; break;
4009 case EXPR_DIV: f1 = f1 / f2; break;
4010 case EXPR_SUB: f1 = f1 - f2; break;
4011 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004012 default: SOURCING_LNUM = iptr->isn_lnum;
4013 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004014 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004015 }
4016 clear_tv(tv1);
4017 clear_tv(tv2);
4018 tv1->v_type = VAR_FLOAT;
4019 tv1->vval.v_float = f1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004020 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004021 }
4022 else
4023#endif
4024 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004025 int failed = FALSE;
4026
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004027 switch (iptr->isn_arg.op.op_type)
4028 {
4029 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004030 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
4031 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004032 goto on_error;
4033 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034 case EXPR_SUB: n1 = n1 - n2; break;
4035 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01004036 default: n1 = num_modulus(n1, n2, &failed);
4037 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01004038 goto on_error;
4039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 }
4041 clear_tv(tv1);
4042 clear_tv(tv2);
4043 tv1->v_type = VAR_NUMBER;
4044 tv1->vval.v_number = n1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004045 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004046 }
4047 }
4048 break;
4049
4050 case ISN_CONCAT:
4051 {
4052 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
4053 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
4054 char_u *res;
4055
4056 res = concat_str(str1, str2);
4057 clear_tv(STACK_TV_BOT(-2));
4058 clear_tv(STACK_TV_BOT(-1));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004059 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004060 STACK_TV_BOT(-1)->vval.v_string = res;
4061 }
4062 break;
4063
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004064 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004065 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004066 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004067 int is_slice = iptr->isn_type == ISN_STRSLICE;
4068 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004069 char_u *res;
4070
4071 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004072 // string slice: string is at stack-3, first index at
4073 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004074 if (is_slice)
4075 {
4076 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004077 n1 = tv->vval.v_number;
4078 }
4079
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004080 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004081 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004082
Bram Moolenaar4c137212021-04-19 16:48:48 +02004083 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004084 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004085 if (is_slice)
4086 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01004087 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004088 else
4089 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01004090 // single character (including composing characters).
4091 // If the index is too big or negative the result is
4092 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004093 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004094 vim_free(tv->vval.v_string);
4095 tv->vval.v_string = res;
4096 }
4097 break;
4098
4099 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02004100 case ISN_LISTSLICE:
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004101 case ISN_BLOBINDEX:
4102 case ISN_BLOBSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004103 {
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004104 int is_slice = iptr->isn_type == ISN_LISTSLICE
4105 || iptr->isn_type == ISN_BLOBSLICE;
4106 int is_blob = iptr->isn_type == ISN_BLOBINDEX
4107 || iptr->isn_type == ISN_BLOBSLICE;
Bram Moolenaared591872020-08-15 22:14:53 +02004108 varnumber_T n1, n2;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004109 typval_T *val_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110
4111 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02004112 // list slice: list is at stack-3, indexes at stack-2 and
4113 // stack-1
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004114 // Same for blob.
4115 val_tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004116
4117 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02004118 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02004120
4121 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 {
Bram Moolenaared591872020-08-15 22:14:53 +02004123 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02004124 n1 = tv->vval.v_number;
4125 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 }
Bram Moolenaared591872020-08-15 22:14:53 +02004127
Bram Moolenaar4c137212021-04-19 16:48:48 +02004128 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02004129 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02004130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfc30232021-04-11 20:26:34 +02004131 if (is_blob)
4132 {
4133 if (blob_slice_or_index(val_tv->vval.v_blob, is_slice,
4134 n1, n2, FALSE, tv) == FAIL)
4135 goto on_error;
4136 }
4137 else
4138 {
4139 if (list_slice_or_index(val_tv->vval.v_list, is_slice,
4140 n1, n2, FALSE, tv, TRUE) == FAIL)
4141 goto on_error;
4142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004143 }
4144 break;
4145
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004146 case ISN_ANYINDEX:
4147 case ISN_ANYSLICE:
4148 {
4149 int is_slice = iptr->isn_type == ISN_ANYSLICE;
4150 typval_T *var1, *var2;
4151 int res;
4152
4153 // index: composite is at stack-2, index at stack-1
4154 // slice: composite is at stack-3, indexes at stack-2 and
4155 // stack-1
4156 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004158 if (check_can_index(tv, TRUE, TRUE) == FAIL)
4159 goto on_error;
4160 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
4161 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01004162 res = eval_index_inner(tv, is_slice, var1, var2,
4163 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004164 clear_tv(var1);
4165 if (is_slice)
4166 clear_tv(var2);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004167 ectx->ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004168 if (res == FAIL)
4169 goto on_error;
4170 }
4171 break;
4172
Bram Moolenaar9af78762020-06-16 11:34:42 +02004173 case ISN_SLICE:
4174 {
4175 list_T *list;
4176 int count = iptr->isn_arg.number;
4177
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02004178 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02004179 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02004180 list = tv->vval.v_list;
4181
4182 // no error for short list, expect it to be checked earlier
4183 if (list != NULL && list->lv_len >= count)
4184 {
4185 list_T *newlist = list_slice(list,
4186 count, list->lv_len - 1);
4187
4188 if (newlist != NULL)
4189 {
4190 list_unref(list);
4191 tv->vval.v_list = newlist;
4192 ++newlist->lv_refcount;
4193 }
4194 }
4195 }
4196 break;
4197
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004198 case ISN_GETITEM:
4199 {
4200 listitem_T *li;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004201 getitem_T *gi = &iptr->isn_arg.getitem;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004202
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02004203 // Get list item: list is at stack-1, push item.
4204 // List type and length is checked for when compiling.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004205 tv = STACK_TV_BOT(-1 - gi->gi_with_op);
4206 li = list_find(tv->vval.v_list, gi->gi_index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004207
Bram Moolenaar35578162021-08-02 19:10:38 +02004208 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004209 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004210 ++ectx->ec_stack.ga_len;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004211 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01004212
4213 // Useful when used in unpack assignment. Reset at
4214 // ISN_DROP.
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02004215 ectx->ec_where.wt_index = gi->gi_index + 1;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004216 ectx->ec_where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004217 }
4218 break;
4219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004220 case ISN_MEMBER:
4221 {
4222 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004223 char_u *key;
4224 dictitem_T *di;
4225
4226 // dict member: dict is at stack-2, key at stack-1
4227 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004228 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004229 dict = tv->vval.v_dict;
4230
4231 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004232 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004233 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01004234 if (key == NULL)
4235 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02004236
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004237 if ((di = dict_find(dict, key, -1)) == NULL)
4238 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004240 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004241
4242 // If :silent! is used we will continue, make sure the
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004243 // stack contents makes sense and the dict stack is
4244 // updated.
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004245 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004246 --ectx->ec_stack.ga_len;
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004247 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004248 (void) dict_stack_save(tv);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01004249 tv->v_type = VAR_NUMBER;
4250 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004251 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004252 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004253 clear_tv(tv);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004254 --ectx->ec_stack.ga_len;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004255 // Put the dict used on the dict stack, it might be used by
4256 // a dict function later.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004257 tv = STACK_TV_BOT(-1);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004258 if (dict_stack_save(tv) == FAIL)
4259 goto on_fatal_error;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02004260 copy_tv(&di->di_tv, tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004261 }
4262 break;
4263
4264 // dict member with string key
4265 case ISN_STRINGMEMBER:
4266 {
4267 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 dictitem_T *di;
4269
4270 tv = STACK_TV_BOT(-1);
4271 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
4272 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004273 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004274 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02004275 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 }
4277 dict = tv->vval.v_dict;
4278
4279 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
4280 == NULL)
4281 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004282 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02004284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004285 }
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004286 // Put the dict used on the dict stack, it might be used by
4287 // a dict function later.
4288 if (dict_stack_save(tv) == FAIL)
4289 goto on_fatal_error;
4290
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004291 copy_tv(&di->di_tv, tv);
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004292 }
4293 break;
4294
4295 case ISN_CLEARDICT:
4296 dict_stack_drop();
4297 break;
4298
4299 case ISN_USEDICT:
4300 {
4301 typval_T *dict_tv = dict_stack_get_tv();
4302
4303 // Turn "dict.Func" into a partial for "Func" bound to
4304 // "dict". Don't do this when "Func" is already a partial
4305 // that was bound explicitly (pt_auto is FALSE).
4306 tv = STACK_TV_BOT(-1);
4307 if (dict_tv != NULL
4308 && dict_tv->v_type == VAR_DICT
4309 && dict_tv->vval.v_dict != NULL
4310 && (tv->v_type == VAR_FUNC
4311 || (tv->v_type == VAR_PARTIAL
4312 && (tv->vval.v_partial->pt_auto
4313 || tv->vval.v_partial->pt_dict == NULL))))
4314 dict_tv->vval.v_dict =
4315 make_partial(dict_tv->vval.v_dict, tv);
4316 dict_stack_drop();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 }
4318 break;
4319
4320 case ISN_NEGATENR:
4321 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004322 if (tv->v_type != VAR_NUMBER
4323#ifdef FEAT_FLOAT
4324 && tv->v_type != VAR_FLOAT
4325#endif
4326 )
4327 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004328 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004329 emsg(_(e_number_expected));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004330 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02004331 }
4332#ifdef FEAT_FLOAT
4333 if (tv->v_type == VAR_FLOAT)
4334 tv->vval.v_float = -tv->vval.v_float;
4335 else
4336#endif
4337 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338 break;
4339
4340 case ISN_CHECKNR:
4341 {
4342 int error = FALSE;
4343
4344 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02004345 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004347 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 (void)tv_get_number_chk(tv, &error);
4349 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004350 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004351 }
4352 break;
4353
4354 case ISN_CHECKTYPE:
4355 {
4356 checktype_T *ct = &iptr->isn_arg.type;
4357
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004358 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004360 if (!ectx->ec_where.wt_variable)
4361 ectx->ec_where.wt_index = ct->ct_arg_idx;
4362 if (check_typval_type(ct->ct_type, tv, ectx->ec_where)
4363 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02004364 goto on_error;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004365 if (!ectx->ec_where.wt_variable)
4366 ectx->ec_where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004367
4368 // number 0 is FALSE, number 1 is TRUE
4369 if (tv->v_type == VAR_NUMBER
4370 && ct->ct_type->tt_type == VAR_BOOL
4371 && (tv->vval.v_number == 0
4372 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02004374 tv->v_type = VAR_BOOL;
4375 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02004376 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
4378 }
4379 break;
4380
Bram Moolenaar9af78762020-06-16 11:34:42 +02004381 case ISN_CHECKLEN:
4382 {
4383 int min_len = iptr->isn_arg.checklen.cl_min_len;
4384 list_T *list = NULL;
4385
4386 tv = STACK_TV_BOT(-1);
4387 if (tv->v_type == VAR_LIST)
4388 list = tv->vval.v_list;
4389 if (list == NULL || list->lv_len < min_len
4390 || (list->lv_len > min_len
4391 && !iptr->isn_arg.checklen.cl_more_OK))
4392 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02004393 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02004394 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02004395 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004396 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004397 }
4398 }
4399 break;
4400
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004401 case ISN_SETTYPE:
4402 {
4403 checktype_T *ct = &iptr->isn_arg.type;
4404
4405 tv = STACK_TV_BOT(-1);
4406 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4407 {
4408 free_type(tv->vval.v_dict->dv_type);
4409 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
4410 }
4411 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
4412 {
4413 free_type(tv->vval.v_list->lv_type);
4414 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
4415 }
4416 }
4417 break;
4418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004420 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 {
4422 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004423 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004424
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004425 if (iptr->isn_type == ISN_2BOOL)
4426 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004427 tv = STACK_TV_BOT(iptr->isn_arg.tobool.offset);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004428 n = tv2bool(tv);
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004429 if (iptr->isn_arg.tobool.invert)
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004430 n = !n;
4431 }
4432 else
4433 {
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004434 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004435 SOURCING_LNUM = iptr->isn_lnum;
4436 n = tv_get_bool_chk(tv, &error);
4437 if (error)
4438 goto on_error;
4439 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004440 clear_tv(tv);
4441 tv->v_type = VAR_BOOL;
4442 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
4443 }
4444 break;
4445
4446 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004447 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01004448 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02004449 if (do_2string(STACK_TV_BOT(iptr->isn_arg.tostring.offset),
4450 iptr->isn_type == ISN_2STRING_ANY,
4451 iptr->isn_arg.tostring.tolerant) == FAIL)
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004452 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004453 break;
4454
Bram Moolenaar08597872020-12-10 19:43:40 +01004455 case ISN_RANGE:
4456 {
4457 exarg_T ea;
4458 char *errormsg;
4459
Bram Moolenaarece0b872021-01-08 20:40:45 +01004460 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01004461 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004462 ea.addr_type = ADDR_LINES;
4463 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01004464 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01004465 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01004466 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004467
Bram Moolenaar35578162021-08-02 19:10:38 +02004468 if (GA_GROW_FAILS(&ectx->ec_stack, 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004469 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004470 ++ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004471 tv = STACK_TV_BOT(-1);
4472 tv->v_type = VAR_NUMBER;
4473 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01004474 if (ea.addr_count == 0)
4475 tv->vval.v_number = curwin->w_cursor.lnum;
4476 else
4477 tv->vval.v_number = ea.line2;
4478 }
4479 break;
4480
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004481 case ISN_PUT:
4482 {
4483 int regname = iptr->isn_arg.put.put_regname;
4484 linenr_T lnum = iptr->isn_arg.put.put_lnum;
4485 char_u *expr = NULL;
4486 int dir = FORWARD;
4487
Bram Moolenaar08597872020-12-10 19:43:40 +01004488 if (lnum < -2)
4489 {
4490 // line number was put on the stack by ISN_RANGE
4491 tv = STACK_TV_BOT(-1);
4492 curwin->w_cursor.lnum = tv->vval.v_number;
4493 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
4494 dir = BACKWARD;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004495 --ectx->ec_stack.ga_len;
Bram Moolenaar08597872020-12-10 19:43:40 +01004496 }
4497 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004498 // :put! above cursor
4499 dir = BACKWARD;
4500 else if (lnum >= 0)
4501 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004502
4503 if (regname == '=')
4504 {
4505 tv = STACK_TV_BOT(-1);
4506 if (tv->v_type == VAR_STRING)
4507 expr = tv->vval.v_string;
4508 else
4509 {
4510 expr = typval2string(tv, TRUE); // allocates value
4511 clear_tv(tv);
4512 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004513 --ectx->ec_stack.ga_len;
Bram Moolenaara28639e2021-01-19 22:48:09 +01004514 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004515 check_cursor();
4516 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
4517 vim_free(expr);
4518 }
4519 break;
4520
Bram Moolenaar02194d22020-10-24 23:08:38 +02004521 case ISN_CMDMOD:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004522 ectx->ec_funclocal.floc_save_cmdmod = cmdmod;
4523 ectx->ec_funclocal.floc_restore_cmdmod = TRUE;
4524 ectx->ec_funclocal.floc_restore_cmdmod_stacklen =
4525 ectx->ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02004526 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
4527 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004528 break;
4529
Bram Moolenaar02194d22020-10-24 23:08:38 +02004530 case ISN_CMDMOD_REV:
4531 // filter regprog is owned by the instruction, don't free it
4532 cmdmod.cmod_filter_regmatch.regprog = NULL;
4533 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004534 cmdmod = ectx->ec_funclocal.floc_save_cmdmod;
4535 ectx->ec_funclocal.floc_restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004536 break;
4537
Bram Moolenaar792f7862020-11-23 08:31:18 +01004538 case ISN_UNPACK:
4539 {
4540 int count = iptr->isn_arg.unpack.unp_count;
4541 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
4542 list_T *l;
4543 listitem_T *li;
4544 int i;
4545
4546 // Check there is a valid list to unpack.
4547 tv = STACK_TV_BOT(-1);
4548 if (tv->v_type != VAR_LIST)
4549 {
4550 SOURCING_LNUM = iptr->isn_lnum;
4551 emsg(_(e_for_argument_must_be_sequence_of_lists));
4552 goto on_error;
4553 }
4554 l = tv->vval.v_list;
4555 if (l == NULL
4556 || l->lv_len < (semicolon ? count - 1 : count))
4557 {
4558 SOURCING_LNUM = iptr->isn_lnum;
4559 emsg(_(e_list_value_does_not_have_enough_items));
4560 goto on_error;
4561 }
4562 else if (!semicolon && l->lv_len > count)
4563 {
4564 SOURCING_LNUM = iptr->isn_lnum;
4565 emsg(_(e_list_value_has_more_items_than_targets));
4566 goto on_error;
4567 }
4568
4569 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar35578162021-08-02 19:10:38 +02004570 if (GA_GROW_FAILS(&ectx->ec_stack, count - 1))
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004571 goto theend;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004572 ectx->ec_stack.ga_len += count - 1;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004573
4574 // Variable after semicolon gets a list with the remaining
4575 // items.
4576 if (semicolon)
4577 {
4578 list_T *rem_list =
4579 list_alloc_with_items(l->lv_len - count + 1);
4580
4581 if (rem_list == NULL)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004582 goto theend;
Bram Moolenaar792f7862020-11-23 08:31:18 +01004583 tv = STACK_TV_BOT(-count);
4584 tv->vval.v_list = rem_list;
4585 ++rem_list->lv_refcount;
4586 tv->v_lock = 0;
4587 li = l->lv_first;
4588 for (i = 0; i < count - 1; ++i)
4589 li = li->li_next;
4590 for (i = 0; li != NULL; ++i)
4591 {
4592 list_set_item(rem_list, i, &li->li_tv);
4593 li = li->li_next;
4594 }
4595 --count;
4596 }
4597
4598 // Produce the values in reverse order, first item last.
4599 li = l->lv_first;
4600 for (i = 0; i < count; ++i)
4601 {
4602 tv = STACK_TV_BOT(-i - 1);
4603 copy_tv(&li->li_tv, tv);
4604 li = li->li_next;
4605 }
4606
4607 list_unref(l);
4608 }
4609 break;
4610
Bram Moolenaarb2049902021-01-24 12:53:53 +01004611 case ISN_PROF_START:
4612 case ISN_PROF_END:
4613 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01004614#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01004615 funccall_T cookie;
4616 ufunc_T *cur_ufunc =
4617 (((dfunc_T *)def_functions.ga_data)
Bram Moolenaarb69c6fb2021-06-14 20:40:37 +02004618 + ectx->ec_dfunc_idx)->df_ufunc;
Bram Moolenaarb2049902021-01-24 12:53:53 +01004619
4620 cookie.func = cur_ufunc;
4621 if (iptr->isn_type == ISN_PROF_START)
4622 {
4623 func_line_start(&cookie, iptr->isn_lnum);
4624 // if we get here the instruction is executed
4625 func_line_exec(&cookie);
4626 }
4627 else
4628 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01004629#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01004630 }
4631 break;
4632
Bram Moolenaare99d4222021-06-13 14:01:26 +02004633 case ISN_DEBUG:
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004634 handle_debug(iptr, ectx);
Bram Moolenaare99d4222021-06-13 14:01:26 +02004635 break;
4636
Bram Moolenaar389df252020-07-09 21:20:47 +02004637 case ISN_SHUFFLE:
4638 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01004639 typval_T tmp_tv;
4640 int item = iptr->isn_arg.shuffle.shfl_item;
4641 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02004642
4643 tmp_tv = *STACK_TV_BOT(-item);
4644 for ( ; up > 0 && item > 1; --up)
4645 {
4646 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
4647 --item;
4648 }
4649 *STACK_TV_BOT(-item) = tmp_tv;
4650 }
4651 break;
4652
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004653 case ISN_DROP:
Bram Moolenaar4c137212021-04-19 16:48:48 +02004654 --ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 clear_tv(STACK_TV_BOT(0));
Bram Moolenaar4c137212021-04-19 16:48:48 +02004656 ectx->ec_where.wt_index = 0;
4657 ectx->ec_where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004658 break;
4659 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004660 continue;
4661
Bram Moolenaard032f342020-07-18 18:13:02 +02004662func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004663 // Restore previous function. If the frame pointer is where we started
4664 // then there is none and we are done.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004665 if (ectx->ec_frame_idx == ectx->ec_initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02004666 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02004667
Bram Moolenaar4c137212021-04-19 16:48:48 +02004668 if (func_return(ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02004669 // only fails when out of memory
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004670 goto theend;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02004671 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02004672
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02004673on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004674 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004675 // If "emsg_silent" is set then ignore the error, unless it was set
4676 // when calling the function.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004677 if (did_emsg_cumul + did_emsg == ectx->ec_did_emsg_before
Bram Moolenaar56602ba2020-12-05 21:22:08 +01004678 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004679 {
4680 // If a sequence of instructions causes an error while ":silent!"
4681 // was used, restore the stack length and jump ahead to restoring
4682 // the cmdmod.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004683 if (ectx->ec_funclocal.floc_restore_cmdmod)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004684 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004685 while (ectx->ec_stack.ga_len
4686 > ectx->ec_funclocal.floc_restore_cmdmod_stacklen)
Bram Moolenaarf9041332021-01-21 19:41:16 +01004687 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004688 --ectx->ec_stack.ga_len;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004689 clear_tv(STACK_TV_BOT(0));
4690 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004691 while (ectx->ec_instr[ectx->ec_iidx].isn_type != ISN_CMDMOD_REV)
4692 ++ectx->ec_iidx;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004693 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01004694 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01004695 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01004696on_fatal_error:
4697 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01004698 // If we are not inside a try-catch started here, abort execution.
Bram Moolenaar4c137212021-04-19 16:48:48 +02004699 if (trylevel <= ectx->ec_trylevel_at_start)
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004700 goto theend;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701 }
4702
4703done:
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004704 ret = OK;
4705theend:
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02004706 dict_stack_clear(dict_stack_len_at_start);
Bram Moolenaarcbe178e2021-05-18 17:49:59 +02004707 ectx->ec_trylevel_at_start = save_trylevel_at_start;
4708 return ret;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004709}
4710
4711/*
Bram Moolenaarf18332f2021-05-07 17:55:55 +02004712 * Execute the instructions from a VAR_INSTR typeval and put the result in
4713 * "rettv".
4714 * Return OK or FAIL.
4715 */
4716 int
4717exe_typval_instr(typval_T *tv, typval_T *rettv)
4718{
4719 ectx_T *ectx = tv->vval.v_instr->instr_ectx;
4720 isn_T *save_instr = ectx->ec_instr;
4721 int save_iidx = ectx->ec_iidx;
4722 int res;
4723
4724 ectx->ec_instr = tv->vval.v_instr->instr_instr;
4725 res = exec_instructions(ectx);
4726 if (res == OK)
4727 {
4728 *rettv = *STACK_TV_BOT(-1);
4729 --ectx->ec_stack.ga_len;
4730 }
4731
4732 ectx->ec_instr = save_instr;
4733 ectx->ec_iidx = save_iidx;
4734
4735 return res;
4736}
4737
4738/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02004739 * Execute the instructions from an ISN_SUBSTITUTE command, which are in
4740 * "substitute_instr".
4741 */
4742 char_u *
4743exe_substitute_instr(void)
4744{
4745 ectx_T *ectx = substitute_instr->subs_ectx;
4746 isn_T *save_instr = ectx->ec_instr;
4747 int save_iidx = ectx->ec_iidx;
4748 char_u *res;
4749
4750 ectx->ec_instr = substitute_instr->subs_instr;
4751 if (exec_instructions(ectx) == OK)
Bram Moolenaar90193e62021-04-04 20:49:50 +02004752 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004753 typval_T *tv = STACK_TV_BOT(-1);
4754
Bram Moolenaar27523602021-06-05 21:36:19 +02004755 res = typval2string(tv, TRUE);
Bram Moolenaar4c137212021-04-19 16:48:48 +02004756 --ectx->ec_stack.ga_len;
4757 clear_tv(tv);
Bram Moolenaar90193e62021-04-04 20:49:50 +02004758 }
4759 else
4760 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02004761 substitute_instr->subs_status = FAIL;
4762 res = vim_strsave((char_u *)"");
Bram Moolenaar90193e62021-04-04 20:49:50 +02004763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004764
Bram Moolenaar4c137212021-04-19 16:48:48 +02004765 ectx->ec_instr = save_instr;
4766 ectx->ec_iidx = save_iidx;
4767
4768 return res;
4769}
4770
4771/*
4772 * Call a "def" function from old Vim script.
4773 * Return OK or FAIL.
4774 */
4775 int
4776call_def_function(
4777 ufunc_T *ufunc,
4778 int argc_arg, // nr of arguments
4779 typval_T *argv, // arguments
4780 partial_T *partial, // optional partial for context
4781 typval_T *rettv) // return value
4782{
4783 ectx_T ectx; // execution context
4784 int argc = argc_arg;
4785 typval_T *tv;
4786 int idx;
4787 int ret = FAIL;
4788 int defcount = ufunc->uf_args.ga_len - argc;
4789 sctx_T save_current_sctx = current_sctx;
4790 int did_emsg_before = did_emsg_cumul + did_emsg;
4791 int save_suppress_errthrow = suppress_errthrow;
4792 msglist_T **saved_msg_list = NULL;
4793 msglist_T *private_msg_list = NULL;
4794 int save_emsg_silent_def = emsg_silent_def;
4795 int save_did_emsg_def = did_emsg_def;
4796 int orig_funcdepth;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004797 int orig_nesting_level = ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004798
4799// Get pointer to item in the stack.
4800#undef STACK_TV
4801#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
4802
4803// Get pointer to item at the bottom of the stack, -1 is the bottom.
4804#undef STACK_TV_BOT
4805#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
4806
4807// Get pointer to a local variable on the stack. Negative for arguments.
4808#undef STACK_TV_VAR
4809#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
4810
Bram Moolenaar4f8f5422021-06-20 19:28:14 +02004811 // Update uf_has_breakpoint if needed.
4812 update_has_breakpoint(ufunc);
4813
Bram Moolenaar4c137212021-04-19 16:48:48 +02004814 if (ufunc->uf_def_status == UF_NOT_COMPILED
4815 || ufunc->uf_def_status == UF_COMPILE_ERROR
Bram Moolenaare99d4222021-06-13 14:01:26 +02004816 || (func_needs_compiling(ufunc, COMPILE_TYPE(ufunc))
4817 && compile_def_function(ufunc, FALSE, COMPILE_TYPE(ufunc), NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004818 == FAIL))
4819 {
4820 if (did_emsg_cumul + did_emsg == did_emsg_before)
4821 semsg(_(e_function_is_not_compiled_str),
4822 printable_func_name(ufunc));
4823 return FAIL;
4824 }
4825
4826 {
4827 // Check the function was really compiled.
4828 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4829 + ufunc->uf_dfunc_idx;
4830 if (INSTRUCTIONS(dfunc) == NULL)
4831 {
4832 iemsg("using call_def_function() on not compiled function");
4833 return FAIL;
4834 }
4835 }
4836
4837 // If depth of calling is getting too high, don't execute the function.
4838 orig_funcdepth = funcdepth_get();
4839 if (funcdepth_increment() == FAIL)
4840 return FAIL;
4841
4842 CLEAR_FIELD(ectx);
4843 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
4844 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
Bram Moolenaar35578162021-08-02 19:10:38 +02004845 if (GA_GROW_FAILS(&ectx.ec_stack, 20))
Bram Moolenaar4c137212021-04-19 16:48:48 +02004846 {
4847 funcdepth_decrement();
4848 return FAIL;
4849 }
4850 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
4851 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
4852 ectx.ec_did_emsg_before = did_emsg_before;
Bram Moolenaare99d4222021-06-13 14:01:26 +02004853 ++ex_nesting_level;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004854
4855 idx = argc - ufunc->uf_args.ga_len;
4856 if (idx > 0 && ufunc->uf_va_name == NULL)
4857 {
4858 if (idx == 1)
4859 emsg(_(e_one_argument_too_many));
4860 else
4861 semsg(_(e_nr_arguments_too_many), idx);
4862 goto failed_early;
4863 }
Bram Moolenaarc6d71532021-06-05 18:49:38 +02004864 idx = argc - ufunc->uf_args.ga_len + ufunc->uf_def_args.ga_len;
4865 if (idx < 0)
Bram Moolenaar8da6d6d2021-06-05 18:15:09 +02004866 {
4867 if (idx == -1)
4868 emsg(_(e_one_argument_too_few));
4869 else
4870 semsg(_(e_nr_arguments_too_few), -idx);
4871 goto failed_early;
4872 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004873
4874 // Put arguments on the stack, but no more than what the function expects.
4875 // A lambda can be called with more arguments than it uses.
4876 for (idx = 0; idx < argc
4877 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
4878 ++idx)
4879 {
4880 if (idx >= ufunc->uf_args.ga_len - ufunc->uf_def_args.ga_len
4881 && argv[idx].v_type == VAR_SPECIAL
4882 && argv[idx].vval.v_number == VVAL_NONE)
4883 {
4884 // Use the default value.
4885 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4886 }
4887 else
4888 {
4889 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
4890 && check_typval_arg_type(
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004891 ufunc->uf_arg_types[idx], &argv[idx],
4892 NULL, idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004893 goto failed_early;
4894 copy_tv(&argv[idx], STACK_TV_BOT(0));
4895 }
4896 ++ectx.ec_stack.ga_len;
4897 }
4898
4899 // Turn varargs into a list. Empty list if no args.
4900 if (ufunc->uf_va_name != NULL)
4901 {
4902 int vararg_count = argc - ufunc->uf_args.ga_len;
4903
4904 if (vararg_count < 0)
4905 vararg_count = 0;
4906 else
4907 argc -= vararg_count;
4908 if (exe_newlist(vararg_count, &ectx) == FAIL)
4909 goto failed_early;
4910
4911 // Check the type of the list items.
4912 tv = STACK_TV_BOT(-1);
4913 if (ufunc->uf_va_type != NULL
4914 && ufunc->uf_va_type != &t_list_any
4915 && ufunc->uf_va_type->tt_member != &t_any
4916 && tv->vval.v_list != NULL)
4917 {
4918 type_T *expected = ufunc->uf_va_type->tt_member;
4919 listitem_T *li = tv->vval.v_list->lv_first;
4920
4921 for (idx = 0; idx < vararg_count; ++idx)
4922 {
4923 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar7a3fe3e2021-07-22 14:58:47 +02004924 NULL, argc + idx + 1) == FAIL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004925 goto failed_early;
4926 li = li->li_next;
4927 }
4928 }
4929
4930 if (defcount > 0)
4931 // Move varargs list to below missing default arguments.
4932 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
4933 --ectx.ec_stack.ga_len;
4934 }
4935
4936 // Make space for omitted arguments, will store default value below.
4937 // Any varargs list goes after them.
4938 if (defcount > 0)
4939 for (idx = 0; idx < defcount; ++idx)
4940 {
4941 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
4942 ++ectx.ec_stack.ga_len;
4943 }
4944 if (ufunc->uf_va_name != NULL)
4945 ++ectx.ec_stack.ga_len;
4946
4947 // Frame pointer points to just after arguments.
4948 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
4949 ectx.ec_initial_frame_idx = ectx.ec_frame_idx;
4950
4951 {
4952 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4953 + ufunc->uf_dfunc_idx;
4954 ufunc_T *base_ufunc = dfunc->df_ufunc;
4955
4956 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
4957 // by copy_func().
4958 if (partial != NULL || base_ufunc->uf_partial != NULL)
4959 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004960 ectx.ec_outer_ref = ALLOC_CLEAR_ONE(outer_ref_T);
4961 if (ectx.ec_outer_ref == NULL)
Bram Moolenaar4c137212021-04-19 16:48:48 +02004962 goto failed_early;
4963 if (partial != NULL)
4964 {
4965 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
4966 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004967 if (current_ectx->ec_outer_ref != NULL
4968 && current_ectx->ec_outer_ref->or_outer != NULL)
4969 ectx.ec_outer_ref->or_outer =
4970 current_ectx->ec_outer_ref->or_outer;
Bram Moolenaar4c137212021-04-19 16:48:48 +02004971 }
4972 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004973 {
4974 ectx.ec_outer_ref->or_outer = &partial->pt_outer;
4975 ++partial->pt_refcount;
4976 ectx.ec_outer_ref->or_partial = partial;
4977 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004978 }
4979 else
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02004980 {
4981 ectx.ec_outer_ref->or_outer = &base_ufunc->uf_partial->pt_outer;
4982 ++base_ufunc->uf_partial->pt_refcount;
4983 ectx.ec_outer_ref->or_partial = base_ufunc->uf_partial;
4984 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02004985 }
4986 }
4987
4988 // dummy frame entries
4989 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
4990 {
4991 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
4992 ++ectx.ec_stack.ga_len;
4993 }
4994
4995 {
4996 // Reserve space for local variables and any closure reference count.
4997 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
4998 + ufunc->uf_dfunc_idx;
4999
5000 for (idx = 0; idx < dfunc->df_varcount; ++idx)
5001 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
5002 ectx.ec_stack.ga_len += dfunc->df_varcount;
5003 if (dfunc->df_has_closure)
5004 {
5005 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
5006 STACK_TV_VAR(idx)->vval.v_number = 0;
5007 ++ectx.ec_stack.ga_len;
5008 }
5009
5010 ectx.ec_instr = INSTRUCTIONS(dfunc);
5011 }
5012
5013 // Following errors are in the function, not the caller.
5014 // Commands behave like vim9script.
5015 estack_push_ufunc(ufunc, 1);
5016 current_sctx = ufunc->uf_script_ctx;
5017 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
5018
5019 // Use a specific location for storing error messages to be converted to an
5020 // exception.
5021 saved_msg_list = msg_list;
5022 msg_list = &private_msg_list;
5023
5024 // Do turn errors into exceptions.
5025 suppress_errthrow = FALSE;
5026
5027 // Do not delete the function while executing it.
5028 ++ufunc->uf_calls;
5029
5030 // When ":silent!" was used before calling then we still abort the
5031 // function. If ":silent!" is used in the function then we don't.
5032 emsg_silent_def = emsg_silent;
5033 did_emsg_def = 0;
5034
5035 ectx.ec_where.wt_index = 0;
5036 ectx.ec_where.wt_variable = FALSE;
5037
5038 // Execute the instructions until done.
5039 ret = exec_instructions(&ectx);
5040 if (ret == OK)
5041 {
5042 // function finished, get result from the stack.
5043 if (ufunc->uf_ret_type == &t_void)
5044 {
5045 rettv->v_type = VAR_VOID;
5046 }
5047 else
5048 {
5049 tv = STACK_TV_BOT(-1);
5050 *rettv = *tv;
5051 tv->v_type = VAR_UNKNOWN;
5052 }
5053 }
5054
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01005055 // When failed need to unwind the call stack.
Bram Moolenaar4c137212021-04-19 16:48:48 +02005056 while (ectx.ec_frame_idx != ectx.ec_initial_frame_idx)
5057 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005058
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005059 // Deal with any remaining closures, they may be in use somewhere.
5060 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01005061 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005062 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01005063 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
5064 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02005065
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005066 estack_pop();
5067 current_sctx = save_current_sctx;
5068
Bram Moolenaar2336c372021-12-06 15:06:54 +00005069 if (--ufunc->uf_calls <= 0 && ufunc->uf_refcount <= 0)
5070 // Function was unreferenced while being used, free it now.
5071 func_clear_free(ufunc, FALSE);
Bram Moolenaarc970e422021-03-17 15:03:04 +01005072
Bram Moolenaar352134b2020-10-17 22:04:08 +02005073 if (*msg_list != NULL && saved_msg_list != NULL)
5074 {
5075 msglist_T **plist = saved_msg_list;
5076
5077 // Append entries from the current msg_list (uncaught exceptions) to
5078 // the saved msg_list.
5079 while (*plist != NULL)
5080 plist = &(*plist)->next;
5081
5082 *plist = *msg_list;
5083 }
5084 msg_list = saved_msg_list;
5085
Bram Moolenaar4c137212021-04-19 16:48:48 +02005086 if (ectx.ec_funclocal.floc_restore_cmdmod)
Bram Moolenaar02194d22020-10-24 23:08:38 +02005087 {
5088 cmdmod.cmod_filter_regmatch.regprog = NULL;
5089 undo_cmdmod(&cmdmod);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005090 cmdmod = ectx.ec_funclocal.floc_save_cmdmod;
Bram Moolenaar02194d22020-10-24 23:08:38 +02005091 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01005092 emsg_silent_def = save_emsg_silent_def;
5093 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02005094
Bram Moolenaaree8580e2020-08-28 17:19:07 +02005095failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005096 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005097 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
5098 clear_tv(STACK_TV(idx));
Bram Moolenaare99d4222021-06-13 14:01:26 +02005099 ex_nesting_level = orig_nesting_level;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02005100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005101 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01005102 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005103 if (ectx.ec_outer_ref != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01005104 {
Bram Moolenaarc04f2a42021-06-09 19:30:03 +02005105 if (ectx.ec_outer_ref->or_outer_allocated)
5106 vim_free(ectx.ec_outer_ref->or_outer);
5107 partial_unref(ectx.ec_outer_ref->or_partial);
5108 vim_free(ectx.ec_outer_ref);
Bram Moolenaar0186e582021-01-10 18:33:11 +01005109 }
5110
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02005111 // Not sure if this is necessary.
5112 suppress_errthrow = save_suppress_errthrow;
5113
Bram Moolenaarb5841b92021-07-15 18:09:53 +02005114 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before
5115 && !need_rethrow)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02005116 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02005117 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01005118 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005119 return ret;
5120}
5121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005122/*
Bram Moolenaar4c137212021-04-19 16:48:48 +02005123 * List instructions "instr" up to "instr_count" or until ISN_FINISH.
5124 * "ufunc" has the source lines, NULL for the instructions of ISN_SUBSTITUTE.
5125 * "pfx" is prefixed to every line.
5126 */
5127 static void
5128list_instructions(char *pfx, isn_T *instr, int instr_count, ufunc_T *ufunc)
5129{
5130 int line_idx = 0;
5131 int prev_current = 0;
5132 int current;
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005133 int def_arg_idx = 0;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005134
5135 for (current = 0; current < instr_count; ++current)
5136 {
5137 isn_T *iptr = &instr[current];
5138 char *line;
5139
5140 if (ufunc != NULL)
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005141 {
Bram Moolenaar4c137212021-04-19 16:48:48 +02005142 while (line_idx < iptr->isn_lnum
5143 && line_idx < ufunc->uf_lines.ga_len)
5144 {
5145 if (current > prev_current)
5146 {
5147 msg_puts("\n\n");
5148 prev_current = current;
5149 }
5150 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
5151 if (line != NULL)
5152 msg(line);
5153 }
Bram Moolenaar9ce47ec2021-04-20 22:16:39 +02005154 if (iptr->isn_type == ISN_JUMP_IF_ARG_SET)
5155 {
5156 int first_def_arg = ufunc->uf_args.ga_len
5157 - ufunc->uf_def_args.ga_len;
5158
5159 if (def_arg_idx > 0)
5160 msg_puts("\n\n");
5161 msg_start();
5162 msg_puts(" ");
5163 msg_puts(((char **)(ufunc->uf_args.ga_data))[
5164 first_def_arg + def_arg_idx]);
5165 msg_puts(" = ");
5166 msg_puts(((char **)(ufunc->uf_def_args.ga_data))[def_arg_idx++]);
5167 msg_clr_eos();
5168 msg_end();
5169 }
5170 }
Bram Moolenaar4c137212021-04-19 16:48:48 +02005171
5172 switch (iptr->isn_type)
5173 {
5174 case ISN_EXEC:
5175 smsg("%s%4d EXEC %s", pfx, current, iptr->isn_arg.string);
5176 break;
Bram Moolenaar20677332021-06-06 17:02:53 +02005177 case ISN_EXEC_SPLIT:
5178 smsg("%s%4d EXEC_SPLIT %s", pfx, current, iptr->isn_arg.string);
5179 break;
Bram Moolenaare4eed8c2021-12-01 15:22:56 +00005180 case ISN_EXECRANGE:
5181 smsg("%s%4d EXECRANGE %s", pfx, current, iptr->isn_arg.string);
5182 break;
Bram Moolenaar3b1373b2021-05-17 00:01:42 +02005183 case ISN_LEGACY_EVAL:
5184 smsg("%s%4d EVAL legacy %s", pfx, current,
5185 iptr->isn_arg.string);
5186 break;
Bram Moolenaar2d1c57e2021-04-19 20:50:03 +02005187 case ISN_REDIRSTART:
5188 smsg("%s%4d REDIR", pfx, current);
5189 break;
5190 case ISN_REDIREND:
5191 smsg("%s%4d REDIR END%s", pfx, current,
5192 iptr->isn_arg.number ? " append" : "");
5193 break;
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005194 case ISN_CEXPR_AUCMD:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005195#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005196 smsg("%s%4d CEXPR pre %s", pfx, current,
5197 cexpr_get_auname(iptr->isn_arg.number));
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005198#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005199 break;
5200 case ISN_CEXPR_CORE:
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005201#ifdef FEAT_QUICKFIX
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005202 {
5203 cexprref_T *cer = iptr->isn_arg.cexpr.cexpr_ref;
5204
5205 smsg("%s%4d CEXPR core %s%s \"%s\"", pfx, current,
5206 cexpr_get_auname(cer->cer_cmdidx),
5207 cer->cer_forceit ? "!" : "",
5208 cer->cer_cmdline);
5209 }
Bram Moolenaarb7c97812021-05-05 22:51:39 +02005210#endif
Bram Moolenaar5f7d4c02021-05-05 21:31:39 +02005211 break;
Bram Moolenaarf18332f2021-05-07 17:55:55 +02005212 case ISN_INSTR:
5213 {
5214 smsg("%s%4d INSTR", pfx, current);
5215 list_instructions(" ", iptr->isn_arg.instr,
5216 INT_MAX, NULL);
5217 msg(" -------------");
5218 }
5219 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005220 case ISN_SUBSTITUTE:
5221 {
5222 subs_T *subs = &iptr->isn_arg.subs;
5223
5224 smsg("%s%4d SUBSTITUTE %s", pfx, current, subs->subs_cmd);
5225 list_instructions(" ", subs->subs_instr, INT_MAX, NULL);
5226 msg(" -------------");
5227 }
5228 break;
5229 case ISN_EXECCONCAT:
5230 smsg("%s%4d EXECCONCAT %lld", pfx, current,
5231 (varnumber_T)iptr->isn_arg.number);
5232 break;
5233 case ISN_ECHO:
5234 {
5235 echo_T *echo = &iptr->isn_arg.echo;
5236
5237 smsg("%s%4d %s %d", pfx, current,
5238 echo->echo_with_white ? "ECHO" : "ECHON",
5239 echo->echo_count);
5240 }
5241 break;
5242 case ISN_EXECUTE:
5243 smsg("%s%4d EXECUTE %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005244 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005245 break;
5246 case ISN_ECHOMSG:
5247 smsg("%s%4d ECHOMSG %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005248 (varnumber_T)(iptr->isn_arg.number));
5249 break;
5250 case ISN_ECHOCONSOLE:
5251 smsg("%s%4d ECHOCONSOLE %lld", pfx, current,
5252 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005253 break;
5254 case ISN_ECHOERR:
5255 smsg("%s%4d ECHOERR %lld", pfx, current,
Bram Moolenaar7de62622021-08-07 15:05:47 +02005256 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005257 break;
5258 case ISN_LOAD:
5259 {
5260 if (iptr->isn_arg.number < 0)
5261 smsg("%s%4d LOAD arg[%lld]", pfx, current,
5262 (varnumber_T)(iptr->isn_arg.number
5263 + STACK_FRAME_SIZE));
5264 else
5265 smsg("%s%4d LOAD $%lld", pfx, current,
5266 (varnumber_T)(iptr->isn_arg.number));
5267 }
5268 break;
5269 case ISN_LOADOUTER:
5270 {
5271 if (iptr->isn_arg.number < 0)
5272 smsg("%s%4d LOADOUTER level %d arg[%d]", pfx, current,
5273 iptr->isn_arg.outer.outer_depth,
5274 iptr->isn_arg.outer.outer_idx
5275 + STACK_FRAME_SIZE);
5276 else
5277 smsg("%s%4d LOADOUTER level %d $%d", pfx, current,
5278 iptr->isn_arg.outer.outer_depth,
5279 iptr->isn_arg.outer.outer_idx);
5280 }
5281 break;
5282 case ISN_LOADV:
5283 smsg("%s%4d LOADV v:%s", pfx, current,
5284 get_vim_var_name(iptr->isn_arg.number));
5285 break;
5286 case ISN_LOADSCRIPT:
5287 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005288 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5289 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5290 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005291
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005292 sv = get_script_svar(sref, -1);
5293 if (sv == NULL)
5294 smsg("%s%4d LOADSCRIPT [deleted] from %s",
5295 pfx, current, si->sn_name);
5296 else
5297 smsg("%s%4d LOADSCRIPT %s-%d from %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005298 sv->sv_name,
5299 sref->sref_idx,
5300 si->sn_name);
5301 }
5302 break;
5303 case ISN_LOADS:
5304 {
5305 scriptitem_T *si = SCRIPT_ITEM(
5306 iptr->isn_arg.loadstore.ls_sid);
5307
5308 smsg("%s%4d LOADS s:%s from %s", pfx, current,
5309 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5310 }
5311 break;
5312 case ISN_LOADAUTO:
5313 smsg("%s%4d LOADAUTO %s", pfx, current, iptr->isn_arg.string);
5314 break;
5315 case ISN_LOADG:
5316 smsg("%s%4d LOADG g:%s", pfx, current, iptr->isn_arg.string);
5317 break;
5318 case ISN_LOADB:
5319 smsg("%s%4d LOADB b:%s", pfx, current, iptr->isn_arg.string);
5320 break;
5321 case ISN_LOADW:
5322 smsg("%s%4d LOADW w:%s", pfx, current, iptr->isn_arg.string);
5323 break;
5324 case ISN_LOADT:
5325 smsg("%s%4d LOADT t:%s", pfx, current, iptr->isn_arg.string);
5326 break;
5327 case ISN_LOADGDICT:
5328 smsg("%s%4d LOAD g:", pfx, current);
5329 break;
5330 case ISN_LOADBDICT:
5331 smsg("%s%4d LOAD b:", pfx, current);
5332 break;
5333 case ISN_LOADWDICT:
5334 smsg("%s%4d LOAD w:", pfx, current);
5335 break;
5336 case ISN_LOADTDICT:
5337 smsg("%s%4d LOAD t:", pfx, current);
5338 break;
5339 case ISN_LOADOPT:
5340 smsg("%s%4d LOADOPT %s", pfx, current, iptr->isn_arg.string);
5341 break;
5342 case ISN_LOADENV:
5343 smsg("%s%4d LOADENV %s", pfx, current, iptr->isn_arg.string);
5344 break;
5345 case ISN_LOADREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005346 smsg("%s%4d LOADREG @%c", pfx, current,
5347 (int)(iptr->isn_arg.number));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005348 break;
5349
5350 case ISN_STORE:
5351 if (iptr->isn_arg.number < 0)
5352 smsg("%s%4d STORE arg[%lld]", pfx, current,
5353 iptr->isn_arg.number + STACK_FRAME_SIZE);
5354 else
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005355 smsg("%s%4d STORE $%lld", pfx, current,
5356 iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005357 break;
5358 case ISN_STOREOUTER:
5359 {
5360 if (iptr->isn_arg.number < 0)
5361 smsg("%s%4d STOREOUTEr level %d arg[%d]", pfx, current,
5362 iptr->isn_arg.outer.outer_depth,
5363 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
5364 else
5365 smsg("%s%4d STOREOUTER level %d $%d", pfx, current,
5366 iptr->isn_arg.outer.outer_depth,
5367 iptr->isn_arg.outer.outer_idx);
5368 }
5369 break;
5370 case ISN_STOREV:
5371 smsg("%s%4d STOREV v:%s", pfx, current,
5372 get_vim_var_name(iptr->isn_arg.number));
5373 break;
5374 case ISN_STOREAUTO:
5375 smsg("%s%4d STOREAUTO %s", pfx, current, iptr->isn_arg.string);
5376 break;
5377 case ISN_STOREG:
5378 smsg("%s%4d STOREG %s", pfx, current, iptr->isn_arg.string);
5379 break;
5380 case ISN_STOREB:
5381 smsg("%s%4d STOREB %s", pfx, current, iptr->isn_arg.string);
5382 break;
5383 case ISN_STOREW:
5384 smsg("%s%4d STOREW %s", pfx, current, iptr->isn_arg.string);
5385 break;
5386 case ISN_STORET:
5387 smsg("%s%4d STORET %s", pfx, current, iptr->isn_arg.string);
5388 break;
5389 case ISN_STORES:
5390 {
5391 scriptitem_T *si = SCRIPT_ITEM(
5392 iptr->isn_arg.loadstore.ls_sid);
5393
5394 smsg("%s%4d STORES %s in %s", pfx, current,
5395 iptr->isn_arg.loadstore.ls_name, si->sn_name);
5396 }
5397 break;
5398 case ISN_STORESCRIPT:
5399 {
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005400 scriptref_T *sref = iptr->isn_arg.script.scriptref;
5401 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
5402 svar_T *sv;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005403
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005404 sv = get_script_svar(sref, -1);
5405 if (sv == NULL)
5406 smsg("%s%4d STORESCRIPT [deleted] in %s",
5407 pfx, current, si->sn_name);
5408 else
5409 smsg("%s%4d STORESCRIPT %s-%d in %s", pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005410 sv->sv_name,
5411 sref->sref_idx,
5412 si->sn_name);
5413 }
5414 break;
5415 case ISN_STOREOPT:
Bram Moolenaardcb53be2021-12-09 14:23:43 +00005416 case ISN_STOREFUNCOPT:
5417 smsg("%s%4d %s &%s", pfx, current,
5418 iptr->isn_type == ISN_STOREOPT ? "STOREOPT" : "STOREFUNCOPT",
Bram Moolenaar4c137212021-04-19 16:48:48 +02005419 iptr->isn_arg.storeopt.so_name);
5420 break;
5421 case ISN_STOREENV:
5422 smsg("%s%4d STOREENV $%s", pfx, current, iptr->isn_arg.string);
5423 break;
5424 case ISN_STOREREG:
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005425 smsg("%s%4d STOREREG @%c", pfx, current,
5426 (int)iptr->isn_arg.number);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005427 break;
5428 case ISN_STORENR:
5429 smsg("%s%4d STORE %lld in $%d", pfx, current,
5430 iptr->isn_arg.storenr.stnr_val,
5431 iptr->isn_arg.storenr.stnr_idx);
5432 break;
5433
5434 case ISN_STOREINDEX:
5435 smsg("%s%4d STOREINDEX %s", pfx, current,
5436 vartype_name(iptr->isn_arg.vartype));
5437 break;
5438
5439 case ISN_STORERANGE:
5440 smsg("%s%4d STORERANGE", pfx, current);
5441 break;
5442
5443 // constants
5444 case ISN_PUSHNR:
5445 smsg("%s%4d PUSHNR %lld", pfx, current,
5446 (varnumber_T)(iptr->isn_arg.number));
5447 break;
5448 case ISN_PUSHBOOL:
5449 case ISN_PUSHSPEC:
5450 smsg("%s%4d PUSH %s", pfx, current,
5451 get_var_special_name(iptr->isn_arg.number));
5452 break;
5453 case ISN_PUSHF:
5454#ifdef FEAT_FLOAT
5455 smsg("%s%4d PUSHF %g", pfx, current, iptr->isn_arg.fnumber);
5456#endif
5457 break;
5458 case ISN_PUSHS:
5459 smsg("%s%4d PUSHS \"%s\"", pfx, current, iptr->isn_arg.string);
5460 break;
5461 case ISN_PUSHBLOB:
5462 {
5463 char_u *r;
5464 char_u numbuf[NUMBUFLEN];
5465 char_u *tofree;
5466
5467 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
5468 smsg("%s%4d PUSHBLOB %s", pfx, current, r);
5469 vim_free(tofree);
5470 }
5471 break;
5472 case ISN_PUSHFUNC:
5473 {
5474 char *name = (char *)iptr->isn_arg.string;
5475
5476 smsg("%s%4d PUSHFUNC \"%s\"", pfx, current,
5477 name == NULL ? "[none]" : name);
5478 }
5479 break;
5480 case ISN_PUSHCHANNEL:
5481#ifdef FEAT_JOB_CHANNEL
5482 {
5483 channel_T *channel = iptr->isn_arg.channel;
5484
5485 smsg("%s%4d PUSHCHANNEL %d", pfx, current,
5486 channel == NULL ? 0 : channel->ch_id);
5487 }
5488#endif
5489 break;
5490 case ISN_PUSHJOB:
5491#ifdef FEAT_JOB_CHANNEL
5492 {
5493 typval_T tv;
5494 char_u *name;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005495 char_u buf[NUMBUFLEN];
Bram Moolenaar4c137212021-04-19 16:48:48 +02005496
5497 tv.v_type = VAR_JOB;
5498 tv.vval.v_job = iptr->isn_arg.job;
Bram Moolenaar1328bde2021-06-05 20:51:38 +02005499 name = job_to_string_buf(&tv, buf);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005500 smsg("%s%4d PUSHJOB \"%s\"", pfx, current, name);
5501 }
5502#endif
5503 break;
5504 case ISN_PUSHEXC:
5505 smsg("%s%4d PUSH v:exception", pfx, current);
5506 break;
5507 case ISN_UNLET:
5508 smsg("%s%4d UNLET%s %s", pfx, current,
5509 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5510 iptr->isn_arg.unlet.ul_name);
5511 break;
5512 case ISN_UNLETENV:
5513 smsg("%s%4d UNLETENV%s $%s", pfx, current,
5514 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
5515 iptr->isn_arg.unlet.ul_name);
5516 break;
5517 case ISN_UNLETINDEX:
5518 smsg("%s%4d UNLETINDEX", pfx, current);
5519 break;
5520 case ISN_UNLETRANGE:
5521 smsg("%s%4d UNLETRANGE", pfx, current);
5522 break;
Bram Moolenaaraacc9662021-08-13 19:40:51 +02005523 case ISN_LOCKUNLOCK:
5524 smsg("%s%4d LOCKUNLOCK %s", pfx, current, iptr->isn_arg.string);
5525 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005526 case ISN_LOCKCONST:
5527 smsg("%s%4d LOCKCONST", pfx, current);
5528 break;
5529 case ISN_NEWLIST:
5530 smsg("%s%4d NEWLIST size %lld", pfx, current,
5531 (varnumber_T)(iptr->isn_arg.number));
5532 break;
5533 case ISN_NEWDICT:
5534 smsg("%s%4d NEWDICT size %lld", pfx, current,
5535 (varnumber_T)(iptr->isn_arg.number));
5536 break;
5537
5538 // function call
5539 case ISN_BCALL:
5540 {
5541 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
5542
5543 smsg("%s%4d BCALL %s(argc %d)", pfx, current,
5544 internal_func_name(cbfunc->cbf_idx),
5545 cbfunc->cbf_argcount);
5546 }
5547 break;
5548 case ISN_DCALL:
5549 {
5550 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
5551 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5552 + cdfunc->cdf_idx;
5553
5554 smsg("%s%4d DCALL %s(argc %d)", pfx, current,
Bram Moolenaar6db660b2021-08-01 14:08:54 +02005555 printable_func_name(df->df_ufunc),
5556 cdfunc->cdf_argcount);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005557 }
5558 break;
5559 case ISN_UCALL:
5560 {
5561 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
5562
5563 smsg("%s%4d UCALL %s(argc %d)", pfx, current,
5564 cufunc->cuf_name, cufunc->cuf_argcount);
5565 }
5566 break;
5567 case ISN_PCALL:
5568 {
5569 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
5570
5571 smsg("%s%4d PCALL%s (argc %d)", pfx, current,
5572 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
5573 }
5574 break;
5575 case ISN_PCALL_END:
5576 smsg("%s%4d PCALL end", pfx, current);
5577 break;
5578 case ISN_RETURN:
5579 smsg("%s%4d RETURN", pfx, current);
5580 break;
Bram Moolenaarf57b43c2021-06-15 22:13:27 +02005581 case ISN_RETURN_VOID:
5582 smsg("%s%4d RETURN void", pfx, current);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005583 break;
5584 case ISN_FUNCREF:
5585 {
5586 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar38453522021-11-28 22:00:12 +00005587 char_u *name;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005588
Bram Moolenaar38453522021-11-28 22:00:12 +00005589 if (funcref->fr_func_name == NULL)
5590 {
5591 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
5592 + funcref->fr_dfunc_idx;
5593 name = df->df_ufunc->uf_name;
5594 }
5595 else
5596 name = funcref->fr_func_name;
5597 smsg("%s%4d FUNCREF %s", pfx, current, name);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005598 }
5599 break;
5600
5601 case ISN_NEWFUNC:
5602 {
5603 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
5604
5605 smsg("%s%4d NEWFUNC %s %s", pfx, current,
5606 newfunc->nf_lambda, newfunc->nf_global);
5607 }
5608 break;
5609
5610 case ISN_DEF:
5611 {
5612 char_u *name = iptr->isn_arg.string;
5613
5614 smsg("%s%4d DEF %s", pfx, current,
5615 name == NULL ? (char_u *)"" : name);
5616 }
5617 break;
5618
5619 case ISN_JUMP:
5620 {
5621 char *when = "?";
5622
5623 switch (iptr->isn_arg.jump.jump_when)
5624 {
5625 case JUMP_ALWAYS:
5626 when = "JUMP";
5627 break;
Bram Moolenaar1a7ee4d2021-09-16 16:15:07 +02005628 case JUMP_NEVER:
5629 iemsg("JUMP_NEVER should not be used");
5630 break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005631 case JUMP_AND_KEEP_IF_TRUE:
5632 when = "JUMP_AND_KEEP_IF_TRUE";
5633 break;
5634 case JUMP_IF_FALSE:
5635 when = "JUMP_IF_FALSE";
5636 break;
5637 case JUMP_AND_KEEP_IF_FALSE:
5638 when = "JUMP_AND_KEEP_IF_FALSE";
5639 break;
5640 case JUMP_IF_COND_FALSE:
5641 when = "JUMP_IF_COND_FALSE";
5642 break;
5643 case JUMP_IF_COND_TRUE:
5644 when = "JUMP_IF_COND_TRUE";
5645 break;
5646 }
5647 smsg("%s%4d %s -> %d", pfx, current, when,
5648 iptr->isn_arg.jump.jump_where);
5649 }
5650 break;
5651
5652 case ISN_JUMP_IF_ARG_SET:
5653 smsg("%s%4d JUMP_IF_ARG_SET arg[%d] -> %d", pfx, current,
5654 iptr->isn_arg.jumparg.jump_arg_off + STACK_FRAME_SIZE,
5655 iptr->isn_arg.jump.jump_where);
5656 break;
5657
5658 case ISN_FOR:
5659 {
5660 forloop_T *forloop = &iptr->isn_arg.forloop;
5661
5662 smsg("%s%4d FOR $%d -> %d", pfx, current,
5663 forloop->for_idx, forloop->for_end);
5664 }
5665 break;
5666
5667 case ISN_TRY:
5668 {
5669 try_T *try = &iptr->isn_arg.try;
5670
5671 if (try->try_ref->try_finally == 0)
5672 smsg("%s%4d TRY catch -> %d, endtry -> %d",
5673 pfx, current,
5674 try->try_ref->try_catch,
5675 try->try_ref->try_endtry);
5676 else
5677 smsg("%s%4d TRY catch -> %d, finally -> %d, endtry -> %d",
5678 pfx, current,
5679 try->try_ref->try_catch,
5680 try->try_ref->try_finally,
5681 try->try_ref->try_endtry);
5682 }
5683 break;
5684 case ISN_CATCH:
Bram Moolenaar4c137212021-04-19 16:48:48 +02005685 smsg("%s%4d CATCH", pfx, current);
5686 break;
5687 case ISN_TRYCONT:
5688 {
5689 trycont_T *trycont = &iptr->isn_arg.trycont;
5690
5691 smsg("%s%4d TRY-CONTINUE %d level%s -> %d", pfx, current,
5692 trycont->tct_levels,
5693 trycont->tct_levels == 1 ? "" : "s",
5694 trycont->tct_where);
5695 }
5696 break;
5697 case ISN_FINALLY:
5698 smsg("%s%4d FINALLY", pfx, current);
5699 break;
5700 case ISN_ENDTRY:
5701 smsg("%s%4d ENDTRY", pfx, current);
5702 break;
5703 case ISN_THROW:
5704 smsg("%s%4d THROW", pfx, current);
5705 break;
5706
5707 // expression operations on number
5708 case ISN_OPNR:
5709 case ISN_OPFLOAT:
5710 case ISN_OPANY:
5711 {
5712 char *what;
5713 char *ins;
5714
5715 switch (iptr->isn_arg.op.op_type)
5716 {
5717 case EXPR_MULT: what = "*"; break;
5718 case EXPR_DIV: what = "/"; break;
5719 case EXPR_REM: what = "%"; break;
5720 case EXPR_SUB: what = "-"; break;
5721 case EXPR_ADD: what = "+"; break;
5722 default: what = "???"; break;
5723 }
5724 switch (iptr->isn_type)
5725 {
5726 case ISN_OPNR: ins = "OPNR"; break;
5727 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
5728 case ISN_OPANY: ins = "OPANY"; break;
5729 default: ins = "???"; break;
5730 }
5731 smsg("%s%4d %s %s", pfx, current, ins, what);
5732 }
5733 break;
5734
5735 case ISN_COMPAREBOOL:
5736 case ISN_COMPARESPECIAL:
5737 case ISN_COMPARENR:
5738 case ISN_COMPAREFLOAT:
5739 case ISN_COMPARESTRING:
5740 case ISN_COMPAREBLOB:
5741 case ISN_COMPARELIST:
5742 case ISN_COMPAREDICT:
5743 case ISN_COMPAREFUNC:
5744 case ISN_COMPAREANY:
5745 {
5746 char *p;
5747 char buf[10];
5748 char *type;
5749
5750 switch (iptr->isn_arg.op.op_type)
5751 {
5752 case EXPR_EQUAL: p = "=="; break;
5753 case EXPR_NEQUAL: p = "!="; break;
5754 case EXPR_GREATER: p = ">"; break;
5755 case EXPR_GEQUAL: p = ">="; break;
5756 case EXPR_SMALLER: p = "<"; break;
5757 case EXPR_SEQUAL: p = "<="; break;
5758 case EXPR_MATCH: p = "=~"; break;
5759 case EXPR_IS: p = "is"; break;
5760 case EXPR_ISNOT: p = "isnot"; break;
5761 case EXPR_NOMATCH: p = "!~"; break;
5762 default: p = "???"; break;
5763 }
5764 STRCPY(buf, p);
5765 if (iptr->isn_arg.op.op_ic == TRUE)
5766 strcat(buf, "?");
5767 switch(iptr->isn_type)
5768 {
5769 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
5770 case ISN_COMPARESPECIAL:
5771 type = "COMPARESPECIAL"; break;
5772 case ISN_COMPARENR: type = "COMPARENR"; break;
5773 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
5774 case ISN_COMPARESTRING:
5775 type = "COMPARESTRING"; break;
5776 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
5777 case ISN_COMPARELIST: type = "COMPARELIST"; break;
5778 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
5779 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
5780 case ISN_COMPAREANY: type = "COMPAREANY"; break;
5781 default: type = "???"; break;
5782 }
5783
5784 smsg("%s%4d %s %s", pfx, current, type, buf);
5785 }
5786 break;
5787
5788 case ISN_ADDLIST: smsg("%s%4d ADDLIST", pfx, current); break;
5789 case ISN_ADDBLOB: smsg("%s%4d ADDBLOB", pfx, current); break;
5790
5791 // expression operations
5792 case ISN_CONCAT: smsg("%s%4d CONCAT", pfx, current); break;
5793 case ISN_STRINDEX: smsg("%s%4d STRINDEX", pfx, current); break;
5794 case ISN_STRSLICE: smsg("%s%4d STRSLICE", pfx, current); break;
5795 case ISN_BLOBINDEX: smsg("%s%4d BLOBINDEX", pfx, current); break;
5796 case ISN_BLOBSLICE: smsg("%s%4d BLOBSLICE", pfx, current); break;
5797 case ISN_LISTAPPEND: smsg("%s%4d LISTAPPEND", pfx, current); break;
5798 case ISN_BLOBAPPEND: smsg("%s%4d BLOBAPPEND", pfx, current); break;
5799 case ISN_LISTINDEX: smsg("%s%4d LISTINDEX", pfx, current); break;
5800 case ISN_LISTSLICE: smsg("%s%4d LISTSLICE", pfx, current); break;
5801 case ISN_ANYINDEX: smsg("%s%4d ANYINDEX", pfx, current); break;
5802 case ISN_ANYSLICE: smsg("%s%4d ANYSLICE", pfx, current); break;
5803 case ISN_SLICE: smsg("%s%4d SLICE %lld",
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005804 pfx, current, iptr->isn_arg.number); break;
Bram Moolenaar035bd1c2021-06-21 19:44:11 +02005805 case ISN_GETITEM: smsg("%s%4d ITEM %lld%s", pfx, current,
5806 iptr->isn_arg.getitem.gi_index,
5807 iptr->isn_arg.getitem.gi_with_op ?
5808 " with op" : ""); break;
Bram Moolenaar4c137212021-04-19 16:48:48 +02005809 case ISN_MEMBER: smsg("%s%4d MEMBER", pfx, current); break;
5810 case ISN_STRINGMEMBER: smsg("%s%4d MEMBER %s", pfx, current,
5811 iptr->isn_arg.string); break;
Bram Moolenaarb1b6f4d2021-09-13 18:25:54 +02005812 case ISN_CLEARDICT: smsg("%s%4d CLEARDICT", pfx, current); break;
5813 case ISN_USEDICT: smsg("%s%4d USEDICT", pfx, current); break;
5814
Bram Moolenaar4c137212021-04-19 16:48:48 +02005815 case ISN_NEGATENR: smsg("%s%4d NEGATENR", pfx, current); break;
5816
5817 case ISN_CHECKNR: smsg("%s%4d CHECKNR", pfx, current); break;
5818 case ISN_CHECKTYPE:
5819 {
5820 checktype_T *ct = &iptr->isn_arg.type;
5821 char *tofree;
5822
5823 if (ct->ct_arg_idx == 0)
5824 smsg("%s%4d CHECKTYPE %s stack[%d]", pfx, current,
5825 type_name(ct->ct_type, &tofree),
5826 (int)ct->ct_off);
5827 else
Bram Moolenaar4f0884d2021-08-11 21:49:23 +02005828 smsg("%s%4d CHECKTYPE %s stack[%d] arg %d",
5829 pfx, current,
Bram Moolenaar4c137212021-04-19 16:48:48 +02005830 type_name(ct->ct_type, &tofree),
5831 (int)ct->ct_off,
5832 (int)ct->ct_arg_idx);
5833 vim_free(tofree);
5834 break;
5835 }
5836 case ISN_CHECKLEN: smsg("%s%4d CHECKLEN %s%d", pfx, current,
5837 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
5838 iptr->isn_arg.checklen.cl_min_len);
5839 break;
5840 case ISN_SETTYPE:
5841 {
5842 char *tofree;
5843
5844 smsg("%s%4d SETTYPE %s", pfx, current,
5845 type_name(iptr->isn_arg.type.ct_type, &tofree));
5846 vim_free(tofree);
5847 break;
5848 }
5849 case ISN_COND2BOOL: smsg("%s%4d COND2BOOL", pfx, current); break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005850 case ISN_2BOOL: if (iptr->isn_arg.tobool.invert)
5851 smsg("%s%4d INVERT %d (!val)", pfx, current,
5852 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005853 else
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005854 smsg("%s%4d 2BOOL %d (!!val)", pfx, current,
5855 iptr->isn_arg.tobool.offset);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005856 break;
5857 case ISN_2STRING: smsg("%s%4d 2STRING stack[%lld]", pfx, current,
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005858 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005859 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005860 case ISN_2STRING_ANY: smsg("%s%4d 2STRING_ANY stack[%lld]",
5861 pfx, current,
5862 (varnumber_T)(iptr->isn_arg.tostring.offset));
Bram Moolenaar4c137212021-04-19 16:48:48 +02005863 break;
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005864 case ISN_RANGE: smsg("%s%4d RANGE %s", pfx, current,
5865 iptr->isn_arg.string);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005866 break;
5867 case ISN_PUT:
5868 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
5869 smsg("%s%4d PUT %c above range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005870 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005871 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
5872 smsg("%s%4d PUT %c range",
Bram Moolenaar5fa9b242021-06-04 21:00:32 +02005873 pfx, current, iptr->isn_arg.put.put_regname);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005874 else
5875 smsg("%s%4d PUT %c %ld", pfx, current,
5876 iptr->isn_arg.put.put_regname,
5877 (long)iptr->isn_arg.put.put_lnum);
5878 break;
5879
Bram Moolenaar4c137212021-04-19 16:48:48 +02005880 case ISN_CMDMOD:
5881 {
5882 char_u *buf;
5883 size_t len = produce_cmdmods(
5884 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5885
5886 buf = alloc(len + 1);
Dominique Pelle5a9e5842021-07-24 19:32:12 +02005887 if (likely(buf != NULL))
Bram Moolenaar4c137212021-04-19 16:48:48 +02005888 {
5889 (void)produce_cmdmods(
5890 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
5891 smsg("%s%4d CMDMOD %s", pfx, current, buf);
5892 vim_free(buf);
5893 }
5894 break;
5895 }
5896 case ISN_CMDMOD_REV: smsg("%s%4d CMDMOD_REV", pfx, current); break;
5897
5898 case ISN_PROF_START:
Bram Moolenaare99d4222021-06-13 14:01:26 +02005899 smsg("%s%4d PROFILE START line %d", pfx, current,
5900 iptr->isn_lnum);
Bram Moolenaar4c137212021-04-19 16:48:48 +02005901 break;
5902
5903 case ISN_PROF_END:
5904 smsg("%s%4d PROFILE END", pfx, current);
5905 break;
5906
Bram Moolenaare99d4222021-06-13 14:01:26 +02005907 case ISN_DEBUG:
Bram Moolenaar8cec9272021-06-23 20:20:53 +02005908 smsg("%s%4d DEBUG line %d-%d varcount %lld", pfx, current,
5909 iptr->isn_arg.debug.dbg_break_lnum + 1,
5910 iptr->isn_lnum,
5911 iptr->isn_arg.debug.dbg_var_names_len);
Bram Moolenaare99d4222021-06-13 14:01:26 +02005912 break;
5913
Bram Moolenaar4c137212021-04-19 16:48:48 +02005914 case ISN_UNPACK: smsg("%s%4d UNPACK %d%s", pfx, current,
5915 iptr->isn_arg.unpack.unp_count,
5916 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
5917 break;
5918 case ISN_SHUFFLE: smsg("%s%4d SHUFFLE %d up %d", pfx, current,
5919 iptr->isn_arg.shuffle.shfl_item,
5920 iptr->isn_arg.shuffle.shfl_up);
5921 break;
5922 case ISN_DROP: smsg("%s%4d DROP", pfx, current); break;
5923
5924 case ISN_FINISH: // End of list of instructions for ISN_SUBSTITUTE.
5925 return;
5926 }
5927
5928 out_flush(); // output one line at a time
5929 ui_breakcheck();
5930 if (got_int)
5931 break;
5932 }
5933}
5934
5935/*
Bram Moolenaar4ee9d8e2021-06-13 18:38:48 +02005936 * Handle command line completion for the :disassemble command.
5937 */
5938 void
5939set_context_in_disassemble_cmd(expand_T *xp, char_u *arg)
5940{
5941 char_u *p;
5942
5943 // Default: expand user functions, "debug" and "profile"
5944 xp->xp_context = EXPAND_DISASSEMBLE;
5945 xp->xp_pattern = arg;
5946
5947 // first argument already typed: only user function names
5948 if (*arg != NUL && *(p = skiptowhite(arg)) != NUL)
5949 {
5950 xp->xp_context = EXPAND_USER_FUNC;
5951 xp->xp_pattern = skipwhite(p);
5952 }
5953}
5954
5955/*
5956 * Function given to ExpandGeneric() to obtain the list of :disassemble
5957 * arguments.
5958 */
5959 char_u *
5960get_disassemble_argument(expand_T *xp, int idx)
5961{
5962 if (idx == 0)
5963 return (char_u *)"debug";
5964 if (idx == 1)
5965 return (char_u *)"profile";
5966 return get_user_func_name(xp, idx - 2);
5967}
5968
5969/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01005970 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01005971 * We don't really need this at runtime, but we do have tests that require it,
5972 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005973 */
5974 void
5975ex_disassemble(exarg_T *eap)
5976{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01005977 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01005978 char_u *fname;
5979 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005980 dfunc_T *dfunc;
5981 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01005982 int instr_count;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02005983 int is_global = FALSE;
Bram Moolenaare99d4222021-06-13 14:01:26 +02005984 compiletype_T compile_type = CT_NONE;
5985
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005986 if (STRNCMP(arg, "profile", 7) == 0 && VIM_ISWHITE(arg[7]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005987 {
5988 compile_type = CT_PROFILE;
5989 arg = skipwhite(arg + 7);
5990 }
Bram Moolenaarc7d5fc82021-12-05 17:20:24 +00005991 else if (STRNCMP(arg, "debug", 5) == 0 && VIM_ISWHITE(arg[5]))
Bram Moolenaare99d4222021-06-13 14:01:26 +02005992 {
5993 compile_type = CT_DEBUG;
5994 arg = skipwhite(arg + 5);
5995 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01005996
Bram Moolenaarbfd65582020-07-13 18:18:00 +02005997 if (STRNCMP(arg, "<lambda>", 8) == 0)
5998 {
5999 arg += 8;
6000 (void)getdigits(&arg);
6001 fname = vim_strnsave(eap->arg, arg - eap->arg);
6002 }
6003 else
6004 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01006005 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01006006 if (fname == NULL)
6007 {
6008 semsg(_(e_invarg2), eap->arg);
6009 return;
6010 }
6011
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006012 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006013 if (ufunc == NULL)
6014 {
6015 char_u *p = untrans_function_name(fname);
6016
6017 if (p != NULL)
6018 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02006019 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02006020 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01006021 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006022 if (ufunc == NULL)
6023 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006024 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006025 return;
6026 }
Bram Moolenaare99d4222021-06-13 14:01:26 +02006027 if (func_needs_compiling(ufunc, compile_type)
6028 && compile_def_function(ufunc, FALSE, compile_type, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02006029 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02006030 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006031 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02006032 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006033 return;
6034 }
Bram Moolenaar6db660b2021-08-01 14:08:54 +02006035 msg((char *)printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006036
6037 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaare99d4222021-06-13 14:01:26 +02006038 switch (compile_type)
6039 {
6040 case CT_PROFILE:
Bram Moolenaarf002a412021-01-24 13:34:18 +01006041#ifdef FEAT_PROFILE
Bram Moolenaare99d4222021-06-13 14:01:26 +02006042 instr = dfunc->df_instr_prof;
6043 instr_count = dfunc->df_instr_prof_count;
6044 break;
Bram Moolenaarf002a412021-01-24 13:34:18 +01006045#endif
Bram Moolenaare99d4222021-06-13 14:01:26 +02006046 // FALLTHROUGH
6047 case CT_NONE:
6048 instr = dfunc->df_instr;
6049 instr_count = dfunc->df_instr_count;
6050 break;
6051 case CT_DEBUG:
6052 instr = dfunc->df_instr_debug;
6053 instr_count = dfunc->df_instr_debug_count;
6054 break;
6055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006056
Bram Moolenaar4c137212021-04-19 16:48:48 +02006057 list_instructions("", instr, instr_count, ufunc);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006058}
6059
6060/*
Bram Moolenaar13106602020-10-04 16:06:05 +02006061 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006062 * list, etc. Mostly like what JavaScript does, except that empty list and
6063 * empty dictionary are FALSE.
6064 */
6065 int
6066tv2bool(typval_T *tv)
6067{
6068 switch (tv->v_type)
6069 {
6070 case VAR_NUMBER:
6071 return tv->vval.v_number != 0;
6072 case VAR_FLOAT:
6073#ifdef FEAT_FLOAT
6074 return tv->vval.v_float != 0.0;
6075#else
6076 break;
6077#endif
6078 case VAR_PARTIAL:
6079 return tv->vval.v_partial != NULL;
6080 case VAR_FUNC:
6081 case VAR_STRING:
6082 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
6083 case VAR_LIST:
6084 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
6085 case VAR_DICT:
6086 return tv->vval.v_dict != NULL
6087 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
6088 case VAR_BOOL:
6089 case VAR_SPECIAL:
6090 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
6091 case VAR_JOB:
6092#ifdef FEAT_JOB_CHANNEL
6093 return tv->vval.v_job != NULL;
6094#else
6095 break;
6096#endif
6097 case VAR_CHANNEL:
6098#ifdef FEAT_JOB_CHANNEL
6099 return tv->vval.v_channel != NULL;
6100#else
6101 break;
6102#endif
6103 case VAR_BLOB:
6104 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
6105 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02006106 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006107 case VAR_VOID:
Bram Moolenaarf18332f2021-05-07 17:55:55 +02006108 case VAR_INSTR:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006109 break;
6110 }
6111 return FALSE;
6112}
6113
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006114 void
6115emsg_using_string_as(typval_T *tv, int as_number)
6116{
6117 semsg(_(as_number ? e_using_string_as_number_str
6118 : e_using_string_as_bool_str),
6119 tv->vval.v_string == NULL
6120 ? (char_u *)"" : tv->vval.v_string);
6121}
6122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006123/*
6124 * If "tv" is a string give an error and return FAIL.
6125 */
6126 int
6127check_not_string(typval_T *tv)
6128{
6129 if (tv->v_type == VAR_STRING)
6130 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01006131 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006132 clear_tv(tv);
6133 return FAIL;
6134 }
6135 return OK;
6136}
6137
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01006138
6139#endif // FEAT_EVAL