blob: 957988e65859af2f0304d3be560d2c1539743010 [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 Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010057struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaar0186e582021-01-10 18:33:11 +010061 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 garray_T ec_trystack; // stack of trycmd_T values
64 int ec_in_catch; // when TRUE in catch or finally block
65
66 int ec_dfunc_idx; // current function index
67 isn_T *ec_instr; // array with instructions
68 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020069
70 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010071};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
73// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020074#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 +010075
Bram Moolenaar418f1df2020-08-12 21:34:49 +020076 void
77to_string_error(vartype_T vartype)
78{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020079 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020080}
81
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010083 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 */
85 static int
86ufunc_argcount(ufunc_T *ufunc)
87{
88 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
89}
90
91/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010092 * Set the instruction index, depending on omitted arguments, where the default
93 * values are to be computed. If all optional arguments are present, start
94 * with the function body.
95 * The expression evaluation is at the start of the instructions:
96 * 0 -> EVAL default1
97 * STORE arg[-2]
98 * 1 -> EVAL default2
99 * STORE arg[-1]
100 * 2 -> function body
101 */
102 static void
103init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
104{
105 if (ufunc->uf_def_args.ga_len == 0)
106 ectx->ec_iidx = 0;
107 else
108 {
109 int defcount = ufunc->uf_args.ga_len - argcount;
110
111 // If there is a varargs argument defcount can be negative, no defaults
112 // to evaluate then.
113 if (defcount < 0)
114 defcount = 0;
115 ectx->ec_iidx = ufunc->uf_def_arg_idx[
116 ufunc->uf_def_args.ga_len - defcount];
117 }
118}
119
120/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200121 * Create a new list from "count" items at the bottom of the stack.
122 * When "count" is zero an empty list is added to the stack.
123 */
124 static int
125exe_newlist(int count, ectx_T *ectx)
126{
127 list_T *list = list_alloc_with_items(count);
128 int idx;
129 typval_T *tv;
130
131 if (list == NULL)
132 return FAIL;
133 for (idx = 0; idx < count; ++idx)
134 list_set_item(list, idx, STACK_TV_BOT(idx - count));
135
136 if (count > 0)
137 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200138 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200139 return FAIL;
140 else
141 ++ectx->ec_stack.ga_len;
142 tv = STACK_TV_BOT(-1);
143 tv->v_type = VAR_LIST;
144 tv->vval.v_list = list;
145 ++list->lv_refcount;
146 return OK;
147}
148
149/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100151 * This adds a stack frame and sets the instruction pointer to the start of the
152 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100153 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154 *
155 * Stack has:
156 * - current arguments (already there)
157 * - omitted optional argument (default values) added here
158 * - stack frame:
159 * - pointer to calling function
160 * - Index of next instruction in calling function
161 * - previous frame pointer
162 * - reserved space for local variables
163 */
164 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100165call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200167 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
169 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 int arg_to_add;
171 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200172 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200174 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175
176 if (dfunc->df_deleted)
177 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100178 // don't use ufunc->uf_name, it may have been freed
179 emsg_funcname(e_func_deleted,
180 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 return FAIL;
182 }
183
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 if (ufunc->uf_va_name != NULL)
185 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200186 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200187 // Stack at time of call with 2 varargs:
188 // normal_arg
189 // optional_arg
190 // vararg_1
191 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 // After creating the list:
193 // normal_arg
194 // optional_arg
195 // vararg-list
196 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 // After creating the list
199 // normal_arg
200 // (space for optional_arg)
201 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200202 vararg_count = argcount - ufunc->uf_args.ga_len;
203 if (vararg_count < 0)
204 vararg_count = 0;
205 else
206 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200207 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200208 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209
210 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200211 }
212
Bram Moolenaarfe270812020-04-11 22:31:27 +0200213 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 if (arg_to_add < 0)
215 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200216 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200217 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200218 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 return FAIL;
221 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200222
223 // Reserve space for:
224 // - missing arguments
225 // - stack frame
226 // - local variables
227 // - if needed: a counter for number of closures created in
228 // ectx->ec_funcrefs.
229 varcount = dfunc->df_varcount + dfunc->df_has_closure;
230 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
231 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 return FAIL;
233
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100234 // If depth of calling is getting too high, don't execute the function.
235 if (funcdepth_increment() == FAIL)
236 return FAIL;
237
Bram Moolenaarfe270812020-04-11 22:31:27 +0200238 // Move the vararg-list to below the missing optional arguments.
239 if (vararg_count > 0 && arg_to_add > 0)
240 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100241
242 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200244 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
247 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100248 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
249 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
250 if (ectx->ec_outer != NULL)
251 printf("here");
252 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
253 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200254 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255
256 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200257 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200259 if (dfunc->df_has_closure)
260 {
261 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
262
263 tv->v_type = VAR_NUMBER;
264 tv->vval.v_number = 0;
265 }
266 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267
Bram Moolenaar0186e582021-01-10 18:33:11 +0100268 if (pt != NULL || ufunc->uf_partial != NULL || ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100269 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100270 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
271
272 if (outer == NULL)
273 return FAIL;
274 if (pt != NULL)
275 {
276 *outer = pt->pt_outer;
277 outer->out_up_is_copy = TRUE;
278 }
279 else if (ufunc->uf_partial != NULL)
280 {
281 *outer = ufunc->uf_partial->pt_outer;
282 outer->out_up_is_copy = TRUE;
283 }
284 else
285 {
286 outer->out_stack = &ectx->ec_stack;
287 outer->out_frame_idx = ectx->ec_frame_idx;
288 outer->out_up = ectx->ec_outer;
289 }
290 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100291 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100292 else
293 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100294
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100295 // Set execution state to the start of the called function.
296 ectx->ec_dfunc_idx = cdf_idx;
297 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200298 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
299 if (entry != NULL)
300 {
301 // Set the script context to the script where the function was defined.
302 // TODO: save more than the SID?
303 entry->es_save_sid = current_sctx.sc_sid;
304 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
305 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100306
307 // Decide where to start execution, handles optional arguments.
308 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100309
310 return OK;
311}
312
313// Get pointer to item in the stack.
314#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
315
316/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200317 * Used when returning from a function: Check if any closure is still
318 * referenced. If so then move the arguments and variables to a separate piece
319 * of stack to be used when the closure is called.
320 * When "free_arguments" is TRUE the arguments are to be freed.
321 * Returns FAIL when out of memory.
322 */
323 static int
324handle_closure_in_use(ectx_T *ectx, int free_arguments)
325{
326 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
327 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200328 int argcount;
329 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200330 int idx;
331 typval_T *tv;
332 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 garray_T *gap = &ectx->ec_funcrefs;
334 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200335
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200336 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200337 return OK; // function was freed
338 if (dfunc->df_has_closure == 0)
339 return OK; // no closures
340 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
341 closure_count = tv->vval.v_number;
342 if (closure_count == 0)
343 return OK; // no funcrefs created
344
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200345 argcount = ufunc_argcount(dfunc->df_ufunc);
346 top = ectx->ec_frame_idx - argcount;
347
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200349 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200350 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200351 partial_T *pt;
352 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200353
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200354 if (off < 0)
355 continue; // count is off or already done
356 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200357 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200358 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200359 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200360 int i;
361
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200362 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200363 // unreferenced on return.
364 for (i = 0; i < dfunc->df_varcount; ++i)
365 {
366 typval_T *stv = STACK_TV(ectx->ec_frame_idx
367 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200369 --refcount;
370 }
371 if (refcount > 1)
372 {
373 closure_in_use = TRUE;
374 break;
375 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200376 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200377 }
378
379 if (closure_in_use)
380 {
381 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
382 typval_T *stack;
383
384 // A closure is using the arguments and/or local variables.
385 // Move them to the called function.
386 if (funcstack == NULL)
387 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200388 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
389 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200390 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
391 funcstack->fs_ga.ga_data = stack;
392 if (stack == NULL)
393 {
394 vim_free(funcstack);
395 return FAIL;
396 }
397
398 // Move or copy the arguments.
399 for (idx = 0; idx < argcount; ++idx)
400 {
401 tv = STACK_TV(top + idx);
402 if (free_arguments)
403 {
404 *(stack + idx) = *tv;
405 tv->v_type = VAR_UNKNOWN;
406 }
407 else
408 copy_tv(tv, stack + idx);
409 }
410 // Move the local variables.
411 for (idx = 0; idx < dfunc->df_varcount; ++idx)
412 {
413 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200414
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200415 // A partial created for a local function, that is also used as a
416 // local variable, has a reference count for the variable, thus
417 // will never go down to zero. When all these refcounts are one
418 // then the funcstack is unused. We need to count how many we have
419 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200420 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
421 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200422 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200423
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200425 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
426 gap->ga_len - closure_count + i])
427 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200428 }
429
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200430 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200431 tv->v_type = VAR_UNKNOWN;
432 }
433
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200434 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200435 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200436 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
437 - closure_count + idx];
438 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200439 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200440 ++funcstack->fs_refcount;
441 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100442 pt->pt_outer.out_stack = &funcstack->fs_ga;
443 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
444 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 }
446 }
447 }
448
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200449 for (idx = 0; idx < closure_count; ++idx)
450 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
451 - closure_count + idx]);
452 gap->ga_len -= closure_count;
453 if (gap->ga_len == 0)
454 ga_clear(gap);
455
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200456 return OK;
457}
458
459/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200460 * Called when a partial is freed or its reference count goes down to one. The
461 * funcstack may be the only reference to the partials in the local variables.
462 * Go over all of them, the funcref and can be freed if all partials
463 * referencing the funcstack have a reference count of one.
464 */
465 void
466funcstack_check_refcount(funcstack_T *funcstack)
467{
468 int i;
469 garray_T *gap = &funcstack->fs_ga;
470 int done = 0;
471
472 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
473 return;
474 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
475 {
476 typval_T *tv = ((typval_T *)gap->ga_data) + i;
477
478 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
479 && tv->vval.v_partial->pt_funcstack == funcstack
480 && tv->vval.v_partial->pt_refcount == 1)
481 ++done;
482 }
483 if (done == funcstack->fs_min_refcount)
484 {
485 typval_T *stack = gap->ga_data;
486
487 // All partials referencing the funcstack have a reference count of
488 // one, thus the funcstack is no longer of use.
489 for (i = 0; i < gap->ga_len; ++i)
490 clear_tv(stack + i);
491 vim_free(stack);
492 vim_free(funcstack);
493 }
494}
495
496/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 * Return from the current function.
498 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200499 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500func_return(ectx_T *ectx)
501{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100503 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200504 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
505 + ectx->ec_dfunc_idx;
506 int argcount = ufunc_argcount(dfunc->df_ufunc);
507 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200508 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509
510 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200511 entry = estack_pop();
512 if (entry != NULL)
513 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200515 if (handle_closure_in_use(ectx, TRUE) == FAIL)
516 return FAIL;
517
518 // Clear the arguments.
519 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
520 clear_tv(STACK_TV(idx));
521
522 // Clear local variables and temp values, but not the return value.
523 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100524 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100526
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100527 // The return value should be on top of the stack. However, when aborting
528 // it may not be there and ec_frame_idx is the top of the stack.
529 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100530 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100531 ret_idx = 0;
532
Bram Moolenaar0186e582021-01-10 18:33:11 +0100533 if (ectx->ec_outer != NULL)
534 printf("here");
535 vim_free(ectx->ec_outer);
536
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100537 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100538 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
539 + STACK_FRAME_FUNC_OFF)->vval.v_number;
540 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
541 + STACK_FRAME_IIDX_OFF)->vval.v_number;
542 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
543 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200544 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100545 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
546 + STACK_FRAME_IDX_OFF)->vval.v_number;
547 if (ectx->ec_outer != NULL)
548 printf("here");
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
550 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100551
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100552 if (ret_idx > 0)
553 {
554 // Reset the stack to the position before the call, with a spot for the
555 // return value, moved there from above the frame.
556 ectx->ec_stack.ga_len = top + 1;
557 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
558 }
559 else
560 // Reset the stack to the position before the call.
561 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200562
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100563 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200564 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565}
566
567#undef STACK_TV
568
569/*
570 * Prepare arguments and rettv for calling a builtin or user function.
571 */
572 static int
573call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
574{
575 int idx;
576 typval_T *tv;
577
578 // Move arguments from bottom of the stack to argvars[] and add terminator.
579 for (idx = 0; idx < argcount; ++idx)
580 argvars[idx] = *STACK_TV_BOT(idx - argcount);
581 argvars[argcount].v_type = VAR_UNKNOWN;
582
583 // Result replaces the arguments on the stack.
584 if (argcount > 0)
585 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200586 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 return FAIL;
588 else
589 ++ectx->ec_stack.ga_len;
590
591 // Default return value is zero.
592 tv = STACK_TV_BOT(-1);
593 tv->v_type = VAR_NUMBER;
594 tv->vval.v_number = 0;
595
596 return OK;
597}
598
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200599// Ugly global to avoid passing the execution context around through many
600// layers.
601static ectx_T *current_ectx = NULL;
602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603/*
604 * Call a builtin function by index.
605 */
606 static int
607call_bfunc(int func_idx, int argcount, ectx_T *ectx)
608{
609 typval_T argvars[MAX_FUNC_ARGS];
610 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100611 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200612 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613
614 if (call_prepare(argcount, argvars, ectx) == FAIL)
615 return FAIL;
616
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200617 // Call the builtin function. Set "current_ectx" so that when it
618 // recursively invokes call_def_function() a closure context can be set.
619 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200621 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622
623 // Clear the arguments.
624 for (idx = 0; idx < argcount; ++idx)
625 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200626
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100627 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200628 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return OK;
630}
631
632/*
633 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100634 * If the function is compiled this will add a stack frame and set the
635 * instruction pointer at the start of the function.
636 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100637 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100638 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 */
640 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100641call_ufunc(
642 ufunc_T *ufunc,
643 partial_T *pt,
644 int argcount,
645 ectx_T *ectx,
646 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647{
648 typval_T argvars[MAX_FUNC_ARGS];
649 funcexe_T funcexe;
650 int error;
651 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100652 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200654 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200655 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
656 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200657 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100658 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100659 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100660 if (error != FCERR_UNKNOWN)
661 {
662 if (error == FCERR_TOOMANY)
663 semsg(_(e_toomanyarg), ufunc->uf_name);
664 else
665 semsg(_(e_toofewarg), ufunc->uf_name);
666 return FAIL;
667 }
668
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 // The function has been compiled, can call it quickly. For a function
670 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100671 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100672 if (iptr != NULL)
673 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100674 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100675 iptr->isn_type = ISN_DCALL;
676 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
677 iptr->isn_arg.dfunc.cdf_argcount = argcount;
678 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100679 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681
682 if (call_prepare(argcount, argvars, ectx) == FAIL)
683 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200684 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 funcexe.evaluate = TRUE;
686
687 // Call the user function. Result goes in last position on the stack.
688 // TODO: add selfdict if there is one
689 error = call_user_func_check(ufunc, argcount, argvars,
690 STACK_TV_BOT(-1), &funcexe, NULL);
691
692 // Clear the arguments.
693 for (idx = 0; idx < argcount; ++idx)
694 clear_tv(&argvars[idx]);
695
696 if (error != FCERR_NONE)
697 {
698 user_func_error(error, ufunc->uf_name);
699 return FAIL;
700 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100701 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200702 // Error other than from calling the function itself.
703 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 return OK;
705}
706
707/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200708 * Return TRUE if an error was given or CTRL-C was pressed.
709 */
710 static int
711vim9_aborting(int prev_called_emsg)
712{
713 return called_emsg > prev_called_emsg || got_int || did_throw;
714}
715
716/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 * Execute a function by "name".
718 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100719 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 * Returns FAIL if not found without an error message.
721 */
722 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724{
725 ufunc_T *ufunc;
726
727 if (builtin_function(name, -1))
728 {
729 int func_idx = find_internal_func(name);
730
731 if (func_idx < 0)
732 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200733 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 return FAIL;
735 return call_bfunc(func_idx, argcount, ectx);
736 }
737
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200738 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200739
740 if (ufunc == NULL)
741 {
742 int called_emsg_before = called_emsg;
743
744 if (script_autoload(name, TRUE))
745 // loaded a package, search for the function again
746 ufunc = find_func(name, FALSE, NULL);
747 if (vim9_aborting(called_emsg_before))
748 return FAIL; // bail out if loading the script caused an error
749 }
750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100752 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753
754 return FAIL;
755}
756
757 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200758call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200760 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200761 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200763 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
765 if (tv->v_type == VAR_PARTIAL)
766 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200767 partial_T *pt = tv->vval.v_partial;
768 int i;
769
770 if (pt->pt_argc > 0)
771 {
772 // Make space for arguments from the partial, shift the "argcount"
773 // arguments up.
774 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
775 return FAIL;
776 for (i = 1; i <= argcount; ++i)
777 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
778 ectx->ec_stack.ga_len += pt->pt_argc;
779 argcount += pt->pt_argc;
780
781 // copy the arguments from the partial onto the stack
782 for (i = 0; i < pt->pt_argc; ++i)
783 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
786 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100787 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 name = pt->pt_name;
790 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200791 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200793 if (name != NULL)
794 {
795 char_u fname_buf[FLEN_FIXED + 1];
796 char_u *tofree = NULL;
797 int error = FCERR_NONE;
798 char_u *fname;
799
800 // May need to translate <SNR>123_ to K_SNR.
801 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
802 if (error != FCERR_NONE)
803 res = FAIL;
804 else
805 res = call_by_name(fname, argcount, ectx, NULL);
806 vim_free(tofree);
807 }
808
809 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 {
811 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200812 semsg(_(e_unknownfunc),
813 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 return FAIL;
815 }
816 return OK;
817}
818
819/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200820 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
821 * TRUE.
822 */
823 static int
824error_if_locked(int lock, char *error)
825{
826 if (lock & (VAR_LOCKED | VAR_FIXED))
827 {
828 emsg(_(error));
829 return TRUE;
830 }
831 return FALSE;
832}
833
834/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100835 * Store "tv" in variable "name".
836 * This is for s: and g: variables.
837 */
838 static void
839store_var(char_u *name, typval_T *tv)
840{
841 funccal_entry_T entry;
842
843 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100844 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100845 restore_funccal();
846}
847
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100848/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100849 * Convert "tv" to a string.
850 * Return FAIL if not allowed.
851 */
852 static int
853do_2string(typval_T *tv, int is_2string_any)
854{
855 if (tv->v_type != VAR_STRING)
856 {
857 char_u *str;
858
859 if (is_2string_any)
860 {
861 switch (tv->v_type)
862 {
863 case VAR_SPECIAL:
864 case VAR_BOOL:
865 case VAR_NUMBER:
866 case VAR_FLOAT:
867 case VAR_BLOB: break;
868 default: to_string_error(tv->v_type);
869 return FAIL;
870 }
871 }
872 str = typval_tostring(tv);
873 clear_tv(tv);
874 tv->v_type = VAR_STRING;
875 tv->vval.v_string = str;
876 }
877 return OK;
878}
879
880/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100881 * When the value of "sv" is a null list of dict, allocate it.
882 */
883 static void
884allocate_if_null(typval_T *tv)
885{
886 switch (tv->v_type)
887 {
888 case VAR_LIST:
889 if (tv->vval.v_list == NULL)
890 rettv_list_alloc(tv);
891 break;
892 case VAR_DICT:
893 if (tv->vval.v_dict == NULL)
894 rettv_dict_alloc(tv);
895 break;
896 default:
897 break;
898 }
899}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200900
Bram Moolenaare7525c52021-01-09 13:20:37 +0100901/*
902 * Return the character "str[index]" where "index" is the character index. If
903 * "index" is out of range NULL is returned.
904 */
905 char_u *
906char_from_string(char_u *str, varnumber_T index)
907{
908 size_t nbyte = 0;
909 varnumber_T nchar = index;
910 size_t slen;
911
912 if (str == NULL)
913 return NULL;
914 slen = STRLEN(str);
915
916 // do the same as for a list: a negative index counts from the end
917 if (index < 0)
918 {
919 int clen = 0;
920
921 for (nbyte = 0; nbyte < slen; ++clen)
922 nbyte += MB_CPTR2LEN(str + nbyte);
923 nchar = clen + index;
924 if (nchar < 0)
925 // unlike list: index out of range results in empty string
926 return NULL;
927 }
928
929 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
930 nbyte += MB_CPTR2LEN(str + nbyte);
931 if (nbyte >= slen)
932 return NULL;
933 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
934}
935
936/*
937 * Get the byte index for character index "idx" in string "str" with length
938 * "str_len".
939 * If going over the end return "str_len".
940 * If "idx" is negative count from the end, -1 is the last character.
941 * When going over the start return -1.
942 */
943 static long
944char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
945{
946 varnumber_T nchar = idx;
947 size_t nbyte = 0;
948
949 if (nchar >= 0)
950 {
951 while (nchar > 0 && nbyte < str_len)
952 {
953 nbyte += MB_CPTR2LEN(str + nbyte);
954 --nchar;
955 }
956 }
957 else
958 {
959 nbyte = str_len;
960 while (nchar < 0 && nbyte > 0)
961 {
962 --nbyte;
963 nbyte -= mb_head_off(str, str + nbyte);
964 ++nchar;
965 }
966 if (nchar < 0)
967 return -1;
968 }
969 return (long)nbyte;
970}
971
972/*
973 * Return the slice "str[first:last]" using character indexes.
974 * Return NULL when the result is empty.
975 */
976 char_u *
977string_slice(char_u *str, varnumber_T first, varnumber_T last)
978{
979 long start_byte, end_byte;
980 size_t slen;
981
982 if (str == NULL)
983 return NULL;
984 slen = STRLEN(str);
985 start_byte = char_idx2byte(str, slen, first);
986 if (start_byte < 0)
987 start_byte = 0; // first index very negative: use zero
988 if (last == -1)
989 end_byte = (long)slen;
990 else
991 {
992 end_byte = char_idx2byte(str, slen, last);
993 if (end_byte >= 0 && end_byte < (long)slen)
994 // end index is inclusive
995 end_byte += MB_CPTR2LEN(str + end_byte);
996 }
997
998 if (start_byte >= (long)slen || end_byte <= start_byte)
999 return NULL;
1000 return vim_strnsave(str + start_byte, end_byte - start_byte);
1001}
1002
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001003 static svar_T *
1004get_script_svar(scriptref_T *sref, ectx_T *ectx)
1005{
1006 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1007 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1008 + ectx->ec_dfunc_idx;
1009 svar_T *sv;
1010
1011 if (sref->sref_seq != si->sn_script_seq)
1012 {
1013 // The script was reloaded after the function was
1014 // compiled, the script_idx may not be valid.
1015 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1016 dfunc->df_ufunc->uf_name_exp);
1017 return NULL;
1018 }
1019 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1020 if (!equal_type(sv->sv_type, sref->sref_type))
1021 {
1022 emsg(_(e_script_variable_type_changed));
1023 return NULL;
1024 }
1025 return sv;
1026}
1027
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001028/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 * Execute a function by "name".
1030 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 */
1033 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001034call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035{
Bram Moolenaared677f52020-08-12 16:38:10 +02001036 int called_emsg_before = called_emsg;
1037 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038
Bram Moolenaared677f52020-08-12 16:38:10 +02001039 res = call_by_name(name, argcount, ectx, iptr);
1040 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001042 dictitem_T *v;
1043
1044 v = find_var(name, NULL, FALSE);
1045 if (v == NULL)
1046 {
1047 semsg(_(e_unknownfunc), name);
1048 return FAIL;
1049 }
1050 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1051 {
1052 semsg(_(e_unknownfunc), name);
1053 return FAIL;
1054 }
1055 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001057 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001058}
1059
1060/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001061 * When a function reference is used, fill a partial with the information
1062 * needed, especially when it is used as a closure.
1063 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001064 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001065fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1066{
1067 pt->pt_func = ufunc;
1068 pt->pt_refcount = 1;
1069
1070 if (pt->pt_func->uf_flags & FC_CLOSURE)
1071 {
1072 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1073 + ectx->ec_dfunc_idx;
1074
1075 // The closure needs to find arguments and local
1076 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001077 pt->pt_outer.out_stack = &ectx->ec_stack;
1078 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1079 pt->pt_outer.out_up = ectx->ec_outer;
1080 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001081
1082 // If this function returns and the closure is still
1083 // being used, we need to make a copy of the context
1084 // (arguments and local variables). Store a reference
1085 // to the partial so we can handle that.
1086 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1087 {
1088 vim_free(pt);
1089 return FAIL;
1090 }
1091 // Extra variable keeps the count of closures created
1092 // in the current function call.
1093 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1094 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1095
1096 ((partial_T **)ectx->ec_funcrefs.ga_data)
1097 [ectx->ec_funcrefs.ga_len] = pt;
1098 ++pt->pt_refcount;
1099 ++ectx->ec_funcrefs.ga_len;
1100 }
1101 ++pt->pt_func->uf_refcount;
1102 return OK;
1103}
1104
1105/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 * Call a "def" function from old Vim script.
1107 * Return OK or FAIL.
1108 */
1109 int
1110call_def_function(
1111 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001112 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001114 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 typval_T *rettv) // return value
1116{
1117 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001118 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001119 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 typval_T *tv;
1121 int idx;
1122 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001123 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001124 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001125 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001126 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001127 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001128 msglist_T **saved_msg_list = NULL;
1129 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001130 cmdmod_T save_cmdmod;
1131 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001132 int save_emsg_silent_def = emsg_silent_def;
1133 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001134 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001135 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136
1137// Get pointer to item in the stack.
1138#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1139
1140// Get pointer to item at the bottom of the stack, -1 is the bottom.
1141#undef STACK_TV_BOT
1142#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1143
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001144// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001145#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 +01001146
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001147 if (ufunc->uf_def_status == UF_NOT_COMPILED
1148 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001149 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001150 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001151 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001152 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001153 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001154 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001155 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001156
Bram Moolenaar09689a02020-05-09 22:50:08 +02001157 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001158 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1160 + ufunc->uf_dfunc_idx;
1161 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001162 {
1163 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001164 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001165 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001168 // If depth of calling is getting too high, don't execute the function.
1169 orig_funcdepth = funcdepth_get();
1170 if (funcdepth_increment() == FAIL)
1171 return FAIL;
1172
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001173 CLEAR_FIELD(ectx);
1174 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1175 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1176 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001177 {
1178 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001179 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001182 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001184 // Put arguments on the stack, but no more than what the function expects.
1185 // A lambda can be called with more arguments than it uses.
1186 for (idx = 0; idx < argc
1187 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1188 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001190 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001191 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1192 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001193 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 copy_tv(&argv[idx], STACK_TV_BOT(0));
1195 ++ectx.ec_stack.ga_len;
1196 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001197
1198 // Turn varargs into a list. Empty list if no args.
1199 if (ufunc->uf_va_name != NULL)
1200 {
1201 int vararg_count = argc - ufunc->uf_args.ga_len;
1202
1203 if (vararg_count < 0)
1204 vararg_count = 0;
1205 else
1206 argc -= vararg_count;
1207 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001208 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001209
1210 // Check the type of the list items.
1211 tv = STACK_TV_BOT(-1);
1212 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001213 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001214 && ufunc->uf_va_type->tt_member != &t_any
1215 && tv->vval.v_list != NULL)
1216 {
1217 type_T *expected = ufunc->uf_va_type->tt_member;
1218 listitem_T *li = tv->vval.v_list->lv_first;
1219
1220 for (idx = 0; idx < vararg_count; ++idx)
1221 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001222 if (check_typval_type(expected, &li->li_tv,
1223 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001224 goto failed_early;
1225 li = li->li_next;
1226 }
1227 }
1228
Bram Moolenaar23e03252020-04-12 22:22:31 +02001229 if (defcount > 0)
1230 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001231 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001232 --ectx.ec_stack.ga_len;
1233 }
1234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001235 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001236 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001237 if (defcount > 0)
1238 for (idx = 0; idx < defcount; ++idx)
1239 {
1240 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1241 ++ectx.ec_stack.ga_len;
1242 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001243 if (ufunc->uf_va_name != NULL)
1244 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245
1246 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001247 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1248 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249
Bram Moolenaar0186e582021-01-10 18:33:11 +01001250 if (partial != NULL || ufunc->uf_partial != NULL)
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001251 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001252 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1253 if (ectx.ec_outer == NULL)
1254 goto failed_early;
1255 if (partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001256 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001257 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1258 {
1259 if (current_ectx->ec_outer != NULL)
1260 *ectx.ec_outer = *current_ectx->ec_outer;
1261 }
1262 else
1263 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001264 }
1265 else
Bram Moolenaar0186e582021-01-10 18:33:11 +01001266 *ectx.ec_outer = ufunc->uf_partial->pt_outer;
1267 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001268 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001269
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 // dummy frame entries
1271 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1272 {
1273 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1274 ++ectx.ec_stack.ga_len;
1275 }
1276
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001277 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001278 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001279 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1280 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001282 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001283 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001284 ectx.ec_stack.ga_len += dfunc->df_varcount;
1285 if (dfunc->df_has_closure)
1286 {
1287 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1288 STACK_TV_VAR(idx)->vval.v_number = 0;
1289 ++ectx.ec_stack.ga_len;
1290 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001291
1292 ectx.ec_instr = dfunc->df_instr;
1293 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001294
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001295 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001296 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001297 estack_push_ufunc(ufunc, 1);
1298 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001299 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1300
Bram Moolenaar352134b2020-10-17 22:04:08 +02001301 // Use a specific location for storing error messages to be converted to an
1302 // exception.
1303 saved_msg_list = msg_list;
1304 msg_list = &private_msg_list;
1305
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001306 // Do turn errors into exceptions.
1307 suppress_errthrow = FALSE;
1308
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001309 // When ":silent!" was used before calling then we still abort the
1310 // function. If ":silent!" is used in the function then we don't.
1311 emsg_silent_def = emsg_silent;
1312 did_emsg_def = 0;
1313
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001314 // Decide where to start execution, handles optional arguments.
1315 init_instr_idx(ufunc, argc, &ectx);
1316
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 for (;;)
1318 {
1319 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001320
Bram Moolenaar270d0382020-05-15 21:42:53 +02001321 if (++breakcheck_count >= 100)
1322 {
1323 line_breakcheck();
1324 breakcheck_count = 0;
1325 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001326 if (got_int)
1327 {
1328 // Turn CTRL-C into an exception.
1329 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001330 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 goto failed;
1332 did_throw = TRUE;
1333 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334
Bram Moolenaara26b9702020-04-18 19:53:28 +02001335 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1336 {
1337 // Turn an error message into an exception.
1338 did_emsg = FALSE;
1339 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1340 goto failed;
1341 did_throw = TRUE;
1342 *msg_list = NULL;
1343 }
1344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345 if (did_throw && !ectx.ec_in_catch)
1346 {
1347 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001348 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349
1350 // An exception jumps to the first catch, finally, or returns from
1351 // the current function.
1352 if (trystack->ga_len > 0)
1353 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001354 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 {
1356 // jump to ":catch" or ":finally"
1357 ectx.ec_in_catch = TRUE;
1358 ectx.ec_iidx = trycmd->tcd_catch_idx;
1359 }
1360 else
1361 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001362 // Not inside try or need to return from current functions.
1363 // Push a dummy return value.
1364 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1365 goto failed;
1366 tv = STACK_TV_BOT(0);
1367 tv->v_type = VAR_NUMBER;
1368 tv->vval.v_number = 0;
1369 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001370 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001372 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001373 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001374 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1375 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 goto done;
1377 }
1378
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001379 if (func_return(&ectx) == FAIL)
1380 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 }
1382 continue;
1383 }
1384
1385 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1386 switch (iptr->isn_type)
1387 {
1388 // execute Ex command line
1389 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001390 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001391 SOURCING_LNUM = iptr->isn_lnum;
1392 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001393 if (did_emsg)
1394 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001395 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 break;
1397
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001398 // execute Ex command from pieces on the stack
1399 case ISN_EXECCONCAT:
1400 {
1401 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001402 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001403 int pass;
1404 int i;
1405 char_u *cmd = NULL;
1406 char_u *str;
1407
1408 for (pass = 1; pass <= 2; ++pass)
1409 {
1410 for (i = 0; i < count; ++i)
1411 {
1412 tv = STACK_TV_BOT(i - count);
1413 str = tv->vval.v_string;
1414 if (str != NULL && *str != NUL)
1415 {
1416 if (pass == 2)
1417 STRCPY(cmd + len, str);
1418 len += STRLEN(str);
1419 }
1420 if (pass == 2)
1421 clear_tv(tv);
1422 }
1423 if (pass == 1)
1424 {
1425 cmd = alloc(len + 1);
1426 if (cmd == NULL)
1427 goto failed;
1428 len = 0;
1429 }
1430 }
1431
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001433 do_cmdline_cmd(cmd);
1434 vim_free(cmd);
1435 }
1436 break;
1437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 // execute :echo {string} ...
1439 case ISN_ECHO:
1440 {
1441 int count = iptr->isn_arg.echo.echo_count;
1442 int atstart = TRUE;
1443 int needclr = TRUE;
1444
1445 for (idx = 0; idx < count; ++idx)
1446 {
1447 tv = STACK_TV_BOT(idx - count);
1448 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1449 &atstart, &needclr);
1450 clear_tv(tv);
1451 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001452 if (needclr)
1453 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 ectx.ec_stack.ga_len -= count;
1455 }
1456 break;
1457
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001458 // :execute {string} ...
1459 // :echomsg {string} ...
1460 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001461 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001462 case ISN_ECHOMSG:
1463 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001464 {
1465 int count = iptr->isn_arg.number;
1466 garray_T ga;
1467 char_u buf[NUMBUFLEN];
1468 char_u *p;
1469 int len;
1470 int failed = FALSE;
1471
1472 ga_init2(&ga, 1, 80);
1473 for (idx = 0; idx < count; ++idx)
1474 {
1475 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001476 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001477 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001478 if (tv->v_type == VAR_CHANNEL
1479 || tv->v_type == VAR_JOB)
1480 {
1481 SOURCING_LNUM = iptr->isn_lnum;
1482 emsg(_(e_inval_string));
1483 break;
1484 }
1485 else
1486 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001487 }
1488 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001489 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001490
1491 len = (int)STRLEN(p);
1492 if (ga_grow(&ga, len + 2) == FAIL)
1493 failed = TRUE;
1494 else
1495 {
1496 if (ga.ga_len > 0)
1497 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1498 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1499 ga.ga_len += len;
1500 }
1501 clear_tv(tv);
1502 }
1503 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001504 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001505 {
1506 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001507 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001508 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001509
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001510 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001511 {
1512 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001513 {
1514 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001515 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001516 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001517 {
1518 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001519 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001520 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001521 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001522 else
1523 {
1524 msg_sb_eol();
1525 if (iptr->isn_type == ISN_ECHOMSG)
1526 {
1527 msg_attr(ga.ga_data, echo_attr);
1528 out_flush();
1529 }
1530 else
1531 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001532 SOURCING_LNUM = iptr->isn_lnum;
1533 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001534 }
1535 }
1536 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001537 ga_clear(&ga);
1538 }
1539 break;
1540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 // load local variable or argument
1542 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001543 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 goto failed;
1545 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1546 ++ectx.ec_stack.ga_len;
1547 break;
1548
1549 // load v: variable
1550 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001551 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 goto failed;
1553 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1554 ++ectx.ec_stack.ga_len;
1555 break;
1556
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001557 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 case ISN_LOADSCRIPT:
1559 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001560 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 svar_T *sv;
1562
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001563 sv = get_script_svar(sref, &ectx);
1564 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001565 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001566 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001567 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 goto failed;
1569 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1570 ++ectx.ec_stack.ga_len;
1571 }
1572 break;
1573
1574 // load s: variable in old script
1575 case ISN_LOADS:
1576 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001577 hashtab_T *ht = &SCRIPT_VARS(
1578 iptr->isn_arg.loadstore.ls_sid);
1579 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001581
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 if (di == NULL)
1583 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001584 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001585 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001586 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 }
1588 else
1589 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001590 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 goto failed;
1592 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1593 ++ectx.ec_stack.ga_len;
1594 }
1595 }
1596 break;
1597
Bram Moolenaard3aac292020-04-19 14:32:17 +02001598 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001600 case ISN_LOADB:
1601 case ISN_LOADW:
1602 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001604 dictitem_T *di = NULL;
1605 hashtab_T *ht = NULL;
1606 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001607
Bram Moolenaard3aac292020-04-19 14:32:17 +02001608 switch (iptr->isn_type)
1609 {
1610 case ISN_LOADG:
1611 ht = get_globvar_ht();
1612 namespace = 'g';
1613 break;
1614 case ISN_LOADB:
1615 ht = &curbuf->b_vars->dv_hashtab;
1616 namespace = 'b';
1617 break;
1618 case ISN_LOADW:
1619 ht = &curwin->w_vars->dv_hashtab;
1620 namespace = 'w';
1621 break;
1622 case ISN_LOADT:
1623 ht = &curtab->tp_vars->dv_hashtab;
1624 namespace = 't';
1625 break;
1626 default: // Cannot reach here
1627 goto failed;
1628 }
1629 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 if (di == NULL)
1632 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001633 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001634 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001635 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001636 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 }
1638 else
1639 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001640 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 goto failed;
1642 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1643 ++ectx.ec_stack.ga_len;
1644 }
1645 }
1646 break;
1647
Bram Moolenaar03290b82020-12-19 16:30:44 +01001648 // load autoload variable
1649 case ISN_LOADAUTO:
1650 {
1651 char_u *name = iptr->isn_arg.string;
1652
1653 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1654 goto failed;
1655 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001656 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001657 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1658 goto on_error;
1659 ++ectx.ec_stack.ga_len;
1660 }
1661 break;
1662
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001663 // load g:/b:/w:/t: namespace
1664 case ISN_LOADGDICT:
1665 case ISN_LOADBDICT:
1666 case ISN_LOADWDICT:
1667 case ISN_LOADTDICT:
1668 {
1669 dict_T *d = NULL;
1670
1671 switch (iptr->isn_type)
1672 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001673 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1674 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1675 case ISN_LOADWDICT: d = curwin->w_vars; break;
1676 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001677 default: // Cannot reach here
1678 goto failed;
1679 }
1680 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1681 goto failed;
1682 tv = STACK_TV_BOT(0);
1683 tv->v_type = VAR_DICT;
1684 tv->v_lock = 0;
1685 tv->vval.v_dict = d;
1686 ++ectx.ec_stack.ga_len;
1687 }
1688 break;
1689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 // load &option
1691 case ISN_LOADOPT:
1692 {
1693 typval_T optval;
1694 char_u *name = iptr->isn_arg.string;
1695
Bram Moolenaara8c17702020-04-01 21:17:24 +02001696 // This is not expected to fail, name is checked during
1697 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001698 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001700 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001701 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 *STACK_TV_BOT(0) = optval;
1703 ++ectx.ec_stack.ga_len;
1704 }
1705 break;
1706
1707 // load $ENV
1708 case ISN_LOADENV:
1709 {
1710 typval_T optval;
1711 char_u *name = iptr->isn_arg.string;
1712
Bram Moolenaar270d0382020-05-15 21:42:53 +02001713 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001715 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001716 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 *STACK_TV_BOT(0) = optval;
1718 ++ectx.ec_stack.ga_len;
1719 }
1720 break;
1721
1722 // load @register
1723 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001724 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 goto failed;
1726 tv = STACK_TV_BOT(0);
1727 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001728 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001729 // This may result in NULL, which should be equivalent to an
1730 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 tv->vval.v_string = get_reg_contents(
1732 iptr->isn_arg.number, GREG_EXPR_SRC);
1733 ++ectx.ec_stack.ga_len;
1734 break;
1735
1736 // store local variable
1737 case ISN_STORE:
1738 --ectx.ec_stack.ga_len;
1739 tv = STACK_TV_VAR(iptr->isn_arg.number);
1740 clear_tv(tv);
1741 *tv = *STACK_TV_BOT(0);
1742 break;
1743
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001744 // store s: variable in old script
1745 case ISN_STORES:
1746 {
1747 hashtab_T *ht = &SCRIPT_VARS(
1748 iptr->isn_arg.loadstore.ls_sid);
1749 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001750 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001751
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001752 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001753 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001754 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001755 else
1756 {
1757 clear_tv(&di->di_tv);
1758 di->di_tv = *STACK_TV_BOT(0);
1759 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001760 }
1761 break;
1762
1763 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 case ISN_STORESCRIPT:
1765 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001766 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1767 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001769 sv = get_script_svar(sref, &ectx);
1770 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001771 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 --ectx.ec_stack.ga_len;
1773 clear_tv(sv->sv_tv);
1774 *sv->sv_tv = *STACK_TV_BOT(0);
1775 }
1776 break;
1777
1778 // store option
1779 case ISN_STOREOPT:
1780 {
1781 long n = 0;
1782 char_u *s = NULL;
1783 char *msg;
1784
1785 --ectx.ec_stack.ga_len;
1786 tv = STACK_TV_BOT(0);
1787 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001788 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001790 if (s == NULL)
1791 s = (char_u *)"";
1792 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001794 // must be VAR_NUMBER, CHECKTYPE makes sure
1795 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1797 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001798 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 if (msg != NULL)
1800 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001801 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 }
1806 break;
1807
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001808 // store $ENV
1809 case ISN_STOREENV:
1810 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001811 tv = STACK_TV_BOT(0);
1812 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1813 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001814 break;
1815
1816 // store @r
1817 case ISN_STOREREG:
1818 {
1819 int reg = iptr->isn_arg.number;
1820
1821 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001822 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001823 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001824 tv_get_string(tv), -1, FALSE);
1825 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001826 }
1827 break;
1828
1829 // store v: variable
1830 case ISN_STOREV:
1831 --ectx.ec_stack.ga_len;
1832 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1833 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001834 // should not happen, type is checked when compiling
1835 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001836 break;
1837
Bram Moolenaard3aac292020-04-19 14:32:17 +02001838 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001840 case ISN_STOREB:
1841 case ISN_STOREW:
1842 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001844 dictitem_T *di;
1845 hashtab_T *ht;
1846 char_u *name = iptr->isn_arg.string + 2;
1847
Bram Moolenaard3aac292020-04-19 14:32:17 +02001848 switch (iptr->isn_type)
1849 {
1850 case ISN_STOREG:
1851 ht = get_globvar_ht();
1852 break;
1853 case ISN_STOREB:
1854 ht = &curbuf->b_vars->dv_hashtab;
1855 break;
1856 case ISN_STOREW:
1857 ht = &curwin->w_vars->dv_hashtab;
1858 break;
1859 case ISN_STORET:
1860 ht = &curtab->tp_vars->dv_hashtab;
1861 break;
1862 default: // Cannot reach here
1863 goto failed;
1864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865
1866 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001867 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001869 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 else
1871 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001872 SOURCING_LNUM = iptr->isn_lnum;
1873 if (var_check_permission(di, name) == FAIL)
1874 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 clear_tv(&di->di_tv);
1876 di->di_tv = *STACK_TV_BOT(0);
1877 }
1878 }
1879 break;
1880
Bram Moolenaar03290b82020-12-19 16:30:44 +01001881 // store an autoload variable
1882 case ISN_STOREAUTO:
1883 SOURCING_LNUM = iptr->isn_lnum;
1884 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1885 clear_tv(STACK_TV_BOT(-1));
1886 --ectx.ec_stack.ga_len;
1887 break;
1888
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 // store number in local variable
1890 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001891 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 clear_tv(tv);
1893 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001894 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 break;
1896
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001897 // store value in list or dict variable
1898 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001899 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001900 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001901 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001902 typval_T *tv_dest = STACK_TV_BOT(-1);
1903 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001904
Bram Moolenaar752fc692021-01-04 21:57:11 +01001905 // Stack contains:
1906 // -3 value to be stored
1907 // -2 index
1908 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001909 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001910 SOURCING_LNUM = iptr->isn_lnum;
1911 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001912 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001913 dest_type = tv_dest->v_type;
1914 if (dest_type == VAR_DICT)
1915 status = do_2string(tv_idx, TRUE);
1916 else if (dest_type == VAR_LIST
1917 && tv_idx->v_type != VAR_NUMBER)
1918 {
1919 emsg(_(e_number_exp));
1920 status = FAIL;
1921 }
1922 }
1923 else if (dest_type != tv_dest->v_type)
1924 {
1925 // just in case, should be OK
1926 semsg(_(e_expected_str_but_got_str),
1927 vartype_name(dest_type),
1928 vartype_name(tv_dest->v_type));
1929 status = FAIL;
1930 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001931
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001932 if (status == OK && dest_type == VAR_LIST)
1933 {
1934 varnumber_T lidx = tv_idx->vval.v_number;
1935 list_T *list = tv_dest->vval.v_list;
1936
1937 if (list == NULL)
1938 {
1939 emsg(_(e_list_not_set));
1940 goto on_error;
1941 }
1942 if (lidx < 0 && list->lv_len + lidx >= 0)
1943 // negative index is relative to the end
1944 lidx = list->lv_len + lidx;
1945 if (lidx < 0 || lidx > list->lv_len)
1946 {
1947 semsg(_(e_listidx), lidx);
1948 goto on_error;
1949 }
1950 if (lidx < list->lv_len)
1951 {
1952 listitem_T *li = list_find(list, lidx);
1953
1954 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001955 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001956 goto on_error;
1957 // overwrite existing list item
1958 clear_tv(&li->li_tv);
1959 li->li_tv = *tv;
1960 }
1961 else
1962 {
1963 if (error_if_locked(list->lv_lock,
1964 e_cannot_change_list))
1965 goto on_error;
1966 // append to list, only fails when out of memory
1967 if (list_append_tv(list, tv) == FAIL)
1968 goto failed;
1969 clear_tv(tv);
1970 }
1971 }
1972 else if (status == OK && dest_type == VAR_DICT)
1973 {
1974 char_u *key = tv_idx->vval.v_string;
1975 dict_T *dict = tv_dest->vval.v_dict;
1976 dictitem_T *di;
1977
1978 SOURCING_LNUM = iptr->isn_lnum;
1979 if (dict == NULL)
1980 {
1981 emsg(_(e_dictionary_not_set));
1982 goto on_error;
1983 }
1984 if (key == NULL)
1985 key = (char_u *)"";
1986 di = dict_find(dict, key, -1);
1987 if (di != NULL)
1988 {
1989 if (error_if_locked(di->di_tv.v_lock,
1990 e_cannot_change_dict_item))
1991 goto on_error;
1992 // overwrite existing value
1993 clear_tv(&di->di_tv);
1994 di->di_tv = *tv;
1995 }
1996 else
1997 {
1998 if (error_if_locked(dict->dv_lock,
1999 e_cannot_change_dict))
2000 goto on_error;
2001 // add to dict, only fails when out of memory
2002 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2003 goto failed;
2004 clear_tv(tv);
2005 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002006 }
2007 else
2008 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002009 status = FAIL;
2010 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002011 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002012
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002013 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002014 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002015 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002016 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002017 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002018 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002019 goto on_error;
2020 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002021 }
2022 break;
2023
Bram Moolenaar0186e582021-01-10 18:33:11 +01002024 // load or store variable or argument from outer scope
2025 case ISN_LOADOUTER:
2026 case ISN_STOREOUTER:
2027 {
2028 int depth = iptr->isn_arg.outer.outer_depth;
2029 outer_T *outer = ectx.ec_outer;
2030
2031 while (depth > 1 && outer != NULL)
2032 {
2033 outer = outer->out_up;
2034 --depth;
2035 }
2036 if (outer == NULL)
2037 {
2038 SOURCING_LNUM = iptr->isn_lnum;
2039 iemsg("LOADOUTER depth more than scope levels");
2040 goto failed;
2041 }
2042 tv = ((typval_T *)outer->out_stack->ga_data)
2043 + outer->out_frame_idx + STACK_FRAME_SIZE
2044 + iptr->isn_arg.outer.outer_idx;
2045 if (iptr->isn_type == ISN_LOADOUTER)
2046 {
2047 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2048 goto failed;
2049 copy_tv(tv, STACK_TV_BOT(0));
2050 ++ectx.ec_stack.ga_len;
2051 }
2052 else
2053 {
2054 --ectx.ec_stack.ga_len;
2055 clear_tv(tv);
2056 *tv = *STACK_TV_BOT(0);
2057 }
2058 }
2059 break;
2060
Bram Moolenaar752fc692021-01-04 21:57:11 +01002061 // unlet item in list or dict variable
2062 case ISN_UNLETINDEX:
2063 {
2064 typval_T *tv_idx = STACK_TV_BOT(-2);
2065 typval_T *tv_dest = STACK_TV_BOT(-1);
2066 int status = OK;
2067
2068 // Stack contains:
2069 // -2 index
2070 // -1 dict or list
2071 if (tv_dest->v_type == VAR_DICT)
2072 {
2073 // unlet a dict item, index must be a string
2074 if (tv_idx->v_type != VAR_STRING)
2075 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002076 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002077 semsg(_(e_expected_str_but_got_str),
2078 vartype_name(VAR_STRING),
2079 vartype_name(tv_idx->v_type));
2080 status = FAIL;
2081 }
2082 else
2083 {
2084 dict_T *d = tv_dest->vval.v_dict;
2085 char_u *key = tv_idx->vval.v_string;
2086 dictitem_T *di = NULL;
2087
2088 if (key == NULL)
2089 key = (char_u *)"";
2090 if (d != NULL)
2091 di = dict_find(d, key, (int)STRLEN(key));
2092 if (di == NULL)
2093 {
2094 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002095 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002096 semsg(_(e_dictkey), key);
2097 status = FAIL;
2098 }
2099 else
2100 {
2101 // TODO: check for dict or item locked
2102 dictitem_remove(d, di);
2103 }
2104 }
2105 }
2106 else if (tv_dest->v_type == VAR_LIST)
2107 {
2108 // unlet a List item, index must be a number
2109 if (tv_idx->v_type != VAR_NUMBER)
2110 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002112 semsg(_(e_expected_str_but_got_str),
2113 vartype_name(VAR_NUMBER),
2114 vartype_name(tv_idx->v_type));
2115 status = FAIL;
2116 }
2117 else
2118 {
2119 list_T *l = tv_dest->vval.v_list;
2120 varnumber_T n = tv_idx->vval.v_number;
2121 listitem_T *li = NULL;
2122
2123 li = list_find(l, n);
2124 if (li == NULL)
2125 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002127 semsg(_(e_listidx), n);
2128 status = FAIL;
2129 }
2130 else
2131 // TODO: check for list or item locked
2132 listitem_remove(l, li);
2133 }
2134 }
2135 else
2136 {
2137 status = FAIL;
2138 semsg(_(e_cannot_index_str),
2139 vartype_name(tv_dest->v_type));
2140 }
2141
2142 clear_tv(tv_idx);
2143 clear_tv(tv_dest);
2144 ectx.ec_stack.ga_len -= 2;
2145 if (status == FAIL)
2146 goto on_error;
2147 }
2148 break;
2149
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 // push constant
2151 case ISN_PUSHNR:
2152 case ISN_PUSHBOOL:
2153 case ISN_PUSHSPEC:
2154 case ISN_PUSHF:
2155 case ISN_PUSHS:
2156 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002157 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002158 case ISN_PUSHCHANNEL:
2159 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002160 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 goto failed;
2162 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002163 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 ++ectx.ec_stack.ga_len;
2165 switch (iptr->isn_type)
2166 {
2167 case ISN_PUSHNR:
2168 tv->v_type = VAR_NUMBER;
2169 tv->vval.v_number = iptr->isn_arg.number;
2170 break;
2171 case ISN_PUSHBOOL:
2172 tv->v_type = VAR_BOOL;
2173 tv->vval.v_number = iptr->isn_arg.number;
2174 break;
2175 case ISN_PUSHSPEC:
2176 tv->v_type = VAR_SPECIAL;
2177 tv->vval.v_number = iptr->isn_arg.number;
2178 break;
2179#ifdef FEAT_FLOAT
2180 case ISN_PUSHF:
2181 tv->v_type = VAR_FLOAT;
2182 tv->vval.v_float = iptr->isn_arg.fnumber;
2183 break;
2184#endif
2185 case ISN_PUSHBLOB:
2186 blob_copy(iptr->isn_arg.blob, tv);
2187 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002188 case ISN_PUSHFUNC:
2189 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002190 if (iptr->isn_arg.string == NULL)
2191 tv->vval.v_string = NULL;
2192 else
2193 tv->vval.v_string =
2194 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002195 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002196 case ISN_PUSHCHANNEL:
2197#ifdef FEAT_JOB_CHANNEL
2198 tv->v_type = VAR_CHANNEL;
2199 tv->vval.v_channel = iptr->isn_arg.channel;
2200 if (tv->vval.v_channel != NULL)
2201 ++tv->vval.v_channel->ch_refcount;
2202#endif
2203 break;
2204 case ISN_PUSHJOB:
2205#ifdef FEAT_JOB_CHANNEL
2206 tv->v_type = VAR_JOB;
2207 tv->vval.v_job = iptr->isn_arg.job;
2208 if (tv->vval.v_job != NULL)
2209 ++tv->vval.v_job->jv_refcount;
2210#endif
2211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 default:
2213 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002214 tv->vval.v_string = vim_strsave(
2215 iptr->isn_arg.string == NULL
2216 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 }
2218 break;
2219
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002220 case ISN_UNLET:
2221 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2222 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002223 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002224 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002225 case ISN_UNLETENV:
2226 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2227 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002228
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002229 case ISN_LOCKCONST:
2230 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2231 break;
2232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 // create a list from items on the stack; uses a single allocation
2234 // for the list header and the items
2235 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002236 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2237 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 break;
2239
2240 // create a dict from items on the stack
2241 case ISN_NEWDICT:
2242 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002243 int count = iptr->isn_arg.number;
2244 dict_T *dict = dict_alloc();
2245 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002246 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247
2248 if (dict == NULL)
2249 goto failed;
2250 for (idx = 0; idx < count; ++idx)
2251 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002252 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002254 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002255 key = tv->vval.v_string == NULL
2256 ? (char_u *)"" : tv->vval.v_string;
2257 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002258 if (item != NULL)
2259 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002260 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002261 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002262 dict_unref(dict);
2263 goto on_error;
2264 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002265 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 clear_tv(tv);
2267 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002268 {
2269 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002271 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2273 item->di_tv.v_lock = 0;
2274 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002275 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002276 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002277 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 }
2281
2282 if (count > 0)
2283 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002284 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 goto failed;
2286 else
2287 ++ectx.ec_stack.ga_len;
2288 tv = STACK_TV_BOT(-1);
2289 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002290 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 tv->vval.v_dict = dict;
2292 ++dict->dv_refcount;
2293 }
2294 break;
2295
2296 // call a :def function
2297 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002299 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 iptr->isn_arg.dfunc.cdf_argcount,
2301 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002302 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 break;
2304
2305 // call a builtin function
2306 case ISN_BCALL:
2307 SOURCING_LNUM = iptr->isn_lnum;
2308 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2309 iptr->isn_arg.bfunc.cbf_argcount,
2310 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002311 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 break;
2313
2314 // call a funcref or partial
2315 case ISN_PCALL:
2316 {
2317 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2318 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002319 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320
2321 SOURCING_LNUM = iptr->isn_lnum;
2322 if (pfunc->cpf_top)
2323 {
2324 // funcref is above the arguments
2325 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2326 }
2327 else
2328 {
2329 // Get the funcref from the stack.
2330 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002331 partial_tv = *STACK_TV_BOT(0);
2332 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 }
2334 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002335 if (tv == &partial_tv)
2336 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002338 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 }
2340 break;
2341
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002342 case ISN_PCALL_END:
2343 // PCALL finished, arguments have been consumed and replaced by
2344 // the return value. Now clear the funcref from the stack,
2345 // and move the return value in its place.
2346 --ectx.ec_stack.ga_len;
2347 clear_tv(STACK_TV_BOT(-1));
2348 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2349 break;
2350
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 // call a user defined function or funcref/partial
2352 case ISN_UCALL:
2353 {
2354 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2355
2356 SOURCING_LNUM = iptr->isn_lnum;
2357 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002358 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002359 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 }
2361 break;
2362
2363 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002364 case ISN_RETURN_ZERO:
2365 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2366 goto failed;
2367 tv = STACK_TV_BOT(0);
2368 ++ectx.ec_stack.ga_len;
2369 tv->v_type = VAR_NUMBER;
2370 tv->vval.v_number = 0;
2371 tv->v_lock = 0;
2372 // FALLTHROUGH
2373
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 case ISN_RETURN:
2375 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002376 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002377 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002378
2379 if (trystack->ga_len > 0)
2380 trycmd = ((trycmd_T *)trystack->ga_data)
2381 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002382 if (trycmd != NULL
2383 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 && trycmd->tcd_finally_idx != 0)
2385 {
2386 // jump to ":finally"
2387 ectx.ec_iidx = trycmd->tcd_finally_idx;
2388 trycmd->tcd_return = TRUE;
2389 }
2390 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002391 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 }
2393 break;
2394
2395 // push a function reference to a compiled function
2396 case ISN_FUNCREF:
2397 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002398 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2399 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2400 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 if (pt == NULL)
2403 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002404 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002405 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002406 vim_free(pt);
2407 goto failed;
2408 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002409 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2410 &ectx) == FAIL)
2411 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002412
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 tv = STACK_TV_BOT(0);
2414 ++ectx.ec_stack.ga_len;
2415 tv->vval.v_partial = pt;
2416 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002417 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
2419 break;
2420
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002421 // Create a global function from a lambda.
2422 case ISN_NEWFUNC:
2423 {
2424 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2425
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002426 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002427 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002428 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002429 }
2430 break;
2431
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002432 // List functions
2433 case ISN_DEF:
2434 if (iptr->isn_arg.string == NULL)
2435 list_functions(NULL);
2436 else
2437 {
2438 exarg_T ea;
2439
2440 CLEAR_FIELD(ea);
2441 ea.cmd = ea.arg = iptr->isn_arg.string;
2442 define_function(&ea, NULL);
2443 }
2444 break;
2445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 // jump if a condition is met
2447 case ISN_JUMP:
2448 {
2449 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002450 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 int jump = TRUE;
2452
2453 if (when != JUMP_ALWAYS)
2454 {
2455 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002456 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002457 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002458 || when == JUMP_IF_COND_TRUE)
2459 {
2460 SOURCING_LNUM = iptr->isn_lnum;
2461 jump = tv_get_bool_chk(tv, &error);
2462 if (error)
2463 goto on_error;
2464 }
2465 else
2466 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002468 || when == JUMP_AND_KEEP_IF_FALSE
2469 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002471 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 {
2473 // drop the value from the stack
2474 clear_tv(tv);
2475 --ectx.ec_stack.ga_len;
2476 }
2477 }
2478 if (jump)
2479 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2480 }
2481 break;
2482
2483 // top of a for loop
2484 case ISN_FOR:
2485 {
2486 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2487 typval_T *idxtv =
2488 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2489
2490 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002491 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002493 ++idxtv->vval.v_number;
2494 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 // past the end of the list, jump to "endfor"
2496 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2497 else if (list->lv_first == &range_list_item)
2498 {
2499 // non-materialized range() list
2500 tv = STACK_TV_BOT(0);
2501 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002502 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 tv->vval.v_number = list_find_nr(
2504 list, idxtv->vval.v_number, NULL);
2505 ++ectx.ec_stack.ga_len;
2506 }
2507 else
2508 {
2509 listitem_T *li = list_find(list, idxtv->vval.v_number);
2510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2512 ++ectx.ec_stack.ga_len;
2513 }
2514 }
2515 break;
2516
2517 // start of ":try" block
2518 case ISN_TRY:
2519 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002520 trycmd_T *trycmd = NULL;
2521
Bram Moolenaar270d0382020-05-15 21:42:53 +02002522 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto failed;
2524 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2525 + ectx.ec_trystack.ga_len;
2526 ++ectx.ec_trystack.ga_len;
2527 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002528 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2530 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002531 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002532 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534 break;
2535
2536 case ISN_PUSHEXC:
2537 if (current_exception == NULL)
2538 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002539 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 iemsg("Evaluating catch while current_exception is NULL");
2541 goto failed;
2542 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002543 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 goto failed;
2545 tv = STACK_TV_BOT(0);
2546 ++ectx.ec_stack.ga_len;
2547 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002548 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 tv->vval.v_string = vim_strsave(
2550 (char_u *)current_exception->value);
2551 break;
2552
2553 case ISN_CATCH:
2554 {
2555 garray_T *trystack = &ectx.ec_trystack;
2556
Bram Moolenaar20a76292020-12-25 19:47:24 +01002557 if (restore_cmdmod)
2558 {
2559 cmdmod.cmod_filter_regmatch.regprog = NULL;
2560 undo_cmdmod(&cmdmod);
2561 cmdmod = save_cmdmod;
2562 restore_cmdmod = FALSE;
2563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 if (trystack->ga_len > 0)
2565 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002566 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 + trystack->ga_len - 1;
2568 trycmd->tcd_caught = TRUE;
2569 }
2570 did_emsg = got_int = did_throw = FALSE;
2571 catch_exception(current_exception);
2572 }
2573 break;
2574
2575 // end of ":try" block
2576 case ISN_ENDTRY:
2577 {
2578 garray_T *trystack = &ectx.ec_trystack;
2579
2580 if (trystack->ga_len > 0)
2581 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002582 trycmd_T *trycmd = NULL;
2583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 --trystack->ga_len;
2585 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002586 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 trycmd = ((trycmd_T *)trystack->ga_data)
2588 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002589 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 {
2591 // discard the exception
2592 if (caught_stack == current_exception)
2593 caught_stack = caught_stack->caught;
2594 discard_current_exception();
2595 }
2596
2597 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002598 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 }
2600 }
2601 break;
2602
2603 case ISN_THROW:
2604 --ectx.ec_stack.ga_len;
2605 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002606 if (tv->vval.v_string == NULL
2607 || *skipwhite(tv->vval.v_string) == NUL)
2608 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002609 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002611 emsg(_(e_throw_with_empty_string));
2612 goto failed;
2613 }
2614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2616 {
2617 vim_free(tv->vval.v_string);
2618 goto failed;
2619 }
2620 did_throw = TRUE;
2621 break;
2622
2623 // compare with special values
2624 case ISN_COMPAREBOOL:
2625 case ISN_COMPARESPECIAL:
2626 {
2627 typval_T *tv1 = STACK_TV_BOT(-2);
2628 typval_T *tv2 = STACK_TV_BOT(-1);
2629 varnumber_T arg1 = tv1->vval.v_number;
2630 varnumber_T arg2 = tv2->vval.v_number;
2631 int res;
2632
2633 switch (iptr->isn_arg.op.op_type)
2634 {
2635 case EXPR_EQUAL: res = arg1 == arg2; break;
2636 case EXPR_NEQUAL: res = arg1 != arg2; break;
2637 default: res = 0; break;
2638 }
2639
2640 --ectx.ec_stack.ga_len;
2641 tv1->v_type = VAR_BOOL;
2642 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2643 }
2644 break;
2645
2646 // Operation with two number arguments
2647 case ISN_OPNR:
2648 case ISN_COMPARENR:
2649 {
2650 typval_T *tv1 = STACK_TV_BOT(-2);
2651 typval_T *tv2 = STACK_TV_BOT(-1);
2652 varnumber_T arg1 = tv1->vval.v_number;
2653 varnumber_T arg2 = tv2->vval.v_number;
2654 varnumber_T res;
2655
2656 switch (iptr->isn_arg.op.op_type)
2657 {
2658 case EXPR_MULT: res = arg1 * arg2; break;
2659 case EXPR_DIV: res = arg1 / arg2; break;
2660 case EXPR_REM: res = arg1 % arg2; break;
2661 case EXPR_SUB: res = arg1 - arg2; break;
2662 case EXPR_ADD: res = arg1 + arg2; break;
2663
2664 case EXPR_EQUAL: res = arg1 == arg2; break;
2665 case EXPR_NEQUAL: res = arg1 != arg2; break;
2666 case EXPR_GREATER: res = arg1 > arg2; break;
2667 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2668 case EXPR_SMALLER: res = arg1 < arg2; break;
2669 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2670 default: res = 0; break;
2671 }
2672
2673 --ectx.ec_stack.ga_len;
2674 if (iptr->isn_type == ISN_COMPARENR)
2675 {
2676 tv1->v_type = VAR_BOOL;
2677 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2678 }
2679 else
2680 tv1->vval.v_number = res;
2681 }
2682 break;
2683
2684 // Computation with two float arguments
2685 case ISN_OPFLOAT:
2686 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002687#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 {
2689 typval_T *tv1 = STACK_TV_BOT(-2);
2690 typval_T *tv2 = STACK_TV_BOT(-1);
2691 float_T arg1 = tv1->vval.v_float;
2692 float_T arg2 = tv2->vval.v_float;
2693 float_T res = 0;
2694 int cmp = FALSE;
2695
2696 switch (iptr->isn_arg.op.op_type)
2697 {
2698 case EXPR_MULT: res = arg1 * arg2; break;
2699 case EXPR_DIV: res = arg1 / arg2; break;
2700 case EXPR_SUB: res = arg1 - arg2; break;
2701 case EXPR_ADD: res = arg1 + arg2; break;
2702
2703 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2704 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2705 case EXPR_GREATER: cmp = arg1 > arg2; break;
2706 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2707 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2708 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2709 default: cmp = 0; break;
2710 }
2711 --ectx.ec_stack.ga_len;
2712 if (iptr->isn_type == ISN_COMPAREFLOAT)
2713 {
2714 tv1->v_type = VAR_BOOL;
2715 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2716 }
2717 else
2718 tv1->vval.v_float = res;
2719 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002720#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002721 break;
2722
2723 case ISN_COMPARELIST:
2724 {
2725 typval_T *tv1 = STACK_TV_BOT(-2);
2726 typval_T *tv2 = STACK_TV_BOT(-1);
2727 list_T *arg1 = tv1->vval.v_list;
2728 list_T *arg2 = tv2->vval.v_list;
2729 int cmp = FALSE;
2730 int ic = iptr->isn_arg.op.op_ic;
2731
2732 switch (iptr->isn_arg.op.op_type)
2733 {
2734 case EXPR_EQUAL: cmp =
2735 list_equal(arg1, arg2, ic, FALSE); break;
2736 case EXPR_NEQUAL: cmp =
2737 !list_equal(arg1, arg2, ic, FALSE); break;
2738 case EXPR_IS: cmp = arg1 == arg2; break;
2739 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2740 default: cmp = 0; break;
2741 }
2742 --ectx.ec_stack.ga_len;
2743 clear_tv(tv1);
2744 clear_tv(tv2);
2745 tv1->v_type = VAR_BOOL;
2746 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2747 }
2748 break;
2749
2750 case ISN_COMPAREBLOB:
2751 {
2752 typval_T *tv1 = STACK_TV_BOT(-2);
2753 typval_T *tv2 = STACK_TV_BOT(-1);
2754 blob_T *arg1 = tv1->vval.v_blob;
2755 blob_T *arg2 = tv2->vval.v_blob;
2756 int cmp = FALSE;
2757
2758 switch (iptr->isn_arg.op.op_type)
2759 {
2760 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2761 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2762 case EXPR_IS: cmp = arg1 == arg2; break;
2763 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2764 default: cmp = 0; break;
2765 }
2766 --ectx.ec_stack.ga_len;
2767 clear_tv(tv1);
2768 clear_tv(tv2);
2769 tv1->v_type = VAR_BOOL;
2770 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2771 }
2772 break;
2773
2774 // TODO: handle separately
2775 case ISN_COMPARESTRING:
2776 case ISN_COMPAREDICT:
2777 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 case ISN_COMPAREANY:
2779 {
2780 typval_T *tv1 = STACK_TV_BOT(-2);
2781 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002782 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 int ic = iptr->isn_arg.op.op_ic;
2784
Bram Moolenaareb26f432020-09-14 16:50:05 +02002785 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002786 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 --ectx.ec_stack.ga_len;
2789 }
2790 break;
2791
2792 case ISN_ADDLIST:
2793 case ISN_ADDBLOB:
2794 {
2795 typval_T *tv1 = STACK_TV_BOT(-2);
2796 typval_T *tv2 = STACK_TV_BOT(-1);
2797
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002798 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 if (iptr->isn_type == ISN_ADDLIST)
2800 eval_addlist(tv1, tv2);
2801 else
2802 eval_addblob(tv1, tv2);
2803 clear_tv(tv2);
2804 --ectx.ec_stack.ga_len;
2805 }
2806 break;
2807
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002808 case ISN_LISTAPPEND:
2809 {
2810 typval_T *tv1 = STACK_TV_BOT(-2);
2811 typval_T *tv2 = STACK_TV_BOT(-1);
2812 list_T *l = tv1->vval.v_list;
2813
2814 // add an item to a list
2815 if (l == NULL)
2816 {
2817 SOURCING_LNUM = iptr->isn_lnum;
2818 emsg(_(e_cannot_add_to_null_list));
2819 goto on_error;
2820 }
2821 if (list_append_tv(l, tv2) == FAIL)
2822 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002823 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002824 --ectx.ec_stack.ga_len;
2825 }
2826 break;
2827
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002828 case ISN_BLOBAPPEND:
2829 {
2830 typval_T *tv1 = STACK_TV_BOT(-2);
2831 typval_T *tv2 = STACK_TV_BOT(-1);
2832 blob_T *b = tv1->vval.v_blob;
2833 int error = FALSE;
2834 varnumber_T n;
2835
2836 // add a number to a blob
2837 if (b == NULL)
2838 {
2839 SOURCING_LNUM = iptr->isn_lnum;
2840 emsg(_(e_cannot_add_to_null_blob));
2841 goto on_error;
2842 }
2843 n = tv_get_number_chk(tv2, &error);
2844 if (error)
2845 goto on_error;
2846 ga_append(&b->bv_ga, (int)n);
2847 --ectx.ec_stack.ga_len;
2848 }
2849 break;
2850
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 // Computation with two arguments of unknown type
2852 case ISN_OPANY:
2853 {
2854 typval_T *tv1 = STACK_TV_BOT(-2);
2855 typval_T *tv2 = STACK_TV_BOT(-1);
2856 varnumber_T n1, n2;
2857#ifdef FEAT_FLOAT
2858 float_T f1 = 0, f2 = 0;
2859#endif
2860 int error = FALSE;
2861
2862 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2863 {
2864 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2865 {
2866 eval_addlist(tv1, tv2);
2867 clear_tv(tv2);
2868 --ectx.ec_stack.ga_len;
2869 break;
2870 }
2871 else if (tv1->v_type == VAR_BLOB
2872 && tv2->v_type == VAR_BLOB)
2873 {
2874 eval_addblob(tv1, tv2);
2875 clear_tv(tv2);
2876 --ectx.ec_stack.ga_len;
2877 break;
2878 }
2879 }
2880#ifdef FEAT_FLOAT
2881 if (tv1->v_type == VAR_FLOAT)
2882 {
2883 f1 = tv1->vval.v_float;
2884 n1 = 0;
2885 }
2886 else
2887#endif
2888 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 n1 = tv_get_number_chk(tv1, &error);
2891 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002892 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893#ifdef FEAT_FLOAT
2894 if (tv2->v_type == VAR_FLOAT)
2895 f1 = n1;
2896#endif
2897 }
2898#ifdef FEAT_FLOAT
2899 if (tv2->v_type == VAR_FLOAT)
2900 {
2901 f2 = tv2->vval.v_float;
2902 n2 = 0;
2903 }
2904 else
2905#endif
2906 {
2907 n2 = tv_get_number_chk(tv2, &error);
2908 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002909 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910#ifdef FEAT_FLOAT
2911 if (tv1->v_type == VAR_FLOAT)
2912 f2 = n2;
2913#endif
2914 }
2915#ifdef FEAT_FLOAT
2916 // if there is a float on either side the result is a float
2917 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2918 {
2919 switch (iptr->isn_arg.op.op_type)
2920 {
2921 case EXPR_MULT: f1 = f1 * f2; break;
2922 case EXPR_DIV: f1 = f1 / f2; break;
2923 case EXPR_SUB: f1 = f1 - f2; break;
2924 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002925 default: SOURCING_LNUM = iptr->isn_lnum;
2926 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002927 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 }
2929 clear_tv(tv1);
2930 clear_tv(tv2);
2931 tv1->v_type = VAR_FLOAT;
2932 tv1->vval.v_float = f1;
2933 --ectx.ec_stack.ga_len;
2934 }
2935 else
2936#endif
2937 {
2938 switch (iptr->isn_arg.op.op_type)
2939 {
2940 case EXPR_MULT: n1 = n1 * n2; break;
2941 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2942 case EXPR_SUB: n1 = n1 - n2; break;
2943 case EXPR_ADD: n1 = n1 + n2; break;
2944 default: n1 = num_modulus(n1, n2); break;
2945 }
2946 clear_tv(tv1);
2947 clear_tv(tv2);
2948 tv1->v_type = VAR_NUMBER;
2949 tv1->vval.v_number = n1;
2950 --ectx.ec_stack.ga_len;
2951 }
2952 }
2953 break;
2954
2955 case ISN_CONCAT:
2956 {
2957 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2958 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2959 char_u *res;
2960
2961 res = concat_str(str1, str2);
2962 clear_tv(STACK_TV_BOT(-2));
2963 clear_tv(STACK_TV_BOT(-1));
2964 --ectx.ec_stack.ga_len;
2965 STACK_TV_BOT(-1)->vval.v_string = res;
2966 }
2967 break;
2968
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002969 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002970 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002971 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002972 int is_slice = iptr->isn_type == ISN_STRSLICE;
2973 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002974 char_u *res;
2975
2976 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002977 // string slice: string is at stack-3, first index at
2978 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002979 if (is_slice)
2980 {
2981 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002982 n1 = tv->vval.v_number;
2983 }
2984
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002985 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002986 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002987
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002988 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002989 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002990 if (is_slice)
2991 // Slice: Select the characters from the string
2992 res = string_slice(tv->vval.v_string, n1, n2);
2993 else
2994 // Index: The resulting variable is a string of a
2995 // single character. If the index is too big or
2996 // negative the result is empty.
2997 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002998 vim_free(tv->vval.v_string);
2999 tv->vval.v_string = res;
3000 }
3001 break;
3002
3003 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003004 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 {
Bram Moolenaared591872020-08-15 22:14:53 +02003006 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003008 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009
3010 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003011 // list slice: list is at stack-3, indexes at stack-2 and
3012 // stack-1
3013 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 list = tv->vval.v_list;
3015
3016 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003017 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003019
3020 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 {
Bram Moolenaared591872020-08-15 22:14:53 +02003022 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003023 n1 = tv->vval.v_number;
3024 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 }
Bram Moolenaared591872020-08-15 22:14:53 +02003026
3027 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003028 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003029 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02003030 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
3031 == FAIL)
3032 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003033 }
3034 break;
3035
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003036 case ISN_ANYINDEX:
3037 case ISN_ANYSLICE:
3038 {
3039 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3040 typval_T *var1, *var2;
3041 int res;
3042
3043 // index: composite is at stack-2, index at stack-1
3044 // slice: composite is at stack-3, indexes at stack-2 and
3045 // stack-1
3046 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003048 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3049 goto on_error;
3050 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3051 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3052 res = eval_index_inner(tv, is_slice,
3053 var1, var2, NULL, -1, TRUE);
3054 clear_tv(var1);
3055 if (is_slice)
3056 clear_tv(var2);
3057 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3058 if (res == FAIL)
3059 goto on_error;
3060 }
3061 break;
3062
Bram Moolenaar9af78762020-06-16 11:34:42 +02003063 case ISN_SLICE:
3064 {
3065 list_T *list;
3066 int count = iptr->isn_arg.number;
3067
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003068 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003069 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003070 list = tv->vval.v_list;
3071
3072 // no error for short list, expect it to be checked earlier
3073 if (list != NULL && list->lv_len >= count)
3074 {
3075 list_T *newlist = list_slice(list,
3076 count, list->lv_len - 1);
3077
3078 if (newlist != NULL)
3079 {
3080 list_unref(list);
3081 tv->vval.v_list = newlist;
3082 ++newlist->lv_refcount;
3083 }
3084 }
3085 }
3086 break;
3087
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003088 case ISN_GETITEM:
3089 {
3090 listitem_T *li;
3091 int index = iptr->isn_arg.number;
3092
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003093 // Get list item: list is at stack-1, push item.
3094 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003095 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003096 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003097
3098 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3099 goto failed;
3100 ++ectx.ec_stack.ga_len;
3101 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3102 }
3103 break;
3104
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 case ISN_MEMBER:
3106 {
3107 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003108 char_u *key;
3109 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003110 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003111
3112 // dict member: dict is at stack-2, key at stack-1
3113 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003114 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003115 dict = tv->vval.v_dict;
3116
3117 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003118 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003119 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003120 if (key == NULL)
3121 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003122
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003123 if ((di = dict_find(dict, key, -1)) == NULL)
3124 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003126 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003127
3128 // If :silent! is used we will continue, make sure the
3129 // stack contents makes sense.
3130 clear_tv(tv);
3131 --ectx.ec_stack.ga_len;
3132 tv = STACK_TV_BOT(-1);
3133 clear_tv(tv);
3134 tv->v_type = VAR_NUMBER;
3135 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003136 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003137 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003138 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003139 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003140 // Clear the dict only after getting the item, to avoid
3141 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003142 tv = STACK_TV_BOT(-1);
3143 temp_tv = *tv;
3144 copy_tv(&di->di_tv, tv);
3145 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003146 }
3147 break;
3148
3149 // dict member with string key
3150 case ISN_STRINGMEMBER:
3151 {
3152 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003154 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003155
3156 tv = STACK_TV_BOT(-1);
3157 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3158 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003160 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162 }
3163 dict = tv->vval.v_dict;
3164
3165 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3166 == NULL)
3167 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003168 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003170 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003171 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003172 // Clear the dict after getting the item, to avoid that it
3173 // make the item invalid.
3174 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003176 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 }
3178 break;
3179
3180 case ISN_NEGATENR:
3181 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003182 if (tv->v_type != VAR_NUMBER
3183#ifdef FEAT_FLOAT
3184 && tv->v_type != VAR_FLOAT
3185#endif
3186 )
3187 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003189 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003190 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003191 }
3192#ifdef FEAT_FLOAT
3193 if (tv->v_type == VAR_FLOAT)
3194 tv->vval.v_float = -tv->vval.v_float;
3195 else
3196#endif
3197 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 break;
3199
3200 case ISN_CHECKNR:
3201 {
3202 int error = FALSE;
3203
3204 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 (void)tv_get_number_chk(tv, &error);
3209 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003210 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 break;
3213
3214 case ISN_CHECKTYPE:
3215 {
3216 checktype_T *ct = &iptr->isn_arg.type;
3217
3218 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003219 SOURCING_LNUM = iptr->isn_lnum;
3220 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3221 goto on_error;
3222
3223 // number 0 is FALSE, number 1 is TRUE
3224 if (tv->v_type == VAR_NUMBER
3225 && ct->ct_type->tt_type == VAR_BOOL
3226 && (tv->vval.v_number == 0
3227 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003229 tv->v_type = VAR_BOOL;
3230 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003231 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 }
3233 }
3234 break;
3235
Bram Moolenaar9af78762020-06-16 11:34:42 +02003236 case ISN_CHECKLEN:
3237 {
3238 int min_len = iptr->isn_arg.checklen.cl_min_len;
3239 list_T *list = NULL;
3240
3241 tv = STACK_TV_BOT(-1);
3242 if (tv->v_type == VAR_LIST)
3243 list = tv->vval.v_list;
3244 if (list == NULL || list->lv_len < min_len
3245 || (list->lv_len > min_len
3246 && !iptr->isn_arg.checklen.cl_more_OK))
3247 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003249 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003250 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003251 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003252 }
3253 }
3254 break;
3255
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003256 case ISN_SETTYPE:
3257 {
3258 checktype_T *ct = &iptr->isn_arg.type;
3259
3260 tv = STACK_TV_BOT(-1);
3261 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3262 {
3263 free_type(tv->vval.v_dict->dv_type);
3264 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3265 }
3266 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3267 {
3268 free_type(tv->vval.v_list->lv_type);
3269 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3270 }
3271 }
3272 break;
3273
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003275 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 {
3277 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003278 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279
3280 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003281 if (iptr->isn_type == ISN_2BOOL)
3282 {
3283 n = tv2bool(tv);
3284 if (iptr->isn_arg.number) // invert
3285 n = !n;
3286 }
3287 else
3288 {
3289 SOURCING_LNUM = iptr->isn_lnum;
3290 n = tv_get_bool_chk(tv, &error);
3291 if (error)
3292 goto on_error;
3293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 clear_tv(tv);
3295 tv->v_type = VAR_BOOL;
3296 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3297 }
3298 break;
3299
3300 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003301 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003303 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3304 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3305 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 break;
3307
Bram Moolenaar08597872020-12-10 19:43:40 +01003308 case ISN_RANGE:
3309 {
3310 exarg_T ea;
3311 char *errormsg;
3312
3313 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3314 goto failed;
3315 ++ectx.ec_stack.ga_len;
3316 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003317 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003318 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003319 ea.addr_type = ADDR_LINES;
3320 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003321 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003322 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003323 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003324 if (ea.addr_count == 0)
3325 tv->vval.v_number = curwin->w_cursor.lnum;
3326 else
3327 tv->vval.v_number = ea.line2;
3328 }
3329 break;
3330
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003331 case ISN_PUT:
3332 {
3333 int regname = iptr->isn_arg.put.put_regname;
3334 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3335 char_u *expr = NULL;
3336 int dir = FORWARD;
3337
3338 if (regname == '=')
3339 {
3340 tv = STACK_TV_BOT(-1);
3341 if (tv->v_type == VAR_STRING)
3342 expr = tv->vval.v_string;
3343 else
3344 {
3345 expr = typval_tostring(tv); // allocates value
3346 clear_tv(tv);
3347 }
3348 --ectx.ec_stack.ga_len;
3349 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003350 if (lnum < -2)
3351 {
3352 // line number was put on the stack by ISN_RANGE
3353 tv = STACK_TV_BOT(-1);
3354 curwin->w_cursor.lnum = tv->vval.v_number;
3355 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3356 dir = BACKWARD;
3357 --ectx.ec_stack.ga_len;
3358 }
3359 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003360 // :put! above cursor
3361 dir = BACKWARD;
3362 else if (lnum >= 0)
3363 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3364 check_cursor();
3365 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3366 vim_free(expr);
3367 }
3368 break;
3369
Bram Moolenaar02194d22020-10-24 23:08:38 +02003370 case ISN_CMDMOD:
3371 save_cmdmod = cmdmod;
3372 restore_cmdmod = TRUE;
3373 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3374 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003375 break;
3376
Bram Moolenaar02194d22020-10-24 23:08:38 +02003377 case ISN_CMDMOD_REV:
3378 // filter regprog is owned by the instruction, don't free it
3379 cmdmod.cmod_filter_regmatch.regprog = NULL;
3380 undo_cmdmod(&cmdmod);
3381 cmdmod = save_cmdmod;
3382 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003383 break;
3384
Bram Moolenaar792f7862020-11-23 08:31:18 +01003385 case ISN_UNPACK:
3386 {
3387 int count = iptr->isn_arg.unpack.unp_count;
3388 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3389 list_T *l;
3390 listitem_T *li;
3391 int i;
3392
3393 // Check there is a valid list to unpack.
3394 tv = STACK_TV_BOT(-1);
3395 if (tv->v_type != VAR_LIST)
3396 {
3397 SOURCING_LNUM = iptr->isn_lnum;
3398 emsg(_(e_for_argument_must_be_sequence_of_lists));
3399 goto on_error;
3400 }
3401 l = tv->vval.v_list;
3402 if (l == NULL
3403 || l->lv_len < (semicolon ? count - 1 : count))
3404 {
3405 SOURCING_LNUM = iptr->isn_lnum;
3406 emsg(_(e_list_value_does_not_have_enough_items));
3407 goto on_error;
3408 }
3409 else if (!semicolon && l->lv_len > count)
3410 {
3411 SOURCING_LNUM = iptr->isn_lnum;
3412 emsg(_(e_list_value_has_more_items_than_targets));
3413 goto on_error;
3414 }
3415
3416 CHECK_LIST_MATERIALIZE(l);
3417 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3418 goto failed;
3419 ectx.ec_stack.ga_len += count - 1;
3420
3421 // Variable after semicolon gets a list with the remaining
3422 // items.
3423 if (semicolon)
3424 {
3425 list_T *rem_list =
3426 list_alloc_with_items(l->lv_len - count + 1);
3427
3428 if (rem_list == NULL)
3429 goto failed;
3430 tv = STACK_TV_BOT(-count);
3431 tv->vval.v_list = rem_list;
3432 ++rem_list->lv_refcount;
3433 tv->v_lock = 0;
3434 li = l->lv_first;
3435 for (i = 0; i < count - 1; ++i)
3436 li = li->li_next;
3437 for (i = 0; li != NULL; ++i)
3438 {
3439 list_set_item(rem_list, i, &li->li_tv);
3440 li = li->li_next;
3441 }
3442 --count;
3443 }
3444
3445 // Produce the values in reverse order, first item last.
3446 li = l->lv_first;
3447 for (i = 0; i < count; ++i)
3448 {
3449 tv = STACK_TV_BOT(-i - 1);
3450 copy_tv(&li->li_tv, tv);
3451 li = li->li_next;
3452 }
3453
3454 list_unref(l);
3455 }
3456 break;
3457
Bram Moolenaar389df252020-07-09 21:20:47 +02003458 case ISN_SHUFFLE:
3459 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003460 typval_T tmp_tv;
3461 int item = iptr->isn_arg.shuffle.shfl_item;
3462 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003463
3464 tmp_tv = *STACK_TV_BOT(-item);
3465 for ( ; up > 0 && item > 1; --up)
3466 {
3467 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3468 --item;
3469 }
3470 *STACK_TV_BOT(-item) = tmp_tv;
3471 }
3472 break;
3473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 case ISN_DROP:
3475 --ectx.ec_stack.ga_len;
3476 clear_tv(STACK_TV_BOT(0));
3477 break;
3478 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003479 continue;
3480
Bram Moolenaard032f342020-07-18 18:13:02 +02003481func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003482 // Restore previous function. If the frame pointer is where we started
3483 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003484 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003485 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003486
Bram Moolenaard032f342020-07-18 18:13:02 +02003487 if (func_return(&ectx) == FAIL)
3488 // only fails when out of memory
3489 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003490 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003491
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003492on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003493 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003494 // If "emsg_silent" is set then ignore the error, unless it was set
3495 // when calling the function.
3496 if (did_emsg_cumul + did_emsg == did_emsg_before
3497 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003498 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003499on_fatal_error:
3500 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003501 // If we are not inside a try-catch started here, abort execution.
3502 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003503 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003504 }
3505
3506done:
3507 // function finished, get result from the stack.
3508 tv = STACK_TV_BOT(-1);
3509 *rettv = *tv;
3510 tv->v_type = VAR_UNKNOWN;
3511 ret = OK;
3512
3513failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003514 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003515 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003516 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003517
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003518 // Deal with any remaining closures, they may be in use somewhere.
3519 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003520 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003521 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003522 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3523 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003524
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003525 estack_pop();
3526 current_sctx = save_current_sctx;
3527
Bram Moolenaar352134b2020-10-17 22:04:08 +02003528 if (*msg_list != NULL && saved_msg_list != NULL)
3529 {
3530 msglist_T **plist = saved_msg_list;
3531
3532 // Append entries from the current msg_list (uncaught exceptions) to
3533 // the saved msg_list.
3534 while (*plist != NULL)
3535 plist = &(*plist)->next;
3536
3537 *plist = *msg_list;
3538 }
3539 msg_list = saved_msg_list;
3540
Bram Moolenaar02194d22020-10-24 23:08:38 +02003541 if (restore_cmdmod)
3542 {
3543 cmdmod.cmod_filter_regmatch.regprog = NULL;
3544 undo_cmdmod(&cmdmod);
3545 cmdmod = save_cmdmod;
3546 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003547 emsg_silent_def = save_emsg_silent_def;
3548 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003549
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003550failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003551 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3553 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003556 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003557
Bram Moolenaar0186e582021-01-10 18:33:11 +01003558 while (ectx.ec_outer != NULL)
3559 {
3560 outer_T *up = ectx.ec_outer->out_up_is_copy
3561 ? NULL : ectx.ec_outer->out_up;
3562
3563 vim_free(ectx.ec_outer);
3564 ectx.ec_outer = up;
3565 }
3566
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003567 // Not sure if this is necessary.
3568 suppress_errthrow = save_suppress_errthrow;
3569
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003570 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003571 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003572 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003573 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003574 return ret;
3575}
3576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003578 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003579 * We don't really need this at runtime, but we do have tests that require it,
3580 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581 */
3582 void
3583ex_disassemble(exarg_T *eap)
3584{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003585 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003586 char_u *fname;
3587 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003588 dfunc_T *dfunc;
3589 isn_T *instr;
3590 int current;
3591 int line_idx = 0;
3592 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003593 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003594
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003595 if (STRNCMP(arg, "<lambda>", 8) == 0)
3596 {
3597 arg += 8;
3598 (void)getdigits(&arg);
3599 fname = vim_strnsave(eap->arg, arg - eap->arg);
3600 }
3601 else
3602 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003603 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003604 if (fname == NULL)
3605 {
3606 semsg(_(e_invarg2), eap->arg);
3607 return;
3608 }
3609
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003610 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003611 if (ufunc == NULL)
3612 {
3613 char_u *p = untrans_function_name(fname);
3614
3615 if (p != NULL)
3616 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003617 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003618 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003619 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 if (ufunc == NULL)
3621 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003622 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 return;
3624 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003625 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003626 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3627 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003628 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003630 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 return;
3632 }
3633 if (ufunc->uf_name_exp != NULL)
3634 msg((char *)ufunc->uf_name_exp);
3635 else
3636 msg((char *)ufunc->uf_name);
3637
3638 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3639 instr = dfunc->df_instr;
3640 for (current = 0; current < dfunc->df_instr_count; ++current)
3641 {
3642 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003643 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644
3645 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3646 {
3647 if (current > prev_current)
3648 {
3649 msg_puts("\n\n");
3650 prev_current = current;
3651 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003652 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3653 if (line != NULL)
3654 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
3656
3657 switch (iptr->isn_type)
3658 {
3659 case ISN_EXEC:
3660 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3661 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003662 case ISN_EXECCONCAT:
3663 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003664 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003665 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 case ISN_ECHO:
3667 {
3668 echo_T *echo = &iptr->isn_arg.echo;
3669
3670 smsg("%4d %s %d", current,
3671 echo->echo_with_white ? "ECHO" : "ECHON",
3672 echo->echo_count);
3673 }
3674 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003675 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003676 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003677 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003678 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003679 case ISN_ECHOMSG:
3680 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003681 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003682 break;
3683 case ISN_ECHOERR:
3684 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003685 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003686 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003687 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003688 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003689 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003690 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003691 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003692 + STACK_FRAME_SIZE));
3693 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003694 smsg("%4d LOAD $%lld", current,
3695 (varnumber_T)(iptr->isn_arg.number));
3696 }
3697 break;
3698 case ISN_LOADOUTER:
3699 {
3700 if (iptr->isn_arg.number < 0)
3701 smsg("%4d LOADOUTER level %d arg[%d]", current,
3702 iptr->isn_arg.outer.outer_depth,
3703 iptr->isn_arg.outer.outer_idx
3704 + STACK_FRAME_SIZE);
3705 else
3706 smsg("%4d LOADOUTER level %d $%d", current,
3707 iptr->isn_arg.outer.outer_depth,
3708 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003709 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 break;
3711 case ISN_LOADV:
3712 smsg("%4d LOADV v:%s", current,
3713 get_vim_var_name(iptr->isn_arg.number));
3714 break;
3715 case ISN_LOADSCRIPT:
3716 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003717 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3718 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003720 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003722 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3723 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003724 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003725 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 }
3727 break;
3728 case ISN_LOADS:
3729 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003730 scriptitem_T *si = SCRIPT_ITEM(
3731 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003732
3733 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003734 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
3736 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003737 case ISN_LOADAUTO:
3738 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3739 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 case ISN_LOADG:
3741 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3742 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003743 case ISN_LOADB:
3744 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3745 break;
3746 case ISN_LOADW:
3747 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3748 break;
3749 case ISN_LOADT:
3750 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3751 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003752 case ISN_LOADGDICT:
3753 smsg("%4d LOAD g:", current);
3754 break;
3755 case ISN_LOADBDICT:
3756 smsg("%4d LOAD b:", current);
3757 break;
3758 case ISN_LOADWDICT:
3759 smsg("%4d LOAD w:", current);
3760 break;
3761 case ISN_LOADTDICT:
3762 smsg("%4d LOAD t:", current);
3763 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003764 case ISN_LOADOPT:
3765 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3766 break;
3767 case ISN_LOADENV:
3768 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3769 break;
3770 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003771 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 break;
3773
3774 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003775 if (iptr->isn_arg.number < 0)
3776 smsg("%4d STORE arg[%lld]", current,
3777 iptr->isn_arg.number + STACK_FRAME_SIZE);
3778 else
3779 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3780 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003781 case ISN_STOREOUTER:
3782 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003783 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003784 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3785 iptr->isn_arg.outer.outer_depth,
3786 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003787 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003788 smsg("%4d STOREOUTER level %d $%d", current,
3789 iptr->isn_arg.outer.outer_depth,
3790 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003791 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003792 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003793 case ISN_STOREV:
3794 smsg("%4d STOREV v:%s", current,
3795 get_vim_var_name(iptr->isn_arg.number));
3796 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003797 case ISN_STOREAUTO:
3798 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3799 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003801 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3802 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003803 case ISN_STOREB:
3804 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3805 break;
3806 case ISN_STOREW:
3807 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3808 break;
3809 case ISN_STORET:
3810 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3811 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003812 case ISN_STORES:
3813 {
3814 scriptitem_T *si = SCRIPT_ITEM(
3815 iptr->isn_arg.loadstore.ls_sid);
3816
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003817 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003818 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003819 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003820 break;
3821 case ISN_STORESCRIPT:
3822 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003823 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3824 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003826 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003828 smsg("%4d STORESCRIPT %s-%d in %s", current,
3829 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003830 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003831 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 }
3833 break;
3834 case ISN_STOREOPT:
3835 smsg("%4d STOREOPT &%s", current,
3836 iptr->isn_arg.storeopt.so_name);
3837 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003838 case ISN_STOREENV:
3839 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3840 break;
3841 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003842 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003843 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003844 case ISN_STORENR:
3845 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003846 iptr->isn_arg.storenr.stnr_val,
3847 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 break;
3849
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003850 case ISN_STOREINDEX:
3851 switch (iptr->isn_arg.vartype)
3852 {
3853 case VAR_LIST:
3854 smsg("%4d STORELIST", current);
3855 break;
3856 case VAR_DICT:
3857 smsg("%4d STOREDICT", current);
3858 break;
3859 case VAR_ANY:
3860 smsg("%4d STOREINDEX", current);
3861 break;
3862 default: break;
3863 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003864 break;
3865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 // constants
3867 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003868 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003869 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 break;
3871 case ISN_PUSHBOOL:
3872 case ISN_PUSHSPEC:
3873 smsg("%4d PUSH %s", current,
3874 get_var_special_name(iptr->isn_arg.number));
3875 break;
3876 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003877#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003879#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 break;
3881 case ISN_PUSHS:
3882 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3883 break;
3884 case ISN_PUSHBLOB:
3885 {
3886 char_u *r;
3887 char_u numbuf[NUMBUFLEN];
3888 char_u *tofree;
3889
3890 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003891 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003892 vim_free(tofree);
3893 }
3894 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003895 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003896 {
3897 char *name = (char *)iptr->isn_arg.string;
3898
3899 smsg("%4d PUSHFUNC \"%s\"", current,
3900 name == NULL ? "[none]" : name);
3901 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003902 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003903 case ISN_PUSHCHANNEL:
3904#ifdef FEAT_JOB_CHANNEL
3905 {
3906 channel_T *channel = iptr->isn_arg.channel;
3907
3908 smsg("%4d PUSHCHANNEL %d", current,
3909 channel == NULL ? 0 : channel->ch_id);
3910 }
3911#endif
3912 break;
3913 case ISN_PUSHJOB:
3914#ifdef FEAT_JOB_CHANNEL
3915 {
3916 typval_T tv;
3917 char_u *name;
3918
3919 tv.v_type = VAR_JOB;
3920 tv.vval.v_job = iptr->isn_arg.job;
3921 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003922 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003923 }
3924#endif
3925 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 case ISN_PUSHEXC:
3927 smsg("%4d PUSH v:exception", current);
3928 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003929 case ISN_UNLET:
3930 smsg("%4d UNLET%s %s", current,
3931 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3932 iptr->isn_arg.unlet.ul_name);
3933 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003934 case ISN_UNLETENV:
3935 smsg("%4d UNLETENV%s $%s", current,
3936 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3937 iptr->isn_arg.unlet.ul_name);
3938 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003939 case ISN_UNLETINDEX:
3940 smsg("%4d UNLETINDEX", current);
3941 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003942 case ISN_LOCKCONST:
3943 smsg("%4d LOCKCONST", current);
3944 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003946 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003947 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 break;
3949 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003950 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003951 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 break;
3953
3954 // function call
3955 case ISN_BCALL:
3956 {
3957 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3958
3959 smsg("%4d BCALL %s(argc %d)", current,
3960 internal_func_name(cbfunc->cbf_idx),
3961 cbfunc->cbf_argcount);
3962 }
3963 break;
3964 case ISN_DCALL:
3965 {
3966 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3967 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3968 + cdfunc->cdf_idx;
3969
3970 smsg("%4d DCALL %s(argc %d)", current,
3971 df->df_ufunc->uf_name_exp != NULL
3972 ? df->df_ufunc->uf_name_exp
3973 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3974 }
3975 break;
3976 case ISN_UCALL:
3977 {
3978 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3979
3980 smsg("%4d UCALL %s(argc %d)", current,
3981 cufunc->cuf_name, cufunc->cuf_argcount);
3982 }
3983 break;
3984 case ISN_PCALL:
3985 {
3986 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3987
3988 smsg("%4d PCALL%s (argc %d)", current,
3989 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3990 }
3991 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003992 case ISN_PCALL_END:
3993 smsg("%4d PCALL end", current);
3994 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003995 case ISN_RETURN:
3996 smsg("%4d RETURN", current);
3997 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01003998 case ISN_RETURN_ZERO:
3999 smsg("%4d RETURN 0", current);
4000 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004001 case ISN_FUNCREF:
4002 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004003 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004005 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004007 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 }
4009 break;
4010
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004011 case ISN_NEWFUNC:
4012 {
4013 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4014
4015 smsg("%4d NEWFUNC %s %s", current,
4016 newfunc->nf_lambda, newfunc->nf_global);
4017 }
4018 break;
4019
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004020 case ISN_DEF:
4021 {
4022 char_u *name = iptr->isn_arg.string;
4023
4024 smsg("%4d DEF %s", current,
4025 name == NULL ? (char_u *)"" : name);
4026 }
4027 break;
4028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 case ISN_JUMP:
4030 {
4031 char *when = "?";
4032
4033 switch (iptr->isn_arg.jump.jump_when)
4034 {
4035 case JUMP_ALWAYS:
4036 when = "JUMP";
4037 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004038 case JUMP_AND_KEEP_IF_TRUE:
4039 when = "JUMP_AND_KEEP_IF_TRUE";
4040 break;
4041 case JUMP_IF_FALSE:
4042 when = "JUMP_IF_FALSE";
4043 break;
4044 case JUMP_AND_KEEP_IF_FALSE:
4045 when = "JUMP_AND_KEEP_IF_FALSE";
4046 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004047 case JUMP_IF_COND_FALSE:
4048 when = "JUMP_IF_COND_FALSE";
4049 break;
4050 case JUMP_IF_COND_TRUE:
4051 when = "JUMP_IF_COND_TRUE";
4052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004054 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004055 iptr->isn_arg.jump.jump_where);
4056 }
4057 break;
4058
4059 case ISN_FOR:
4060 {
4061 forloop_T *forloop = &iptr->isn_arg.forloop;
4062
4063 smsg("%4d FOR $%d -> %d", current,
4064 forloop->for_idx, forloop->for_end);
4065 }
4066 break;
4067
4068 case ISN_TRY:
4069 {
4070 try_T *try = &iptr->isn_arg.try;
4071
4072 smsg("%4d TRY catch -> %d, finally -> %d", current,
4073 try->try_catch, try->try_finally);
4074 }
4075 break;
4076 case ISN_CATCH:
4077 // TODO
4078 smsg("%4d CATCH", current);
4079 break;
4080 case ISN_ENDTRY:
4081 smsg("%4d ENDTRY", current);
4082 break;
4083 case ISN_THROW:
4084 smsg("%4d THROW", current);
4085 break;
4086
4087 // expression operations on number
4088 case ISN_OPNR:
4089 case ISN_OPFLOAT:
4090 case ISN_OPANY:
4091 {
4092 char *what;
4093 char *ins;
4094
4095 switch (iptr->isn_arg.op.op_type)
4096 {
4097 case EXPR_MULT: what = "*"; break;
4098 case EXPR_DIV: what = "/"; break;
4099 case EXPR_REM: what = "%"; break;
4100 case EXPR_SUB: what = "-"; break;
4101 case EXPR_ADD: what = "+"; break;
4102 default: what = "???"; break;
4103 }
4104 switch (iptr->isn_type)
4105 {
4106 case ISN_OPNR: ins = "OPNR"; break;
4107 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4108 case ISN_OPANY: ins = "OPANY"; break;
4109 default: ins = "???"; break;
4110 }
4111 smsg("%4d %s %s", current, ins, what);
4112 }
4113 break;
4114
4115 case ISN_COMPAREBOOL:
4116 case ISN_COMPARESPECIAL:
4117 case ISN_COMPARENR:
4118 case ISN_COMPAREFLOAT:
4119 case ISN_COMPARESTRING:
4120 case ISN_COMPAREBLOB:
4121 case ISN_COMPARELIST:
4122 case ISN_COMPAREDICT:
4123 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 case ISN_COMPAREANY:
4125 {
4126 char *p;
4127 char buf[10];
4128 char *type;
4129
4130 switch (iptr->isn_arg.op.op_type)
4131 {
4132 case EXPR_EQUAL: p = "=="; break;
4133 case EXPR_NEQUAL: p = "!="; break;
4134 case EXPR_GREATER: p = ">"; break;
4135 case EXPR_GEQUAL: p = ">="; break;
4136 case EXPR_SMALLER: p = "<"; break;
4137 case EXPR_SEQUAL: p = "<="; break;
4138 case EXPR_MATCH: p = "=~"; break;
4139 case EXPR_IS: p = "is"; break;
4140 case EXPR_ISNOT: p = "isnot"; break;
4141 case EXPR_NOMATCH: p = "!~"; break;
4142 default: p = "???"; break;
4143 }
4144 STRCPY(buf, p);
4145 if (iptr->isn_arg.op.op_ic == TRUE)
4146 strcat(buf, "?");
4147 switch(iptr->isn_type)
4148 {
4149 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4150 case ISN_COMPARESPECIAL:
4151 type = "COMPARESPECIAL"; break;
4152 case ISN_COMPARENR: type = "COMPARENR"; break;
4153 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4154 case ISN_COMPARESTRING:
4155 type = "COMPARESTRING"; break;
4156 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4157 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4158 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4159 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4161 default: type = "???"; break;
4162 }
4163
4164 smsg("%4d %s %s", current, type, buf);
4165 }
4166 break;
4167
4168 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4169 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4170
4171 // expression operations
4172 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004173 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004174 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004175 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004176 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004177 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004178 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004179 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4180 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004181 case ISN_SLICE: smsg("%4d SLICE %lld",
4182 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004183 case ISN_GETITEM: smsg("%4d ITEM %lld",
4184 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004185 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4186 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 iptr->isn_arg.string); break;
4188 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4189
4190 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004191 case ISN_CHECKTYPE:
4192 {
4193 char *tofree;
4194
4195 smsg("%4d CHECKTYPE %s stack[%d]", current,
4196 type_name(iptr->isn_arg.type.ct_type, &tofree),
4197 iptr->isn_arg.type.ct_off);
4198 vim_free(tofree);
4199 break;
4200 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004201 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4202 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4203 iptr->isn_arg.checklen.cl_min_len);
4204 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004205 case ISN_SETTYPE:
4206 {
4207 char *tofree;
4208
4209 smsg("%4d SETTYPE %s", current,
4210 type_name(iptr->isn_arg.type.ct_type, &tofree));
4211 vim_free(tofree);
4212 break;
4213 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004214 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 case ISN_2BOOL: if (iptr->isn_arg.number)
4216 smsg("%4d INVERT (!val)", current);
4217 else
4218 smsg("%4d 2BOOL (!!val)", current);
4219 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004220 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004221 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004222 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004223 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004224 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004225 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004226 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4227 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004228 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004229 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4230 smsg("%4d PUT %c above range",
4231 current, iptr->isn_arg.put.put_regname);
4232 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4233 smsg("%4d PUT %c range",
4234 current, iptr->isn_arg.put.put_regname);
4235 else
4236 smsg("%4d PUT %c %ld", current,
4237 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004238 (long)iptr->isn_arg.put.put_lnum);
4239 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004240
Bram Moolenaar02194d22020-10-24 23:08:38 +02004241 // TODO: summarize modifiers
4242 case ISN_CMDMOD:
4243 {
4244 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004245 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004246 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4247
4248 buf = alloc(len + 1);
4249 if (buf != NULL)
4250 {
4251 (void)produce_cmdmods(
4252 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4253 smsg("%4d CMDMOD %s", current, buf);
4254 vim_free(buf);
4255 }
4256 break;
4257 }
4258 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004259
Bram Moolenaar792f7862020-11-23 08:31:18 +01004260 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4261 iptr->isn_arg.unpack.unp_count,
4262 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4263 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004264 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4265 iptr->isn_arg.shuffle.shfl_item,
4266 iptr->isn_arg.shuffle.shfl_up);
4267 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 case ISN_DROP: smsg("%4d DROP", current); break;
4269 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004270
4271 out_flush(); // output one line at a time
4272 ui_breakcheck();
4273 if (got_int)
4274 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276}
4277
4278/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004279 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 * list, etc. Mostly like what JavaScript does, except that empty list and
4281 * empty dictionary are FALSE.
4282 */
4283 int
4284tv2bool(typval_T *tv)
4285{
4286 switch (tv->v_type)
4287 {
4288 case VAR_NUMBER:
4289 return tv->vval.v_number != 0;
4290 case VAR_FLOAT:
4291#ifdef FEAT_FLOAT
4292 return tv->vval.v_float != 0.0;
4293#else
4294 break;
4295#endif
4296 case VAR_PARTIAL:
4297 return tv->vval.v_partial != NULL;
4298 case VAR_FUNC:
4299 case VAR_STRING:
4300 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4301 case VAR_LIST:
4302 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4303 case VAR_DICT:
4304 return tv->vval.v_dict != NULL
4305 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4306 case VAR_BOOL:
4307 case VAR_SPECIAL:
4308 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4309 case VAR_JOB:
4310#ifdef FEAT_JOB_CHANNEL
4311 return tv->vval.v_job != NULL;
4312#else
4313 break;
4314#endif
4315 case VAR_CHANNEL:
4316#ifdef FEAT_JOB_CHANNEL
4317 return tv->vval.v_channel != NULL;
4318#else
4319 break;
4320#endif
4321 case VAR_BLOB:
4322 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4323 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004324 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 case VAR_VOID:
4326 break;
4327 }
4328 return FALSE;
4329}
4330
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004331 void
4332emsg_using_string_as(typval_T *tv, int as_number)
4333{
4334 semsg(_(as_number ? e_using_string_as_number_str
4335 : e_using_string_as_bool_str),
4336 tv->vval.v_string == NULL
4337 ? (char_u *)"" : tv->vval.v_string);
4338}
4339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004340/*
4341 * If "tv" is a string give an error and return FAIL.
4342 */
4343 int
4344check_not_string(typval_T *tv)
4345{
4346 if (tv->v_type == VAR_STRING)
4347 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004348 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004349 clear_tv(tv);
4350 return FAIL;
4351 }
4352 return OK;
4353}
4354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004355
4356#endif // FEAT_EVAL