blob: 7c4ef2a92c030f60bc4738f60f09c3f91902986e [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;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100250 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
251 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200252 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
254 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200255 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200257 if (dfunc->df_has_closure)
258 {
259 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
260
261 tv->v_type = VAR_NUMBER;
262 tv->vval.v_number = 0;
263 }
264 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265
Bram Moolenaar0186e582021-01-10 18:33:11 +0100266 if (pt != NULL || ufunc->uf_partial != NULL || ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100267 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100268 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
269
270 if (outer == NULL)
271 return FAIL;
272 if (pt != NULL)
273 {
274 *outer = pt->pt_outer;
275 outer->out_up_is_copy = TRUE;
276 }
277 else if (ufunc->uf_partial != NULL)
278 {
279 *outer = ufunc->uf_partial->pt_outer;
280 outer->out_up_is_copy = TRUE;
281 }
282 else
283 {
284 outer->out_stack = &ectx->ec_stack;
285 outer->out_frame_idx = ectx->ec_frame_idx;
286 outer->out_up = ectx->ec_outer;
287 }
288 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100289 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100290 else
291 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 // Set execution state to the start of the called function.
294 ectx->ec_dfunc_idx = cdf_idx;
295 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200296 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
297 if (entry != NULL)
298 {
299 // Set the script context to the script where the function was defined.
300 // TODO: save more than the SID?
301 entry->es_save_sid = current_sctx.sc_sid;
302 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
303 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100304
305 // Decide where to start execution, handles optional arguments.
306 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307
308 return OK;
309}
310
311// Get pointer to item in the stack.
312#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
313
314/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200315 * Used when returning from a function: Check if any closure is still
316 * referenced. If so then move the arguments and variables to a separate piece
317 * of stack to be used when the closure is called.
318 * When "free_arguments" is TRUE the arguments are to be freed.
319 * Returns FAIL when out of memory.
320 */
321 static int
322handle_closure_in_use(ectx_T *ectx, int free_arguments)
323{
324 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
325 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200326 int argcount;
327 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 int idx;
329 typval_T *tv;
330 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200331 garray_T *gap = &ectx->ec_funcrefs;
332 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200333
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200334 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 return OK; // function was freed
336 if (dfunc->df_has_closure == 0)
337 return OK; // no closures
338 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
339 closure_count = tv->vval.v_number;
340 if (closure_count == 0)
341 return OK; // no funcrefs created
342
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200343 argcount = ufunc_argcount(dfunc->df_ufunc);
344 top = ectx->ec_frame_idx - argcount;
345
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200346 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200347 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200349 partial_T *pt;
350 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200351
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200352 if (off < 0)
353 continue; // count is off or already done
354 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200355 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200356 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200357 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200358 int i;
359
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200360 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200361 // unreferenced on return.
362 for (i = 0; i < dfunc->df_varcount; ++i)
363 {
364 typval_T *stv = STACK_TV(ectx->ec_frame_idx
365 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200366 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200367 --refcount;
368 }
369 if (refcount > 1)
370 {
371 closure_in_use = TRUE;
372 break;
373 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 }
376
377 if (closure_in_use)
378 {
379 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
380 typval_T *stack;
381
382 // A closure is using the arguments and/or local variables.
383 // Move them to the called function.
384 if (funcstack == NULL)
385 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200386 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
387 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
389 funcstack->fs_ga.ga_data = stack;
390 if (stack == NULL)
391 {
392 vim_free(funcstack);
393 return FAIL;
394 }
395
396 // Move or copy the arguments.
397 for (idx = 0; idx < argcount; ++idx)
398 {
399 tv = STACK_TV(top + idx);
400 if (free_arguments)
401 {
402 *(stack + idx) = *tv;
403 tv->v_type = VAR_UNKNOWN;
404 }
405 else
406 copy_tv(tv, stack + idx);
407 }
408 // Move the local variables.
409 for (idx = 0; idx < dfunc->df_varcount; ++idx)
410 {
411 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200412
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200413 // A partial created for a local function, that is also used as a
414 // local variable, has a reference count for the variable, thus
415 // will never go down to zero. When all these refcounts are one
416 // then the funcstack is unused. We need to count how many we have
417 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200418 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
419 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200420 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200421
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
424 gap->ga_len - closure_count + i])
425 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200426 }
427
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200428 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200429 tv->v_type = VAR_UNKNOWN;
430 }
431
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200433 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200434 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
435 - closure_count + idx];
436 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200438 ++funcstack->fs_refcount;
439 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100440 pt->pt_outer.out_stack = &funcstack->fs_ga;
441 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
442 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200443 }
444 }
445 }
446
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200447 for (idx = 0; idx < closure_count; ++idx)
448 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
449 - closure_count + idx]);
450 gap->ga_len -= closure_count;
451 if (gap->ga_len == 0)
452 ga_clear(gap);
453
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200454 return OK;
455}
456
457/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200458 * Called when a partial is freed or its reference count goes down to one. The
459 * funcstack may be the only reference to the partials in the local variables.
460 * Go over all of them, the funcref and can be freed if all partials
461 * referencing the funcstack have a reference count of one.
462 */
463 void
464funcstack_check_refcount(funcstack_T *funcstack)
465{
466 int i;
467 garray_T *gap = &funcstack->fs_ga;
468 int done = 0;
469
470 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
471 return;
472 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
473 {
474 typval_T *tv = ((typval_T *)gap->ga_data) + i;
475
476 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
477 && tv->vval.v_partial->pt_funcstack == funcstack
478 && tv->vval.v_partial->pt_refcount == 1)
479 ++done;
480 }
481 if (done == funcstack->fs_min_refcount)
482 {
483 typval_T *stack = gap->ga_data;
484
485 // All partials referencing the funcstack have a reference count of
486 // one, thus the funcstack is no longer of use.
487 for (i = 0; i < gap->ga_len; ++i)
488 clear_tv(stack + i);
489 vim_free(stack);
490 vim_free(funcstack);
491 }
492}
493
494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 * Return from the current function.
496 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498func_return(ectx_T *ectx)
499{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100501 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200502 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
503 + ectx->ec_dfunc_idx;
504 int argcount = ufunc_argcount(dfunc->df_ufunc);
505 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200506 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507
508 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200509 entry = estack_pop();
510 if (entry != NULL)
511 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 if (handle_closure_in_use(ectx, TRUE) == FAIL)
514 return FAIL;
515
516 // Clear the arguments.
517 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
518 clear_tv(STACK_TV(idx));
519
520 // Clear local variables and temp values, but not the return value.
521 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100522 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100524
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100525 // The return value should be on top of the stack. However, when aborting
526 // it may not be there and ec_frame_idx is the top of the stack.
527 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100529 ret_idx = 0;
530
Bram Moolenaar0186e582021-01-10 18:33:11 +0100531 vim_free(ectx->ec_outer);
532
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100533 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100534 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
535 + STACK_FRAME_FUNC_OFF)->vval.v_number;
536 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
537 + STACK_FRAME_IIDX_OFF)->vval.v_number;
538 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
539 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200540 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
542 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
544 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100545
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100546 if (ret_idx > 0)
547 {
548 // Reset the stack to the position before the call, with a spot for the
549 // return value, moved there from above the frame.
550 ectx->ec_stack.ga_len = top + 1;
551 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
552 }
553 else
554 // Reset the stack to the position before the call.
555 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200556
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200558 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559}
560
561#undef STACK_TV
562
563/*
564 * Prepare arguments and rettv for calling a builtin or user function.
565 */
566 static int
567call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
568{
569 int idx;
570 typval_T *tv;
571
572 // Move arguments from bottom of the stack to argvars[] and add terminator.
573 for (idx = 0; idx < argcount; ++idx)
574 argvars[idx] = *STACK_TV_BOT(idx - argcount);
575 argvars[argcount].v_type = VAR_UNKNOWN;
576
577 // Result replaces the arguments on the stack.
578 if (argcount > 0)
579 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200580 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 return FAIL;
582 else
583 ++ectx->ec_stack.ga_len;
584
585 // Default return value is zero.
586 tv = STACK_TV_BOT(-1);
587 tv->v_type = VAR_NUMBER;
588 tv->vval.v_number = 0;
589
590 return OK;
591}
592
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200593// Ugly global to avoid passing the execution context around through many
594// layers.
595static ectx_T *current_ectx = NULL;
596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597/*
598 * Call a builtin function by index.
599 */
600 static int
601call_bfunc(int func_idx, int argcount, ectx_T *ectx)
602{
603 typval_T argvars[MAX_FUNC_ARGS];
604 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100605 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200606 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 if (call_prepare(argcount, argvars, ectx) == FAIL)
609 return FAIL;
610
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200611 // Call the builtin function. Set "current_ectx" so that when it
612 // recursively invokes call_def_function() a closure context can be set.
613 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200615 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
617 // Clear the arguments.
618 for (idx = 0; idx < argcount; ++idx)
619 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200620
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100621 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 return OK;
624}
625
626/*
627 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100628 * If the function is compiled this will add a stack frame and set the
629 * instruction pointer at the start of the function.
630 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100631 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100632 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 */
634 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635call_ufunc(
636 ufunc_T *ufunc,
637 partial_T *pt,
638 int argcount,
639 ectx_T *ectx,
640 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641{
642 typval_T argvars[MAX_FUNC_ARGS];
643 funcexe_T funcexe;
644 int error;
645 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100646 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200648 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200649 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
650 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200651 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100652 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100653 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100654 if (error != FCERR_UNKNOWN)
655 {
656 if (error == FCERR_TOOMANY)
657 semsg(_(e_toomanyarg), ufunc->uf_name);
658 else
659 semsg(_(e_toofewarg), ufunc->uf_name);
660 return FAIL;
661 }
662
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100663 // The function has been compiled, can call it quickly. For a function
664 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100665 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100666 if (iptr != NULL)
667 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100668 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 iptr->isn_type = ISN_DCALL;
670 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
671 iptr->isn_arg.dfunc.cdf_argcount = argcount;
672 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100673 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (call_prepare(argcount, argvars, ectx) == FAIL)
677 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200678 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 funcexe.evaluate = TRUE;
680
681 // Call the user function. Result goes in last position on the stack.
682 // TODO: add selfdict if there is one
683 error = call_user_func_check(ufunc, argcount, argvars,
684 STACK_TV_BOT(-1), &funcexe, NULL);
685
686 // Clear the arguments.
687 for (idx = 0; idx < argcount; ++idx)
688 clear_tv(&argvars[idx]);
689
690 if (error != FCERR_NONE)
691 {
692 user_func_error(error, ufunc->uf_name);
693 return FAIL;
694 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200696 // Error other than from calling the function itself.
697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 return OK;
699}
700
701/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200702 * Return TRUE if an error was given or CTRL-C was pressed.
703 */
704 static int
705vim9_aborting(int prev_called_emsg)
706{
707 return called_emsg > prev_called_emsg || got_int || did_throw;
708}
709
710/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 * Execute a function by "name".
712 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100713 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 * Returns FAIL if not found without an error message.
715 */
716 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718{
719 ufunc_T *ufunc;
720
721 if (builtin_function(name, -1))
722 {
723 int func_idx = find_internal_func(name);
724
725 if (func_idx < 0)
726 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200727 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 return FAIL;
729 return call_bfunc(func_idx, argcount, ectx);
730 }
731
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200732 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200733
734 if (ufunc == NULL)
735 {
736 int called_emsg_before = called_emsg;
737
738 if (script_autoload(name, TRUE))
739 // loaded a package, search for the function again
740 ufunc = find_func(name, FALSE, NULL);
741 if (vim9_aborting(called_emsg_before))
742 return FAIL; // bail out if loading the script caused an error
743 }
744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100746 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
748 return FAIL;
749}
750
751 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200752call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200754 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200755 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100757 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if (tv->v_type == VAR_PARTIAL)
760 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200761 partial_T *pt = tv->vval.v_partial;
762 int i;
763
764 if (pt->pt_argc > 0)
765 {
766 // Make space for arguments from the partial, shift the "argcount"
767 // arguments up.
768 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
769 return FAIL;
770 for (i = 1; i <= argcount; ++i)
771 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
772 ectx->ec_stack.ga_len += pt->pt_argc;
773 argcount += pt->pt_argc;
774
775 // copy the arguments from the partial onto the stack
776 for (i = 0; i < pt->pt_argc; ++i)
777 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
780 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 name = pt->pt_name;
784 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200785 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200787 if (name != NULL)
788 {
789 char_u fname_buf[FLEN_FIXED + 1];
790 char_u *tofree = NULL;
791 int error = FCERR_NONE;
792 char_u *fname;
793
794 // May need to translate <SNR>123_ to K_SNR.
795 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
796 if (error != FCERR_NONE)
797 res = FAIL;
798 else
799 res = call_by_name(fname, argcount, ectx, NULL);
800 vim_free(tofree);
801 }
802
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100803 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 {
805 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200806 semsg(_(e_unknownfunc),
807 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 return FAIL;
809 }
810 return OK;
811}
812
813/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200814 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
815 * TRUE.
816 */
817 static int
818error_if_locked(int lock, char *error)
819{
820 if (lock & (VAR_LOCKED | VAR_FIXED))
821 {
822 emsg(_(error));
823 return TRUE;
824 }
825 return FALSE;
826}
827
828/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100829 * Store "tv" in variable "name".
830 * This is for s: and g: variables.
831 */
832 static void
833store_var(char_u *name, typval_T *tv)
834{
835 funccal_entry_T entry;
836
837 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100838 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100839 restore_funccal();
840}
841
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100842/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100843 * Convert "tv" to a string.
844 * Return FAIL if not allowed.
845 */
846 static int
847do_2string(typval_T *tv, int is_2string_any)
848{
849 if (tv->v_type != VAR_STRING)
850 {
851 char_u *str;
852
853 if (is_2string_any)
854 {
855 switch (tv->v_type)
856 {
857 case VAR_SPECIAL:
858 case VAR_BOOL:
859 case VAR_NUMBER:
860 case VAR_FLOAT:
861 case VAR_BLOB: break;
862 default: to_string_error(tv->v_type);
863 return FAIL;
864 }
865 }
866 str = typval_tostring(tv);
867 clear_tv(tv);
868 tv->v_type = VAR_STRING;
869 tv->vval.v_string = str;
870 }
871 return OK;
872}
873
874/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100875 * When the value of "sv" is a null list of dict, allocate it.
876 */
877 static void
878allocate_if_null(typval_T *tv)
879{
880 switch (tv->v_type)
881 {
882 case VAR_LIST:
883 if (tv->vval.v_list == NULL)
884 rettv_list_alloc(tv);
885 break;
886 case VAR_DICT:
887 if (tv->vval.v_dict == NULL)
888 rettv_dict_alloc(tv);
889 break;
890 default:
891 break;
892 }
893}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200894
Bram Moolenaare7525c52021-01-09 13:20:37 +0100895/*
896 * Return the character "str[index]" where "index" is the character index. If
897 * "index" is out of range NULL is returned.
898 */
899 char_u *
900char_from_string(char_u *str, varnumber_T index)
901{
902 size_t nbyte = 0;
903 varnumber_T nchar = index;
904 size_t slen;
905
906 if (str == NULL)
907 return NULL;
908 slen = STRLEN(str);
909
910 // do the same as for a list: a negative index counts from the end
911 if (index < 0)
912 {
913 int clen = 0;
914
915 for (nbyte = 0; nbyte < slen; ++clen)
916 nbyte += MB_CPTR2LEN(str + nbyte);
917 nchar = clen + index;
918 if (nchar < 0)
919 // unlike list: index out of range results in empty string
920 return NULL;
921 }
922
923 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
924 nbyte += MB_CPTR2LEN(str + nbyte);
925 if (nbyte >= slen)
926 return NULL;
927 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
928}
929
930/*
931 * Get the byte index for character index "idx" in string "str" with length
932 * "str_len".
933 * If going over the end return "str_len".
934 * If "idx" is negative count from the end, -1 is the last character.
935 * When going over the start return -1.
936 */
937 static long
938char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
939{
940 varnumber_T nchar = idx;
941 size_t nbyte = 0;
942
943 if (nchar >= 0)
944 {
945 while (nchar > 0 && nbyte < str_len)
946 {
947 nbyte += MB_CPTR2LEN(str + nbyte);
948 --nchar;
949 }
950 }
951 else
952 {
953 nbyte = str_len;
954 while (nchar < 0 && nbyte > 0)
955 {
956 --nbyte;
957 nbyte -= mb_head_off(str, str + nbyte);
958 ++nchar;
959 }
960 if (nchar < 0)
961 return -1;
962 }
963 return (long)nbyte;
964}
965
966/*
967 * Return the slice "str[first:last]" using character indexes.
968 * Return NULL when the result is empty.
969 */
970 char_u *
971string_slice(char_u *str, varnumber_T first, varnumber_T last)
972{
973 long start_byte, end_byte;
974 size_t slen;
975
976 if (str == NULL)
977 return NULL;
978 slen = STRLEN(str);
979 start_byte = char_idx2byte(str, slen, first);
980 if (start_byte < 0)
981 start_byte = 0; // first index very negative: use zero
982 if (last == -1)
983 end_byte = (long)slen;
984 else
985 {
986 end_byte = char_idx2byte(str, slen, last);
987 if (end_byte >= 0 && end_byte < (long)slen)
988 // end index is inclusive
989 end_byte += MB_CPTR2LEN(str + end_byte);
990 }
991
992 if (start_byte >= (long)slen || end_byte <= start_byte)
993 return NULL;
994 return vim_strnsave(str + start_byte, end_byte - start_byte);
995}
996
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100997 static svar_T *
998get_script_svar(scriptref_T *sref, ectx_T *ectx)
999{
1000 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1001 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1002 + ectx->ec_dfunc_idx;
1003 svar_T *sv;
1004
1005 if (sref->sref_seq != si->sn_script_seq)
1006 {
1007 // The script was reloaded after the function was
1008 // compiled, the script_idx may not be valid.
1009 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1010 dfunc->df_ufunc->uf_name_exp);
1011 return NULL;
1012 }
1013 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1014 if (!equal_type(sv->sv_type, sref->sref_type))
1015 {
1016 emsg(_(e_script_variable_type_changed));
1017 return NULL;
1018 }
1019 return sv;
1020}
1021
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001022/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 * Execute a function by "name".
1024 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001025 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 */
1027 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001028call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029{
Bram Moolenaared677f52020-08-12 16:38:10 +02001030 int called_emsg_before = called_emsg;
1031 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032
Bram Moolenaared677f52020-08-12 16:38:10 +02001033 res = call_by_name(name, argcount, ectx, iptr);
1034 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001036 dictitem_T *v;
1037
1038 v = find_var(name, NULL, FALSE);
1039 if (v == NULL)
1040 {
1041 semsg(_(e_unknownfunc), name);
1042 return FAIL;
1043 }
1044 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1045 {
1046 semsg(_(e_unknownfunc), name);
1047 return FAIL;
1048 }
1049 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001051 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052}
1053
1054/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001055 * When a function reference is used, fill a partial with the information
1056 * needed, especially when it is used as a closure.
1057 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001058 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001059fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1060{
1061 pt->pt_func = ufunc;
1062 pt->pt_refcount = 1;
1063
1064 if (pt->pt_func->uf_flags & FC_CLOSURE)
1065 {
1066 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1067 + ectx->ec_dfunc_idx;
1068
1069 // The closure needs to find arguments and local
1070 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001071 pt->pt_outer.out_stack = &ectx->ec_stack;
1072 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1073 pt->pt_outer.out_up = ectx->ec_outer;
1074 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001075
1076 // If this function returns and the closure is still
1077 // being used, we need to make a copy of the context
1078 // (arguments and local variables). Store a reference
1079 // to the partial so we can handle that.
1080 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1081 {
1082 vim_free(pt);
1083 return FAIL;
1084 }
1085 // Extra variable keeps the count of closures created
1086 // in the current function call.
1087 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1088 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1089
1090 ((partial_T **)ectx->ec_funcrefs.ga_data)
1091 [ectx->ec_funcrefs.ga_len] = pt;
1092 ++pt->pt_refcount;
1093 ++ectx->ec_funcrefs.ga_len;
1094 }
1095 ++pt->pt_func->uf_refcount;
1096 return OK;
1097}
1098
1099/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 * Call a "def" function from old Vim script.
1101 * Return OK or FAIL.
1102 */
1103 int
1104call_def_function(
1105 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001106 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001108 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 typval_T *rettv) // return value
1110{
1111 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001112 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001113 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 typval_T *tv;
1115 int idx;
1116 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001117 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001118 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001119 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001120 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001121 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001122 msglist_T **saved_msg_list = NULL;
1123 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001124 cmdmod_T save_cmdmod;
1125 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001126 int save_emsg_silent_def = emsg_silent_def;
1127 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001128 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001129 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130
1131// Get pointer to item in the stack.
1132#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1133
1134// Get pointer to item at the bottom of the stack, -1 is the bottom.
1135#undef STACK_TV_BOT
1136#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1137
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001138// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001139#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 +01001140
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001141 if (ufunc->uf_def_status == UF_NOT_COMPILED
1142 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001143 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001144 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001145 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001146 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001147 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001148 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001149 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001150
Bram Moolenaar09689a02020-05-09 22:50:08 +02001151 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001152 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001153 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1154 + ufunc->uf_dfunc_idx;
1155 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001156 {
1157 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001158 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001159 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001162 // If depth of calling is getting too high, don't execute the function.
1163 orig_funcdepth = funcdepth_get();
1164 if (funcdepth_increment() == FAIL)
1165 return FAIL;
1166
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001167 CLEAR_FIELD(ectx);
1168 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1169 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1170 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001171 {
1172 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001173 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001174 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001176 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001178 // Put arguments on the stack, but no more than what the function expects.
1179 // A lambda can be called with more arguments than it uses.
1180 for (idx = 0; idx < argc
1181 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1182 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001184 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001185 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1186 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001187 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 copy_tv(&argv[idx], STACK_TV_BOT(0));
1189 ++ectx.ec_stack.ga_len;
1190 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001191
1192 // Turn varargs into a list. Empty list if no args.
1193 if (ufunc->uf_va_name != NULL)
1194 {
1195 int vararg_count = argc - ufunc->uf_args.ga_len;
1196
1197 if (vararg_count < 0)
1198 vararg_count = 0;
1199 else
1200 argc -= vararg_count;
1201 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001202 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001203
1204 // Check the type of the list items.
1205 tv = STACK_TV_BOT(-1);
1206 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001207 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001208 && ufunc->uf_va_type->tt_member != &t_any
1209 && tv->vval.v_list != NULL)
1210 {
1211 type_T *expected = ufunc->uf_va_type->tt_member;
1212 listitem_T *li = tv->vval.v_list->lv_first;
1213
1214 for (idx = 0; idx < vararg_count; ++idx)
1215 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001216 if (check_typval_type(expected, &li->li_tv,
1217 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001218 goto failed_early;
1219 li = li->li_next;
1220 }
1221 }
1222
Bram Moolenaar23e03252020-04-12 22:22:31 +02001223 if (defcount > 0)
1224 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001225 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001226 --ectx.ec_stack.ga_len;
1227 }
1228
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001229 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001230 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001231 if (defcount > 0)
1232 for (idx = 0; idx < defcount; ++idx)
1233 {
1234 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1235 ++ectx.ec_stack.ga_len;
1236 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001237 if (ufunc->uf_va_name != NULL)
1238 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239
1240 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001241 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1242 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243
Bram Moolenaar0186e582021-01-10 18:33:11 +01001244 if (partial != NULL || ufunc->uf_partial != NULL)
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001245 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1247 if (ectx.ec_outer == NULL)
1248 goto failed_early;
1249 if (partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001250 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001251 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1252 {
1253 if (current_ectx->ec_outer != NULL)
1254 *ectx.ec_outer = *current_ectx->ec_outer;
1255 }
1256 else
1257 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001258 }
1259 else
Bram Moolenaar0186e582021-01-10 18:33:11 +01001260 *ectx.ec_outer = ufunc->uf_partial->pt_outer;
1261 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001262 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 // dummy frame entries
1265 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1266 {
1267 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1268 ++ectx.ec_stack.ga_len;
1269 }
1270
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001271 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001272 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001273 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1274 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001276 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001277 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001278 ectx.ec_stack.ga_len += dfunc->df_varcount;
1279 if (dfunc->df_has_closure)
1280 {
1281 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1282 STACK_TV_VAR(idx)->vval.v_number = 0;
1283 ++ectx.ec_stack.ga_len;
1284 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001285
1286 ectx.ec_instr = dfunc->df_instr;
1287 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001288
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001289 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001290 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001291 estack_push_ufunc(ufunc, 1);
1292 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001293 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1294
Bram Moolenaar352134b2020-10-17 22:04:08 +02001295 // Use a specific location for storing error messages to be converted to an
1296 // exception.
1297 saved_msg_list = msg_list;
1298 msg_list = &private_msg_list;
1299
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001300 // Do turn errors into exceptions.
1301 suppress_errthrow = FALSE;
1302
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001303 // When ":silent!" was used before calling then we still abort the
1304 // function. If ":silent!" is used in the function then we don't.
1305 emsg_silent_def = emsg_silent;
1306 did_emsg_def = 0;
1307
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001308 // Decide where to start execution, handles optional arguments.
1309 init_instr_idx(ufunc, argc, &ectx);
1310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 for (;;)
1312 {
1313 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001314
Bram Moolenaar270d0382020-05-15 21:42:53 +02001315 if (++breakcheck_count >= 100)
1316 {
1317 line_breakcheck();
1318 breakcheck_count = 0;
1319 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001320 if (got_int)
1321 {
1322 // Turn CTRL-C into an exception.
1323 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001324 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001325 goto failed;
1326 did_throw = TRUE;
1327 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328
Bram Moolenaara26b9702020-04-18 19:53:28 +02001329 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1330 {
1331 // Turn an error message into an exception.
1332 did_emsg = FALSE;
1333 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1334 goto failed;
1335 did_throw = TRUE;
1336 *msg_list = NULL;
1337 }
1338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 if (did_throw && !ectx.ec_in_catch)
1340 {
1341 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001342 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343
1344 // An exception jumps to the first catch, finally, or returns from
1345 // the current function.
1346 if (trystack->ga_len > 0)
1347 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001348 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001349 {
1350 // jump to ":catch" or ":finally"
1351 ectx.ec_in_catch = TRUE;
1352 ectx.ec_iidx = trycmd->tcd_catch_idx;
1353 }
1354 else
1355 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001356 // Not inside try or need to return from current functions.
1357 // Push a dummy return value.
1358 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1359 goto failed;
1360 tv = STACK_TV_BOT(0);
1361 tv->v_type = VAR_NUMBER;
1362 tv->vval.v_number = 0;
1363 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001364 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001366 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001367 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001368 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1369 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 goto done;
1371 }
1372
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001373 if (func_return(&ectx) == FAIL)
1374 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375 }
1376 continue;
1377 }
1378
1379 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1380 switch (iptr->isn_type)
1381 {
1382 // execute Ex command line
1383 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001384 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001385 source_cookie_T cookie;
1386
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001387 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001388 // Pass getsourceline to get an error for a missing ":end"
1389 // command.
1390 CLEAR_FIELD(cookie);
1391 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1392 if (do_cmdline(iptr->isn_arg.string,
1393 getsourceline, &cookie,
1394 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1395 == FAIL
1396 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001397 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001398 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 break;
1400
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001401 // execute Ex command from pieces on the stack
1402 case ISN_EXECCONCAT:
1403 {
1404 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001405 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001406 int pass;
1407 int i;
1408 char_u *cmd = NULL;
1409 char_u *str;
1410
1411 for (pass = 1; pass <= 2; ++pass)
1412 {
1413 for (i = 0; i < count; ++i)
1414 {
1415 tv = STACK_TV_BOT(i - count);
1416 str = tv->vval.v_string;
1417 if (str != NULL && *str != NUL)
1418 {
1419 if (pass == 2)
1420 STRCPY(cmd + len, str);
1421 len += STRLEN(str);
1422 }
1423 if (pass == 2)
1424 clear_tv(tv);
1425 }
1426 if (pass == 1)
1427 {
1428 cmd = alloc(len + 1);
1429 if (cmd == NULL)
1430 goto failed;
1431 len = 0;
1432 }
1433 }
1434
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001435 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001436 do_cmdline_cmd(cmd);
1437 vim_free(cmd);
1438 }
1439 break;
1440
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 // execute :echo {string} ...
1442 case ISN_ECHO:
1443 {
1444 int count = iptr->isn_arg.echo.echo_count;
1445 int atstart = TRUE;
1446 int needclr = TRUE;
1447
1448 for (idx = 0; idx < count; ++idx)
1449 {
1450 tv = STACK_TV_BOT(idx - count);
1451 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1452 &atstart, &needclr);
1453 clear_tv(tv);
1454 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001455 if (needclr)
1456 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 ectx.ec_stack.ga_len -= count;
1458 }
1459 break;
1460
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001461 // :execute {string} ...
1462 // :echomsg {string} ...
1463 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001464 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001465 case ISN_ECHOMSG:
1466 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001467 {
1468 int count = iptr->isn_arg.number;
1469 garray_T ga;
1470 char_u buf[NUMBUFLEN];
1471 char_u *p;
1472 int len;
1473 int failed = FALSE;
1474
1475 ga_init2(&ga, 1, 80);
1476 for (idx = 0; idx < count; ++idx)
1477 {
1478 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001479 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001480 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001481 if (tv->v_type == VAR_CHANNEL
1482 || tv->v_type == VAR_JOB)
1483 {
1484 SOURCING_LNUM = iptr->isn_lnum;
1485 emsg(_(e_inval_string));
1486 break;
1487 }
1488 else
1489 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001490 }
1491 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001492 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001493
1494 len = (int)STRLEN(p);
1495 if (ga_grow(&ga, len + 2) == FAIL)
1496 failed = TRUE;
1497 else
1498 {
1499 if (ga.ga_len > 0)
1500 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1501 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1502 ga.ga_len += len;
1503 }
1504 clear_tv(tv);
1505 }
1506 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001507 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001508 {
1509 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001510 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001511 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001512
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001513 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001514 {
1515 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001516 {
1517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001518 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001519 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001520 {
1521 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001522 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001523 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001524 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001525 else
1526 {
1527 msg_sb_eol();
1528 if (iptr->isn_type == ISN_ECHOMSG)
1529 {
1530 msg_attr(ga.ga_data, echo_attr);
1531 out_flush();
1532 }
1533 else
1534 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001535 SOURCING_LNUM = iptr->isn_lnum;
1536 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001537 }
1538 }
1539 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540 ga_clear(&ga);
1541 }
1542 break;
1543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 // load local variable or argument
1545 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001546 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 goto failed;
1548 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1549 ++ectx.ec_stack.ga_len;
1550 break;
1551
1552 // load v: variable
1553 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001554 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 goto failed;
1556 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1557 ++ectx.ec_stack.ga_len;
1558 break;
1559
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001560 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 case ISN_LOADSCRIPT:
1562 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001563 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 svar_T *sv;
1565
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001566 sv = get_script_svar(sref, &ectx);
1567 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001568 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001569 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001570 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 goto failed;
1572 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1573 ++ectx.ec_stack.ga_len;
1574 }
1575 break;
1576
1577 // load s: variable in old script
1578 case ISN_LOADS:
1579 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001580 hashtab_T *ht = &SCRIPT_VARS(
1581 iptr->isn_arg.loadstore.ls_sid);
1582 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 if (di == NULL)
1586 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001588 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001589 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 }
1591 else
1592 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001593 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 goto failed;
1595 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1596 ++ectx.ec_stack.ga_len;
1597 }
1598 }
1599 break;
1600
Bram Moolenaard3aac292020-04-19 14:32:17 +02001601 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001603 case ISN_LOADB:
1604 case ISN_LOADW:
1605 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001607 dictitem_T *di = NULL;
1608 hashtab_T *ht = NULL;
1609 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001610
Bram Moolenaard3aac292020-04-19 14:32:17 +02001611 switch (iptr->isn_type)
1612 {
1613 case ISN_LOADG:
1614 ht = get_globvar_ht();
1615 namespace = 'g';
1616 break;
1617 case ISN_LOADB:
1618 ht = &curbuf->b_vars->dv_hashtab;
1619 namespace = 'b';
1620 break;
1621 case ISN_LOADW:
1622 ht = &curwin->w_vars->dv_hashtab;
1623 namespace = 'w';
1624 break;
1625 case ISN_LOADT:
1626 ht = &curtab->tp_vars->dv_hashtab;
1627 namespace = 't';
1628 break;
1629 default: // Cannot reach here
1630 goto failed;
1631 }
1632 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 if (di == NULL)
1635 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001636 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001637 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001638 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001639 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 }
1641 else
1642 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001643 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 goto failed;
1645 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1646 ++ectx.ec_stack.ga_len;
1647 }
1648 }
1649 break;
1650
Bram Moolenaar03290b82020-12-19 16:30:44 +01001651 // load autoload variable
1652 case ISN_LOADAUTO:
1653 {
1654 char_u *name = iptr->isn_arg.string;
1655
1656 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1657 goto failed;
1658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001659 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001660 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1661 goto on_error;
1662 ++ectx.ec_stack.ga_len;
1663 }
1664 break;
1665
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001666 // load g:/b:/w:/t: namespace
1667 case ISN_LOADGDICT:
1668 case ISN_LOADBDICT:
1669 case ISN_LOADWDICT:
1670 case ISN_LOADTDICT:
1671 {
1672 dict_T *d = NULL;
1673
1674 switch (iptr->isn_type)
1675 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001676 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1677 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1678 case ISN_LOADWDICT: d = curwin->w_vars; break;
1679 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001680 default: // Cannot reach here
1681 goto failed;
1682 }
1683 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1684 goto failed;
1685 tv = STACK_TV_BOT(0);
1686 tv->v_type = VAR_DICT;
1687 tv->v_lock = 0;
1688 tv->vval.v_dict = d;
1689 ++ectx.ec_stack.ga_len;
1690 }
1691 break;
1692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 // load &option
1694 case ISN_LOADOPT:
1695 {
1696 typval_T optval;
1697 char_u *name = iptr->isn_arg.string;
1698
Bram Moolenaara8c17702020-04-01 21:17:24 +02001699 // This is not expected to fail, name is checked during
1700 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001701 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001703 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001704 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 *STACK_TV_BOT(0) = optval;
1706 ++ectx.ec_stack.ga_len;
1707 }
1708 break;
1709
1710 // load $ENV
1711 case ISN_LOADENV:
1712 {
1713 typval_T optval;
1714 char_u *name = iptr->isn_arg.string;
1715
Bram Moolenaar270d0382020-05-15 21:42:53 +02001716 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001718 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001719 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 *STACK_TV_BOT(0) = optval;
1721 ++ectx.ec_stack.ga_len;
1722 }
1723 break;
1724
1725 // load @register
1726 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001727 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 goto failed;
1729 tv = STACK_TV_BOT(0);
1730 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001731 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001732 // This may result in NULL, which should be equivalent to an
1733 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 tv->vval.v_string = get_reg_contents(
1735 iptr->isn_arg.number, GREG_EXPR_SRC);
1736 ++ectx.ec_stack.ga_len;
1737 break;
1738
1739 // store local variable
1740 case ISN_STORE:
1741 --ectx.ec_stack.ga_len;
1742 tv = STACK_TV_VAR(iptr->isn_arg.number);
1743 clear_tv(tv);
1744 *tv = *STACK_TV_BOT(0);
1745 break;
1746
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001747 // store s: variable in old script
1748 case ISN_STORES:
1749 {
1750 hashtab_T *ht = &SCRIPT_VARS(
1751 iptr->isn_arg.loadstore.ls_sid);
1752 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001753 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001754
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001755 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001756 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001757 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001758 else
1759 {
1760 clear_tv(&di->di_tv);
1761 di->di_tv = *STACK_TV_BOT(0);
1762 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001763 }
1764 break;
1765
1766 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 case ISN_STORESCRIPT:
1768 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001769 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1770 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001772 sv = get_script_svar(sref, &ectx);
1773 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001774 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 --ectx.ec_stack.ga_len;
1776 clear_tv(sv->sv_tv);
1777 *sv->sv_tv = *STACK_TV_BOT(0);
1778 }
1779 break;
1780
1781 // store option
1782 case ISN_STOREOPT:
1783 {
1784 long n = 0;
1785 char_u *s = NULL;
1786 char *msg;
1787
1788 --ectx.ec_stack.ga_len;
1789 tv = STACK_TV_BOT(0);
1790 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001791 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001793 if (s == NULL)
1794 s = (char_u *)"";
1795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001797 // must be VAR_NUMBER, CHECKTYPE makes sure
1798 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1800 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001801 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 if (msg != NULL)
1803 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001804 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001806 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
1809 break;
1810
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001811 // store $ENV
1812 case ISN_STOREENV:
1813 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001814 tv = STACK_TV_BOT(0);
1815 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1816 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001817 break;
1818
1819 // store @r
1820 case ISN_STOREREG:
1821 {
1822 int reg = iptr->isn_arg.number;
1823
1824 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001825 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001826 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001827 tv_get_string(tv), -1, FALSE);
1828 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001829 }
1830 break;
1831
1832 // store v: variable
1833 case ISN_STOREV:
1834 --ectx.ec_stack.ga_len;
1835 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1836 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001837 // should not happen, type is checked when compiling
1838 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001839 break;
1840
Bram Moolenaard3aac292020-04-19 14:32:17 +02001841 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001843 case ISN_STOREB:
1844 case ISN_STOREW:
1845 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001847 dictitem_T *di;
1848 hashtab_T *ht;
1849 char_u *name = iptr->isn_arg.string + 2;
1850
Bram Moolenaard3aac292020-04-19 14:32:17 +02001851 switch (iptr->isn_type)
1852 {
1853 case ISN_STOREG:
1854 ht = get_globvar_ht();
1855 break;
1856 case ISN_STOREB:
1857 ht = &curbuf->b_vars->dv_hashtab;
1858 break;
1859 case ISN_STOREW:
1860 ht = &curwin->w_vars->dv_hashtab;
1861 break;
1862 case ISN_STORET:
1863 ht = &curtab->tp_vars->dv_hashtab;
1864 break;
1865 default: // Cannot reach here
1866 goto failed;
1867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868
1869 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001870 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001872 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 else
1874 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001875 SOURCING_LNUM = iptr->isn_lnum;
1876 if (var_check_permission(di, name) == FAIL)
1877 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 clear_tv(&di->di_tv);
1879 di->di_tv = *STACK_TV_BOT(0);
1880 }
1881 }
1882 break;
1883
Bram Moolenaar03290b82020-12-19 16:30:44 +01001884 // store an autoload variable
1885 case ISN_STOREAUTO:
1886 SOURCING_LNUM = iptr->isn_lnum;
1887 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1888 clear_tv(STACK_TV_BOT(-1));
1889 --ectx.ec_stack.ga_len;
1890 break;
1891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 // store number in local variable
1893 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001894 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 clear_tv(tv);
1896 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001897 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 break;
1899
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001900 // store value in list or dict variable
1901 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001902 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001903 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001904 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001905 typval_T *tv_dest = STACK_TV_BOT(-1);
1906 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001907
Bram Moolenaar752fc692021-01-04 21:57:11 +01001908 // Stack contains:
1909 // -3 value to be stored
1910 // -2 index
1911 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001912 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001913 SOURCING_LNUM = iptr->isn_lnum;
1914 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001915 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001916 dest_type = tv_dest->v_type;
1917 if (dest_type == VAR_DICT)
1918 status = do_2string(tv_idx, TRUE);
1919 else if (dest_type == VAR_LIST
1920 && tv_idx->v_type != VAR_NUMBER)
1921 {
1922 emsg(_(e_number_exp));
1923 status = FAIL;
1924 }
1925 }
1926 else if (dest_type != tv_dest->v_type)
1927 {
1928 // just in case, should be OK
1929 semsg(_(e_expected_str_but_got_str),
1930 vartype_name(dest_type),
1931 vartype_name(tv_dest->v_type));
1932 status = FAIL;
1933 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001934
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001935 if (status == OK && dest_type == VAR_LIST)
1936 {
1937 varnumber_T lidx = tv_idx->vval.v_number;
1938 list_T *list = tv_dest->vval.v_list;
1939
1940 if (list == NULL)
1941 {
1942 emsg(_(e_list_not_set));
1943 goto on_error;
1944 }
1945 if (lidx < 0 && list->lv_len + lidx >= 0)
1946 // negative index is relative to the end
1947 lidx = list->lv_len + lidx;
1948 if (lidx < 0 || lidx > list->lv_len)
1949 {
1950 semsg(_(e_listidx), lidx);
1951 goto on_error;
1952 }
1953 if (lidx < list->lv_len)
1954 {
1955 listitem_T *li = list_find(list, lidx);
1956
1957 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001958 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001959 goto on_error;
1960 // overwrite existing list item
1961 clear_tv(&li->li_tv);
1962 li->li_tv = *tv;
1963 }
1964 else
1965 {
1966 if (error_if_locked(list->lv_lock,
1967 e_cannot_change_list))
1968 goto on_error;
1969 // append to list, only fails when out of memory
1970 if (list_append_tv(list, tv) == FAIL)
1971 goto failed;
1972 clear_tv(tv);
1973 }
1974 }
1975 else if (status == OK && dest_type == VAR_DICT)
1976 {
1977 char_u *key = tv_idx->vval.v_string;
1978 dict_T *dict = tv_dest->vval.v_dict;
1979 dictitem_T *di;
1980
1981 SOURCING_LNUM = iptr->isn_lnum;
1982 if (dict == NULL)
1983 {
1984 emsg(_(e_dictionary_not_set));
1985 goto on_error;
1986 }
1987 if (key == NULL)
1988 key = (char_u *)"";
1989 di = dict_find(dict, key, -1);
1990 if (di != NULL)
1991 {
1992 if (error_if_locked(di->di_tv.v_lock,
1993 e_cannot_change_dict_item))
1994 goto on_error;
1995 // overwrite existing value
1996 clear_tv(&di->di_tv);
1997 di->di_tv = *tv;
1998 }
1999 else
2000 {
2001 if (error_if_locked(dict->dv_lock,
2002 e_cannot_change_dict))
2003 goto on_error;
2004 // add to dict, only fails when out of memory
2005 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2006 goto failed;
2007 clear_tv(tv);
2008 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002009 }
2010 else
2011 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002012 status = FAIL;
2013 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002014 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002015
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002016 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002017 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002018 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002019 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002020 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002021 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002022 goto on_error;
2023 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002024 }
2025 break;
2026
Bram Moolenaar0186e582021-01-10 18:33:11 +01002027 // load or store variable or argument from outer scope
2028 case ISN_LOADOUTER:
2029 case ISN_STOREOUTER:
2030 {
2031 int depth = iptr->isn_arg.outer.outer_depth;
2032 outer_T *outer = ectx.ec_outer;
2033
2034 while (depth > 1 && outer != NULL)
2035 {
2036 outer = outer->out_up;
2037 --depth;
2038 }
2039 if (outer == NULL)
2040 {
2041 SOURCING_LNUM = iptr->isn_lnum;
2042 iemsg("LOADOUTER depth more than scope levels");
2043 goto failed;
2044 }
2045 tv = ((typval_T *)outer->out_stack->ga_data)
2046 + outer->out_frame_idx + STACK_FRAME_SIZE
2047 + iptr->isn_arg.outer.outer_idx;
2048 if (iptr->isn_type == ISN_LOADOUTER)
2049 {
2050 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2051 goto failed;
2052 copy_tv(tv, STACK_TV_BOT(0));
2053 ++ectx.ec_stack.ga_len;
2054 }
2055 else
2056 {
2057 --ectx.ec_stack.ga_len;
2058 clear_tv(tv);
2059 *tv = *STACK_TV_BOT(0);
2060 }
2061 }
2062 break;
2063
Bram Moolenaar752fc692021-01-04 21:57:11 +01002064 // unlet item in list or dict variable
2065 case ISN_UNLETINDEX:
2066 {
2067 typval_T *tv_idx = STACK_TV_BOT(-2);
2068 typval_T *tv_dest = STACK_TV_BOT(-1);
2069 int status = OK;
2070
2071 // Stack contains:
2072 // -2 index
2073 // -1 dict or list
2074 if (tv_dest->v_type == VAR_DICT)
2075 {
2076 // unlet a dict item, index must be a string
2077 if (tv_idx->v_type != VAR_STRING)
2078 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002079 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002080 semsg(_(e_expected_str_but_got_str),
2081 vartype_name(VAR_STRING),
2082 vartype_name(tv_idx->v_type));
2083 status = FAIL;
2084 }
2085 else
2086 {
2087 dict_T *d = tv_dest->vval.v_dict;
2088 char_u *key = tv_idx->vval.v_string;
2089 dictitem_T *di = NULL;
2090
2091 if (key == NULL)
2092 key = (char_u *)"";
2093 if (d != NULL)
2094 di = dict_find(d, key, (int)STRLEN(key));
2095 if (di == NULL)
2096 {
2097 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002099 semsg(_(e_dictkey), key);
2100 status = FAIL;
2101 }
2102 else
2103 {
2104 // TODO: check for dict or item locked
2105 dictitem_remove(d, di);
2106 }
2107 }
2108 }
2109 else if (tv_dest->v_type == VAR_LIST)
2110 {
2111 // unlet a List item, index must be a number
2112 if (tv_idx->v_type != VAR_NUMBER)
2113 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002114 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002115 semsg(_(e_expected_str_but_got_str),
2116 vartype_name(VAR_NUMBER),
2117 vartype_name(tv_idx->v_type));
2118 status = FAIL;
2119 }
2120 else
2121 {
2122 list_T *l = tv_dest->vval.v_list;
2123 varnumber_T n = tv_idx->vval.v_number;
2124 listitem_T *li = NULL;
2125
2126 li = list_find(l, n);
2127 if (li == NULL)
2128 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002130 semsg(_(e_listidx), n);
2131 status = FAIL;
2132 }
2133 else
2134 // TODO: check for list or item locked
2135 listitem_remove(l, li);
2136 }
2137 }
2138 else
2139 {
2140 status = FAIL;
2141 semsg(_(e_cannot_index_str),
2142 vartype_name(tv_dest->v_type));
2143 }
2144
2145 clear_tv(tv_idx);
2146 clear_tv(tv_dest);
2147 ectx.ec_stack.ga_len -= 2;
2148 if (status == FAIL)
2149 goto on_error;
2150 }
2151 break;
2152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 // push constant
2154 case ISN_PUSHNR:
2155 case ISN_PUSHBOOL:
2156 case ISN_PUSHSPEC:
2157 case ISN_PUSHF:
2158 case ISN_PUSHS:
2159 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002160 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002161 case ISN_PUSHCHANNEL:
2162 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002163 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 goto failed;
2165 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002166 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 ++ectx.ec_stack.ga_len;
2168 switch (iptr->isn_type)
2169 {
2170 case ISN_PUSHNR:
2171 tv->v_type = VAR_NUMBER;
2172 tv->vval.v_number = iptr->isn_arg.number;
2173 break;
2174 case ISN_PUSHBOOL:
2175 tv->v_type = VAR_BOOL;
2176 tv->vval.v_number = iptr->isn_arg.number;
2177 break;
2178 case ISN_PUSHSPEC:
2179 tv->v_type = VAR_SPECIAL;
2180 tv->vval.v_number = iptr->isn_arg.number;
2181 break;
2182#ifdef FEAT_FLOAT
2183 case ISN_PUSHF:
2184 tv->v_type = VAR_FLOAT;
2185 tv->vval.v_float = iptr->isn_arg.fnumber;
2186 break;
2187#endif
2188 case ISN_PUSHBLOB:
2189 blob_copy(iptr->isn_arg.blob, tv);
2190 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002191 case ISN_PUSHFUNC:
2192 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002193 if (iptr->isn_arg.string == NULL)
2194 tv->vval.v_string = NULL;
2195 else
2196 tv->vval.v_string =
2197 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002198 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002199 case ISN_PUSHCHANNEL:
2200#ifdef FEAT_JOB_CHANNEL
2201 tv->v_type = VAR_CHANNEL;
2202 tv->vval.v_channel = iptr->isn_arg.channel;
2203 if (tv->vval.v_channel != NULL)
2204 ++tv->vval.v_channel->ch_refcount;
2205#endif
2206 break;
2207 case ISN_PUSHJOB:
2208#ifdef FEAT_JOB_CHANNEL
2209 tv->v_type = VAR_JOB;
2210 tv->vval.v_job = iptr->isn_arg.job;
2211 if (tv->vval.v_job != NULL)
2212 ++tv->vval.v_job->jv_refcount;
2213#endif
2214 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 default:
2216 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002217 tv->vval.v_string = vim_strsave(
2218 iptr->isn_arg.string == NULL
2219 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 }
2221 break;
2222
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002223 case ISN_UNLET:
2224 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2225 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002226 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002227 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002228 case ISN_UNLETENV:
2229 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2230 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002231
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002232 case ISN_LOCKCONST:
2233 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2234 break;
2235
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 // create a list from items on the stack; uses a single allocation
2237 // for the list header and the items
2238 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002239 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2240 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 break;
2242
2243 // create a dict from items on the stack
2244 case ISN_NEWDICT:
2245 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002246 int count = iptr->isn_arg.number;
2247 dict_T *dict = dict_alloc();
2248 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002249 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250
2251 if (dict == NULL)
2252 goto failed;
2253 for (idx = 0; idx < count; ++idx)
2254 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002255 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002257 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002258 key = tv->vval.v_string == NULL
2259 ? (char_u *)"" : tv->vval.v_string;
2260 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002261 if (item != NULL)
2262 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002263 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002264 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002265 dict_unref(dict);
2266 goto on_error;
2267 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002268 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 clear_tv(tv);
2270 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002271 {
2272 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002274 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2276 item->di_tv.v_lock = 0;
2277 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002278 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002279 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002280 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002282 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 }
2284
2285 if (count > 0)
2286 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002287 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 goto failed;
2289 else
2290 ++ectx.ec_stack.ga_len;
2291 tv = STACK_TV_BOT(-1);
2292 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002293 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 tv->vval.v_dict = dict;
2295 ++dict->dv_refcount;
2296 }
2297 break;
2298
2299 // call a :def function
2300 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002301 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002302 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 iptr->isn_arg.dfunc.cdf_argcount,
2304 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002305 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 break;
2307
2308 // call a builtin function
2309 case ISN_BCALL:
2310 SOURCING_LNUM = iptr->isn_lnum;
2311 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2312 iptr->isn_arg.bfunc.cbf_argcount,
2313 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002314 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 break;
2316
2317 // call a funcref or partial
2318 case ISN_PCALL:
2319 {
2320 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2321 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002322 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323
2324 SOURCING_LNUM = iptr->isn_lnum;
2325 if (pfunc->cpf_top)
2326 {
2327 // funcref is above the arguments
2328 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2329 }
2330 else
2331 {
2332 // Get the funcref from the stack.
2333 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002334 partial_tv = *STACK_TV_BOT(0);
2335 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 }
2337 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002338 if (tv == &partial_tv)
2339 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002340 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002341 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 }
2343 break;
2344
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002345 case ISN_PCALL_END:
2346 // PCALL finished, arguments have been consumed and replaced by
2347 // the return value. Now clear the funcref from the stack,
2348 // and move the return value in its place.
2349 --ectx.ec_stack.ga_len;
2350 clear_tv(STACK_TV_BOT(-1));
2351 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2352 break;
2353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 // call a user defined function or funcref/partial
2355 case ISN_UCALL:
2356 {
2357 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2358
2359 SOURCING_LNUM = iptr->isn_lnum;
2360 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002361 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002362 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 }
2364 break;
2365
2366 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002367 case ISN_RETURN_ZERO:
2368 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2369 goto failed;
2370 tv = STACK_TV_BOT(0);
2371 ++ectx.ec_stack.ga_len;
2372 tv->v_type = VAR_NUMBER;
2373 tv->vval.v_number = 0;
2374 tv->v_lock = 0;
2375 // FALLTHROUGH
2376
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 case ISN_RETURN:
2378 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002379 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002380 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002381
2382 if (trystack->ga_len > 0)
2383 trycmd = ((trycmd_T *)trystack->ga_data)
2384 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002385 if (trycmd != NULL
2386 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 && trycmd->tcd_finally_idx != 0)
2388 {
2389 // jump to ":finally"
2390 ectx.ec_iidx = trycmd->tcd_finally_idx;
2391 trycmd->tcd_return = TRUE;
2392 }
2393 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002394 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 }
2396 break;
2397
2398 // push a function reference to a compiled function
2399 case ISN_FUNCREF:
2400 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002401 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2402 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2403 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 if (pt == NULL)
2406 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002407 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002408 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002409 vim_free(pt);
2410 goto failed;
2411 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002412 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2413 &ectx) == FAIL)
2414 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 tv = STACK_TV_BOT(0);
2417 ++ectx.ec_stack.ga_len;
2418 tv->vval.v_partial = pt;
2419 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002420 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 }
2422 break;
2423
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002424 // Create a global function from a lambda.
2425 case ISN_NEWFUNC:
2426 {
2427 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2428
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002429 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002430 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002431 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002432 }
2433 break;
2434
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002435 // List functions
2436 case ISN_DEF:
2437 if (iptr->isn_arg.string == NULL)
2438 list_functions(NULL);
2439 else
2440 {
2441 exarg_T ea;
2442
2443 CLEAR_FIELD(ea);
2444 ea.cmd = ea.arg = iptr->isn_arg.string;
2445 define_function(&ea, NULL);
2446 }
2447 break;
2448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 // jump if a condition is met
2450 case ISN_JUMP:
2451 {
2452 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002453 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 int jump = TRUE;
2455
2456 if (when != JUMP_ALWAYS)
2457 {
2458 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002459 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002460 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002461 || when == JUMP_IF_COND_TRUE)
2462 {
2463 SOURCING_LNUM = iptr->isn_lnum;
2464 jump = tv_get_bool_chk(tv, &error);
2465 if (error)
2466 goto on_error;
2467 }
2468 else
2469 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002471 || when == JUMP_AND_KEEP_IF_FALSE
2472 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002474 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 {
2476 // drop the value from the stack
2477 clear_tv(tv);
2478 --ectx.ec_stack.ga_len;
2479 }
2480 }
2481 if (jump)
2482 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2483 }
2484 break;
2485
2486 // top of a for loop
2487 case ISN_FOR:
2488 {
2489 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2490 typval_T *idxtv =
2491 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2492
2493 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002494 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002496 ++idxtv->vval.v_number;
2497 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 // past the end of the list, jump to "endfor"
2499 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2500 else if (list->lv_first == &range_list_item)
2501 {
2502 // non-materialized range() list
2503 tv = STACK_TV_BOT(0);
2504 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002505 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 tv->vval.v_number = list_find_nr(
2507 list, idxtv->vval.v_number, NULL);
2508 ++ectx.ec_stack.ga_len;
2509 }
2510 else
2511 {
2512 listitem_T *li = list_find(list, idxtv->vval.v_number);
2513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2515 ++ectx.ec_stack.ga_len;
2516 }
2517 }
2518 break;
2519
2520 // start of ":try" block
2521 case ISN_TRY:
2522 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002523 trycmd_T *trycmd = NULL;
2524
Bram Moolenaar270d0382020-05-15 21:42:53 +02002525 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 goto failed;
2527 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2528 + ectx.ec_trystack.ga_len;
2529 ++ectx.ec_trystack.ga_len;
2530 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002531 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2533 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002534 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002535 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 }
2537 break;
2538
2539 case ISN_PUSHEXC:
2540 if (current_exception == NULL)
2541 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002542 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 iemsg("Evaluating catch while current_exception is NULL");
2544 goto failed;
2545 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002546 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 goto failed;
2548 tv = STACK_TV_BOT(0);
2549 ++ectx.ec_stack.ga_len;
2550 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002551 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 tv->vval.v_string = vim_strsave(
2553 (char_u *)current_exception->value);
2554 break;
2555
2556 case ISN_CATCH:
2557 {
2558 garray_T *trystack = &ectx.ec_trystack;
2559
Bram Moolenaar20a76292020-12-25 19:47:24 +01002560 if (restore_cmdmod)
2561 {
2562 cmdmod.cmod_filter_regmatch.regprog = NULL;
2563 undo_cmdmod(&cmdmod);
2564 cmdmod = save_cmdmod;
2565 restore_cmdmod = FALSE;
2566 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (trystack->ga_len > 0)
2568 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002569 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 + trystack->ga_len - 1;
2571 trycmd->tcd_caught = TRUE;
2572 }
2573 did_emsg = got_int = did_throw = FALSE;
2574 catch_exception(current_exception);
2575 }
2576 break;
2577
2578 // end of ":try" block
2579 case ISN_ENDTRY:
2580 {
2581 garray_T *trystack = &ectx.ec_trystack;
2582
2583 if (trystack->ga_len > 0)
2584 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002585 trycmd_T *trycmd = NULL;
2586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 --trystack->ga_len;
2588 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002589 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 trycmd = ((trycmd_T *)trystack->ga_data)
2591 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002592 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 {
2594 // discard the exception
2595 if (caught_stack == current_exception)
2596 caught_stack = caught_stack->caught;
2597 discard_current_exception();
2598 }
2599
2600 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002601 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 }
2603 }
2604 break;
2605
2606 case ISN_THROW:
2607 --ectx.ec_stack.ga_len;
2608 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002609 if (tv->vval.v_string == NULL
2610 || *skipwhite(tv->vval.v_string) == NUL)
2611 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002612 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002614 emsg(_(e_throw_with_empty_string));
2615 goto failed;
2616 }
2617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2619 {
2620 vim_free(tv->vval.v_string);
2621 goto failed;
2622 }
2623 did_throw = TRUE;
2624 break;
2625
2626 // compare with special values
2627 case ISN_COMPAREBOOL:
2628 case ISN_COMPARESPECIAL:
2629 {
2630 typval_T *tv1 = STACK_TV_BOT(-2);
2631 typval_T *tv2 = STACK_TV_BOT(-1);
2632 varnumber_T arg1 = tv1->vval.v_number;
2633 varnumber_T arg2 = tv2->vval.v_number;
2634 int res;
2635
2636 switch (iptr->isn_arg.op.op_type)
2637 {
2638 case EXPR_EQUAL: res = arg1 == arg2; break;
2639 case EXPR_NEQUAL: res = arg1 != arg2; break;
2640 default: res = 0; break;
2641 }
2642
2643 --ectx.ec_stack.ga_len;
2644 tv1->v_type = VAR_BOOL;
2645 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2646 }
2647 break;
2648
2649 // Operation with two number arguments
2650 case ISN_OPNR:
2651 case ISN_COMPARENR:
2652 {
2653 typval_T *tv1 = STACK_TV_BOT(-2);
2654 typval_T *tv2 = STACK_TV_BOT(-1);
2655 varnumber_T arg1 = tv1->vval.v_number;
2656 varnumber_T arg2 = tv2->vval.v_number;
2657 varnumber_T res;
2658
2659 switch (iptr->isn_arg.op.op_type)
2660 {
2661 case EXPR_MULT: res = arg1 * arg2; break;
2662 case EXPR_DIV: res = arg1 / arg2; break;
2663 case EXPR_REM: res = arg1 % arg2; break;
2664 case EXPR_SUB: res = arg1 - arg2; break;
2665 case EXPR_ADD: res = arg1 + arg2; break;
2666
2667 case EXPR_EQUAL: res = arg1 == arg2; break;
2668 case EXPR_NEQUAL: res = arg1 != arg2; break;
2669 case EXPR_GREATER: res = arg1 > arg2; break;
2670 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2671 case EXPR_SMALLER: res = arg1 < arg2; break;
2672 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2673 default: res = 0; break;
2674 }
2675
2676 --ectx.ec_stack.ga_len;
2677 if (iptr->isn_type == ISN_COMPARENR)
2678 {
2679 tv1->v_type = VAR_BOOL;
2680 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2681 }
2682 else
2683 tv1->vval.v_number = res;
2684 }
2685 break;
2686
2687 // Computation with two float arguments
2688 case ISN_OPFLOAT:
2689 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002690#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002691 {
2692 typval_T *tv1 = STACK_TV_BOT(-2);
2693 typval_T *tv2 = STACK_TV_BOT(-1);
2694 float_T arg1 = tv1->vval.v_float;
2695 float_T arg2 = tv2->vval.v_float;
2696 float_T res = 0;
2697 int cmp = FALSE;
2698
2699 switch (iptr->isn_arg.op.op_type)
2700 {
2701 case EXPR_MULT: res = arg1 * arg2; break;
2702 case EXPR_DIV: res = arg1 / arg2; break;
2703 case EXPR_SUB: res = arg1 - arg2; break;
2704 case EXPR_ADD: res = arg1 + arg2; break;
2705
2706 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2707 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2708 case EXPR_GREATER: cmp = arg1 > arg2; break;
2709 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2710 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2711 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2712 default: cmp = 0; break;
2713 }
2714 --ectx.ec_stack.ga_len;
2715 if (iptr->isn_type == ISN_COMPAREFLOAT)
2716 {
2717 tv1->v_type = VAR_BOOL;
2718 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2719 }
2720 else
2721 tv1->vval.v_float = res;
2722 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002723#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 break;
2725
2726 case ISN_COMPARELIST:
2727 {
2728 typval_T *tv1 = STACK_TV_BOT(-2);
2729 typval_T *tv2 = STACK_TV_BOT(-1);
2730 list_T *arg1 = tv1->vval.v_list;
2731 list_T *arg2 = tv2->vval.v_list;
2732 int cmp = FALSE;
2733 int ic = iptr->isn_arg.op.op_ic;
2734
2735 switch (iptr->isn_arg.op.op_type)
2736 {
2737 case EXPR_EQUAL: cmp =
2738 list_equal(arg1, arg2, ic, FALSE); break;
2739 case EXPR_NEQUAL: cmp =
2740 !list_equal(arg1, arg2, ic, FALSE); break;
2741 case EXPR_IS: cmp = arg1 == arg2; break;
2742 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2743 default: cmp = 0; break;
2744 }
2745 --ectx.ec_stack.ga_len;
2746 clear_tv(tv1);
2747 clear_tv(tv2);
2748 tv1->v_type = VAR_BOOL;
2749 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2750 }
2751 break;
2752
2753 case ISN_COMPAREBLOB:
2754 {
2755 typval_T *tv1 = STACK_TV_BOT(-2);
2756 typval_T *tv2 = STACK_TV_BOT(-1);
2757 blob_T *arg1 = tv1->vval.v_blob;
2758 blob_T *arg2 = tv2->vval.v_blob;
2759 int cmp = FALSE;
2760
2761 switch (iptr->isn_arg.op.op_type)
2762 {
2763 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2764 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2765 case EXPR_IS: cmp = arg1 == arg2; break;
2766 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2767 default: cmp = 0; break;
2768 }
2769 --ectx.ec_stack.ga_len;
2770 clear_tv(tv1);
2771 clear_tv(tv2);
2772 tv1->v_type = VAR_BOOL;
2773 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2774 }
2775 break;
2776
2777 // TODO: handle separately
2778 case ISN_COMPARESTRING:
2779 case ISN_COMPAREDICT:
2780 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002781 case ISN_COMPAREANY:
2782 {
2783 typval_T *tv1 = STACK_TV_BOT(-2);
2784 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002785 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 int ic = iptr->isn_arg.op.op_ic;
2787
Bram Moolenaareb26f432020-09-14 16:50:05 +02002788 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002789 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 --ectx.ec_stack.ga_len;
2792 }
2793 break;
2794
2795 case ISN_ADDLIST:
2796 case ISN_ADDBLOB:
2797 {
2798 typval_T *tv1 = STACK_TV_BOT(-2);
2799 typval_T *tv2 = STACK_TV_BOT(-1);
2800
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002801 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 if (iptr->isn_type == ISN_ADDLIST)
2803 eval_addlist(tv1, tv2);
2804 else
2805 eval_addblob(tv1, tv2);
2806 clear_tv(tv2);
2807 --ectx.ec_stack.ga_len;
2808 }
2809 break;
2810
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002811 case ISN_LISTAPPEND:
2812 {
2813 typval_T *tv1 = STACK_TV_BOT(-2);
2814 typval_T *tv2 = STACK_TV_BOT(-1);
2815 list_T *l = tv1->vval.v_list;
2816
2817 // add an item to a list
2818 if (l == NULL)
2819 {
2820 SOURCING_LNUM = iptr->isn_lnum;
2821 emsg(_(e_cannot_add_to_null_list));
2822 goto on_error;
2823 }
2824 if (list_append_tv(l, tv2) == FAIL)
2825 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002826 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002827 --ectx.ec_stack.ga_len;
2828 }
2829 break;
2830
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002831 case ISN_BLOBAPPEND:
2832 {
2833 typval_T *tv1 = STACK_TV_BOT(-2);
2834 typval_T *tv2 = STACK_TV_BOT(-1);
2835 blob_T *b = tv1->vval.v_blob;
2836 int error = FALSE;
2837 varnumber_T n;
2838
2839 // add a number to a blob
2840 if (b == NULL)
2841 {
2842 SOURCING_LNUM = iptr->isn_lnum;
2843 emsg(_(e_cannot_add_to_null_blob));
2844 goto on_error;
2845 }
2846 n = tv_get_number_chk(tv2, &error);
2847 if (error)
2848 goto on_error;
2849 ga_append(&b->bv_ga, (int)n);
2850 --ectx.ec_stack.ga_len;
2851 }
2852 break;
2853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 // Computation with two arguments of unknown type
2855 case ISN_OPANY:
2856 {
2857 typval_T *tv1 = STACK_TV_BOT(-2);
2858 typval_T *tv2 = STACK_TV_BOT(-1);
2859 varnumber_T n1, n2;
2860#ifdef FEAT_FLOAT
2861 float_T f1 = 0, f2 = 0;
2862#endif
2863 int error = FALSE;
2864
2865 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2866 {
2867 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2868 {
2869 eval_addlist(tv1, tv2);
2870 clear_tv(tv2);
2871 --ectx.ec_stack.ga_len;
2872 break;
2873 }
2874 else if (tv1->v_type == VAR_BLOB
2875 && tv2->v_type == VAR_BLOB)
2876 {
2877 eval_addblob(tv1, tv2);
2878 clear_tv(tv2);
2879 --ectx.ec_stack.ga_len;
2880 break;
2881 }
2882 }
2883#ifdef FEAT_FLOAT
2884 if (tv1->v_type == VAR_FLOAT)
2885 {
2886 f1 = tv1->vval.v_float;
2887 n1 = 0;
2888 }
2889 else
2890#endif
2891 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002892 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 n1 = tv_get_number_chk(tv1, &error);
2894 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002895 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896#ifdef FEAT_FLOAT
2897 if (tv2->v_type == VAR_FLOAT)
2898 f1 = n1;
2899#endif
2900 }
2901#ifdef FEAT_FLOAT
2902 if (tv2->v_type == VAR_FLOAT)
2903 {
2904 f2 = tv2->vval.v_float;
2905 n2 = 0;
2906 }
2907 else
2908#endif
2909 {
2910 n2 = tv_get_number_chk(tv2, &error);
2911 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002912 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002913#ifdef FEAT_FLOAT
2914 if (tv1->v_type == VAR_FLOAT)
2915 f2 = n2;
2916#endif
2917 }
2918#ifdef FEAT_FLOAT
2919 // if there is a float on either side the result is a float
2920 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2921 {
2922 switch (iptr->isn_arg.op.op_type)
2923 {
2924 case EXPR_MULT: f1 = f1 * f2; break;
2925 case EXPR_DIV: f1 = f1 / f2; break;
2926 case EXPR_SUB: f1 = f1 - f2; break;
2927 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002928 default: SOURCING_LNUM = iptr->isn_lnum;
2929 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002930 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002931 }
2932 clear_tv(tv1);
2933 clear_tv(tv2);
2934 tv1->v_type = VAR_FLOAT;
2935 tv1->vval.v_float = f1;
2936 --ectx.ec_stack.ga_len;
2937 }
2938 else
2939#endif
2940 {
2941 switch (iptr->isn_arg.op.op_type)
2942 {
2943 case EXPR_MULT: n1 = n1 * n2; break;
2944 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2945 case EXPR_SUB: n1 = n1 - n2; break;
2946 case EXPR_ADD: n1 = n1 + n2; break;
2947 default: n1 = num_modulus(n1, n2); break;
2948 }
2949 clear_tv(tv1);
2950 clear_tv(tv2);
2951 tv1->v_type = VAR_NUMBER;
2952 tv1->vval.v_number = n1;
2953 --ectx.ec_stack.ga_len;
2954 }
2955 }
2956 break;
2957
2958 case ISN_CONCAT:
2959 {
2960 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2961 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2962 char_u *res;
2963
2964 res = concat_str(str1, str2);
2965 clear_tv(STACK_TV_BOT(-2));
2966 clear_tv(STACK_TV_BOT(-1));
2967 --ectx.ec_stack.ga_len;
2968 STACK_TV_BOT(-1)->vval.v_string = res;
2969 }
2970 break;
2971
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002972 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002973 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002974 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002975 int is_slice = iptr->isn_type == ISN_STRSLICE;
2976 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002977 char_u *res;
2978
2979 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002980 // string slice: string is at stack-3, first index at
2981 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002982 if (is_slice)
2983 {
2984 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002985 n1 = tv->vval.v_number;
2986 }
2987
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002988 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002989 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002990
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002991 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002992 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002993 if (is_slice)
2994 // Slice: Select the characters from the string
2995 res = string_slice(tv->vval.v_string, n1, n2);
2996 else
2997 // Index: The resulting variable is a string of a
2998 // single character. If the index is too big or
2999 // negative the result is empty.
3000 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003001 vim_free(tv->vval.v_string);
3002 tv->vval.v_string = res;
3003 }
3004 break;
3005
3006 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003007 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 {
Bram Moolenaared591872020-08-15 22:14:53 +02003009 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003011 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012
3013 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003014 // list slice: list is at stack-3, indexes at stack-2 and
3015 // stack-1
3016 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 list = tv->vval.v_list;
3018
3019 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003020 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003021 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003022
3023 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 {
Bram Moolenaared591872020-08-15 22:14:53 +02003025 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003026 n1 = tv->vval.v_number;
3027 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 }
Bram Moolenaared591872020-08-15 22:14:53 +02003029
3030 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003031 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02003033 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
3034 == FAIL)
3035 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 }
3037 break;
3038
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003039 case ISN_ANYINDEX:
3040 case ISN_ANYSLICE:
3041 {
3042 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3043 typval_T *var1, *var2;
3044 int res;
3045
3046 // index: composite is at stack-2, index at stack-1
3047 // slice: composite is at stack-3, indexes at stack-2 and
3048 // stack-1
3049 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003050 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003051 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3052 goto on_error;
3053 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3054 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
3055 res = eval_index_inner(tv, is_slice,
3056 var1, var2, NULL, -1, TRUE);
3057 clear_tv(var1);
3058 if (is_slice)
3059 clear_tv(var2);
3060 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3061 if (res == FAIL)
3062 goto on_error;
3063 }
3064 break;
3065
Bram Moolenaar9af78762020-06-16 11:34:42 +02003066 case ISN_SLICE:
3067 {
3068 list_T *list;
3069 int count = iptr->isn_arg.number;
3070
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003071 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003072 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003073 list = tv->vval.v_list;
3074
3075 // no error for short list, expect it to be checked earlier
3076 if (list != NULL && list->lv_len >= count)
3077 {
3078 list_T *newlist = list_slice(list,
3079 count, list->lv_len - 1);
3080
3081 if (newlist != NULL)
3082 {
3083 list_unref(list);
3084 tv->vval.v_list = newlist;
3085 ++newlist->lv_refcount;
3086 }
3087 }
3088 }
3089 break;
3090
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003091 case ISN_GETITEM:
3092 {
3093 listitem_T *li;
3094 int index = iptr->isn_arg.number;
3095
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003096 // Get list item: list is at stack-1, push item.
3097 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003098 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003099 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003100
3101 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3102 goto failed;
3103 ++ectx.ec_stack.ga_len;
3104 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3105 }
3106 break;
3107
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 case ISN_MEMBER:
3109 {
3110 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003111 char_u *key;
3112 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003113 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003114
3115 // dict member: dict is at stack-2, key at stack-1
3116 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003117 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003118 dict = tv->vval.v_dict;
3119
3120 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003121 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003122 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003123 if (key == NULL)
3124 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003125
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003126 if ((di = dict_find(dict, key, -1)) == NULL)
3127 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003129 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003130
3131 // If :silent! is used we will continue, make sure the
3132 // stack contents makes sense.
3133 clear_tv(tv);
3134 --ectx.ec_stack.ga_len;
3135 tv = STACK_TV_BOT(-1);
3136 clear_tv(tv);
3137 tv->v_type = VAR_NUMBER;
3138 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003139 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003141 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003142 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003143 // Clear the dict only after getting the item, to avoid
3144 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003145 tv = STACK_TV_BOT(-1);
3146 temp_tv = *tv;
3147 copy_tv(&di->di_tv, tv);
3148 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003149 }
3150 break;
3151
3152 // dict member with string key
3153 case ISN_STRINGMEMBER:
3154 {
3155 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003157 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158
3159 tv = STACK_TV_BOT(-1);
3160 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3161 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003162 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003163 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003164 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165 }
3166 dict = tv->vval.v_dict;
3167
3168 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3169 == NULL)
3170 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003171 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003173 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003175 // Clear the dict after getting the item, to avoid that it
3176 // make the item invalid.
3177 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003179 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 }
3181 break;
3182
3183 case ISN_NEGATENR:
3184 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003185 if (tv->v_type != VAR_NUMBER
3186#ifdef FEAT_FLOAT
3187 && tv->v_type != VAR_FLOAT
3188#endif
3189 )
3190 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003191 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003192 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003193 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003194 }
3195#ifdef FEAT_FLOAT
3196 if (tv->v_type == VAR_FLOAT)
3197 tv->vval.v_float = -tv->vval.v_float;
3198 else
3199#endif
3200 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 break;
3202
3203 case ISN_CHECKNR:
3204 {
3205 int error = FALSE;
3206
3207 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003208 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003210 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 (void)tv_get_number_chk(tv, &error);
3212 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003213 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 break;
3216
3217 case ISN_CHECKTYPE:
3218 {
3219 checktype_T *ct = &iptr->isn_arg.type;
3220
3221 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003222 SOURCING_LNUM = iptr->isn_lnum;
3223 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3224 goto on_error;
3225
3226 // number 0 is FALSE, number 1 is TRUE
3227 if (tv->v_type == VAR_NUMBER
3228 && ct->ct_type->tt_type == VAR_BOOL
3229 && (tv->vval.v_number == 0
3230 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003232 tv->v_type = VAR_BOOL;
3233 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003234 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 }
3236 }
3237 break;
3238
Bram Moolenaar9af78762020-06-16 11:34:42 +02003239 case ISN_CHECKLEN:
3240 {
3241 int min_len = iptr->isn_arg.checklen.cl_min_len;
3242 list_T *list = NULL;
3243
3244 tv = STACK_TV_BOT(-1);
3245 if (tv->v_type == VAR_LIST)
3246 list = tv->vval.v_list;
3247 if (list == NULL || list->lv_len < min_len
3248 || (list->lv_len > min_len
3249 && !iptr->isn_arg.checklen.cl_more_OK))
3250 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003251 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003252 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003253 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003254 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003255 }
3256 }
3257 break;
3258
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003259 case ISN_SETTYPE:
3260 {
3261 checktype_T *ct = &iptr->isn_arg.type;
3262
3263 tv = STACK_TV_BOT(-1);
3264 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3265 {
3266 free_type(tv->vval.v_dict->dv_type);
3267 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3268 }
3269 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3270 {
3271 free_type(tv->vval.v_list->lv_type);
3272 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3273 }
3274 }
3275 break;
3276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003278 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 {
3280 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003281 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282
3283 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003284 if (iptr->isn_type == ISN_2BOOL)
3285 {
3286 n = tv2bool(tv);
3287 if (iptr->isn_arg.number) // invert
3288 n = !n;
3289 }
3290 else
3291 {
3292 SOURCING_LNUM = iptr->isn_lnum;
3293 n = tv_get_bool_chk(tv, &error);
3294 if (error)
3295 goto on_error;
3296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 clear_tv(tv);
3298 tv->v_type = VAR_BOOL;
3299 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3300 }
3301 break;
3302
3303 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003304 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003305 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003306 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3307 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3308 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 break;
3310
Bram Moolenaar08597872020-12-10 19:43:40 +01003311 case ISN_RANGE:
3312 {
3313 exarg_T ea;
3314 char *errormsg;
3315
3316 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3317 goto failed;
3318 ++ectx.ec_stack.ga_len;
3319 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003320 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003321 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003322 ea.addr_type = ADDR_LINES;
3323 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003324 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003325 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003326 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003327 if (ea.addr_count == 0)
3328 tv->vval.v_number = curwin->w_cursor.lnum;
3329 else
3330 tv->vval.v_number = ea.line2;
3331 }
3332 break;
3333
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003334 case ISN_PUT:
3335 {
3336 int regname = iptr->isn_arg.put.put_regname;
3337 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3338 char_u *expr = NULL;
3339 int dir = FORWARD;
3340
3341 if (regname == '=')
3342 {
3343 tv = STACK_TV_BOT(-1);
3344 if (tv->v_type == VAR_STRING)
3345 expr = tv->vval.v_string;
3346 else
3347 {
3348 expr = typval_tostring(tv); // allocates value
3349 clear_tv(tv);
3350 }
3351 --ectx.ec_stack.ga_len;
3352 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003353 if (lnum < -2)
3354 {
3355 // line number was put on the stack by ISN_RANGE
3356 tv = STACK_TV_BOT(-1);
3357 curwin->w_cursor.lnum = tv->vval.v_number;
3358 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3359 dir = BACKWARD;
3360 --ectx.ec_stack.ga_len;
3361 }
3362 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003363 // :put! above cursor
3364 dir = BACKWARD;
3365 else if (lnum >= 0)
3366 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3367 check_cursor();
3368 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3369 vim_free(expr);
3370 }
3371 break;
3372
Bram Moolenaar02194d22020-10-24 23:08:38 +02003373 case ISN_CMDMOD:
3374 save_cmdmod = cmdmod;
3375 restore_cmdmod = TRUE;
3376 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3377 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003378 break;
3379
Bram Moolenaar02194d22020-10-24 23:08:38 +02003380 case ISN_CMDMOD_REV:
3381 // filter regprog is owned by the instruction, don't free it
3382 cmdmod.cmod_filter_regmatch.regprog = NULL;
3383 undo_cmdmod(&cmdmod);
3384 cmdmod = save_cmdmod;
3385 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003386 break;
3387
Bram Moolenaar792f7862020-11-23 08:31:18 +01003388 case ISN_UNPACK:
3389 {
3390 int count = iptr->isn_arg.unpack.unp_count;
3391 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3392 list_T *l;
3393 listitem_T *li;
3394 int i;
3395
3396 // Check there is a valid list to unpack.
3397 tv = STACK_TV_BOT(-1);
3398 if (tv->v_type != VAR_LIST)
3399 {
3400 SOURCING_LNUM = iptr->isn_lnum;
3401 emsg(_(e_for_argument_must_be_sequence_of_lists));
3402 goto on_error;
3403 }
3404 l = tv->vval.v_list;
3405 if (l == NULL
3406 || l->lv_len < (semicolon ? count - 1 : count))
3407 {
3408 SOURCING_LNUM = iptr->isn_lnum;
3409 emsg(_(e_list_value_does_not_have_enough_items));
3410 goto on_error;
3411 }
3412 else if (!semicolon && l->lv_len > count)
3413 {
3414 SOURCING_LNUM = iptr->isn_lnum;
3415 emsg(_(e_list_value_has_more_items_than_targets));
3416 goto on_error;
3417 }
3418
3419 CHECK_LIST_MATERIALIZE(l);
3420 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3421 goto failed;
3422 ectx.ec_stack.ga_len += count - 1;
3423
3424 // Variable after semicolon gets a list with the remaining
3425 // items.
3426 if (semicolon)
3427 {
3428 list_T *rem_list =
3429 list_alloc_with_items(l->lv_len - count + 1);
3430
3431 if (rem_list == NULL)
3432 goto failed;
3433 tv = STACK_TV_BOT(-count);
3434 tv->vval.v_list = rem_list;
3435 ++rem_list->lv_refcount;
3436 tv->v_lock = 0;
3437 li = l->lv_first;
3438 for (i = 0; i < count - 1; ++i)
3439 li = li->li_next;
3440 for (i = 0; li != NULL; ++i)
3441 {
3442 list_set_item(rem_list, i, &li->li_tv);
3443 li = li->li_next;
3444 }
3445 --count;
3446 }
3447
3448 // Produce the values in reverse order, first item last.
3449 li = l->lv_first;
3450 for (i = 0; i < count; ++i)
3451 {
3452 tv = STACK_TV_BOT(-i - 1);
3453 copy_tv(&li->li_tv, tv);
3454 li = li->li_next;
3455 }
3456
3457 list_unref(l);
3458 }
3459 break;
3460
Bram Moolenaar389df252020-07-09 21:20:47 +02003461 case ISN_SHUFFLE:
3462 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003463 typval_T tmp_tv;
3464 int item = iptr->isn_arg.shuffle.shfl_item;
3465 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003466
3467 tmp_tv = *STACK_TV_BOT(-item);
3468 for ( ; up > 0 && item > 1; --up)
3469 {
3470 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3471 --item;
3472 }
3473 *STACK_TV_BOT(-item) = tmp_tv;
3474 }
3475 break;
3476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 case ISN_DROP:
3478 --ectx.ec_stack.ga_len;
3479 clear_tv(STACK_TV_BOT(0));
3480 break;
3481 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003482 continue;
3483
Bram Moolenaard032f342020-07-18 18:13:02 +02003484func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003485 // Restore previous function. If the frame pointer is where we started
3486 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003487 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003488 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003489
Bram Moolenaard032f342020-07-18 18:13:02 +02003490 if (func_return(&ectx) == FAIL)
3491 // only fails when out of memory
3492 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003493 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003494
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003495on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003496 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003497 // If "emsg_silent" is set then ignore the error, unless it was set
3498 // when calling the function.
3499 if (did_emsg_cumul + did_emsg == did_emsg_before
3500 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003501 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003502on_fatal_error:
3503 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003504 // If we are not inside a try-catch started here, abort execution.
3505 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003506 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 }
3508
3509done:
3510 // function finished, get result from the stack.
3511 tv = STACK_TV_BOT(-1);
3512 *rettv = *tv;
3513 tv->v_type = VAR_UNKNOWN;
3514 ret = OK;
3515
3516failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003517 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003518 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003519 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003520
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003521 // Deal with any remaining closures, they may be in use somewhere.
3522 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003523 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003524 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003525 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3526 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003527
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003528 estack_pop();
3529 current_sctx = save_current_sctx;
3530
Bram Moolenaar352134b2020-10-17 22:04:08 +02003531 if (*msg_list != NULL && saved_msg_list != NULL)
3532 {
3533 msglist_T **plist = saved_msg_list;
3534
3535 // Append entries from the current msg_list (uncaught exceptions) to
3536 // the saved msg_list.
3537 while (*plist != NULL)
3538 plist = &(*plist)->next;
3539
3540 *plist = *msg_list;
3541 }
3542 msg_list = saved_msg_list;
3543
Bram Moolenaar02194d22020-10-24 23:08:38 +02003544 if (restore_cmdmod)
3545 {
3546 cmdmod.cmod_filter_regmatch.regprog = NULL;
3547 undo_cmdmod(&cmdmod);
3548 cmdmod = save_cmdmod;
3549 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003550 emsg_silent_def = save_emsg_silent_def;
3551 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003552
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003553failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003554 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3556 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003559 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003560
Bram Moolenaar0186e582021-01-10 18:33:11 +01003561 while (ectx.ec_outer != NULL)
3562 {
3563 outer_T *up = ectx.ec_outer->out_up_is_copy
3564 ? NULL : ectx.ec_outer->out_up;
3565
3566 vim_free(ectx.ec_outer);
3567 ectx.ec_outer = up;
3568 }
3569
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003570 // Not sure if this is necessary.
3571 suppress_errthrow = save_suppress_errthrow;
3572
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003573 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003574 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003575 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003576 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 return ret;
3578}
3579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003581 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003582 * We don't really need this at runtime, but we do have tests that require it,
3583 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003584 */
3585 void
3586ex_disassemble(exarg_T *eap)
3587{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003588 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003589 char_u *fname;
3590 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003591 dfunc_T *dfunc;
3592 isn_T *instr;
3593 int current;
3594 int line_idx = 0;
3595 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003596 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003598 if (STRNCMP(arg, "<lambda>", 8) == 0)
3599 {
3600 arg += 8;
3601 (void)getdigits(&arg);
3602 fname = vim_strnsave(eap->arg, arg - eap->arg);
3603 }
3604 else
3605 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003606 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003607 if (fname == NULL)
3608 {
3609 semsg(_(e_invarg2), eap->arg);
3610 return;
3611 }
3612
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003613 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003614 if (ufunc == NULL)
3615 {
3616 char_u *p = untrans_function_name(fname);
3617
3618 if (p != NULL)
3619 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003620 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003621 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003622 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623 if (ufunc == NULL)
3624 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003625 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003626 return;
3627 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003628 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003629 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3630 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003631 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003633 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 return;
3635 }
3636 if (ufunc->uf_name_exp != NULL)
3637 msg((char *)ufunc->uf_name_exp);
3638 else
3639 msg((char *)ufunc->uf_name);
3640
3641 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3642 instr = dfunc->df_instr;
3643 for (current = 0; current < dfunc->df_instr_count; ++current)
3644 {
3645 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003646 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003647
3648 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3649 {
3650 if (current > prev_current)
3651 {
3652 msg_puts("\n\n");
3653 prev_current = current;
3654 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003655 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3656 if (line != NULL)
3657 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003658 }
3659
3660 switch (iptr->isn_type)
3661 {
3662 case ISN_EXEC:
3663 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3664 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003665 case ISN_EXECCONCAT:
3666 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003667 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003668 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 case ISN_ECHO:
3670 {
3671 echo_T *echo = &iptr->isn_arg.echo;
3672
3673 smsg("%4d %s %d", current,
3674 echo->echo_with_white ? "ECHO" : "ECHON",
3675 echo->echo_count);
3676 }
3677 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003678 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003679 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003680 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003681 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003682 case ISN_ECHOMSG:
3683 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003684 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003685 break;
3686 case ISN_ECHOERR:
3687 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003688 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003689 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003691 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003692 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003693 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003694 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003695 + STACK_FRAME_SIZE));
3696 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003697 smsg("%4d LOAD $%lld", current,
3698 (varnumber_T)(iptr->isn_arg.number));
3699 }
3700 break;
3701 case ISN_LOADOUTER:
3702 {
3703 if (iptr->isn_arg.number < 0)
3704 smsg("%4d LOADOUTER level %d arg[%d]", current,
3705 iptr->isn_arg.outer.outer_depth,
3706 iptr->isn_arg.outer.outer_idx
3707 + STACK_FRAME_SIZE);
3708 else
3709 smsg("%4d LOADOUTER level %d $%d", current,
3710 iptr->isn_arg.outer.outer_depth,
3711 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713 break;
3714 case ISN_LOADV:
3715 smsg("%4d LOADV v:%s", current,
3716 get_vim_var_name(iptr->isn_arg.number));
3717 break;
3718 case ISN_LOADSCRIPT:
3719 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003720 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3721 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003723 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003725 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3726 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003727 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003728 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003729 }
3730 break;
3731 case ISN_LOADS:
3732 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003733 scriptitem_T *si = SCRIPT_ITEM(
3734 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735
3736 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003737 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 }
3739 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003740 case ISN_LOADAUTO:
3741 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3742 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003743 case ISN_LOADG:
3744 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3745 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003746 case ISN_LOADB:
3747 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3748 break;
3749 case ISN_LOADW:
3750 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3751 break;
3752 case ISN_LOADT:
3753 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3754 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003755 case ISN_LOADGDICT:
3756 smsg("%4d LOAD g:", current);
3757 break;
3758 case ISN_LOADBDICT:
3759 smsg("%4d LOAD b:", current);
3760 break;
3761 case ISN_LOADWDICT:
3762 smsg("%4d LOAD w:", current);
3763 break;
3764 case ISN_LOADTDICT:
3765 smsg("%4d LOAD t:", current);
3766 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 case ISN_LOADOPT:
3768 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3769 break;
3770 case ISN_LOADENV:
3771 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3772 break;
3773 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003774 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 break;
3776
3777 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003778 if (iptr->isn_arg.number < 0)
3779 smsg("%4d STORE arg[%lld]", current,
3780 iptr->isn_arg.number + STACK_FRAME_SIZE);
3781 else
3782 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3783 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003784 case ISN_STOREOUTER:
3785 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003786 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003787 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3788 iptr->isn_arg.outer.outer_depth,
3789 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003790 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003791 smsg("%4d STOREOUTER level %d $%d", current,
3792 iptr->isn_arg.outer.outer_depth,
3793 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003796 case ISN_STOREV:
3797 smsg("%4d STOREV v:%s", current,
3798 get_vim_var_name(iptr->isn_arg.number));
3799 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003800 case ISN_STOREAUTO:
3801 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3802 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003803 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003804 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3805 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003806 case ISN_STOREB:
3807 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3808 break;
3809 case ISN_STOREW:
3810 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3811 break;
3812 case ISN_STORET:
3813 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3814 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003815 case ISN_STORES:
3816 {
3817 scriptitem_T *si = SCRIPT_ITEM(
3818 iptr->isn_arg.loadstore.ls_sid);
3819
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003820 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003821 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003822 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 break;
3824 case ISN_STORESCRIPT:
3825 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003826 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3827 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003828 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003829 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003830
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003831 smsg("%4d STORESCRIPT %s-%d in %s", current,
3832 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003833 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003834 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003835 }
3836 break;
3837 case ISN_STOREOPT:
3838 smsg("%4d STOREOPT &%s", current,
3839 iptr->isn_arg.storeopt.so_name);
3840 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003841 case ISN_STOREENV:
3842 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3843 break;
3844 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003845 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003846 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 case ISN_STORENR:
3848 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003849 iptr->isn_arg.storenr.stnr_val,
3850 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003851 break;
3852
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003853 case ISN_STOREINDEX:
3854 switch (iptr->isn_arg.vartype)
3855 {
3856 case VAR_LIST:
3857 smsg("%4d STORELIST", current);
3858 break;
3859 case VAR_DICT:
3860 smsg("%4d STOREDICT", current);
3861 break;
3862 case VAR_ANY:
3863 smsg("%4d STOREINDEX", current);
3864 break;
3865 default: break;
3866 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003867 break;
3868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 // constants
3870 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003871 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003872 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 break;
3874 case ISN_PUSHBOOL:
3875 case ISN_PUSHSPEC:
3876 smsg("%4d PUSH %s", current,
3877 get_var_special_name(iptr->isn_arg.number));
3878 break;
3879 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003880#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003882#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003883 break;
3884 case ISN_PUSHS:
3885 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3886 break;
3887 case ISN_PUSHBLOB:
3888 {
3889 char_u *r;
3890 char_u numbuf[NUMBUFLEN];
3891 char_u *tofree;
3892
3893 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003894 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003895 vim_free(tofree);
3896 }
3897 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003898 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003899 {
3900 char *name = (char *)iptr->isn_arg.string;
3901
3902 smsg("%4d PUSHFUNC \"%s\"", current,
3903 name == NULL ? "[none]" : name);
3904 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003905 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003906 case ISN_PUSHCHANNEL:
3907#ifdef FEAT_JOB_CHANNEL
3908 {
3909 channel_T *channel = iptr->isn_arg.channel;
3910
3911 smsg("%4d PUSHCHANNEL %d", current,
3912 channel == NULL ? 0 : channel->ch_id);
3913 }
3914#endif
3915 break;
3916 case ISN_PUSHJOB:
3917#ifdef FEAT_JOB_CHANNEL
3918 {
3919 typval_T tv;
3920 char_u *name;
3921
3922 tv.v_type = VAR_JOB;
3923 tv.vval.v_job = iptr->isn_arg.job;
3924 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003925 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003926 }
3927#endif
3928 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 case ISN_PUSHEXC:
3930 smsg("%4d PUSH v:exception", current);
3931 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003932 case ISN_UNLET:
3933 smsg("%4d UNLET%s %s", current,
3934 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3935 iptr->isn_arg.unlet.ul_name);
3936 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003937 case ISN_UNLETENV:
3938 smsg("%4d UNLETENV%s $%s", current,
3939 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3940 iptr->isn_arg.unlet.ul_name);
3941 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003942 case ISN_UNLETINDEX:
3943 smsg("%4d UNLETINDEX", current);
3944 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003945 case ISN_LOCKCONST:
3946 smsg("%4d LOCKCONST", current);
3947 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003949 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003950 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951 break;
3952 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003953 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003954 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003955 break;
3956
3957 // function call
3958 case ISN_BCALL:
3959 {
3960 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3961
3962 smsg("%4d BCALL %s(argc %d)", current,
3963 internal_func_name(cbfunc->cbf_idx),
3964 cbfunc->cbf_argcount);
3965 }
3966 break;
3967 case ISN_DCALL:
3968 {
3969 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3970 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3971 + cdfunc->cdf_idx;
3972
3973 smsg("%4d DCALL %s(argc %d)", current,
3974 df->df_ufunc->uf_name_exp != NULL
3975 ? df->df_ufunc->uf_name_exp
3976 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3977 }
3978 break;
3979 case ISN_UCALL:
3980 {
3981 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3982
3983 smsg("%4d UCALL %s(argc %d)", current,
3984 cufunc->cuf_name, cufunc->cuf_argcount);
3985 }
3986 break;
3987 case ISN_PCALL:
3988 {
3989 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3990
3991 smsg("%4d PCALL%s (argc %d)", current,
3992 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3993 }
3994 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003995 case ISN_PCALL_END:
3996 smsg("%4d PCALL end", current);
3997 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 case ISN_RETURN:
3999 smsg("%4d RETURN", current);
4000 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004001 case ISN_RETURN_ZERO:
4002 smsg("%4d RETURN 0", current);
4003 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 case ISN_FUNCREF:
4005 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004006 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004008 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004010 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011 }
4012 break;
4013
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004014 case ISN_NEWFUNC:
4015 {
4016 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4017
4018 smsg("%4d NEWFUNC %s %s", current,
4019 newfunc->nf_lambda, newfunc->nf_global);
4020 }
4021 break;
4022
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004023 case ISN_DEF:
4024 {
4025 char_u *name = iptr->isn_arg.string;
4026
4027 smsg("%4d DEF %s", current,
4028 name == NULL ? (char_u *)"" : name);
4029 }
4030 break;
4031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 case ISN_JUMP:
4033 {
4034 char *when = "?";
4035
4036 switch (iptr->isn_arg.jump.jump_when)
4037 {
4038 case JUMP_ALWAYS:
4039 when = "JUMP";
4040 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 case JUMP_AND_KEEP_IF_TRUE:
4042 when = "JUMP_AND_KEEP_IF_TRUE";
4043 break;
4044 case JUMP_IF_FALSE:
4045 when = "JUMP_IF_FALSE";
4046 break;
4047 case JUMP_AND_KEEP_IF_FALSE:
4048 when = "JUMP_AND_KEEP_IF_FALSE";
4049 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004050 case JUMP_IF_COND_FALSE:
4051 when = "JUMP_IF_COND_FALSE";
4052 break;
4053 case JUMP_IF_COND_TRUE:
4054 when = "JUMP_IF_COND_TRUE";
4055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004057 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 iptr->isn_arg.jump.jump_where);
4059 }
4060 break;
4061
4062 case ISN_FOR:
4063 {
4064 forloop_T *forloop = &iptr->isn_arg.forloop;
4065
4066 smsg("%4d FOR $%d -> %d", current,
4067 forloop->for_idx, forloop->for_end);
4068 }
4069 break;
4070
4071 case ISN_TRY:
4072 {
4073 try_T *try = &iptr->isn_arg.try;
4074
4075 smsg("%4d TRY catch -> %d, finally -> %d", current,
4076 try->try_catch, try->try_finally);
4077 }
4078 break;
4079 case ISN_CATCH:
4080 // TODO
4081 smsg("%4d CATCH", current);
4082 break;
4083 case ISN_ENDTRY:
4084 smsg("%4d ENDTRY", current);
4085 break;
4086 case ISN_THROW:
4087 smsg("%4d THROW", current);
4088 break;
4089
4090 // expression operations on number
4091 case ISN_OPNR:
4092 case ISN_OPFLOAT:
4093 case ISN_OPANY:
4094 {
4095 char *what;
4096 char *ins;
4097
4098 switch (iptr->isn_arg.op.op_type)
4099 {
4100 case EXPR_MULT: what = "*"; break;
4101 case EXPR_DIV: what = "/"; break;
4102 case EXPR_REM: what = "%"; break;
4103 case EXPR_SUB: what = "-"; break;
4104 case EXPR_ADD: what = "+"; break;
4105 default: what = "???"; break;
4106 }
4107 switch (iptr->isn_type)
4108 {
4109 case ISN_OPNR: ins = "OPNR"; break;
4110 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4111 case ISN_OPANY: ins = "OPANY"; break;
4112 default: ins = "???"; break;
4113 }
4114 smsg("%4d %s %s", current, ins, what);
4115 }
4116 break;
4117
4118 case ISN_COMPAREBOOL:
4119 case ISN_COMPARESPECIAL:
4120 case ISN_COMPARENR:
4121 case ISN_COMPAREFLOAT:
4122 case ISN_COMPARESTRING:
4123 case ISN_COMPAREBLOB:
4124 case ISN_COMPARELIST:
4125 case ISN_COMPAREDICT:
4126 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004127 case ISN_COMPAREANY:
4128 {
4129 char *p;
4130 char buf[10];
4131 char *type;
4132
4133 switch (iptr->isn_arg.op.op_type)
4134 {
4135 case EXPR_EQUAL: p = "=="; break;
4136 case EXPR_NEQUAL: p = "!="; break;
4137 case EXPR_GREATER: p = ">"; break;
4138 case EXPR_GEQUAL: p = ">="; break;
4139 case EXPR_SMALLER: p = "<"; break;
4140 case EXPR_SEQUAL: p = "<="; break;
4141 case EXPR_MATCH: p = "=~"; break;
4142 case EXPR_IS: p = "is"; break;
4143 case EXPR_ISNOT: p = "isnot"; break;
4144 case EXPR_NOMATCH: p = "!~"; break;
4145 default: p = "???"; break;
4146 }
4147 STRCPY(buf, p);
4148 if (iptr->isn_arg.op.op_ic == TRUE)
4149 strcat(buf, "?");
4150 switch(iptr->isn_type)
4151 {
4152 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4153 case ISN_COMPARESPECIAL:
4154 type = "COMPARESPECIAL"; break;
4155 case ISN_COMPARENR: type = "COMPARENR"; break;
4156 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4157 case ISN_COMPARESTRING:
4158 type = "COMPARESTRING"; break;
4159 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4160 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4161 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4162 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004163 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4164 default: type = "???"; break;
4165 }
4166
4167 smsg("%4d %s %s", current, type, buf);
4168 }
4169 break;
4170
4171 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4172 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4173
4174 // expression operations
4175 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004176 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004177 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004178 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004179 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004180 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004181 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004182 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4183 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004184 case ISN_SLICE: smsg("%4d SLICE %lld",
4185 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004186 case ISN_GETITEM: smsg("%4d ITEM %lld",
4187 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004188 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4189 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004190 iptr->isn_arg.string); break;
4191 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4192
4193 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004194 case ISN_CHECKTYPE:
4195 {
4196 char *tofree;
4197
4198 smsg("%4d CHECKTYPE %s stack[%d]", current,
4199 type_name(iptr->isn_arg.type.ct_type, &tofree),
4200 iptr->isn_arg.type.ct_off);
4201 vim_free(tofree);
4202 break;
4203 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004204 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4205 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4206 iptr->isn_arg.checklen.cl_min_len);
4207 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004208 case ISN_SETTYPE:
4209 {
4210 char *tofree;
4211
4212 smsg("%4d SETTYPE %s", current,
4213 type_name(iptr->isn_arg.type.ct_type, &tofree));
4214 vim_free(tofree);
4215 break;
4216 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004217 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004218 case ISN_2BOOL: if (iptr->isn_arg.number)
4219 smsg("%4d INVERT (!val)", current);
4220 else
4221 smsg("%4d 2BOOL (!!val)", current);
4222 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004223 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004224 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004225 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004226 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004227 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004228 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004229 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4230 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004231 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004232 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4233 smsg("%4d PUT %c above range",
4234 current, iptr->isn_arg.put.put_regname);
4235 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4236 smsg("%4d PUT %c range",
4237 current, iptr->isn_arg.put.put_regname);
4238 else
4239 smsg("%4d PUT %c %ld", current,
4240 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004241 (long)iptr->isn_arg.put.put_lnum);
4242 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004243
Bram Moolenaar02194d22020-10-24 23:08:38 +02004244 // TODO: summarize modifiers
4245 case ISN_CMDMOD:
4246 {
4247 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004248 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004249 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4250
4251 buf = alloc(len + 1);
4252 if (buf != NULL)
4253 {
4254 (void)produce_cmdmods(
4255 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4256 smsg("%4d CMDMOD %s", current, buf);
4257 vim_free(buf);
4258 }
4259 break;
4260 }
4261 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004262
Bram Moolenaar792f7862020-11-23 08:31:18 +01004263 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4264 iptr->isn_arg.unpack.unp_count,
4265 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4266 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004267 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4268 iptr->isn_arg.shuffle.shfl_item,
4269 iptr->isn_arg.shuffle.shfl_up);
4270 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004271 case ISN_DROP: smsg("%4d DROP", current); break;
4272 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004273
4274 out_flush(); // output one line at a time
4275 ui_breakcheck();
4276 if (got_int)
4277 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279}
4280
4281/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004282 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 * list, etc. Mostly like what JavaScript does, except that empty list and
4284 * empty dictionary are FALSE.
4285 */
4286 int
4287tv2bool(typval_T *tv)
4288{
4289 switch (tv->v_type)
4290 {
4291 case VAR_NUMBER:
4292 return tv->vval.v_number != 0;
4293 case VAR_FLOAT:
4294#ifdef FEAT_FLOAT
4295 return tv->vval.v_float != 0.0;
4296#else
4297 break;
4298#endif
4299 case VAR_PARTIAL:
4300 return tv->vval.v_partial != NULL;
4301 case VAR_FUNC:
4302 case VAR_STRING:
4303 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4304 case VAR_LIST:
4305 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4306 case VAR_DICT:
4307 return tv->vval.v_dict != NULL
4308 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4309 case VAR_BOOL:
4310 case VAR_SPECIAL:
4311 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4312 case VAR_JOB:
4313#ifdef FEAT_JOB_CHANNEL
4314 return tv->vval.v_job != NULL;
4315#else
4316 break;
4317#endif
4318 case VAR_CHANNEL:
4319#ifdef FEAT_JOB_CHANNEL
4320 return tv->vval.v_channel != NULL;
4321#else
4322 break;
4323#endif
4324 case VAR_BLOB:
4325 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4326 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004327 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 case VAR_VOID:
4329 break;
4330 }
4331 return FALSE;
4332}
4333
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004334 void
4335emsg_using_string_as(typval_T *tv, int as_number)
4336{
4337 semsg(_(as_number ? e_using_string_as_number_str
4338 : e_using_string_as_bool_str),
4339 tv->vval.v_string == NULL
4340 ? (char_u *)"" : tv->vval.v_string);
4341}
4342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343/*
4344 * If "tv" is a string give an error and return FAIL.
4345 */
4346 int
4347check_not_string(typval_T *tv)
4348{
4349 if (tv->v_type == VAR_STRING)
4350 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004351 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004352 clear_tv(tv);
4353 return FAIL;
4354 }
4355 return OK;
4356}
4357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358
4359#endif // FEAT_EVAL