blob: e4948859f93a10b4bdff4adf7fedadfd11186f89 [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 Moolenaar0d3de8c2021-01-22 20:46:27 +0100266 if (pt != NULL || ufunc->uf_partial != NULL
267 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100268 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100269 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
270
271 if (outer == NULL)
272 return FAIL;
273 if (pt != NULL)
274 {
275 *outer = pt->pt_outer;
276 outer->out_up_is_copy = TRUE;
277 }
278 else if (ufunc->uf_partial != NULL)
279 {
280 *outer = ufunc->uf_partial->pt_outer;
281 outer->out_up_is_copy = TRUE;
282 }
283 else
284 {
285 outer->out_stack = &ectx->ec_stack;
286 outer->out_frame_idx = ectx->ec_frame_idx;
287 outer->out_up = ectx->ec_outer;
288 }
289 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100290 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100291 else
292 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100294 // Set execution state to the start of the called function.
295 ectx->ec_dfunc_idx = cdf_idx;
296 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100297 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200298 if (entry != NULL)
299 {
300 // Set the script context to the script where the function was defined.
301 // TODO: save more than the SID?
302 entry->es_save_sid = current_sctx.sc_sid;
303 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
304 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100305
306 // Decide where to start execution, handles optional arguments.
307 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100308
309 return OK;
310}
311
312// Get pointer to item in the stack.
313#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
314
315/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200316 * Used when returning from a function: Check if any closure is still
317 * referenced. If so then move the arguments and variables to a separate piece
318 * of stack to be used when the closure is called.
319 * When "free_arguments" is TRUE the arguments are to be freed.
320 * Returns FAIL when out of memory.
321 */
322 static int
323handle_closure_in_use(ectx_T *ectx, int free_arguments)
324{
325 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
326 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200327 int argcount;
328 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200329 int idx;
330 typval_T *tv;
331 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 garray_T *gap = &ectx->ec_funcrefs;
333 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200334
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200335 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 return OK; // function was freed
337 if (dfunc->df_has_closure == 0)
338 return OK; // no closures
339 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
340 closure_count = tv->vval.v_number;
341 if (closure_count == 0)
342 return OK; // no funcrefs created
343
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200344 argcount = ufunc_argcount(dfunc->df_ufunc);
345 top = ectx->ec_frame_idx - argcount;
346
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200347 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200348 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200349 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200350 partial_T *pt;
351 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200352
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200353 if (off < 0)
354 continue; // count is off or already done
355 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200356 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200357 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200358 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200359 int i;
360
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200361 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200362 // unreferenced on return.
363 for (i = 0; i < dfunc->df_varcount; ++i)
364 {
365 typval_T *stv = STACK_TV(ectx->ec_frame_idx
366 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200368 --refcount;
369 }
370 if (refcount > 1)
371 {
372 closure_in_use = TRUE;
373 break;
374 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200375 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200376 }
377
378 if (closure_in_use)
379 {
380 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
381 typval_T *stack;
382
383 // A closure is using the arguments and/or local variables.
384 // Move them to the called function.
385 if (funcstack == NULL)
386 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200387 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
388 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
390 funcstack->fs_ga.ga_data = stack;
391 if (stack == NULL)
392 {
393 vim_free(funcstack);
394 return FAIL;
395 }
396
397 // Move or copy the arguments.
398 for (idx = 0; idx < argcount; ++idx)
399 {
400 tv = STACK_TV(top + idx);
401 if (free_arguments)
402 {
403 *(stack + idx) = *tv;
404 tv->v_type = VAR_UNKNOWN;
405 }
406 else
407 copy_tv(tv, stack + idx);
408 }
409 // Move the local variables.
410 for (idx = 0; idx < dfunc->df_varcount; ++idx)
411 {
412 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200413
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200414 // A partial created for a local function, that is also used as a
415 // local variable, has a reference count for the variable, thus
416 // will never go down to zero. When all these refcounts are one
417 // then the funcstack is unused. We need to count how many we have
418 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200419 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
420 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200421 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200422
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200423 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200424 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
425 gap->ga_len - closure_count + i])
426 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200427 }
428
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200429 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200430 tv->v_type = VAR_UNKNOWN;
431 }
432
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200433 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200435 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
436 - closure_count + idx];
437 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200438 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200439 ++funcstack->fs_refcount;
440 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100441 pt->pt_outer.out_stack = &funcstack->fs_ga;
442 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
443 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200444 }
445 }
446 }
447
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200448 for (idx = 0; idx < closure_count; ++idx)
449 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
450 - closure_count + idx]);
451 gap->ga_len -= closure_count;
452 if (gap->ga_len == 0)
453 ga_clear(gap);
454
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200455 return OK;
456}
457
458/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200459 * Called when a partial is freed or its reference count goes down to one. The
460 * funcstack may be the only reference to the partials in the local variables.
461 * Go over all of them, the funcref and can be freed if all partials
462 * referencing the funcstack have a reference count of one.
463 */
464 void
465funcstack_check_refcount(funcstack_T *funcstack)
466{
467 int i;
468 garray_T *gap = &funcstack->fs_ga;
469 int done = 0;
470
471 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
472 return;
473 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
474 {
475 typval_T *tv = ((typval_T *)gap->ga_data) + i;
476
477 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
478 && tv->vval.v_partial->pt_funcstack == funcstack
479 && tv->vval.v_partial->pt_refcount == 1)
480 ++done;
481 }
482 if (done == funcstack->fs_min_refcount)
483 {
484 typval_T *stack = gap->ga_data;
485
486 // All partials referencing the funcstack have a reference count of
487 // one, thus the funcstack is no longer of use.
488 for (i = 0; i < gap->ga_len; ++i)
489 clear_tv(stack + i);
490 vim_free(stack);
491 vim_free(funcstack);
492 }
493}
494
495/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 * Return from the current function.
497 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200498 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499func_return(ectx_T *ectx)
500{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100502 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200503 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
504 + ectx->ec_dfunc_idx;
505 int argcount = ufunc_argcount(dfunc->df_ufunc);
506 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200507 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508
509 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200510 entry = estack_pop();
511 if (entry != NULL)
512 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200514 if (handle_closure_in_use(ectx, TRUE) == FAIL)
515 return FAIL;
516
517 // Clear the arguments.
518 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
519 clear_tv(STACK_TV(idx));
520
521 // Clear local variables and temp values, but not the return value.
522 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100523 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100525
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100526 // The return value should be on top of the stack. However, when aborting
527 // it may not be there and ec_frame_idx is the top of the stack.
528 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100529 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100530 ret_idx = 0;
531
Bram Moolenaar0186e582021-01-10 18:33:11 +0100532 vim_free(ectx->ec_outer);
533
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100534 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100535 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
536 + STACK_FRAME_FUNC_OFF)->vval.v_number;
537 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
538 + STACK_FRAME_IIDX_OFF)->vval.v_number;
539 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
540 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200541 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100542 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
543 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
545 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100546
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100547 if (ret_idx > 0)
548 {
549 // Reset the stack to the position before the call, with a spot for the
550 // return value, moved there from above the frame.
551 ectx->ec_stack.ga_len = top + 1;
552 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
553 }
554 else
555 // Reset the stack to the position before the call.
556 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200557
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100558 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200559 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560}
561
562#undef STACK_TV
563
564/*
565 * Prepare arguments and rettv for calling a builtin or user function.
566 */
567 static int
568call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
569{
570 int idx;
571 typval_T *tv;
572
573 // Move arguments from bottom of the stack to argvars[] and add terminator.
574 for (idx = 0; idx < argcount; ++idx)
575 argvars[idx] = *STACK_TV_BOT(idx - argcount);
576 argvars[argcount].v_type = VAR_UNKNOWN;
577
578 // Result replaces the arguments on the stack.
579 if (argcount > 0)
580 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200581 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 return FAIL;
583 else
584 ++ectx->ec_stack.ga_len;
585
586 // Default return value is zero.
587 tv = STACK_TV_BOT(-1);
588 tv->v_type = VAR_NUMBER;
589 tv->vval.v_number = 0;
590
591 return OK;
592}
593
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200594// Ugly global to avoid passing the execution context around through many
595// layers.
596static ectx_T *current_ectx = NULL;
597
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598/*
599 * Call a builtin function by index.
600 */
601 static int
602call_bfunc(int func_idx, int argcount, ectx_T *ectx)
603{
604 typval_T argvars[MAX_FUNC_ARGS];
605 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100606 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200607 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608
609 if (call_prepare(argcount, argvars, ectx) == FAIL)
610 return FAIL;
611
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200612 // Call the builtin function. Set "current_ectx" so that when it
613 // recursively invokes call_def_function() a closure context can be set.
614 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200616 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617
618 // Clear the arguments.
619 for (idx = 0; idx < argcount; ++idx)
620 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200621
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100622 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return OK;
625}
626
627/*
628 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100629 * If the function is compiled this will add a stack frame and set the
630 * instruction pointer at the start of the function.
631 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100632 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100633 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 */
635 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100636call_ufunc(
637 ufunc_T *ufunc,
638 partial_T *pt,
639 int argcount,
640 ectx_T *ectx,
641 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642{
643 typval_T argvars[MAX_FUNC_ARGS];
644 funcexe_T funcexe;
645 int error;
646 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100647 int did_emsg_before = did_emsg;
Bram Moolenaarb2049902021-01-24 12:53:53 +0100648 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649
Bram Moolenaarb2049902021-01-24 12:53:53 +0100650 if (func_needs_compiling(ufunc, profiling)
651 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200652 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200653 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100654 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100655 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100656 if (error != FCERR_UNKNOWN)
657 {
658 if (error == FCERR_TOOMANY)
659 semsg(_(e_toomanyarg), ufunc->uf_name);
660 else
661 semsg(_(e_toofewarg), ufunc->uf_name);
662 return FAIL;
663 }
664
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100665 // The function has been compiled, can call it quickly. For a function
666 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100667 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100668 if (iptr != NULL)
669 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100670 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100671 iptr->isn_type = ISN_DCALL;
672 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
673 iptr->isn_arg.dfunc.cdf_argcount = argcount;
674 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100675 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
678 if (call_prepare(argcount, argvars, ectx) == FAIL)
679 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200680 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 funcexe.evaluate = TRUE;
682
683 // Call the user function. Result goes in last position on the stack.
684 // TODO: add selfdict if there is one
685 error = call_user_func_check(ufunc, argcount, argvars,
686 STACK_TV_BOT(-1), &funcexe, NULL);
687
688 // Clear the arguments.
689 for (idx = 0; idx < argcount; ++idx)
690 clear_tv(&argvars[idx]);
691
692 if (error != FCERR_NONE)
693 {
694 user_func_error(error, ufunc->uf_name);
695 return FAIL;
696 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100697 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200698 // Error other than from calling the function itself.
699 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 return OK;
701}
702
703/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200704 * Return TRUE if an error was given or CTRL-C was pressed.
705 */
706 static int
707vim9_aborting(int prev_called_emsg)
708{
709 return called_emsg > prev_called_emsg || got_int || did_throw;
710}
711
712/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 * Execute a function by "name".
714 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100715 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 * Returns FAIL if not found without an error message.
717 */
718 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100719call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720{
721 ufunc_T *ufunc;
722
723 if (builtin_function(name, -1))
724 {
725 int func_idx = find_internal_func(name);
726
727 if (func_idx < 0)
728 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200729 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 return FAIL;
731 return call_bfunc(func_idx, argcount, ectx);
732 }
733
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200734 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200735
736 if (ufunc == NULL)
737 {
738 int called_emsg_before = called_emsg;
739
740 if (script_autoload(name, TRUE))
741 // loaded a package, search for the function again
742 ufunc = find_func(name, FALSE, NULL);
743 if (vim9_aborting(called_emsg_before))
744 return FAIL; // bail out if loading the script caused an error
745 }
746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100748 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749
750 return FAIL;
751}
752
753 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200754call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200756 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200757 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100759 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760
761 if (tv->v_type == VAR_PARTIAL)
762 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200763 partial_T *pt = tv->vval.v_partial;
764 int i;
765
766 if (pt->pt_argc > 0)
767 {
768 // Make space for arguments from the partial, shift the "argcount"
769 // arguments up.
770 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
771 return FAIL;
772 for (i = 1; i <= argcount; ++i)
773 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
774 ectx->ec_stack.ga_len += pt->pt_argc;
775 argcount += pt->pt_argc;
776
777 // copy the arguments from the partial onto the stack
778 for (i = 0; i < pt->pt_argc; ++i)
779 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
782 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100783 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785 name = pt->pt_name;
786 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200787 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200789 if (name != NULL)
790 {
791 char_u fname_buf[FLEN_FIXED + 1];
792 char_u *tofree = NULL;
793 int error = FCERR_NONE;
794 char_u *fname;
795
796 // May need to translate <SNR>123_ to K_SNR.
797 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
798 if (error != FCERR_NONE)
799 res = FAIL;
800 else
801 res = call_by_name(fname, argcount, ectx, NULL);
802 vim_free(tofree);
803 }
804
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100805 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 {
807 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200808 semsg(_(e_unknownfunc),
809 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 return FAIL;
811 }
812 return OK;
813}
814
815/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200816 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
817 * TRUE.
818 */
819 static int
820error_if_locked(int lock, char *error)
821{
822 if (lock & (VAR_LOCKED | VAR_FIXED))
823 {
824 emsg(_(error));
825 return TRUE;
826 }
827 return FALSE;
828}
829
830/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100831 * Store "tv" in variable "name".
832 * This is for s: and g: variables.
833 */
834 static void
835store_var(char_u *name, typval_T *tv)
836{
837 funccal_entry_T entry;
838
839 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100840 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100841 restore_funccal();
842}
843
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100844/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100845 * Convert "tv" to a string.
846 * Return FAIL if not allowed.
847 */
848 static int
849do_2string(typval_T *tv, int is_2string_any)
850{
851 if (tv->v_type != VAR_STRING)
852 {
853 char_u *str;
854
855 if (is_2string_any)
856 {
857 switch (tv->v_type)
858 {
859 case VAR_SPECIAL:
860 case VAR_BOOL:
861 case VAR_NUMBER:
862 case VAR_FLOAT:
863 case VAR_BLOB: break;
864 default: to_string_error(tv->v_type);
865 return FAIL;
866 }
867 }
868 str = typval_tostring(tv);
869 clear_tv(tv);
870 tv->v_type = VAR_STRING;
871 tv->vval.v_string = str;
872 }
873 return OK;
874}
875
876/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100877 * When the value of "sv" is a null list of dict, allocate it.
878 */
879 static void
880allocate_if_null(typval_T *tv)
881{
882 switch (tv->v_type)
883 {
884 case VAR_LIST:
885 if (tv->vval.v_list == NULL)
886 rettv_list_alloc(tv);
887 break;
888 case VAR_DICT:
889 if (tv->vval.v_dict == NULL)
890 rettv_dict_alloc(tv);
891 break;
892 default:
893 break;
894 }
895}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200896
Bram Moolenaare7525c52021-01-09 13:20:37 +0100897/*
898 * Return the character "str[index]" where "index" is the character index. If
899 * "index" is out of range NULL is returned.
900 */
901 char_u *
902char_from_string(char_u *str, varnumber_T index)
903{
904 size_t nbyte = 0;
905 varnumber_T nchar = index;
906 size_t slen;
907
908 if (str == NULL)
909 return NULL;
910 slen = STRLEN(str);
911
912 // do the same as for a list: a negative index counts from the end
913 if (index < 0)
914 {
915 int clen = 0;
916
917 for (nbyte = 0; nbyte < slen; ++clen)
918 nbyte += MB_CPTR2LEN(str + nbyte);
919 nchar = clen + index;
920 if (nchar < 0)
921 // unlike list: index out of range results in empty string
922 return NULL;
923 }
924
925 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
926 nbyte += MB_CPTR2LEN(str + nbyte);
927 if (nbyte >= slen)
928 return NULL;
929 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
930}
931
932/*
933 * Get the byte index for character index "idx" in string "str" with length
934 * "str_len".
935 * If going over the end return "str_len".
936 * If "idx" is negative count from the end, -1 is the last character.
937 * When going over the start return -1.
938 */
939 static long
940char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
941{
942 varnumber_T nchar = idx;
943 size_t nbyte = 0;
944
945 if (nchar >= 0)
946 {
947 while (nchar > 0 && nbyte < str_len)
948 {
949 nbyte += MB_CPTR2LEN(str + nbyte);
950 --nchar;
951 }
952 }
953 else
954 {
955 nbyte = str_len;
956 while (nchar < 0 && nbyte > 0)
957 {
958 --nbyte;
959 nbyte -= mb_head_off(str, str + nbyte);
960 ++nchar;
961 }
962 if (nchar < 0)
963 return -1;
964 }
965 return (long)nbyte;
966}
967
968/*
969 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100970 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100971 * Return NULL when the result is empty.
972 */
973 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100974string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100975{
976 long start_byte, end_byte;
977 size_t slen;
978
979 if (str == NULL)
980 return NULL;
981 slen = STRLEN(str);
982 start_byte = char_idx2byte(str, slen, first);
983 if (start_byte < 0)
984 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100985 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100986 end_byte = (long)slen;
987 else
988 {
989 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100990 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100991 // end index is inclusive
992 end_byte += MB_CPTR2LEN(str + end_byte);
993 }
994
995 if (start_byte >= (long)slen || end_byte <= start_byte)
996 return NULL;
997 return vim_strnsave(str + start_byte, end_byte - start_byte);
998}
999
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001000 static svar_T *
1001get_script_svar(scriptref_T *sref, ectx_T *ectx)
1002{
1003 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1004 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1005 + ectx->ec_dfunc_idx;
1006 svar_T *sv;
1007
1008 if (sref->sref_seq != si->sn_script_seq)
1009 {
1010 // The script was reloaded after the function was
1011 // compiled, the script_idx may not be valid.
1012 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1013 dfunc->df_ufunc->uf_name_exp);
1014 return NULL;
1015 }
1016 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1017 if (!equal_type(sv->sv_type, sref->sref_type))
1018 {
1019 emsg(_(e_script_variable_type_changed));
1020 return NULL;
1021 }
1022 return sv;
1023}
1024
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001025/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 * Execute a function by "name".
1027 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001028 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 */
1030 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001031call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032{
Bram Moolenaared677f52020-08-12 16:38:10 +02001033 int called_emsg_before = called_emsg;
1034 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035
Bram Moolenaared677f52020-08-12 16:38:10 +02001036 res = call_by_name(name, argcount, ectx, iptr);
1037 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001039 dictitem_T *v;
1040
1041 v = find_var(name, NULL, FALSE);
1042 if (v == NULL)
1043 {
1044 semsg(_(e_unknownfunc), name);
1045 return FAIL;
1046 }
1047 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1048 {
1049 semsg(_(e_unknownfunc), name);
1050 return FAIL;
1051 }
1052 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001054 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055}
1056
1057/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001058 * When a function reference is used, fill a partial with the information
1059 * needed, especially when it is used as a closure.
1060 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001061 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001062fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1063{
1064 pt->pt_func = ufunc;
1065 pt->pt_refcount = 1;
1066
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001067 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001068 {
1069 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1070 + ectx->ec_dfunc_idx;
1071
1072 // The closure needs to find arguments and local
1073 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001074 pt->pt_outer.out_stack = &ectx->ec_stack;
1075 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1076 pt->pt_outer.out_up = ectx->ec_outer;
1077 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001078
1079 // If this function returns and the closure is still
1080 // being used, we need to make a copy of the context
1081 // (arguments and local variables). Store a reference
1082 // to the partial so we can handle that.
1083 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1084 {
1085 vim_free(pt);
1086 return FAIL;
1087 }
1088 // Extra variable keeps the count of closures created
1089 // in the current function call.
1090 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1091 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1092
1093 ((partial_T **)ectx->ec_funcrefs.ga_data)
1094 [ectx->ec_funcrefs.ga_len] = pt;
1095 ++pt->pt_refcount;
1096 ++ectx->ec_funcrefs.ga_len;
1097 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001098 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001099 return OK;
1100}
1101
1102/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 * Call a "def" function from old Vim script.
1104 * Return OK or FAIL.
1105 */
1106 int
1107call_def_function(
1108 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001109 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001111 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 typval_T *rettv) // return value
1113{
1114 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001115 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001116 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 typval_T *tv;
1118 int idx;
1119 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001120 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001121 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001122 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001123 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001124 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001125 msglist_T **saved_msg_list = NULL;
1126 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001127 cmdmod_T save_cmdmod;
1128 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001129 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001130 int save_emsg_silent_def = emsg_silent_def;
1131 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001132 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001133 int orig_funcdepth;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001134 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135
1136// Get pointer to item in the stack.
1137#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1138
1139// Get pointer to item at the bottom of the stack, -1 is the bottom.
1140#undef STACK_TV_BOT
1141#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1142
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001143// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001144#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 +01001145
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001146 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaarb2049902021-01-24 12:53:53 +01001147 || (func_needs_compiling(ufunc, profiling)
1148 && compile_def_function(ufunc, FALSE, profiling, NULL)
1149 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001150 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001151 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001152 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001153 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001154 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001155 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001156
Bram Moolenaar09689a02020-05-09 22:50:08 +02001157 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001158 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001159 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1160 + ufunc->uf_dfunc_idx;
Bram Moolenaarb2049902021-01-24 12:53:53 +01001161 if ((profiling ? dfunc->df_instr_prof : dfunc->df_instr) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001162 {
1163 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001164 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001165 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001166 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001168 // If depth of calling is getting too high, don't execute the function.
1169 orig_funcdepth = funcdepth_get();
1170 if (funcdepth_increment() == FAIL)
1171 return FAIL;
1172
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001173 CLEAR_FIELD(ectx);
1174 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1175 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1176 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001177 {
1178 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001179 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001182 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001184 // Put arguments on the stack, but no more than what the function expects.
1185 // A lambda can be called with more arguments than it uses.
1186 for (idx = 0; idx < argc
1187 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1188 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001190 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001191 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1192 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001193 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 copy_tv(&argv[idx], STACK_TV_BOT(0));
1195 ++ectx.ec_stack.ga_len;
1196 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001197
1198 // Turn varargs into a list. Empty list if no args.
1199 if (ufunc->uf_va_name != NULL)
1200 {
1201 int vararg_count = argc - ufunc->uf_args.ga_len;
1202
1203 if (vararg_count < 0)
1204 vararg_count = 0;
1205 else
1206 argc -= vararg_count;
1207 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001208 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001209
1210 // Check the type of the list items.
1211 tv = STACK_TV_BOT(-1);
1212 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001213 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001214 && ufunc->uf_va_type->tt_member != &t_any
1215 && tv->vval.v_list != NULL)
1216 {
1217 type_T *expected = ufunc->uf_va_type->tt_member;
1218 listitem_T *li = tv->vval.v_list->lv_first;
1219
1220 for (idx = 0; idx < vararg_count; ++idx)
1221 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001222 if (check_typval_type(expected, &li->li_tv,
1223 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001224 goto failed_early;
1225 li = li->li_next;
1226 }
1227 }
1228
Bram Moolenaar23e03252020-04-12 22:22:31 +02001229 if (defcount > 0)
1230 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001231 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001232 --ectx.ec_stack.ga_len;
1233 }
1234
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001235 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001236 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001237 if (defcount > 0)
1238 for (idx = 0; idx < defcount; ++idx)
1239 {
1240 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1241 ++ectx.ec_stack.ga_len;
1242 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001243 if (ufunc->uf_va_name != NULL)
1244 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245
1246 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001247 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1248 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001250 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001251 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1252 + ufunc->uf_dfunc_idx;
1253 ufunc_T *base_ufunc = dfunc->df_ufunc;
1254
1255 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1256 // by copy_func().
1257 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001258 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001259 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1260 if (ectx.ec_outer == NULL)
1261 goto failed_early;
1262 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001263 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001264 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1265 {
1266 if (current_ectx->ec_outer != NULL)
1267 *ectx.ec_outer = *current_ectx->ec_outer;
1268 }
1269 else
1270 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001271 }
1272 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001273 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1274 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001275 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001276 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 // dummy frame entries
1279 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1280 {
1281 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1282 ++ectx.ec_stack.ga_len;
1283 }
1284
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001285 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001286 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001287 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1288 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001290 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001291 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001292 ectx.ec_stack.ga_len += dfunc->df_varcount;
1293 if (dfunc->df_has_closure)
1294 {
1295 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1296 STACK_TV_VAR(idx)->vval.v_number = 0;
1297 ++ectx.ec_stack.ga_len;
1298 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001299
Bram Moolenaarb2049902021-01-24 12:53:53 +01001300 ectx.ec_instr = profiling ? dfunc->df_instr_prof : dfunc->df_instr;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001301 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001302
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001303 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001304 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001305 estack_push_ufunc(ufunc, 1);
1306 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001307 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1308
Bram Moolenaar352134b2020-10-17 22:04:08 +02001309 // Use a specific location for storing error messages to be converted to an
1310 // exception.
1311 saved_msg_list = msg_list;
1312 msg_list = &private_msg_list;
1313
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001314 // Do turn errors into exceptions.
1315 suppress_errthrow = FALSE;
1316
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001317 // When ":silent!" was used before calling then we still abort the
1318 // function. If ":silent!" is used in the function then we don't.
1319 emsg_silent_def = emsg_silent;
1320 did_emsg_def = 0;
1321
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001322 // Decide where to start execution, handles optional arguments.
1323 init_instr_idx(ufunc, argc, &ectx);
1324
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 for (;;)
1326 {
1327 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001328
Bram Moolenaar270d0382020-05-15 21:42:53 +02001329 if (++breakcheck_count >= 100)
1330 {
1331 line_breakcheck();
1332 breakcheck_count = 0;
1333 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001334 if (got_int)
1335 {
1336 // Turn CTRL-C into an exception.
1337 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001338 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001339 goto failed;
1340 did_throw = TRUE;
1341 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaara26b9702020-04-18 19:53:28 +02001343 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1344 {
1345 // Turn an error message into an exception.
1346 did_emsg = FALSE;
1347 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1348 goto failed;
1349 did_throw = TRUE;
1350 *msg_list = NULL;
1351 }
1352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 if (did_throw && !ectx.ec_in_catch)
1354 {
1355 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001356 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357
1358 // An exception jumps to the first catch, finally, or returns from
1359 // the current function.
1360 if (trystack->ga_len > 0)
1361 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001362 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 {
1364 // jump to ":catch" or ":finally"
1365 ectx.ec_in_catch = TRUE;
1366 ectx.ec_iidx = trycmd->tcd_catch_idx;
1367 }
1368 else
1369 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001370 // Not inside try or need to return from current functions.
1371 // Push a dummy return value.
1372 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1373 goto failed;
1374 tv = STACK_TV_BOT(0);
1375 tv->v_type = VAR_NUMBER;
1376 tv->vval.v_number = 0;
1377 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001378 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001380 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001381 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001382 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1383 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 goto done;
1385 }
1386
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001387 if (func_return(&ectx) == FAIL)
1388 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 }
1390 continue;
1391 }
1392
1393 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1394 switch (iptr->isn_type)
1395 {
1396 // execute Ex command line
1397 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001398 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001399 source_cookie_T cookie;
1400
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001401 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001402 // Pass getsourceline to get an error for a missing ":end"
1403 // command.
1404 CLEAR_FIELD(cookie);
1405 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1406 if (do_cmdline(iptr->isn_arg.string,
1407 getsourceline, &cookie,
1408 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1409 == FAIL
1410 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001411 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 break;
1414
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001415 // execute Ex command from pieces on the stack
1416 case ISN_EXECCONCAT:
1417 {
1418 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001419 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001420 int pass;
1421 int i;
1422 char_u *cmd = NULL;
1423 char_u *str;
1424
1425 for (pass = 1; pass <= 2; ++pass)
1426 {
1427 for (i = 0; i < count; ++i)
1428 {
1429 tv = STACK_TV_BOT(i - count);
1430 str = tv->vval.v_string;
1431 if (str != NULL && *str != NUL)
1432 {
1433 if (pass == 2)
1434 STRCPY(cmd + len, str);
1435 len += STRLEN(str);
1436 }
1437 if (pass == 2)
1438 clear_tv(tv);
1439 }
1440 if (pass == 1)
1441 {
1442 cmd = alloc(len + 1);
1443 if (cmd == NULL)
1444 goto failed;
1445 len = 0;
1446 }
1447 }
1448
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001449 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001450 do_cmdline_cmd(cmd);
1451 vim_free(cmd);
1452 }
1453 break;
1454
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 // execute :echo {string} ...
1456 case ISN_ECHO:
1457 {
1458 int count = iptr->isn_arg.echo.echo_count;
1459 int atstart = TRUE;
1460 int needclr = TRUE;
1461
1462 for (idx = 0; idx < count; ++idx)
1463 {
1464 tv = STACK_TV_BOT(idx - count);
1465 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1466 &atstart, &needclr);
1467 clear_tv(tv);
1468 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001469 if (needclr)
1470 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 ectx.ec_stack.ga_len -= count;
1472 }
1473 break;
1474
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001475 // :execute {string} ...
1476 // :echomsg {string} ...
1477 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001478 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001479 case ISN_ECHOMSG:
1480 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001481 {
1482 int count = iptr->isn_arg.number;
1483 garray_T ga;
1484 char_u buf[NUMBUFLEN];
1485 char_u *p;
1486 int len;
1487 int failed = FALSE;
1488
1489 ga_init2(&ga, 1, 80);
1490 for (idx = 0; idx < count; ++idx)
1491 {
1492 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001493 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001494 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001495 if (tv->v_type == VAR_CHANNEL
1496 || tv->v_type == VAR_JOB)
1497 {
1498 SOURCING_LNUM = iptr->isn_lnum;
1499 emsg(_(e_inval_string));
1500 break;
1501 }
1502 else
1503 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001504 }
1505 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001506 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001507
1508 len = (int)STRLEN(p);
1509 if (ga_grow(&ga, len + 2) == FAIL)
1510 failed = TRUE;
1511 else
1512 {
1513 if (ga.ga_len > 0)
1514 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1515 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1516 ga.ga_len += len;
1517 }
1518 clear_tv(tv);
1519 }
1520 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001521 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001522 {
1523 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001524 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001525 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001526
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001527 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001528 {
1529 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001530 {
1531 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001532 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001533 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001534 {
1535 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001536 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001537 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001538 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001539 else
1540 {
1541 msg_sb_eol();
1542 if (iptr->isn_type == ISN_ECHOMSG)
1543 {
1544 msg_attr(ga.ga_data, echo_attr);
1545 out_flush();
1546 }
1547 else
1548 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001549 SOURCING_LNUM = iptr->isn_lnum;
1550 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001551 }
1552 }
1553 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001554 ga_clear(&ga);
1555 }
1556 break;
1557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 // load local variable or argument
1559 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001560 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 goto failed;
1562 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1563 ++ectx.ec_stack.ga_len;
1564 break;
1565
1566 // load v: variable
1567 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001568 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 goto failed;
1570 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1571 ++ectx.ec_stack.ga_len;
1572 break;
1573
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001574 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 case ISN_LOADSCRIPT:
1576 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001577 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 svar_T *sv;
1579
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001580 sv = get_script_svar(sref, &ectx);
1581 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001582 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001583 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001584 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 goto failed;
1586 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1587 ++ectx.ec_stack.ga_len;
1588 }
1589 break;
1590
1591 // load s: variable in old script
1592 case ISN_LOADS:
1593 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001594 hashtab_T *ht = &SCRIPT_VARS(
1595 iptr->isn_arg.loadstore.ls_sid);
1596 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 if (di == NULL)
1600 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001601 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001602 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001603 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 }
1605 else
1606 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001607 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 goto failed;
1609 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1610 ++ectx.ec_stack.ga_len;
1611 }
1612 }
1613 break;
1614
Bram Moolenaard3aac292020-04-19 14:32:17 +02001615 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001617 case ISN_LOADB:
1618 case ISN_LOADW:
1619 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001621 dictitem_T *di = NULL;
1622 hashtab_T *ht = NULL;
1623 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001624
Bram Moolenaard3aac292020-04-19 14:32:17 +02001625 switch (iptr->isn_type)
1626 {
1627 case ISN_LOADG:
1628 ht = get_globvar_ht();
1629 namespace = 'g';
1630 break;
1631 case ISN_LOADB:
1632 ht = &curbuf->b_vars->dv_hashtab;
1633 namespace = 'b';
1634 break;
1635 case ISN_LOADW:
1636 ht = &curwin->w_vars->dv_hashtab;
1637 namespace = 'w';
1638 break;
1639 case ISN_LOADT:
1640 ht = &curtab->tp_vars->dv_hashtab;
1641 namespace = 't';
1642 break;
1643 default: // Cannot reach here
1644 goto failed;
1645 }
1646 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 if (di == NULL)
1649 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001650 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001651 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001652 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001653 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 }
1655 else
1656 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001657 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001658 goto failed;
1659 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1660 ++ectx.ec_stack.ga_len;
1661 }
1662 }
1663 break;
1664
Bram Moolenaar03290b82020-12-19 16:30:44 +01001665 // load autoload variable
1666 case ISN_LOADAUTO:
1667 {
1668 char_u *name = iptr->isn_arg.string;
1669
1670 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1671 goto failed;
1672 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001673 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001674 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1675 goto on_error;
1676 ++ectx.ec_stack.ga_len;
1677 }
1678 break;
1679
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001680 // load g:/b:/w:/t: namespace
1681 case ISN_LOADGDICT:
1682 case ISN_LOADBDICT:
1683 case ISN_LOADWDICT:
1684 case ISN_LOADTDICT:
1685 {
1686 dict_T *d = NULL;
1687
1688 switch (iptr->isn_type)
1689 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001690 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1691 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1692 case ISN_LOADWDICT: d = curwin->w_vars; break;
1693 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001694 default: // Cannot reach here
1695 goto failed;
1696 }
1697 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1698 goto failed;
1699 tv = STACK_TV_BOT(0);
1700 tv->v_type = VAR_DICT;
1701 tv->v_lock = 0;
1702 tv->vval.v_dict = d;
1703 ++ectx.ec_stack.ga_len;
1704 }
1705 break;
1706
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 // load &option
1708 case ISN_LOADOPT:
1709 {
1710 typval_T optval;
1711 char_u *name = iptr->isn_arg.string;
1712
Bram Moolenaara8c17702020-04-01 21:17:24 +02001713 // This is not expected to fail, name is checked during
1714 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001715 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001717 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001718 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 *STACK_TV_BOT(0) = optval;
1720 ++ectx.ec_stack.ga_len;
1721 }
1722 break;
1723
1724 // load $ENV
1725 case ISN_LOADENV:
1726 {
1727 typval_T optval;
1728 char_u *name = iptr->isn_arg.string;
1729
Bram Moolenaar270d0382020-05-15 21:42:53 +02001730 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001732 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001733 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 *STACK_TV_BOT(0) = optval;
1735 ++ectx.ec_stack.ga_len;
1736 }
1737 break;
1738
1739 // load @register
1740 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001741 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 goto failed;
1743 tv = STACK_TV_BOT(0);
1744 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001745 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001746 // This may result in NULL, which should be equivalent to an
1747 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 tv->vval.v_string = get_reg_contents(
1749 iptr->isn_arg.number, GREG_EXPR_SRC);
1750 ++ectx.ec_stack.ga_len;
1751 break;
1752
1753 // store local variable
1754 case ISN_STORE:
1755 --ectx.ec_stack.ga_len;
1756 tv = STACK_TV_VAR(iptr->isn_arg.number);
1757 clear_tv(tv);
1758 *tv = *STACK_TV_BOT(0);
1759 break;
1760
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001761 // store s: variable in old script
1762 case ISN_STORES:
1763 {
1764 hashtab_T *ht = &SCRIPT_VARS(
1765 iptr->isn_arg.loadstore.ls_sid);
1766 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001767 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001768
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001769 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001770 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001771 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001772 else
1773 {
1774 clear_tv(&di->di_tv);
1775 di->di_tv = *STACK_TV_BOT(0);
1776 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001777 }
1778 break;
1779
1780 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 case ISN_STORESCRIPT:
1782 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1784 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001786 sv = get_script_svar(sref, &ectx);
1787 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001788 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 --ectx.ec_stack.ga_len;
1790 clear_tv(sv->sv_tv);
1791 *sv->sv_tv = *STACK_TV_BOT(0);
1792 }
1793 break;
1794
1795 // store option
1796 case ISN_STOREOPT:
1797 {
1798 long n = 0;
1799 char_u *s = NULL;
1800 char *msg;
1801
1802 --ectx.ec_stack.ga_len;
1803 tv = STACK_TV_BOT(0);
1804 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001805 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001807 if (s == NULL)
1808 s = (char_u *)"";
1809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001811 // must be VAR_NUMBER, CHECKTYPE makes sure
1812 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1814 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001815 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 if (msg != NULL)
1817 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001818 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001820 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 }
1823 break;
1824
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001825 // store $ENV
1826 case ISN_STOREENV:
1827 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001828 tv = STACK_TV_BOT(0);
1829 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1830 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001831 break;
1832
1833 // store @r
1834 case ISN_STOREREG:
1835 {
1836 int reg = iptr->isn_arg.number;
1837
1838 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001839 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001840 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001841 tv_get_string(tv), -1, FALSE);
1842 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001843 }
1844 break;
1845
1846 // store v: variable
1847 case ISN_STOREV:
1848 --ectx.ec_stack.ga_len;
1849 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1850 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001851 // should not happen, type is checked when compiling
1852 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001853 break;
1854
Bram Moolenaard3aac292020-04-19 14:32:17 +02001855 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001857 case ISN_STOREB:
1858 case ISN_STOREW:
1859 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001861 dictitem_T *di;
1862 hashtab_T *ht;
1863 char_u *name = iptr->isn_arg.string + 2;
1864
Bram Moolenaard3aac292020-04-19 14:32:17 +02001865 switch (iptr->isn_type)
1866 {
1867 case ISN_STOREG:
1868 ht = get_globvar_ht();
1869 break;
1870 case ISN_STOREB:
1871 ht = &curbuf->b_vars->dv_hashtab;
1872 break;
1873 case ISN_STOREW:
1874 ht = &curwin->w_vars->dv_hashtab;
1875 break;
1876 case ISN_STORET:
1877 ht = &curtab->tp_vars->dv_hashtab;
1878 break;
1879 default: // Cannot reach here
1880 goto failed;
1881 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882
1883 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001884 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001886 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 else
1888 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001889 SOURCING_LNUM = iptr->isn_lnum;
1890 if (var_check_permission(di, name) == FAIL)
1891 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 clear_tv(&di->di_tv);
1893 di->di_tv = *STACK_TV_BOT(0);
1894 }
1895 }
1896 break;
1897
Bram Moolenaar03290b82020-12-19 16:30:44 +01001898 // store an autoload variable
1899 case ISN_STOREAUTO:
1900 SOURCING_LNUM = iptr->isn_lnum;
1901 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1902 clear_tv(STACK_TV_BOT(-1));
1903 --ectx.ec_stack.ga_len;
1904 break;
1905
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 // store number in local variable
1907 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001908 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 clear_tv(tv);
1910 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001911 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 break;
1913
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001914 // store value in list or dict variable
1915 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001916 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001917 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001918 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001919 typval_T *tv_dest = STACK_TV_BOT(-1);
1920 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001921
Bram Moolenaar752fc692021-01-04 21:57:11 +01001922 // Stack contains:
1923 // -3 value to be stored
1924 // -2 index
1925 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001926 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001927 SOURCING_LNUM = iptr->isn_lnum;
1928 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001929 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001930 dest_type = tv_dest->v_type;
1931 if (dest_type == VAR_DICT)
1932 status = do_2string(tv_idx, TRUE);
1933 else if (dest_type == VAR_LIST
1934 && tv_idx->v_type != VAR_NUMBER)
1935 {
1936 emsg(_(e_number_exp));
1937 status = FAIL;
1938 }
1939 }
1940 else if (dest_type != tv_dest->v_type)
1941 {
1942 // just in case, should be OK
1943 semsg(_(e_expected_str_but_got_str),
1944 vartype_name(dest_type),
1945 vartype_name(tv_dest->v_type));
1946 status = FAIL;
1947 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001948
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001949 if (status == OK && dest_type == VAR_LIST)
1950 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001951 long lidx = (long)tv_idx->vval.v_number;
1952 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001953
1954 if (list == NULL)
1955 {
1956 emsg(_(e_list_not_set));
1957 goto on_error;
1958 }
1959 if (lidx < 0 && list->lv_len + lidx >= 0)
1960 // negative index is relative to the end
1961 lidx = list->lv_len + lidx;
1962 if (lidx < 0 || lidx > list->lv_len)
1963 {
1964 semsg(_(e_listidx), lidx);
1965 goto on_error;
1966 }
1967 if (lidx < list->lv_len)
1968 {
1969 listitem_T *li = list_find(list, lidx);
1970
1971 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001972 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001973 goto on_error;
1974 // overwrite existing list item
1975 clear_tv(&li->li_tv);
1976 li->li_tv = *tv;
1977 }
1978 else
1979 {
1980 if (error_if_locked(list->lv_lock,
1981 e_cannot_change_list))
1982 goto on_error;
1983 // append to list, only fails when out of memory
1984 if (list_append_tv(list, tv) == FAIL)
1985 goto failed;
1986 clear_tv(tv);
1987 }
1988 }
1989 else if (status == OK && dest_type == VAR_DICT)
1990 {
1991 char_u *key = tv_idx->vval.v_string;
1992 dict_T *dict = tv_dest->vval.v_dict;
1993 dictitem_T *di;
1994
1995 SOURCING_LNUM = iptr->isn_lnum;
1996 if (dict == NULL)
1997 {
1998 emsg(_(e_dictionary_not_set));
1999 goto on_error;
2000 }
2001 if (key == NULL)
2002 key = (char_u *)"";
2003 di = dict_find(dict, key, -1);
2004 if (di != NULL)
2005 {
2006 if (error_if_locked(di->di_tv.v_lock,
2007 e_cannot_change_dict_item))
2008 goto on_error;
2009 // overwrite existing value
2010 clear_tv(&di->di_tv);
2011 di->di_tv = *tv;
2012 }
2013 else
2014 {
2015 if (error_if_locked(dict->dv_lock,
2016 e_cannot_change_dict))
2017 goto on_error;
2018 // add to dict, only fails when out of memory
2019 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2020 goto failed;
2021 clear_tv(tv);
2022 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002023 }
2024 else
2025 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002026 status = FAIL;
2027 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002028 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002029
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002030 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002031 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002032 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002033 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002034 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002035 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002036 goto on_error;
2037 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002038 }
2039 break;
2040
Bram Moolenaar0186e582021-01-10 18:33:11 +01002041 // load or store variable or argument from outer scope
2042 case ISN_LOADOUTER:
2043 case ISN_STOREOUTER:
2044 {
2045 int depth = iptr->isn_arg.outer.outer_depth;
2046 outer_T *outer = ectx.ec_outer;
2047
2048 while (depth > 1 && outer != NULL)
2049 {
2050 outer = outer->out_up;
2051 --depth;
2052 }
2053 if (outer == NULL)
2054 {
2055 SOURCING_LNUM = iptr->isn_lnum;
2056 iemsg("LOADOUTER depth more than scope levels");
2057 goto failed;
2058 }
2059 tv = ((typval_T *)outer->out_stack->ga_data)
2060 + outer->out_frame_idx + STACK_FRAME_SIZE
2061 + iptr->isn_arg.outer.outer_idx;
2062 if (iptr->isn_type == ISN_LOADOUTER)
2063 {
2064 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2065 goto failed;
2066 copy_tv(tv, STACK_TV_BOT(0));
2067 ++ectx.ec_stack.ga_len;
2068 }
2069 else
2070 {
2071 --ectx.ec_stack.ga_len;
2072 clear_tv(tv);
2073 *tv = *STACK_TV_BOT(0);
2074 }
2075 }
2076 break;
2077
Bram Moolenaar752fc692021-01-04 21:57:11 +01002078 // unlet item in list or dict variable
2079 case ISN_UNLETINDEX:
2080 {
2081 typval_T *tv_idx = STACK_TV_BOT(-2);
2082 typval_T *tv_dest = STACK_TV_BOT(-1);
2083 int status = OK;
2084
2085 // Stack contains:
2086 // -2 index
2087 // -1 dict or list
2088 if (tv_dest->v_type == VAR_DICT)
2089 {
2090 // unlet a dict item, index must be a string
2091 if (tv_idx->v_type != VAR_STRING)
2092 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002093 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002094 semsg(_(e_expected_str_but_got_str),
2095 vartype_name(VAR_STRING),
2096 vartype_name(tv_idx->v_type));
2097 status = FAIL;
2098 }
2099 else
2100 {
2101 dict_T *d = tv_dest->vval.v_dict;
2102 char_u *key = tv_idx->vval.v_string;
2103 dictitem_T *di = NULL;
2104
2105 if (key == NULL)
2106 key = (char_u *)"";
2107 if (d != NULL)
2108 di = dict_find(d, key, (int)STRLEN(key));
2109 if (di == NULL)
2110 {
2111 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002113 semsg(_(e_dictkey), key);
2114 status = FAIL;
2115 }
2116 else
2117 {
2118 // TODO: check for dict or item locked
2119 dictitem_remove(d, di);
2120 }
2121 }
2122 }
2123 else if (tv_dest->v_type == VAR_LIST)
2124 {
2125 // unlet a List item, index must be a number
2126 if (tv_idx->v_type != VAR_NUMBER)
2127 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002129 semsg(_(e_expected_str_but_got_str),
2130 vartype_name(VAR_NUMBER),
2131 vartype_name(tv_idx->v_type));
2132 status = FAIL;
2133 }
2134 else
2135 {
2136 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002137 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002138 listitem_T *li = NULL;
2139
2140 li = list_find(l, n);
2141 if (li == NULL)
2142 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002143 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002144 semsg(_(e_listidx), n);
2145 status = FAIL;
2146 }
2147 else
2148 // TODO: check for list or item locked
2149 listitem_remove(l, li);
2150 }
2151 }
2152 else
2153 {
2154 status = FAIL;
2155 semsg(_(e_cannot_index_str),
2156 vartype_name(tv_dest->v_type));
2157 }
2158
2159 clear_tv(tv_idx);
2160 clear_tv(tv_dest);
2161 ectx.ec_stack.ga_len -= 2;
2162 if (status == FAIL)
2163 goto on_error;
2164 }
2165 break;
2166
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 // push constant
2168 case ISN_PUSHNR:
2169 case ISN_PUSHBOOL:
2170 case ISN_PUSHSPEC:
2171 case ISN_PUSHF:
2172 case ISN_PUSHS:
2173 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002174 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002175 case ISN_PUSHCHANNEL:
2176 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002177 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 goto failed;
2179 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002180 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 ++ectx.ec_stack.ga_len;
2182 switch (iptr->isn_type)
2183 {
2184 case ISN_PUSHNR:
2185 tv->v_type = VAR_NUMBER;
2186 tv->vval.v_number = iptr->isn_arg.number;
2187 break;
2188 case ISN_PUSHBOOL:
2189 tv->v_type = VAR_BOOL;
2190 tv->vval.v_number = iptr->isn_arg.number;
2191 break;
2192 case ISN_PUSHSPEC:
2193 tv->v_type = VAR_SPECIAL;
2194 tv->vval.v_number = iptr->isn_arg.number;
2195 break;
2196#ifdef FEAT_FLOAT
2197 case ISN_PUSHF:
2198 tv->v_type = VAR_FLOAT;
2199 tv->vval.v_float = iptr->isn_arg.fnumber;
2200 break;
2201#endif
2202 case ISN_PUSHBLOB:
2203 blob_copy(iptr->isn_arg.blob, tv);
2204 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002205 case ISN_PUSHFUNC:
2206 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002207 if (iptr->isn_arg.string == NULL)
2208 tv->vval.v_string = NULL;
2209 else
2210 tv->vval.v_string =
2211 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002212 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002213 case ISN_PUSHCHANNEL:
2214#ifdef FEAT_JOB_CHANNEL
2215 tv->v_type = VAR_CHANNEL;
2216 tv->vval.v_channel = iptr->isn_arg.channel;
2217 if (tv->vval.v_channel != NULL)
2218 ++tv->vval.v_channel->ch_refcount;
2219#endif
2220 break;
2221 case ISN_PUSHJOB:
2222#ifdef FEAT_JOB_CHANNEL
2223 tv->v_type = VAR_JOB;
2224 tv->vval.v_job = iptr->isn_arg.job;
2225 if (tv->vval.v_job != NULL)
2226 ++tv->vval.v_job->jv_refcount;
2227#endif
2228 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 default:
2230 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002231 tv->vval.v_string = vim_strsave(
2232 iptr->isn_arg.string == NULL
2233 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234 }
2235 break;
2236
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002237 case ISN_UNLET:
2238 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2239 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002240 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002241 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002242 case ISN_UNLETENV:
2243 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2244 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002245
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002246 case ISN_LOCKCONST:
2247 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2248 break;
2249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 // create a list from items on the stack; uses a single allocation
2251 // for the list header and the items
2252 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002253 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2254 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 break;
2256
2257 // create a dict from items on the stack
2258 case ISN_NEWDICT:
2259 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002260 int count = iptr->isn_arg.number;
2261 dict_T *dict = dict_alloc();
2262 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002263 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264
2265 if (dict == NULL)
2266 goto failed;
2267 for (idx = 0; idx < count; ++idx)
2268 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002269 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002271 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002272 key = tv->vval.v_string == NULL
2273 ? (char_u *)"" : tv->vval.v_string;
2274 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002275 if (item != NULL)
2276 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002277 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002278 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002279 dict_unref(dict);
2280 goto on_error;
2281 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002282 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 clear_tv(tv);
2284 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002285 {
2286 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002288 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2290 item->di_tv.v_lock = 0;
2291 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002292 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002293 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002294 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 }
2298
2299 if (count > 0)
2300 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002301 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 goto failed;
2303 else
2304 ++ectx.ec_stack.ga_len;
2305 tv = STACK_TV_BOT(-1);
2306 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002307 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 tv->vval.v_dict = dict;
2309 ++dict->dv_refcount;
2310 }
2311 break;
2312
2313 // call a :def function
2314 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002315 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002316 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 iptr->isn_arg.dfunc.cdf_argcount,
2318 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002319 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 break;
2321
2322 // call a builtin function
2323 case ISN_BCALL:
2324 SOURCING_LNUM = iptr->isn_lnum;
2325 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2326 iptr->isn_arg.bfunc.cbf_argcount,
2327 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002328 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 break;
2330
2331 // call a funcref or partial
2332 case ISN_PCALL:
2333 {
2334 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2335 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002336 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337
2338 SOURCING_LNUM = iptr->isn_lnum;
2339 if (pfunc->cpf_top)
2340 {
2341 // funcref is above the arguments
2342 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2343 }
2344 else
2345 {
2346 // Get the funcref from the stack.
2347 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002348 partial_tv = *STACK_TV_BOT(0);
2349 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 }
2351 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002352 if (tv == &partial_tv)
2353 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 }
2357 break;
2358
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002359 case ISN_PCALL_END:
2360 // PCALL finished, arguments have been consumed and replaced by
2361 // the return value. Now clear the funcref from the stack,
2362 // and move the return value in its place.
2363 --ectx.ec_stack.ga_len;
2364 clear_tv(STACK_TV_BOT(-1));
2365 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2366 break;
2367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 // call a user defined function or funcref/partial
2369 case ISN_UCALL:
2370 {
2371 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2372
2373 SOURCING_LNUM = iptr->isn_lnum;
2374 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002375 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002376 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 }
2378 break;
2379
2380 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002381 case ISN_RETURN_ZERO:
2382 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2383 goto failed;
2384 tv = STACK_TV_BOT(0);
2385 ++ectx.ec_stack.ga_len;
2386 tv->v_type = VAR_NUMBER;
2387 tv->vval.v_number = 0;
2388 tv->v_lock = 0;
2389 // FALLTHROUGH
2390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 case ISN_RETURN:
2392 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002393 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002394 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002395
2396 if (trystack->ga_len > 0)
2397 trycmd = ((trycmd_T *)trystack->ga_data)
2398 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002399 if (trycmd != NULL
2400 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 && trycmd->tcd_finally_idx != 0)
2402 {
2403 // jump to ":finally"
2404 ectx.ec_iidx = trycmd->tcd_finally_idx;
2405 trycmd->tcd_return = TRUE;
2406 }
2407 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002408 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 }
2410 break;
2411
2412 // push a function reference to a compiled function
2413 case ISN_FUNCREF:
2414 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002415 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2416 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2417 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 if (pt == NULL)
2420 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002421 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002422 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002423 vim_free(pt);
2424 goto failed;
2425 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002426 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2427 &ectx) == FAIL)
2428 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 tv = STACK_TV_BOT(0);
2431 ++ectx.ec_stack.ga_len;
2432 tv->vval.v_partial = pt;
2433 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002434 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 }
2436 break;
2437
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002438 // Create a global function from a lambda.
2439 case ISN_NEWFUNC:
2440 {
2441 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2442
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002443 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002444 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002445 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002446 }
2447 break;
2448
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002449 // List functions
2450 case ISN_DEF:
2451 if (iptr->isn_arg.string == NULL)
2452 list_functions(NULL);
2453 else
2454 {
2455 exarg_T ea;
2456
2457 CLEAR_FIELD(ea);
2458 ea.cmd = ea.arg = iptr->isn_arg.string;
2459 define_function(&ea, NULL);
2460 }
2461 break;
2462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 // jump if a condition is met
2464 case ISN_JUMP:
2465 {
2466 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002467 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 int jump = TRUE;
2469
2470 if (when != JUMP_ALWAYS)
2471 {
2472 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002473 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002474 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002475 || when == JUMP_IF_COND_TRUE)
2476 {
2477 SOURCING_LNUM = iptr->isn_lnum;
2478 jump = tv_get_bool_chk(tv, &error);
2479 if (error)
2480 goto on_error;
2481 }
2482 else
2483 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002485 || when == JUMP_AND_KEEP_IF_FALSE
2486 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002488 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 {
2490 // drop the value from the stack
2491 clear_tv(tv);
2492 --ectx.ec_stack.ga_len;
2493 }
2494 }
2495 if (jump)
2496 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2497 }
2498 break;
2499
2500 // top of a for loop
2501 case ISN_FOR:
2502 {
2503 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2504 typval_T *idxtv =
2505 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2506
2507 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002508 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002510 ++idxtv->vval.v_number;
2511 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 // past the end of the list, jump to "endfor"
2513 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2514 else if (list->lv_first == &range_list_item)
2515 {
2516 // non-materialized range() list
2517 tv = STACK_TV_BOT(0);
2518 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002519 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 tv->vval.v_number = list_find_nr(
2521 list, idxtv->vval.v_number, NULL);
2522 ++ectx.ec_stack.ga_len;
2523 }
2524 else
2525 {
2526 listitem_T *li = list_find(list, idxtv->vval.v_number);
2527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2529 ++ectx.ec_stack.ga_len;
2530 }
2531 }
2532 break;
2533
2534 // start of ":try" block
2535 case ISN_TRY:
2536 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002537 trycmd_T *trycmd = NULL;
2538
Bram Moolenaar270d0382020-05-15 21:42:53 +02002539 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 goto failed;
2541 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2542 + ectx.ec_trystack.ga_len;
2543 ++ectx.ec_trystack.ga_len;
2544 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002545 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2547 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002548 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002549 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 }
2551 break;
2552
2553 case ISN_PUSHEXC:
2554 if (current_exception == NULL)
2555 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002556 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 iemsg("Evaluating catch while current_exception is NULL");
2558 goto failed;
2559 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002560 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 goto failed;
2562 tv = STACK_TV_BOT(0);
2563 ++ectx.ec_stack.ga_len;
2564 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002565 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 tv->vval.v_string = vim_strsave(
2567 (char_u *)current_exception->value);
2568 break;
2569
2570 case ISN_CATCH:
2571 {
2572 garray_T *trystack = &ectx.ec_trystack;
2573
Bram Moolenaar20a76292020-12-25 19:47:24 +01002574 if (restore_cmdmod)
2575 {
2576 cmdmod.cmod_filter_regmatch.regprog = NULL;
2577 undo_cmdmod(&cmdmod);
2578 cmdmod = save_cmdmod;
2579 restore_cmdmod = FALSE;
2580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 if (trystack->ga_len > 0)
2582 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002583 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 + trystack->ga_len - 1;
2585 trycmd->tcd_caught = TRUE;
2586 }
2587 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002588 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 catch_exception(current_exception);
2590 }
2591 break;
2592
2593 // end of ":try" block
2594 case ISN_ENDTRY:
2595 {
2596 garray_T *trystack = &ectx.ec_trystack;
2597
2598 if (trystack->ga_len > 0)
2599 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002600 trycmd_T *trycmd = NULL;
2601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 --trystack->ga_len;
2603 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002604 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 trycmd = ((trycmd_T *)trystack->ga_data)
2606 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002607 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608 {
2609 // discard the exception
2610 if (caught_stack == current_exception)
2611 caught_stack = caught_stack->caught;
2612 discard_current_exception();
2613 }
2614
2615 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002616 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 }
2618 }
2619 break;
2620
2621 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002622 if (ectx.ec_trystack.ga_len == 0 && trylevel == 0
2623 && emsg_silent)
2624 {
2625 // throwing an exception while using "silent!" causes the
2626 // function to abort but not display an error.
2627 tv = STACK_TV_BOT(-1);
2628 clear_tv(tv);
2629 tv->v_type = VAR_NUMBER;
2630 tv->vval.v_number = 0;
2631 goto done;
2632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 --ectx.ec_stack.ga_len;
2634 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002635 if (tv->vval.v_string == NULL
2636 || *skipwhite(tv->vval.v_string) == NUL)
2637 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002638 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002639 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002640 emsg(_(e_throw_with_empty_string));
2641 goto failed;
2642 }
2643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2645 {
2646 vim_free(tv->vval.v_string);
2647 goto failed;
2648 }
2649 did_throw = TRUE;
2650 break;
2651
2652 // compare with special values
2653 case ISN_COMPAREBOOL:
2654 case ISN_COMPARESPECIAL:
2655 {
2656 typval_T *tv1 = STACK_TV_BOT(-2);
2657 typval_T *tv2 = STACK_TV_BOT(-1);
2658 varnumber_T arg1 = tv1->vval.v_number;
2659 varnumber_T arg2 = tv2->vval.v_number;
2660 int res;
2661
2662 switch (iptr->isn_arg.op.op_type)
2663 {
2664 case EXPR_EQUAL: res = arg1 == arg2; break;
2665 case EXPR_NEQUAL: res = arg1 != arg2; break;
2666 default: res = 0; break;
2667 }
2668
2669 --ectx.ec_stack.ga_len;
2670 tv1->v_type = VAR_BOOL;
2671 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2672 }
2673 break;
2674
2675 // Operation with two number arguments
2676 case ISN_OPNR:
2677 case ISN_COMPARENR:
2678 {
2679 typval_T *tv1 = STACK_TV_BOT(-2);
2680 typval_T *tv2 = STACK_TV_BOT(-1);
2681 varnumber_T arg1 = tv1->vval.v_number;
2682 varnumber_T arg2 = tv2->vval.v_number;
2683 varnumber_T res;
2684
2685 switch (iptr->isn_arg.op.op_type)
2686 {
2687 case EXPR_MULT: res = arg1 * arg2; break;
2688 case EXPR_DIV: res = arg1 / arg2; break;
2689 case EXPR_REM: res = arg1 % arg2; break;
2690 case EXPR_SUB: res = arg1 - arg2; break;
2691 case EXPR_ADD: res = arg1 + arg2; break;
2692
2693 case EXPR_EQUAL: res = arg1 == arg2; break;
2694 case EXPR_NEQUAL: res = arg1 != arg2; break;
2695 case EXPR_GREATER: res = arg1 > arg2; break;
2696 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2697 case EXPR_SMALLER: res = arg1 < arg2; break;
2698 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2699 default: res = 0; break;
2700 }
2701
2702 --ectx.ec_stack.ga_len;
2703 if (iptr->isn_type == ISN_COMPARENR)
2704 {
2705 tv1->v_type = VAR_BOOL;
2706 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2707 }
2708 else
2709 tv1->vval.v_number = res;
2710 }
2711 break;
2712
2713 // Computation with two float arguments
2714 case ISN_OPFLOAT:
2715 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002716#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717 {
2718 typval_T *tv1 = STACK_TV_BOT(-2);
2719 typval_T *tv2 = STACK_TV_BOT(-1);
2720 float_T arg1 = tv1->vval.v_float;
2721 float_T arg2 = tv2->vval.v_float;
2722 float_T res = 0;
2723 int cmp = FALSE;
2724
2725 switch (iptr->isn_arg.op.op_type)
2726 {
2727 case EXPR_MULT: res = arg1 * arg2; break;
2728 case EXPR_DIV: res = arg1 / arg2; break;
2729 case EXPR_SUB: res = arg1 - arg2; break;
2730 case EXPR_ADD: res = arg1 + arg2; break;
2731
2732 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2733 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2734 case EXPR_GREATER: cmp = arg1 > arg2; break;
2735 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2736 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2737 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2738 default: cmp = 0; break;
2739 }
2740 --ectx.ec_stack.ga_len;
2741 if (iptr->isn_type == ISN_COMPAREFLOAT)
2742 {
2743 tv1->v_type = VAR_BOOL;
2744 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2745 }
2746 else
2747 tv1->vval.v_float = res;
2748 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002749#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 break;
2751
2752 case ISN_COMPARELIST:
2753 {
2754 typval_T *tv1 = STACK_TV_BOT(-2);
2755 typval_T *tv2 = STACK_TV_BOT(-1);
2756 list_T *arg1 = tv1->vval.v_list;
2757 list_T *arg2 = tv2->vval.v_list;
2758 int cmp = FALSE;
2759 int ic = iptr->isn_arg.op.op_ic;
2760
2761 switch (iptr->isn_arg.op.op_type)
2762 {
2763 case EXPR_EQUAL: cmp =
2764 list_equal(arg1, arg2, ic, FALSE); break;
2765 case EXPR_NEQUAL: cmp =
2766 !list_equal(arg1, arg2, ic, FALSE); break;
2767 case EXPR_IS: cmp = arg1 == arg2; break;
2768 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2769 default: cmp = 0; break;
2770 }
2771 --ectx.ec_stack.ga_len;
2772 clear_tv(tv1);
2773 clear_tv(tv2);
2774 tv1->v_type = VAR_BOOL;
2775 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2776 }
2777 break;
2778
2779 case ISN_COMPAREBLOB:
2780 {
2781 typval_T *tv1 = STACK_TV_BOT(-2);
2782 typval_T *tv2 = STACK_TV_BOT(-1);
2783 blob_T *arg1 = tv1->vval.v_blob;
2784 blob_T *arg2 = tv2->vval.v_blob;
2785 int cmp = FALSE;
2786
2787 switch (iptr->isn_arg.op.op_type)
2788 {
2789 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2790 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2791 case EXPR_IS: cmp = arg1 == arg2; break;
2792 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2793 default: cmp = 0; break;
2794 }
2795 --ectx.ec_stack.ga_len;
2796 clear_tv(tv1);
2797 clear_tv(tv2);
2798 tv1->v_type = VAR_BOOL;
2799 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2800 }
2801 break;
2802
2803 // TODO: handle separately
2804 case ISN_COMPARESTRING:
2805 case ISN_COMPAREDICT:
2806 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 case ISN_COMPAREANY:
2808 {
2809 typval_T *tv1 = STACK_TV_BOT(-2);
2810 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002811 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 int ic = iptr->isn_arg.op.op_ic;
2813
Bram Moolenaareb26f432020-09-14 16:50:05 +02002814 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002815 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 --ectx.ec_stack.ga_len;
2818 }
2819 break;
2820
2821 case ISN_ADDLIST:
2822 case ISN_ADDBLOB:
2823 {
2824 typval_T *tv1 = STACK_TV_BOT(-2);
2825 typval_T *tv2 = STACK_TV_BOT(-1);
2826
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002827 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 if (iptr->isn_type == ISN_ADDLIST)
2829 eval_addlist(tv1, tv2);
2830 else
2831 eval_addblob(tv1, tv2);
2832 clear_tv(tv2);
2833 --ectx.ec_stack.ga_len;
2834 }
2835 break;
2836
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002837 case ISN_LISTAPPEND:
2838 {
2839 typval_T *tv1 = STACK_TV_BOT(-2);
2840 typval_T *tv2 = STACK_TV_BOT(-1);
2841 list_T *l = tv1->vval.v_list;
2842
2843 // add an item to a list
2844 if (l == NULL)
2845 {
2846 SOURCING_LNUM = iptr->isn_lnum;
2847 emsg(_(e_cannot_add_to_null_list));
2848 goto on_error;
2849 }
2850 if (list_append_tv(l, tv2) == FAIL)
2851 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002852 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002853 --ectx.ec_stack.ga_len;
2854 }
2855 break;
2856
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002857 case ISN_BLOBAPPEND:
2858 {
2859 typval_T *tv1 = STACK_TV_BOT(-2);
2860 typval_T *tv2 = STACK_TV_BOT(-1);
2861 blob_T *b = tv1->vval.v_blob;
2862 int error = FALSE;
2863 varnumber_T n;
2864
2865 // add a number to a blob
2866 if (b == NULL)
2867 {
2868 SOURCING_LNUM = iptr->isn_lnum;
2869 emsg(_(e_cannot_add_to_null_blob));
2870 goto on_error;
2871 }
2872 n = tv_get_number_chk(tv2, &error);
2873 if (error)
2874 goto on_error;
2875 ga_append(&b->bv_ga, (int)n);
2876 --ectx.ec_stack.ga_len;
2877 }
2878 break;
2879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 // Computation with two arguments of unknown type
2881 case ISN_OPANY:
2882 {
2883 typval_T *tv1 = STACK_TV_BOT(-2);
2884 typval_T *tv2 = STACK_TV_BOT(-1);
2885 varnumber_T n1, n2;
2886#ifdef FEAT_FLOAT
2887 float_T f1 = 0, f2 = 0;
2888#endif
2889 int error = FALSE;
2890
2891 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2892 {
2893 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2894 {
2895 eval_addlist(tv1, tv2);
2896 clear_tv(tv2);
2897 --ectx.ec_stack.ga_len;
2898 break;
2899 }
2900 else if (tv1->v_type == VAR_BLOB
2901 && tv2->v_type == VAR_BLOB)
2902 {
2903 eval_addblob(tv1, tv2);
2904 clear_tv(tv2);
2905 --ectx.ec_stack.ga_len;
2906 break;
2907 }
2908 }
2909#ifdef FEAT_FLOAT
2910 if (tv1->v_type == VAR_FLOAT)
2911 {
2912 f1 = tv1->vval.v_float;
2913 n1 = 0;
2914 }
2915 else
2916#endif
2917 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002918 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 n1 = tv_get_number_chk(tv1, &error);
2920 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002921 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002922#ifdef FEAT_FLOAT
2923 if (tv2->v_type == VAR_FLOAT)
2924 f1 = n1;
2925#endif
2926 }
2927#ifdef FEAT_FLOAT
2928 if (tv2->v_type == VAR_FLOAT)
2929 {
2930 f2 = tv2->vval.v_float;
2931 n2 = 0;
2932 }
2933 else
2934#endif
2935 {
2936 n2 = tv_get_number_chk(tv2, &error);
2937 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002938 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939#ifdef FEAT_FLOAT
2940 if (tv1->v_type == VAR_FLOAT)
2941 f2 = n2;
2942#endif
2943 }
2944#ifdef FEAT_FLOAT
2945 // if there is a float on either side the result is a float
2946 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2947 {
2948 switch (iptr->isn_arg.op.op_type)
2949 {
2950 case EXPR_MULT: f1 = f1 * f2; break;
2951 case EXPR_DIV: f1 = f1 / f2; break;
2952 case EXPR_SUB: f1 = f1 - f2; break;
2953 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002954 default: SOURCING_LNUM = iptr->isn_lnum;
2955 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002956 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 }
2958 clear_tv(tv1);
2959 clear_tv(tv2);
2960 tv1->v_type = VAR_FLOAT;
2961 tv1->vval.v_float = f1;
2962 --ectx.ec_stack.ga_len;
2963 }
2964 else
2965#endif
2966 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002967 int failed = FALSE;
2968
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969 switch (iptr->isn_arg.op.op_type)
2970 {
2971 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002972 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
2973 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002974 goto on_error;
2975 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 case EXPR_SUB: n1 = n1 - n2; break;
2977 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002978 default: n1 = num_modulus(n1, n2, &failed);
2979 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002980 goto on_error;
2981 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 }
2983 clear_tv(tv1);
2984 clear_tv(tv2);
2985 tv1->v_type = VAR_NUMBER;
2986 tv1->vval.v_number = n1;
2987 --ectx.ec_stack.ga_len;
2988 }
2989 }
2990 break;
2991
2992 case ISN_CONCAT:
2993 {
2994 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2995 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2996 char_u *res;
2997
2998 res = concat_str(str1, str2);
2999 clear_tv(STACK_TV_BOT(-2));
3000 clear_tv(STACK_TV_BOT(-1));
3001 --ectx.ec_stack.ga_len;
3002 STACK_TV_BOT(-1)->vval.v_string = res;
3003 }
3004 break;
3005
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003006 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003007 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003008 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003009 int is_slice = iptr->isn_type == ISN_STRSLICE;
3010 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003011 char_u *res;
3012
3013 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003014 // string slice: string is at stack-3, first index at
3015 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003016 if (is_slice)
3017 {
3018 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003019 n1 = tv->vval.v_number;
3020 }
3021
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003022 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003023 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003024
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003025 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003026 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003027 if (is_slice)
3028 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003029 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003030 else
3031 // Index: The resulting variable is a string of a
3032 // single character. If the index is too big or
3033 // negative the result is empty.
3034 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003035 vim_free(tv->vval.v_string);
3036 tv->vval.v_string = res;
3037 }
3038 break;
3039
3040 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003041 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 {
Bram Moolenaared591872020-08-15 22:14:53 +02003043 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003044 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003045 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046
3047 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003048 // list slice: list is at stack-3, indexes at stack-2 and
3049 // stack-1
3050 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003051 list = tv->vval.v_list;
3052
3053 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003054 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003056
3057 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 {
Bram Moolenaared591872020-08-15 22:14:53 +02003059 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003060 n1 = tv->vval.v_number;
3061 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003062 }
Bram Moolenaared591872020-08-15 22:14:53 +02003063
3064 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003065 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003066 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003067 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3068 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003069 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 }
3071 break;
3072
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003073 case ISN_ANYINDEX:
3074 case ISN_ANYSLICE:
3075 {
3076 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3077 typval_T *var1, *var2;
3078 int res;
3079
3080 // index: composite is at stack-2, index at stack-1
3081 // slice: composite is at stack-3, indexes at stack-2 and
3082 // stack-1
3083 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003084 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003085 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3086 goto on_error;
3087 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3088 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003089 res = eval_index_inner(tv, is_slice, var1, var2,
3090 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003091 clear_tv(var1);
3092 if (is_slice)
3093 clear_tv(var2);
3094 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3095 if (res == FAIL)
3096 goto on_error;
3097 }
3098 break;
3099
Bram Moolenaar9af78762020-06-16 11:34:42 +02003100 case ISN_SLICE:
3101 {
3102 list_T *list;
3103 int count = iptr->isn_arg.number;
3104
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003105 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003106 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003107 list = tv->vval.v_list;
3108
3109 // no error for short list, expect it to be checked earlier
3110 if (list != NULL && list->lv_len >= count)
3111 {
3112 list_T *newlist = list_slice(list,
3113 count, list->lv_len - 1);
3114
3115 if (newlist != NULL)
3116 {
3117 list_unref(list);
3118 tv->vval.v_list = newlist;
3119 ++newlist->lv_refcount;
3120 }
3121 }
3122 }
3123 break;
3124
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003125 case ISN_GETITEM:
3126 {
3127 listitem_T *li;
3128 int index = iptr->isn_arg.number;
3129
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003130 // Get list item: list is at stack-1, push item.
3131 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003132 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003133 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003134
3135 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3136 goto failed;
3137 ++ectx.ec_stack.ga_len;
3138 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3139 }
3140 break;
3141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 case ISN_MEMBER:
3143 {
3144 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003145 char_u *key;
3146 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003147 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003148
3149 // dict member: dict is at stack-2, key at stack-1
3150 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003151 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003152 dict = tv->vval.v_dict;
3153
3154 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003155 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003156 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003157 if (key == NULL)
3158 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003159
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003160 if ((di = dict_find(dict, key, -1)) == NULL)
3161 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003162 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003163 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003164
3165 // If :silent! is used we will continue, make sure the
3166 // stack contents makes sense.
3167 clear_tv(tv);
3168 --ectx.ec_stack.ga_len;
3169 tv = STACK_TV_BOT(-1);
3170 clear_tv(tv);
3171 tv->v_type = VAR_NUMBER;
3172 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003173 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003174 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003175 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003176 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003177 // Clear the dict only after getting the item, to avoid
3178 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003179 tv = STACK_TV_BOT(-1);
3180 temp_tv = *tv;
3181 copy_tv(&di->di_tv, tv);
3182 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003183 }
3184 break;
3185
3186 // dict member with string key
3187 case ISN_STRINGMEMBER:
3188 {
3189 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003190 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003191 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192
3193 tv = STACK_TV_BOT(-1);
3194 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3195 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003196 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003198 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 }
3200 dict = tv->vval.v_dict;
3201
3202 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3203 == NULL)
3204 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003207 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003209 // Clear the dict after getting the item, to avoid that it
3210 // make the item invalid.
3211 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003213 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003214 }
3215 break;
3216
3217 case ISN_NEGATENR:
3218 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003219 if (tv->v_type != VAR_NUMBER
3220#ifdef FEAT_FLOAT
3221 && tv->v_type != VAR_FLOAT
3222#endif
3223 )
3224 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003225 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003226 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003227 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003228 }
3229#ifdef FEAT_FLOAT
3230 if (tv->v_type == VAR_FLOAT)
3231 tv->vval.v_float = -tv->vval.v_float;
3232 else
3233#endif
3234 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 break;
3236
3237 case ISN_CHECKNR:
3238 {
3239 int error = FALSE;
3240
3241 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003242 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003244 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 (void)tv_get_number_chk(tv, &error);
3246 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 break;
3250
3251 case ISN_CHECKTYPE:
3252 {
3253 checktype_T *ct = &iptr->isn_arg.type;
3254
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003255 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003256 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare32e5162021-01-21 20:21:29 +01003257 if (check_typval_type(ct->ct_type, tv, ct->ct_arg_idx)
3258 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003259 goto on_error;
3260
3261 // number 0 is FALSE, number 1 is TRUE
3262 if (tv->v_type == VAR_NUMBER
3263 && ct->ct_type->tt_type == VAR_BOOL
3264 && (tv->vval.v_number == 0
3265 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003267 tv->v_type = VAR_BOOL;
3268 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003269 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 }
3271 }
3272 break;
3273
Bram Moolenaar9af78762020-06-16 11:34:42 +02003274 case ISN_CHECKLEN:
3275 {
3276 int min_len = iptr->isn_arg.checklen.cl_min_len;
3277 list_T *list = NULL;
3278
3279 tv = STACK_TV_BOT(-1);
3280 if (tv->v_type == VAR_LIST)
3281 list = tv->vval.v_list;
3282 if (list == NULL || list->lv_len < min_len
3283 || (list->lv_len > min_len
3284 && !iptr->isn_arg.checklen.cl_more_OK))
3285 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003287 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003288 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003289 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003290 }
3291 }
3292 break;
3293
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003294 case ISN_SETTYPE:
3295 {
3296 checktype_T *ct = &iptr->isn_arg.type;
3297
3298 tv = STACK_TV_BOT(-1);
3299 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3300 {
3301 free_type(tv->vval.v_dict->dv_type);
3302 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3303 }
3304 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3305 {
3306 free_type(tv->vval.v_list->lv_type);
3307 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3308 }
3309 }
3310 break;
3311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003313 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 {
3315 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003316 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003317
3318 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003319 if (iptr->isn_type == ISN_2BOOL)
3320 {
3321 n = tv2bool(tv);
3322 if (iptr->isn_arg.number) // invert
3323 n = !n;
3324 }
3325 else
3326 {
3327 SOURCING_LNUM = iptr->isn_lnum;
3328 n = tv_get_bool_chk(tv, &error);
3329 if (error)
3330 goto on_error;
3331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 clear_tv(tv);
3333 tv->v_type = VAR_BOOL;
3334 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3335 }
3336 break;
3337
3338 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003339 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003340 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003341 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3342 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3343 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003344 break;
3345
Bram Moolenaar08597872020-12-10 19:43:40 +01003346 case ISN_RANGE:
3347 {
3348 exarg_T ea;
3349 char *errormsg;
3350
Bram Moolenaarece0b872021-01-08 20:40:45 +01003351 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003352 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003353 ea.addr_type = ADDR_LINES;
3354 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003355 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003356 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003357 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003358
3359 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3360 goto failed;
3361 ++ectx.ec_stack.ga_len;
3362 tv = STACK_TV_BOT(-1);
3363 tv->v_type = VAR_NUMBER;
3364 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003365 if (ea.addr_count == 0)
3366 tv->vval.v_number = curwin->w_cursor.lnum;
3367 else
3368 tv->vval.v_number = ea.line2;
3369 }
3370 break;
3371
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003372 case ISN_PUT:
3373 {
3374 int regname = iptr->isn_arg.put.put_regname;
3375 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3376 char_u *expr = NULL;
3377 int dir = FORWARD;
3378
Bram Moolenaar08597872020-12-10 19:43:40 +01003379 if (lnum < -2)
3380 {
3381 // line number was put on the stack by ISN_RANGE
3382 tv = STACK_TV_BOT(-1);
3383 curwin->w_cursor.lnum = tv->vval.v_number;
3384 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3385 dir = BACKWARD;
3386 --ectx.ec_stack.ga_len;
3387 }
3388 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003389 // :put! above cursor
3390 dir = BACKWARD;
3391 else if (lnum >= 0)
3392 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003393
3394 if (regname == '=')
3395 {
3396 tv = STACK_TV_BOT(-1);
3397 if (tv->v_type == VAR_STRING)
3398 expr = tv->vval.v_string;
3399 else
3400 {
3401 expr = typval2string(tv, TRUE); // allocates value
3402 clear_tv(tv);
3403 }
3404 --ectx.ec_stack.ga_len;
3405 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003406 check_cursor();
3407 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3408 vim_free(expr);
3409 }
3410 break;
3411
Bram Moolenaar02194d22020-10-24 23:08:38 +02003412 case ISN_CMDMOD:
3413 save_cmdmod = cmdmod;
3414 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003415 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003416 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3417 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003418 break;
3419
Bram Moolenaar02194d22020-10-24 23:08:38 +02003420 case ISN_CMDMOD_REV:
3421 // filter regprog is owned by the instruction, don't free it
3422 cmdmod.cmod_filter_regmatch.regprog = NULL;
3423 undo_cmdmod(&cmdmod);
3424 cmdmod = save_cmdmod;
3425 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003426 break;
3427
Bram Moolenaar792f7862020-11-23 08:31:18 +01003428 case ISN_UNPACK:
3429 {
3430 int count = iptr->isn_arg.unpack.unp_count;
3431 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3432 list_T *l;
3433 listitem_T *li;
3434 int i;
3435
3436 // Check there is a valid list to unpack.
3437 tv = STACK_TV_BOT(-1);
3438 if (tv->v_type != VAR_LIST)
3439 {
3440 SOURCING_LNUM = iptr->isn_lnum;
3441 emsg(_(e_for_argument_must_be_sequence_of_lists));
3442 goto on_error;
3443 }
3444 l = tv->vval.v_list;
3445 if (l == NULL
3446 || l->lv_len < (semicolon ? count - 1 : count))
3447 {
3448 SOURCING_LNUM = iptr->isn_lnum;
3449 emsg(_(e_list_value_does_not_have_enough_items));
3450 goto on_error;
3451 }
3452 else if (!semicolon && l->lv_len > count)
3453 {
3454 SOURCING_LNUM = iptr->isn_lnum;
3455 emsg(_(e_list_value_has_more_items_than_targets));
3456 goto on_error;
3457 }
3458
3459 CHECK_LIST_MATERIALIZE(l);
3460 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3461 goto failed;
3462 ectx.ec_stack.ga_len += count - 1;
3463
3464 // Variable after semicolon gets a list with the remaining
3465 // items.
3466 if (semicolon)
3467 {
3468 list_T *rem_list =
3469 list_alloc_with_items(l->lv_len - count + 1);
3470
3471 if (rem_list == NULL)
3472 goto failed;
3473 tv = STACK_TV_BOT(-count);
3474 tv->vval.v_list = rem_list;
3475 ++rem_list->lv_refcount;
3476 tv->v_lock = 0;
3477 li = l->lv_first;
3478 for (i = 0; i < count - 1; ++i)
3479 li = li->li_next;
3480 for (i = 0; li != NULL; ++i)
3481 {
3482 list_set_item(rem_list, i, &li->li_tv);
3483 li = li->li_next;
3484 }
3485 --count;
3486 }
3487
3488 // Produce the values in reverse order, first item last.
3489 li = l->lv_first;
3490 for (i = 0; i < count; ++i)
3491 {
3492 tv = STACK_TV_BOT(-i - 1);
3493 copy_tv(&li->li_tv, tv);
3494 li = li->li_next;
3495 }
3496
3497 list_unref(l);
3498 }
3499 break;
3500
Bram Moolenaarb2049902021-01-24 12:53:53 +01003501 case ISN_PROF_START:
3502 case ISN_PROF_END:
3503 {
3504 funccall_T cookie;
3505 ufunc_T *cur_ufunc =
3506 (((dfunc_T *)def_functions.ga_data)
3507 + ectx.ec_dfunc_idx)->df_ufunc;
3508
3509 cookie.func = cur_ufunc;
3510 if (iptr->isn_type == ISN_PROF_START)
3511 {
3512 func_line_start(&cookie, iptr->isn_lnum);
3513 // if we get here the instruction is executed
3514 func_line_exec(&cookie);
3515 }
3516 else
3517 func_line_end(&cookie);
3518 }
3519 break;
3520
Bram Moolenaar389df252020-07-09 21:20:47 +02003521 case ISN_SHUFFLE:
3522 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003523 typval_T tmp_tv;
3524 int item = iptr->isn_arg.shuffle.shfl_item;
3525 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003526
3527 tmp_tv = *STACK_TV_BOT(-item);
3528 for ( ; up > 0 && item > 1; --up)
3529 {
3530 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3531 --item;
3532 }
3533 *STACK_TV_BOT(-item) = tmp_tv;
3534 }
3535 break;
3536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 case ISN_DROP:
3538 --ectx.ec_stack.ga_len;
3539 clear_tv(STACK_TV_BOT(0));
3540 break;
3541 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003542 continue;
3543
Bram Moolenaard032f342020-07-18 18:13:02 +02003544func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003545 // Restore previous function. If the frame pointer is where we started
3546 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003547 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003548 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003549
Bram Moolenaard032f342020-07-18 18:13:02 +02003550 if (func_return(&ectx) == FAIL)
3551 // only fails when out of memory
3552 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003553 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003554
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003555on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003556 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003557 // If "emsg_silent" is set then ignore the error, unless it was set
3558 // when calling the function.
3559 if (did_emsg_cumul + did_emsg == did_emsg_before
3560 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003561 {
3562 // If a sequence of instructions causes an error while ":silent!"
3563 // was used, restore the stack length and jump ahead to restoring
3564 // the cmdmod.
3565 if (restore_cmdmod)
3566 {
3567 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3568 {
3569 --ectx.ec_stack.ga_len;
3570 clear_tv(STACK_TV_BOT(0));
3571 }
3572 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3573 ++ectx.ec_iidx;
3574 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003575 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003576 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003577on_fatal_error:
3578 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003579 // If we are not inside a try-catch started here, abort execution.
3580 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003581 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 }
3583
3584done:
3585 // function finished, get result from the stack.
3586 tv = STACK_TV_BOT(-1);
3587 *rettv = *tv;
3588 tv->v_type = VAR_UNKNOWN;
3589 ret = OK;
3590
3591failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003592 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003593 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003594 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003595
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003596 // Deal with any remaining closures, they may be in use somewhere.
3597 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003598 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003599 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003600 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3601 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003602
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003603 estack_pop();
3604 current_sctx = save_current_sctx;
3605
Bram Moolenaar352134b2020-10-17 22:04:08 +02003606 if (*msg_list != NULL && saved_msg_list != NULL)
3607 {
3608 msglist_T **plist = saved_msg_list;
3609
3610 // Append entries from the current msg_list (uncaught exceptions) to
3611 // the saved msg_list.
3612 while (*plist != NULL)
3613 plist = &(*plist)->next;
3614
3615 *plist = *msg_list;
3616 }
3617 msg_list = saved_msg_list;
3618
Bram Moolenaar02194d22020-10-24 23:08:38 +02003619 if (restore_cmdmod)
3620 {
3621 cmdmod.cmod_filter_regmatch.regprog = NULL;
3622 undo_cmdmod(&cmdmod);
3623 cmdmod = save_cmdmod;
3624 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003625 emsg_silent_def = save_emsg_silent_def;
3626 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003627
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003628failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003629 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3631 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003632
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003634 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003635
Bram Moolenaar0186e582021-01-10 18:33:11 +01003636 while (ectx.ec_outer != NULL)
3637 {
3638 outer_T *up = ectx.ec_outer->out_up_is_copy
3639 ? NULL : ectx.ec_outer->out_up;
3640
3641 vim_free(ectx.ec_outer);
3642 ectx.ec_outer = up;
3643 }
3644
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003645 // Not sure if this is necessary.
3646 suppress_errthrow = save_suppress_errthrow;
3647
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003648 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003649 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003650 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003651 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003652 return ret;
3653}
3654
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003656 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003657 * We don't really need this at runtime, but we do have tests that require it,
3658 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 */
3660 void
3661ex_disassemble(exarg_T *eap)
3662{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003663 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003664 char_u *fname;
3665 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 dfunc_T *dfunc;
3667 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003668 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 int current;
3670 int line_idx = 0;
3671 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003672 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003673
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003674 if (STRNCMP(arg, "<lambda>", 8) == 0)
3675 {
3676 arg += 8;
3677 (void)getdigits(&arg);
3678 fname = vim_strnsave(eap->arg, arg - eap->arg);
3679 }
3680 else
3681 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003682 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003683 if (fname == NULL)
3684 {
3685 semsg(_(e_invarg2), eap->arg);
3686 return;
3687 }
3688
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003689 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003690 if (ufunc == NULL)
3691 {
3692 char_u *p = untrans_function_name(fname);
3693
3694 if (p != NULL)
3695 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003696 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003697 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003698 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699 if (ufunc == NULL)
3700 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003701 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 return;
3703 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003704 if (func_needs_compiling(ufunc, eap->forceit)
3705 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003706 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003707 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003709 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return;
3711 }
3712 if (ufunc->uf_name_exp != NULL)
3713 msg((char *)ufunc->uf_name_exp);
3714 else
3715 msg((char *)ufunc->uf_name);
3716
3717 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003718 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3719 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3720 : dfunc->df_instr_count;
3721 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003722 {
3723 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003724 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725
3726 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3727 {
3728 if (current > prev_current)
3729 {
3730 msg_puts("\n\n");
3731 prev_current = current;
3732 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003733 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3734 if (line != NULL)
3735 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736 }
3737
3738 switch (iptr->isn_type)
3739 {
3740 case ISN_EXEC:
3741 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3742 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003743 case ISN_EXECCONCAT:
3744 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003745 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003746 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003747 case ISN_ECHO:
3748 {
3749 echo_T *echo = &iptr->isn_arg.echo;
3750
3751 smsg("%4d %s %d", current,
3752 echo->echo_with_white ? "ECHO" : "ECHON",
3753 echo->echo_count);
3754 }
3755 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003756 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003757 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003758 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003759 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003760 case ISN_ECHOMSG:
3761 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003762 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003763 break;
3764 case ISN_ECHOERR:
3765 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003766 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003767 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003769 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003770 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003771 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003772 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003773 + STACK_FRAME_SIZE));
3774 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003775 smsg("%4d LOAD $%lld", current,
3776 (varnumber_T)(iptr->isn_arg.number));
3777 }
3778 break;
3779 case ISN_LOADOUTER:
3780 {
3781 if (iptr->isn_arg.number < 0)
3782 smsg("%4d LOADOUTER level %d arg[%d]", current,
3783 iptr->isn_arg.outer.outer_depth,
3784 iptr->isn_arg.outer.outer_idx
3785 + STACK_FRAME_SIZE);
3786 else
3787 smsg("%4d LOADOUTER level %d $%d", current,
3788 iptr->isn_arg.outer.outer_depth,
3789 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003790 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 break;
3792 case ISN_LOADV:
3793 smsg("%4d LOADV v:%s", current,
3794 get_vim_var_name(iptr->isn_arg.number));
3795 break;
3796 case ISN_LOADSCRIPT:
3797 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003798 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3799 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003800 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003801 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003803 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3804 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003805 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003806 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 }
3808 break;
3809 case ISN_LOADS:
3810 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003811 scriptitem_T *si = SCRIPT_ITEM(
3812 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813
3814 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003815 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003816 }
3817 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003818 case ISN_LOADAUTO:
3819 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3820 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003821 case ISN_LOADG:
3822 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3823 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003824 case ISN_LOADB:
3825 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3826 break;
3827 case ISN_LOADW:
3828 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3829 break;
3830 case ISN_LOADT:
3831 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3832 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003833 case ISN_LOADGDICT:
3834 smsg("%4d LOAD g:", current);
3835 break;
3836 case ISN_LOADBDICT:
3837 smsg("%4d LOAD b:", current);
3838 break;
3839 case ISN_LOADWDICT:
3840 smsg("%4d LOAD w:", current);
3841 break;
3842 case ISN_LOADTDICT:
3843 smsg("%4d LOAD t:", current);
3844 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845 case ISN_LOADOPT:
3846 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3847 break;
3848 case ISN_LOADENV:
3849 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3850 break;
3851 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003852 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003853 break;
3854
3855 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003856 if (iptr->isn_arg.number < 0)
3857 smsg("%4d STORE arg[%lld]", current,
3858 iptr->isn_arg.number + STACK_FRAME_SIZE);
3859 else
3860 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3861 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003862 case ISN_STOREOUTER:
3863 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003864 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003865 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3866 iptr->isn_arg.outer.outer_depth,
3867 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003868 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003869 smsg("%4d STOREOUTER level %d $%d", current,
3870 iptr->isn_arg.outer.outer_depth,
3871 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003872 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003874 case ISN_STOREV:
3875 smsg("%4d STOREV v:%s", current,
3876 get_vim_var_name(iptr->isn_arg.number));
3877 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003878 case ISN_STOREAUTO:
3879 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3880 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003881 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003882 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3883 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003884 case ISN_STOREB:
3885 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3886 break;
3887 case ISN_STOREW:
3888 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3889 break;
3890 case ISN_STORET:
3891 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3892 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003893 case ISN_STORES:
3894 {
3895 scriptitem_T *si = SCRIPT_ITEM(
3896 iptr->isn_arg.loadstore.ls_sid);
3897
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003898 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003899 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 break;
3902 case ISN_STORESCRIPT:
3903 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003904 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3905 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003907 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003909 smsg("%4d STORESCRIPT %s-%d in %s", current,
3910 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003911 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003912 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913 }
3914 break;
3915 case ISN_STOREOPT:
3916 smsg("%4d STOREOPT &%s", current,
3917 iptr->isn_arg.storeopt.so_name);
3918 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003919 case ISN_STOREENV:
3920 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3921 break;
3922 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003923 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003924 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 case ISN_STORENR:
3926 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003927 iptr->isn_arg.storenr.stnr_val,
3928 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 break;
3930
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003931 case ISN_STOREINDEX:
3932 switch (iptr->isn_arg.vartype)
3933 {
3934 case VAR_LIST:
3935 smsg("%4d STORELIST", current);
3936 break;
3937 case VAR_DICT:
3938 smsg("%4d STOREDICT", current);
3939 break;
3940 case VAR_ANY:
3941 smsg("%4d STOREINDEX", current);
3942 break;
3943 default: break;
3944 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003945 break;
3946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 // constants
3948 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003949 smsg("%4d PUSHNR %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_PUSHBOOL:
3953 case ISN_PUSHSPEC:
3954 smsg("%4d PUSH %s", current,
3955 get_var_special_name(iptr->isn_arg.number));
3956 break;
3957 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003958#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003959 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003960#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 break;
3962 case ISN_PUSHS:
3963 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3964 break;
3965 case ISN_PUSHBLOB:
3966 {
3967 char_u *r;
3968 char_u numbuf[NUMBUFLEN];
3969 char_u *tofree;
3970
3971 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003972 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 vim_free(tofree);
3974 }
3975 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003976 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003977 {
3978 char *name = (char *)iptr->isn_arg.string;
3979
3980 smsg("%4d PUSHFUNC \"%s\"", current,
3981 name == NULL ? "[none]" : name);
3982 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003983 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003984 case ISN_PUSHCHANNEL:
3985#ifdef FEAT_JOB_CHANNEL
3986 {
3987 channel_T *channel = iptr->isn_arg.channel;
3988
3989 smsg("%4d PUSHCHANNEL %d", current,
3990 channel == NULL ? 0 : channel->ch_id);
3991 }
3992#endif
3993 break;
3994 case ISN_PUSHJOB:
3995#ifdef FEAT_JOB_CHANNEL
3996 {
3997 typval_T tv;
3998 char_u *name;
3999
4000 tv.v_type = VAR_JOB;
4001 tv.vval.v_job = iptr->isn_arg.job;
4002 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004003 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004004 }
4005#endif
4006 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 case ISN_PUSHEXC:
4008 smsg("%4d PUSH v:exception", current);
4009 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004010 case ISN_UNLET:
4011 smsg("%4d UNLET%s %s", current,
4012 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4013 iptr->isn_arg.unlet.ul_name);
4014 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004015 case ISN_UNLETENV:
4016 smsg("%4d UNLETENV%s $%s", current,
4017 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4018 iptr->isn_arg.unlet.ul_name);
4019 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004020 case ISN_UNLETINDEX:
4021 smsg("%4d UNLETINDEX", current);
4022 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004023 case ISN_LOCKCONST:
4024 smsg("%4d LOCKCONST", current);
4025 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004026 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004027 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004028 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004029 break;
4030 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004031 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004032 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 break;
4034
4035 // function call
4036 case ISN_BCALL:
4037 {
4038 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4039
4040 smsg("%4d BCALL %s(argc %d)", current,
4041 internal_func_name(cbfunc->cbf_idx),
4042 cbfunc->cbf_argcount);
4043 }
4044 break;
4045 case ISN_DCALL:
4046 {
4047 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4048 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4049 + cdfunc->cdf_idx;
4050
4051 smsg("%4d DCALL %s(argc %d)", current,
4052 df->df_ufunc->uf_name_exp != NULL
4053 ? df->df_ufunc->uf_name_exp
4054 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4055 }
4056 break;
4057 case ISN_UCALL:
4058 {
4059 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4060
4061 smsg("%4d UCALL %s(argc %d)", current,
4062 cufunc->cuf_name, cufunc->cuf_argcount);
4063 }
4064 break;
4065 case ISN_PCALL:
4066 {
4067 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4068
4069 smsg("%4d PCALL%s (argc %d)", current,
4070 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4071 }
4072 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004073 case ISN_PCALL_END:
4074 smsg("%4d PCALL end", current);
4075 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 case ISN_RETURN:
4077 smsg("%4d RETURN", current);
4078 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004079 case ISN_RETURN_ZERO:
4080 smsg("%4d RETURN 0", current);
4081 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 case ISN_FUNCREF:
4083 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004084 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004086 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004087
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004088 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 }
4090 break;
4091
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004092 case ISN_NEWFUNC:
4093 {
4094 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4095
4096 smsg("%4d NEWFUNC %s %s", current,
4097 newfunc->nf_lambda, newfunc->nf_global);
4098 }
4099 break;
4100
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004101 case ISN_DEF:
4102 {
4103 char_u *name = iptr->isn_arg.string;
4104
4105 smsg("%4d DEF %s", current,
4106 name == NULL ? (char_u *)"" : name);
4107 }
4108 break;
4109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 case ISN_JUMP:
4111 {
4112 char *when = "?";
4113
4114 switch (iptr->isn_arg.jump.jump_when)
4115 {
4116 case JUMP_ALWAYS:
4117 when = "JUMP";
4118 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 case JUMP_AND_KEEP_IF_TRUE:
4120 when = "JUMP_AND_KEEP_IF_TRUE";
4121 break;
4122 case JUMP_IF_FALSE:
4123 when = "JUMP_IF_FALSE";
4124 break;
4125 case JUMP_AND_KEEP_IF_FALSE:
4126 when = "JUMP_AND_KEEP_IF_FALSE";
4127 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004128 case JUMP_IF_COND_FALSE:
4129 when = "JUMP_IF_COND_FALSE";
4130 break;
4131 case JUMP_IF_COND_TRUE:
4132 when = "JUMP_IF_COND_TRUE";
4133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004135 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004136 iptr->isn_arg.jump.jump_where);
4137 }
4138 break;
4139
4140 case ISN_FOR:
4141 {
4142 forloop_T *forloop = &iptr->isn_arg.forloop;
4143
4144 smsg("%4d FOR $%d -> %d", current,
4145 forloop->for_idx, forloop->for_end);
4146 }
4147 break;
4148
4149 case ISN_TRY:
4150 {
4151 try_T *try = &iptr->isn_arg.try;
4152
4153 smsg("%4d TRY catch -> %d, finally -> %d", current,
4154 try->try_catch, try->try_finally);
4155 }
4156 break;
4157 case ISN_CATCH:
4158 // TODO
4159 smsg("%4d CATCH", current);
4160 break;
4161 case ISN_ENDTRY:
4162 smsg("%4d ENDTRY", current);
4163 break;
4164 case ISN_THROW:
4165 smsg("%4d THROW", current);
4166 break;
4167
4168 // expression operations on number
4169 case ISN_OPNR:
4170 case ISN_OPFLOAT:
4171 case ISN_OPANY:
4172 {
4173 char *what;
4174 char *ins;
4175
4176 switch (iptr->isn_arg.op.op_type)
4177 {
4178 case EXPR_MULT: what = "*"; break;
4179 case EXPR_DIV: what = "/"; break;
4180 case EXPR_REM: what = "%"; break;
4181 case EXPR_SUB: what = "-"; break;
4182 case EXPR_ADD: what = "+"; break;
4183 default: what = "???"; break;
4184 }
4185 switch (iptr->isn_type)
4186 {
4187 case ISN_OPNR: ins = "OPNR"; break;
4188 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4189 case ISN_OPANY: ins = "OPANY"; break;
4190 default: ins = "???"; break;
4191 }
4192 smsg("%4d %s %s", current, ins, what);
4193 }
4194 break;
4195
4196 case ISN_COMPAREBOOL:
4197 case ISN_COMPARESPECIAL:
4198 case ISN_COMPARENR:
4199 case ISN_COMPAREFLOAT:
4200 case ISN_COMPARESTRING:
4201 case ISN_COMPAREBLOB:
4202 case ISN_COMPARELIST:
4203 case ISN_COMPAREDICT:
4204 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 case ISN_COMPAREANY:
4206 {
4207 char *p;
4208 char buf[10];
4209 char *type;
4210
4211 switch (iptr->isn_arg.op.op_type)
4212 {
4213 case EXPR_EQUAL: p = "=="; break;
4214 case EXPR_NEQUAL: p = "!="; break;
4215 case EXPR_GREATER: p = ">"; break;
4216 case EXPR_GEQUAL: p = ">="; break;
4217 case EXPR_SMALLER: p = "<"; break;
4218 case EXPR_SEQUAL: p = "<="; break;
4219 case EXPR_MATCH: p = "=~"; break;
4220 case EXPR_IS: p = "is"; break;
4221 case EXPR_ISNOT: p = "isnot"; break;
4222 case EXPR_NOMATCH: p = "!~"; break;
4223 default: p = "???"; break;
4224 }
4225 STRCPY(buf, p);
4226 if (iptr->isn_arg.op.op_ic == TRUE)
4227 strcat(buf, "?");
4228 switch(iptr->isn_type)
4229 {
4230 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4231 case ISN_COMPARESPECIAL:
4232 type = "COMPARESPECIAL"; break;
4233 case ISN_COMPARENR: type = "COMPARENR"; break;
4234 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4235 case ISN_COMPARESTRING:
4236 type = "COMPARESTRING"; break;
4237 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4238 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4239 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4240 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4242 default: type = "???"; break;
4243 }
4244
4245 smsg("%4d %s %s", current, type, buf);
4246 }
4247 break;
4248
4249 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4250 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4251
4252 // expression operations
4253 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004254 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004255 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004256 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004257 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004258 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004259 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004260 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4261 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004262 case ISN_SLICE: smsg("%4d SLICE %lld",
4263 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004264 case ISN_GETITEM: smsg("%4d ITEM %lld",
4265 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004266 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4267 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 iptr->isn_arg.string); break;
4269 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4270
4271 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004272 case ISN_CHECKTYPE:
4273 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004274 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004275 char *tofree;
4276
Bram Moolenaare32e5162021-01-21 20:21:29 +01004277 if (ct->ct_arg_idx == 0)
4278 smsg("%4d CHECKTYPE %s stack[%d]", current,
4279 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004280 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004281 else
4282 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4283 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004284 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004285 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004286 vim_free(tofree);
4287 break;
4288 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004289 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4290 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4291 iptr->isn_arg.checklen.cl_min_len);
4292 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004293 case ISN_SETTYPE:
4294 {
4295 char *tofree;
4296
4297 smsg("%4d SETTYPE %s", current,
4298 type_name(iptr->isn_arg.type.ct_type, &tofree));
4299 vim_free(tofree);
4300 break;
4301 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004302 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004303 case ISN_2BOOL: if (iptr->isn_arg.number)
4304 smsg("%4d INVERT (!val)", current);
4305 else
4306 smsg("%4d 2BOOL (!!val)", current);
4307 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004308 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004309 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004310 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004311 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004312 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004313 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004314 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4315 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004316 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004317 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4318 smsg("%4d PUT %c above range",
4319 current, iptr->isn_arg.put.put_regname);
4320 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4321 smsg("%4d PUT %c range",
4322 current, iptr->isn_arg.put.put_regname);
4323 else
4324 smsg("%4d PUT %c %ld", current,
4325 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004326 (long)iptr->isn_arg.put.put_lnum);
4327 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328
Bram Moolenaar02194d22020-10-24 23:08:38 +02004329 // TODO: summarize modifiers
4330 case ISN_CMDMOD:
4331 {
4332 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004333 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004334 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4335
4336 buf = alloc(len + 1);
4337 if (buf != NULL)
4338 {
4339 (void)produce_cmdmods(
4340 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4341 smsg("%4d CMDMOD %s", current, buf);
4342 vim_free(buf);
4343 }
4344 break;
4345 }
4346 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004347
Bram Moolenaarb2049902021-01-24 12:53:53 +01004348 case ISN_PROF_START:
4349 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4350 break;
4351
4352 case ISN_PROF_END:
4353 smsg("%4d PROFILE END", current);
4354 break;
4355
Bram Moolenaar792f7862020-11-23 08:31:18 +01004356 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4357 iptr->isn_arg.unpack.unp_count,
4358 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4359 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004360 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4361 iptr->isn_arg.shuffle.shfl_item,
4362 iptr->isn_arg.shuffle.shfl_up);
4363 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 case ISN_DROP: smsg("%4d DROP", current); break;
4365 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004366
4367 out_flush(); // output one line at a time
4368 ui_breakcheck();
4369 if (got_int)
4370 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004372}
4373
4374/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004375 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 * list, etc. Mostly like what JavaScript does, except that empty list and
4377 * empty dictionary are FALSE.
4378 */
4379 int
4380tv2bool(typval_T *tv)
4381{
4382 switch (tv->v_type)
4383 {
4384 case VAR_NUMBER:
4385 return tv->vval.v_number != 0;
4386 case VAR_FLOAT:
4387#ifdef FEAT_FLOAT
4388 return tv->vval.v_float != 0.0;
4389#else
4390 break;
4391#endif
4392 case VAR_PARTIAL:
4393 return tv->vval.v_partial != NULL;
4394 case VAR_FUNC:
4395 case VAR_STRING:
4396 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4397 case VAR_LIST:
4398 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4399 case VAR_DICT:
4400 return tv->vval.v_dict != NULL
4401 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4402 case VAR_BOOL:
4403 case VAR_SPECIAL:
4404 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4405 case VAR_JOB:
4406#ifdef FEAT_JOB_CHANNEL
4407 return tv->vval.v_job != NULL;
4408#else
4409 break;
4410#endif
4411 case VAR_CHANNEL:
4412#ifdef FEAT_JOB_CHANNEL
4413 return tv->vval.v_channel != NULL;
4414#else
4415 break;
4416#endif
4417 case VAR_BLOB:
4418 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4419 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004420 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004421 case VAR_VOID:
4422 break;
4423 }
4424 return FALSE;
4425}
4426
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004427 void
4428emsg_using_string_as(typval_T *tv, int as_number)
4429{
4430 semsg(_(as_number ? e_using_string_as_number_str
4431 : e_using_string_as_bool_str),
4432 tv->vval.v_string == NULL
4433 ? (char_u *)"" : tv->vval.v_string);
4434}
4435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004436/*
4437 * If "tv" is a string give an error and return FAIL.
4438 */
4439 int
4440check_not_string(typval_T *tv)
4441{
4442 if (tv->v_type == VAR_STRING)
4443 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004444 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004445 clear_tv(tv);
4446 return FAIL;
4447 }
4448 return OK;
4449}
4450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004451
4452#endif // FEAT_EVAL