blob: 4c144f67aee3024328887c50c96a05a62ce08b28 [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 Moolenaarf002a412021-01-24 13:34:18 +0100648#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100649 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100650#else
651# define profiling FALSE
652#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653
Bram Moolenaarb2049902021-01-24 12:53:53 +0100654 if (func_needs_compiling(ufunc, profiling)
655 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200656 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200657 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100658 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100659 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100660 if (error != FCERR_UNKNOWN)
661 {
662 if (error == FCERR_TOOMANY)
663 semsg(_(e_toomanyarg), ufunc->uf_name);
664 else
665 semsg(_(e_toofewarg), ufunc->uf_name);
666 return FAIL;
667 }
668
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 // The function has been compiled, can call it quickly. For a function
670 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100671 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100672 if (iptr != NULL)
673 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100674 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100675 iptr->isn_type = ISN_DCALL;
676 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
677 iptr->isn_arg.dfunc.cdf_argcount = argcount;
678 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100679 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681
682 if (call_prepare(argcount, argvars, ectx) == FAIL)
683 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200684 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 funcexe.evaluate = TRUE;
686
687 // Call the user function. Result goes in last position on the stack.
688 // TODO: add selfdict if there is one
689 error = call_user_func_check(ufunc, argcount, argvars,
690 STACK_TV_BOT(-1), &funcexe, NULL);
691
692 // Clear the arguments.
693 for (idx = 0; idx < argcount; ++idx)
694 clear_tv(&argvars[idx]);
695
696 if (error != FCERR_NONE)
697 {
698 user_func_error(error, ufunc->uf_name);
699 return FAIL;
700 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100701 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200702 // Error other than from calling the function itself.
703 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 return OK;
705}
706
707/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200708 * Return TRUE if an error was given or CTRL-C was pressed.
709 */
710 static int
711vim9_aborting(int prev_called_emsg)
712{
713 return called_emsg > prev_called_emsg || got_int || did_throw;
714}
715
716/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 * Execute a function by "name".
718 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100719 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 * Returns FAIL if not found without an error message.
721 */
722 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724{
725 ufunc_T *ufunc;
726
727 if (builtin_function(name, -1))
728 {
729 int func_idx = find_internal_func(name);
730
731 if (func_idx < 0)
732 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200733 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 return FAIL;
735 return call_bfunc(func_idx, argcount, ectx);
736 }
737
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200738 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200739
740 if (ufunc == NULL)
741 {
742 int called_emsg_before = called_emsg;
743
744 if (script_autoload(name, TRUE))
745 // loaded a package, search for the function again
746 ufunc = find_func(name, FALSE, NULL);
747 if (vim9_aborting(called_emsg_before))
748 return FAIL; // bail out if loading the script caused an error
749 }
750
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100751 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100752 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753
754 return FAIL;
755}
756
757 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200758call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200760 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200761 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100763 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
765 if (tv->v_type == VAR_PARTIAL)
766 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200767 partial_T *pt = tv->vval.v_partial;
768 int i;
769
770 if (pt->pt_argc > 0)
771 {
772 // Make space for arguments from the partial, shift the "argcount"
773 // arguments up.
774 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
775 return FAIL;
776 for (i = 1; i <= argcount; ++i)
777 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
778 ectx->ec_stack.ga_len += pt->pt_argc;
779 argcount += pt->pt_argc;
780
781 // copy the arguments from the partial onto the stack
782 for (i = 0; i < pt->pt_argc; ++i)
783 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
784 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785
786 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100787 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200788
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100789 name = pt->pt_name;
790 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200791 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200793 if (name != NULL)
794 {
795 char_u fname_buf[FLEN_FIXED + 1];
796 char_u *tofree = NULL;
797 int error = FCERR_NONE;
798 char_u *fname;
799
800 // May need to translate <SNR>123_ to K_SNR.
801 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
802 if (error != FCERR_NONE)
803 res = FAIL;
804 else
805 res = call_by_name(fname, argcount, ectx, NULL);
806 vim_free(tofree);
807 }
808
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100809 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 {
811 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200812 semsg(_(e_unknownfunc),
813 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 return FAIL;
815 }
816 return OK;
817}
818
819/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200820 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
821 * TRUE.
822 */
823 static int
824error_if_locked(int lock, char *error)
825{
826 if (lock & (VAR_LOCKED | VAR_FIXED))
827 {
828 emsg(_(error));
829 return TRUE;
830 }
831 return FALSE;
832}
833
834/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100835 * Store "tv" in variable "name".
836 * This is for s: and g: variables.
837 */
838 static void
839store_var(char_u *name, typval_T *tv)
840{
841 funccal_entry_T entry;
842
843 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100844 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100845 restore_funccal();
846}
847
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100848/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100849 * Convert "tv" to a string.
850 * Return FAIL if not allowed.
851 */
852 static int
853do_2string(typval_T *tv, int is_2string_any)
854{
855 if (tv->v_type != VAR_STRING)
856 {
857 char_u *str;
858
859 if (is_2string_any)
860 {
861 switch (tv->v_type)
862 {
863 case VAR_SPECIAL:
864 case VAR_BOOL:
865 case VAR_NUMBER:
866 case VAR_FLOAT:
867 case VAR_BLOB: break;
868 default: to_string_error(tv->v_type);
869 return FAIL;
870 }
871 }
872 str = typval_tostring(tv);
873 clear_tv(tv);
874 tv->v_type = VAR_STRING;
875 tv->vval.v_string = str;
876 }
877 return OK;
878}
879
880/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100881 * When the value of "sv" is a null list of dict, allocate it.
882 */
883 static void
884allocate_if_null(typval_T *tv)
885{
886 switch (tv->v_type)
887 {
888 case VAR_LIST:
889 if (tv->vval.v_list == NULL)
890 rettv_list_alloc(tv);
891 break;
892 case VAR_DICT:
893 if (tv->vval.v_dict == NULL)
894 rettv_dict_alloc(tv);
895 break;
896 default:
897 break;
898 }
899}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200900
Bram Moolenaare7525c52021-01-09 13:20:37 +0100901/*
902 * Return the character "str[index]" where "index" is the character index. If
903 * "index" is out of range NULL is returned.
904 */
905 char_u *
906char_from_string(char_u *str, varnumber_T index)
907{
908 size_t nbyte = 0;
909 varnumber_T nchar = index;
910 size_t slen;
911
912 if (str == NULL)
913 return NULL;
914 slen = STRLEN(str);
915
916 // do the same as for a list: a negative index counts from the end
917 if (index < 0)
918 {
919 int clen = 0;
920
921 for (nbyte = 0; nbyte < slen; ++clen)
922 nbyte += MB_CPTR2LEN(str + nbyte);
923 nchar = clen + index;
924 if (nchar < 0)
925 // unlike list: index out of range results in empty string
926 return NULL;
927 }
928
929 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
930 nbyte += MB_CPTR2LEN(str + nbyte);
931 if (nbyte >= slen)
932 return NULL;
933 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
934}
935
936/*
937 * Get the byte index for character index "idx" in string "str" with length
938 * "str_len".
939 * If going over the end return "str_len".
940 * If "idx" is negative count from the end, -1 is the last character.
941 * When going over the start return -1.
942 */
943 static long
944char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
945{
946 varnumber_T nchar = idx;
947 size_t nbyte = 0;
948
949 if (nchar >= 0)
950 {
951 while (nchar > 0 && nbyte < str_len)
952 {
953 nbyte += MB_CPTR2LEN(str + nbyte);
954 --nchar;
955 }
956 }
957 else
958 {
959 nbyte = str_len;
960 while (nchar < 0 && nbyte > 0)
961 {
962 --nbyte;
963 nbyte -= mb_head_off(str, str + nbyte);
964 ++nchar;
965 }
966 if (nchar < 0)
967 return -1;
968 }
969 return (long)nbyte;
970}
971
972/*
973 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100974 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100975 * Return NULL when the result is empty.
976 */
977 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100978string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100979{
980 long start_byte, end_byte;
981 size_t slen;
982
983 if (str == NULL)
984 return NULL;
985 slen = STRLEN(str);
986 start_byte = char_idx2byte(str, slen, first);
987 if (start_byte < 0)
988 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100989 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100990 end_byte = (long)slen;
991 else
992 {
993 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100994 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100995 // end index is inclusive
996 end_byte += MB_CPTR2LEN(str + end_byte);
997 }
998
999 if (start_byte >= (long)slen || end_byte <= start_byte)
1000 return NULL;
1001 return vim_strnsave(str + start_byte, end_byte - start_byte);
1002}
1003
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001004 static svar_T *
1005get_script_svar(scriptref_T *sref, ectx_T *ectx)
1006{
1007 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1008 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1009 + ectx->ec_dfunc_idx;
1010 svar_T *sv;
1011
1012 if (sref->sref_seq != si->sn_script_seq)
1013 {
1014 // The script was reloaded after the function was
1015 // compiled, the script_idx may not be valid.
1016 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1017 dfunc->df_ufunc->uf_name_exp);
1018 return NULL;
1019 }
1020 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1021 if (!equal_type(sv->sv_type, sref->sref_type))
1022 {
1023 emsg(_(e_script_variable_type_changed));
1024 return NULL;
1025 }
1026 return sv;
1027}
1028
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001029/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 * Execute a function by "name".
1031 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001032 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 */
1034 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001035call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036{
Bram Moolenaared677f52020-08-12 16:38:10 +02001037 int called_emsg_before = called_emsg;
1038 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039
Bram Moolenaared677f52020-08-12 16:38:10 +02001040 res = call_by_name(name, argcount, ectx, iptr);
1041 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001042 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001043 dictitem_T *v;
1044
1045 v = find_var(name, NULL, FALSE);
1046 if (v == NULL)
1047 {
1048 semsg(_(e_unknownfunc), name);
1049 return FAIL;
1050 }
1051 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1052 {
1053 semsg(_(e_unknownfunc), name);
1054 return FAIL;
1055 }
1056 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001058 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059}
1060
1061/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001062 * When a function reference is used, fill a partial with the information
1063 * needed, especially when it is used as a closure.
1064 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001065 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001066fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1067{
1068 pt->pt_func = ufunc;
1069 pt->pt_refcount = 1;
1070
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001071 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001072 {
1073 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1074 + ectx->ec_dfunc_idx;
1075
1076 // The closure needs to find arguments and local
1077 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001078 pt->pt_outer.out_stack = &ectx->ec_stack;
1079 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1080 pt->pt_outer.out_up = ectx->ec_outer;
1081 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001082
1083 // If this function returns and the closure is still
1084 // being used, we need to make a copy of the context
1085 // (arguments and local variables). Store a reference
1086 // to the partial so we can handle that.
1087 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1088 {
1089 vim_free(pt);
1090 return FAIL;
1091 }
1092 // Extra variable keeps the count of closures created
1093 // in the current function call.
1094 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1095 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1096
1097 ((partial_T **)ectx->ec_funcrefs.ga_data)
1098 [ectx->ec_funcrefs.ga_len] = pt;
1099 ++pt->pt_refcount;
1100 ++ectx->ec_funcrefs.ga_len;
1101 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001102 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001103 return OK;
1104}
1105
1106/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 * Call a "def" function from old Vim script.
1108 * Return OK or FAIL.
1109 */
1110 int
1111call_def_function(
1112 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001113 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001115 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 typval_T *rettv) // return value
1117{
1118 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001119 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001120 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 typval_T *tv;
1122 int idx;
1123 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001124 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001125 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001126 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001127 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001128 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001129 msglist_T **saved_msg_list = NULL;
1130 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001131 cmdmod_T save_cmdmod;
1132 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001133 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001134 int save_emsg_silent_def = emsg_silent_def;
1135 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001136 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001137 int orig_funcdepth;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001138#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001139 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001140#else
1141# define profiling FALSE
1142#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143
1144// Get pointer to item in the stack.
1145#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1146
1147// Get pointer to item at the bottom of the stack, -1 is the bottom.
1148#undef STACK_TV_BOT
1149#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1150
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001151// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001152#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 +01001153
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001154 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaarb2049902021-01-24 12:53:53 +01001155 || (func_needs_compiling(ufunc, profiling)
1156 && compile_def_function(ufunc, FALSE, profiling, NULL)
1157 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001158 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001159 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001160 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001161 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001162 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001163 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001164
Bram Moolenaar09689a02020-05-09 22:50:08 +02001165 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001166 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001167 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1168 + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001169 if ((
1170#ifdef FEAT_PROFILE
1171 profiling ? dfunc->df_instr_prof :
1172#endif
1173 dfunc->df_instr) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001174 {
1175 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001176 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001177 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001178 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001180 // If depth of calling is getting too high, don't execute the function.
1181 orig_funcdepth = funcdepth_get();
1182 if (funcdepth_increment() == FAIL)
1183 return FAIL;
1184
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001185 CLEAR_FIELD(ectx);
1186 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1187 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1188 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001189 {
1190 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001191 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001194 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001196 // Put arguments on the stack, but no more than what the function expects.
1197 // A lambda can be called with more arguments than it uses.
1198 for (idx = 0; idx < argc
1199 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1200 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001202 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001203 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1204 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001205 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 copy_tv(&argv[idx], STACK_TV_BOT(0));
1207 ++ectx.ec_stack.ga_len;
1208 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001209
1210 // Turn varargs into a list. Empty list if no args.
1211 if (ufunc->uf_va_name != NULL)
1212 {
1213 int vararg_count = argc - ufunc->uf_args.ga_len;
1214
1215 if (vararg_count < 0)
1216 vararg_count = 0;
1217 else
1218 argc -= vararg_count;
1219 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001220 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001221
1222 // Check the type of the list items.
1223 tv = STACK_TV_BOT(-1);
1224 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001225 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001226 && ufunc->uf_va_type->tt_member != &t_any
1227 && tv->vval.v_list != NULL)
1228 {
1229 type_T *expected = ufunc->uf_va_type->tt_member;
1230 listitem_T *li = tv->vval.v_list->lv_first;
1231
1232 for (idx = 0; idx < vararg_count; ++idx)
1233 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001234 if (check_typval_type(expected, &li->li_tv,
1235 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001236 goto failed_early;
1237 li = li->li_next;
1238 }
1239 }
1240
Bram Moolenaar23e03252020-04-12 22:22:31 +02001241 if (defcount > 0)
1242 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001243 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001244 --ectx.ec_stack.ga_len;
1245 }
1246
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001247 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001248 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001249 if (defcount > 0)
1250 for (idx = 0; idx < defcount; ++idx)
1251 {
1252 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1253 ++ectx.ec_stack.ga_len;
1254 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001255 if (ufunc->uf_va_name != NULL)
1256 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001259 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1260 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001262 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001263 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1264 + ufunc->uf_dfunc_idx;
1265 ufunc_T *base_ufunc = dfunc->df_ufunc;
1266
1267 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1268 // by copy_func().
1269 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001270 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001271 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1272 if (ectx.ec_outer == NULL)
1273 goto failed_early;
1274 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001275 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001276 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1277 {
1278 if (current_ectx->ec_outer != NULL)
1279 *ectx.ec_outer = *current_ectx->ec_outer;
1280 }
1281 else
1282 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001283 }
1284 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001285 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1286 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001287 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001288 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 // dummy frame entries
1291 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1292 {
1293 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1294 ++ectx.ec_stack.ga_len;
1295 }
1296
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001297 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001298 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001299 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1300 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001301
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001302 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001303 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001304 ectx.ec_stack.ga_len += dfunc->df_varcount;
1305 if (dfunc->df_has_closure)
1306 {
1307 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1308 STACK_TV_VAR(idx)->vval.v_number = 0;
1309 ++ectx.ec_stack.ga_len;
1310 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001311
Bram Moolenaarf002a412021-01-24 13:34:18 +01001312#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01001313 ectx.ec_instr = profiling ? dfunc->df_instr_prof : dfunc->df_instr;
Bram Moolenaarf002a412021-01-24 13:34:18 +01001314#else
1315 ectx.ec_instr = dfunc->df_instr;
1316#endif
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001317 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001318
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001319 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001320 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001321 estack_push_ufunc(ufunc, 1);
1322 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001323 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1324
Bram Moolenaar352134b2020-10-17 22:04:08 +02001325 // Use a specific location for storing error messages to be converted to an
1326 // exception.
1327 saved_msg_list = msg_list;
1328 msg_list = &private_msg_list;
1329
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001330 // Do turn errors into exceptions.
1331 suppress_errthrow = FALSE;
1332
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001333 // When ":silent!" was used before calling then we still abort the
1334 // function. If ":silent!" is used in the function then we don't.
1335 emsg_silent_def = emsg_silent;
1336 did_emsg_def = 0;
1337
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001338 // Decide where to start execution, handles optional arguments.
1339 init_instr_idx(ufunc, argc, &ectx);
1340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 for (;;)
1342 {
1343 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001344
Bram Moolenaar270d0382020-05-15 21:42:53 +02001345 if (++breakcheck_count >= 100)
1346 {
1347 line_breakcheck();
1348 breakcheck_count = 0;
1349 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001350 if (got_int)
1351 {
1352 // Turn CTRL-C into an exception.
1353 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001354 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001355 goto failed;
1356 did_throw = TRUE;
1357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358
Bram Moolenaara26b9702020-04-18 19:53:28 +02001359 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1360 {
1361 // Turn an error message into an exception.
1362 did_emsg = FALSE;
1363 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1364 goto failed;
1365 did_throw = TRUE;
1366 *msg_list = NULL;
1367 }
1368
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 if (did_throw && !ectx.ec_in_catch)
1370 {
1371 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001372 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373
1374 // An exception jumps to the first catch, finally, or returns from
1375 // the current function.
1376 if (trystack->ga_len > 0)
1377 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001378 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 {
1380 // jump to ":catch" or ":finally"
1381 ectx.ec_in_catch = TRUE;
1382 ectx.ec_iidx = trycmd->tcd_catch_idx;
1383 }
1384 else
1385 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001386 // Not inside try or need to return from current functions.
1387 // Push a dummy return value.
1388 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1389 goto failed;
1390 tv = STACK_TV_BOT(0);
1391 tv->v_type = VAR_NUMBER;
1392 tv->vval.v_number = 0;
1393 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001394 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001396 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001397 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001398 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1399 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 goto done;
1401 }
1402
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001403 if (func_return(&ectx) == FAIL)
1404 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 }
1406 continue;
1407 }
1408
1409 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1410 switch (iptr->isn_type)
1411 {
1412 // execute Ex command line
1413 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001414 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001415 source_cookie_T cookie;
1416
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001417 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001418 // Pass getsourceline to get an error for a missing ":end"
1419 // command.
1420 CLEAR_FIELD(cookie);
1421 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1422 if (do_cmdline(iptr->isn_arg.string,
1423 getsourceline, &cookie,
1424 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1425 == FAIL
1426 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001427 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 break;
1430
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001431 // execute Ex command from pieces on the stack
1432 case ISN_EXECCONCAT:
1433 {
1434 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001435 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001436 int pass;
1437 int i;
1438 char_u *cmd = NULL;
1439 char_u *str;
1440
1441 for (pass = 1; pass <= 2; ++pass)
1442 {
1443 for (i = 0; i < count; ++i)
1444 {
1445 tv = STACK_TV_BOT(i - count);
1446 str = tv->vval.v_string;
1447 if (str != NULL && *str != NUL)
1448 {
1449 if (pass == 2)
1450 STRCPY(cmd + len, str);
1451 len += STRLEN(str);
1452 }
1453 if (pass == 2)
1454 clear_tv(tv);
1455 }
1456 if (pass == 1)
1457 {
1458 cmd = alloc(len + 1);
1459 if (cmd == NULL)
1460 goto failed;
1461 len = 0;
1462 }
1463 }
1464
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001465 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001466 do_cmdline_cmd(cmd);
1467 vim_free(cmd);
1468 }
1469 break;
1470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 // execute :echo {string} ...
1472 case ISN_ECHO:
1473 {
1474 int count = iptr->isn_arg.echo.echo_count;
1475 int atstart = TRUE;
1476 int needclr = TRUE;
1477
1478 for (idx = 0; idx < count; ++idx)
1479 {
1480 tv = STACK_TV_BOT(idx - count);
1481 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1482 &atstart, &needclr);
1483 clear_tv(tv);
1484 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001485 if (needclr)
1486 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 ectx.ec_stack.ga_len -= count;
1488 }
1489 break;
1490
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001491 // :execute {string} ...
1492 // :echomsg {string} ...
1493 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001494 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001495 case ISN_ECHOMSG:
1496 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001497 {
1498 int count = iptr->isn_arg.number;
1499 garray_T ga;
1500 char_u buf[NUMBUFLEN];
1501 char_u *p;
1502 int len;
1503 int failed = FALSE;
1504
1505 ga_init2(&ga, 1, 80);
1506 for (idx = 0; idx < count; ++idx)
1507 {
1508 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001509 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001510 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001511 if (tv->v_type == VAR_CHANNEL
1512 || tv->v_type == VAR_JOB)
1513 {
1514 SOURCING_LNUM = iptr->isn_lnum;
1515 emsg(_(e_inval_string));
1516 break;
1517 }
1518 else
1519 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001520 }
1521 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001522 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001523
1524 len = (int)STRLEN(p);
1525 if (ga_grow(&ga, len + 2) == FAIL)
1526 failed = TRUE;
1527 else
1528 {
1529 if (ga.ga_len > 0)
1530 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1531 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1532 ga.ga_len += len;
1533 }
1534 clear_tv(tv);
1535 }
1536 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001537 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001538 {
1539 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001540 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001541 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001542
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001543 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001544 {
1545 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001546 {
1547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001549 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001550 {
1551 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001552 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001553 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001554 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001555 else
1556 {
1557 msg_sb_eol();
1558 if (iptr->isn_type == ISN_ECHOMSG)
1559 {
1560 msg_attr(ga.ga_data, echo_attr);
1561 out_flush();
1562 }
1563 else
1564 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001565 SOURCING_LNUM = iptr->isn_lnum;
1566 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001567 }
1568 }
1569 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001570 ga_clear(&ga);
1571 }
1572 break;
1573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 // load local variable or argument
1575 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001576 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 goto failed;
1578 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1579 ++ectx.ec_stack.ga_len;
1580 break;
1581
1582 // load v: variable
1583 case ISN_LOADV:
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(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1587 ++ectx.ec_stack.ga_len;
1588 break;
1589
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001590 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 case ISN_LOADSCRIPT:
1592 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001593 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 svar_T *sv;
1595
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001596 sv = get_script_svar(sref, &ectx);
1597 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001598 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001599 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001600 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 goto failed;
1602 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1603 ++ectx.ec_stack.ga_len;
1604 }
1605 break;
1606
1607 // load s: variable in old script
1608 case ISN_LOADS:
1609 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001610 hashtab_T *ht = &SCRIPT_VARS(
1611 iptr->isn_arg.loadstore.ls_sid);
1612 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001614
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 if (di == NULL)
1616 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001617 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001618 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001619 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 }
1621 else
1622 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001623 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001624 goto failed;
1625 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1626 ++ectx.ec_stack.ga_len;
1627 }
1628 }
1629 break;
1630
Bram Moolenaard3aac292020-04-19 14:32:17 +02001631 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001633 case ISN_LOADB:
1634 case ISN_LOADW:
1635 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001637 dictitem_T *di = NULL;
1638 hashtab_T *ht = NULL;
1639 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001640
Bram Moolenaard3aac292020-04-19 14:32:17 +02001641 switch (iptr->isn_type)
1642 {
1643 case ISN_LOADG:
1644 ht = get_globvar_ht();
1645 namespace = 'g';
1646 break;
1647 case ISN_LOADB:
1648 ht = &curbuf->b_vars->dv_hashtab;
1649 namespace = 'b';
1650 break;
1651 case ISN_LOADW:
1652 ht = &curwin->w_vars->dv_hashtab;
1653 namespace = 'w';
1654 break;
1655 case ISN_LOADT:
1656 ht = &curtab->tp_vars->dv_hashtab;
1657 namespace = 't';
1658 break;
1659 default: // Cannot reach here
1660 goto failed;
1661 }
1662 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001663
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if (di == NULL)
1665 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001666 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001667 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001668 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001669 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 }
1671 else
1672 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001673 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 goto failed;
1675 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1676 ++ectx.ec_stack.ga_len;
1677 }
1678 }
1679 break;
1680
Bram Moolenaar03290b82020-12-19 16:30:44 +01001681 // load autoload variable
1682 case ISN_LOADAUTO:
1683 {
1684 char_u *name = iptr->isn_arg.string;
1685
1686 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1687 goto failed;
1688 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001689 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001690 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1691 goto on_error;
1692 ++ectx.ec_stack.ga_len;
1693 }
1694 break;
1695
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001696 // load g:/b:/w:/t: namespace
1697 case ISN_LOADGDICT:
1698 case ISN_LOADBDICT:
1699 case ISN_LOADWDICT:
1700 case ISN_LOADTDICT:
1701 {
1702 dict_T *d = NULL;
1703
1704 switch (iptr->isn_type)
1705 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001706 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1707 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1708 case ISN_LOADWDICT: d = curwin->w_vars; break;
1709 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001710 default: // Cannot reach here
1711 goto failed;
1712 }
1713 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1714 goto failed;
1715 tv = STACK_TV_BOT(0);
1716 tv->v_type = VAR_DICT;
1717 tv->v_lock = 0;
1718 tv->vval.v_dict = d;
1719 ++ectx.ec_stack.ga_len;
1720 }
1721 break;
1722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723 // load &option
1724 case ISN_LOADOPT:
1725 {
1726 typval_T optval;
1727 char_u *name = iptr->isn_arg.string;
1728
Bram Moolenaara8c17702020-04-01 21:17:24 +02001729 // This is not expected to fail, name is checked during
1730 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001731 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001732 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001733 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001734 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 *STACK_TV_BOT(0) = optval;
1736 ++ectx.ec_stack.ga_len;
1737 }
1738 break;
1739
1740 // load $ENV
1741 case ISN_LOADENV:
1742 {
1743 typval_T optval;
1744 char_u *name = iptr->isn_arg.string;
1745
Bram Moolenaar270d0382020-05-15 21:42:53 +02001746 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001748 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001749 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 *STACK_TV_BOT(0) = optval;
1751 ++ectx.ec_stack.ga_len;
1752 }
1753 break;
1754
1755 // load @register
1756 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001757 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 goto failed;
1759 tv = STACK_TV_BOT(0);
1760 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001761 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001762 // This may result in NULL, which should be equivalent to an
1763 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 tv->vval.v_string = get_reg_contents(
1765 iptr->isn_arg.number, GREG_EXPR_SRC);
1766 ++ectx.ec_stack.ga_len;
1767 break;
1768
1769 // store local variable
1770 case ISN_STORE:
1771 --ectx.ec_stack.ga_len;
1772 tv = STACK_TV_VAR(iptr->isn_arg.number);
1773 clear_tv(tv);
1774 *tv = *STACK_TV_BOT(0);
1775 break;
1776
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001777 // store s: variable in old script
1778 case ISN_STORES:
1779 {
1780 hashtab_T *ht = &SCRIPT_VARS(
1781 iptr->isn_arg.loadstore.ls_sid);
1782 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001783 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001784
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001785 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001786 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001787 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001788 else
1789 {
1790 clear_tv(&di->di_tv);
1791 di->di_tv = *STACK_TV_BOT(0);
1792 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001793 }
1794 break;
1795
1796 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 case ISN_STORESCRIPT:
1798 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001799 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1800 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001802 sv = get_script_svar(sref, &ectx);
1803 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001804 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 --ectx.ec_stack.ga_len;
1806 clear_tv(sv->sv_tv);
1807 *sv->sv_tv = *STACK_TV_BOT(0);
1808 }
1809 break;
1810
1811 // store option
1812 case ISN_STOREOPT:
1813 {
1814 long n = 0;
1815 char_u *s = NULL;
1816 char *msg;
1817
1818 --ectx.ec_stack.ga_len;
1819 tv = STACK_TV_BOT(0);
1820 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001821 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001823 if (s == NULL)
1824 s = (char_u *)"";
1825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001827 // must be VAR_NUMBER, CHECKTYPE makes sure
1828 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1830 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001831 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 if (msg != NULL)
1833 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001834 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001836 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 }
1839 break;
1840
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001841 // store $ENV
1842 case ISN_STOREENV:
1843 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001844 tv = STACK_TV_BOT(0);
1845 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1846 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001847 break;
1848
1849 // store @r
1850 case ISN_STOREREG:
1851 {
1852 int reg = iptr->isn_arg.number;
1853
1854 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001855 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001856 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001857 tv_get_string(tv), -1, FALSE);
1858 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001859 }
1860 break;
1861
1862 // store v: variable
1863 case ISN_STOREV:
1864 --ectx.ec_stack.ga_len;
1865 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1866 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001867 // should not happen, type is checked when compiling
1868 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001869 break;
1870
Bram Moolenaard3aac292020-04-19 14:32:17 +02001871 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001873 case ISN_STOREB:
1874 case ISN_STOREW:
1875 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001877 dictitem_T *di;
1878 hashtab_T *ht;
1879 char_u *name = iptr->isn_arg.string + 2;
1880
Bram Moolenaard3aac292020-04-19 14:32:17 +02001881 switch (iptr->isn_type)
1882 {
1883 case ISN_STOREG:
1884 ht = get_globvar_ht();
1885 break;
1886 case ISN_STOREB:
1887 ht = &curbuf->b_vars->dv_hashtab;
1888 break;
1889 case ISN_STOREW:
1890 ht = &curwin->w_vars->dv_hashtab;
1891 break;
1892 case ISN_STORET:
1893 ht = &curtab->tp_vars->dv_hashtab;
1894 break;
1895 default: // Cannot reach here
1896 goto failed;
1897 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898
1899 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001900 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001902 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 else
1904 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001905 SOURCING_LNUM = iptr->isn_lnum;
1906 if (var_check_permission(di, name) == FAIL)
1907 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 clear_tv(&di->di_tv);
1909 di->di_tv = *STACK_TV_BOT(0);
1910 }
1911 }
1912 break;
1913
Bram Moolenaar03290b82020-12-19 16:30:44 +01001914 // store an autoload variable
1915 case ISN_STOREAUTO:
1916 SOURCING_LNUM = iptr->isn_lnum;
1917 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1918 clear_tv(STACK_TV_BOT(-1));
1919 --ectx.ec_stack.ga_len;
1920 break;
1921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 // store number in local variable
1923 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001924 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 clear_tv(tv);
1926 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001927 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 break;
1929
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001930 // store value in list or dict variable
1931 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001932 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001933 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001934 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001935 typval_T *tv_dest = STACK_TV_BOT(-1);
1936 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001937
Bram Moolenaar752fc692021-01-04 21:57:11 +01001938 // Stack contains:
1939 // -3 value to be stored
1940 // -2 index
1941 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001942 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001943 SOURCING_LNUM = iptr->isn_lnum;
1944 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001945 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001946 dest_type = tv_dest->v_type;
1947 if (dest_type == VAR_DICT)
1948 status = do_2string(tv_idx, TRUE);
1949 else if (dest_type == VAR_LIST
1950 && tv_idx->v_type != VAR_NUMBER)
1951 {
1952 emsg(_(e_number_exp));
1953 status = FAIL;
1954 }
1955 }
1956 else if (dest_type != tv_dest->v_type)
1957 {
1958 // just in case, should be OK
1959 semsg(_(e_expected_str_but_got_str),
1960 vartype_name(dest_type),
1961 vartype_name(tv_dest->v_type));
1962 status = FAIL;
1963 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001964
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001965 if (status == OK && dest_type == VAR_LIST)
1966 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001967 long lidx = (long)tv_idx->vval.v_number;
1968 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001969
1970 if (list == NULL)
1971 {
1972 emsg(_(e_list_not_set));
1973 goto on_error;
1974 }
1975 if (lidx < 0 && list->lv_len + lidx >= 0)
1976 // negative index is relative to the end
1977 lidx = list->lv_len + lidx;
1978 if (lidx < 0 || lidx > list->lv_len)
1979 {
1980 semsg(_(e_listidx), lidx);
1981 goto on_error;
1982 }
1983 if (lidx < list->lv_len)
1984 {
1985 listitem_T *li = list_find(list, lidx);
1986
1987 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001988 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001989 goto on_error;
1990 // overwrite existing list item
1991 clear_tv(&li->li_tv);
1992 li->li_tv = *tv;
1993 }
1994 else
1995 {
1996 if (error_if_locked(list->lv_lock,
1997 e_cannot_change_list))
1998 goto on_error;
1999 // append to list, only fails when out of memory
2000 if (list_append_tv(list, tv) == FAIL)
2001 goto failed;
2002 clear_tv(tv);
2003 }
2004 }
2005 else if (status == OK && dest_type == VAR_DICT)
2006 {
2007 char_u *key = tv_idx->vval.v_string;
2008 dict_T *dict = tv_dest->vval.v_dict;
2009 dictitem_T *di;
2010
2011 SOURCING_LNUM = iptr->isn_lnum;
2012 if (dict == NULL)
2013 {
2014 emsg(_(e_dictionary_not_set));
2015 goto on_error;
2016 }
2017 if (key == NULL)
2018 key = (char_u *)"";
2019 di = dict_find(dict, key, -1);
2020 if (di != NULL)
2021 {
2022 if (error_if_locked(di->di_tv.v_lock,
2023 e_cannot_change_dict_item))
2024 goto on_error;
2025 // overwrite existing value
2026 clear_tv(&di->di_tv);
2027 di->di_tv = *tv;
2028 }
2029 else
2030 {
2031 if (error_if_locked(dict->dv_lock,
2032 e_cannot_change_dict))
2033 goto on_error;
2034 // add to dict, only fails when out of memory
2035 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2036 goto failed;
2037 clear_tv(tv);
2038 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002039 }
2040 else
2041 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002042 status = FAIL;
2043 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002044 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002045
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002046 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002047 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002048 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002049 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002050 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002051 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002052 goto on_error;
2053 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002054 }
2055 break;
2056
Bram Moolenaar0186e582021-01-10 18:33:11 +01002057 // load or store variable or argument from outer scope
2058 case ISN_LOADOUTER:
2059 case ISN_STOREOUTER:
2060 {
2061 int depth = iptr->isn_arg.outer.outer_depth;
2062 outer_T *outer = ectx.ec_outer;
2063
2064 while (depth > 1 && outer != NULL)
2065 {
2066 outer = outer->out_up;
2067 --depth;
2068 }
2069 if (outer == NULL)
2070 {
2071 SOURCING_LNUM = iptr->isn_lnum;
2072 iemsg("LOADOUTER depth more than scope levels");
2073 goto failed;
2074 }
2075 tv = ((typval_T *)outer->out_stack->ga_data)
2076 + outer->out_frame_idx + STACK_FRAME_SIZE
2077 + iptr->isn_arg.outer.outer_idx;
2078 if (iptr->isn_type == ISN_LOADOUTER)
2079 {
2080 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2081 goto failed;
2082 copy_tv(tv, STACK_TV_BOT(0));
2083 ++ectx.ec_stack.ga_len;
2084 }
2085 else
2086 {
2087 --ectx.ec_stack.ga_len;
2088 clear_tv(tv);
2089 *tv = *STACK_TV_BOT(0);
2090 }
2091 }
2092 break;
2093
Bram Moolenaar752fc692021-01-04 21:57:11 +01002094 // unlet item in list or dict variable
2095 case ISN_UNLETINDEX:
2096 {
2097 typval_T *tv_idx = STACK_TV_BOT(-2);
2098 typval_T *tv_dest = STACK_TV_BOT(-1);
2099 int status = OK;
2100
2101 // Stack contains:
2102 // -2 index
2103 // -1 dict or list
2104 if (tv_dest->v_type == VAR_DICT)
2105 {
2106 // unlet a dict item, index must be a string
2107 if (tv_idx->v_type != VAR_STRING)
2108 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002109 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002110 semsg(_(e_expected_str_but_got_str),
2111 vartype_name(VAR_STRING),
2112 vartype_name(tv_idx->v_type));
2113 status = FAIL;
2114 }
2115 else
2116 {
2117 dict_T *d = tv_dest->vval.v_dict;
2118 char_u *key = tv_idx->vval.v_string;
2119 dictitem_T *di = NULL;
2120
2121 if (key == NULL)
2122 key = (char_u *)"";
2123 if (d != NULL)
2124 di = dict_find(d, key, (int)STRLEN(key));
2125 if (di == NULL)
2126 {
2127 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002129 semsg(_(e_dictkey), key);
2130 status = FAIL;
2131 }
2132 else
2133 {
2134 // TODO: check for dict or item locked
2135 dictitem_remove(d, di);
2136 }
2137 }
2138 }
2139 else if (tv_dest->v_type == VAR_LIST)
2140 {
2141 // unlet a List item, index must be a number
2142 if (tv_idx->v_type != VAR_NUMBER)
2143 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002144 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002145 semsg(_(e_expected_str_but_got_str),
2146 vartype_name(VAR_NUMBER),
2147 vartype_name(tv_idx->v_type));
2148 status = FAIL;
2149 }
2150 else
2151 {
2152 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002153 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002154 listitem_T *li = NULL;
2155
2156 li = list_find(l, n);
2157 if (li == NULL)
2158 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002160 semsg(_(e_listidx), n);
2161 status = FAIL;
2162 }
2163 else
2164 // TODO: check for list or item locked
2165 listitem_remove(l, li);
2166 }
2167 }
2168 else
2169 {
2170 status = FAIL;
2171 semsg(_(e_cannot_index_str),
2172 vartype_name(tv_dest->v_type));
2173 }
2174
2175 clear_tv(tv_idx);
2176 clear_tv(tv_dest);
2177 ectx.ec_stack.ga_len -= 2;
2178 if (status == FAIL)
2179 goto on_error;
2180 }
2181 break;
2182
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 // push constant
2184 case ISN_PUSHNR:
2185 case ISN_PUSHBOOL:
2186 case ISN_PUSHSPEC:
2187 case ISN_PUSHF:
2188 case ISN_PUSHS:
2189 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002190 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002191 case ISN_PUSHCHANNEL:
2192 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002193 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 goto failed;
2195 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002196 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 ++ectx.ec_stack.ga_len;
2198 switch (iptr->isn_type)
2199 {
2200 case ISN_PUSHNR:
2201 tv->v_type = VAR_NUMBER;
2202 tv->vval.v_number = iptr->isn_arg.number;
2203 break;
2204 case ISN_PUSHBOOL:
2205 tv->v_type = VAR_BOOL;
2206 tv->vval.v_number = iptr->isn_arg.number;
2207 break;
2208 case ISN_PUSHSPEC:
2209 tv->v_type = VAR_SPECIAL;
2210 tv->vval.v_number = iptr->isn_arg.number;
2211 break;
2212#ifdef FEAT_FLOAT
2213 case ISN_PUSHF:
2214 tv->v_type = VAR_FLOAT;
2215 tv->vval.v_float = iptr->isn_arg.fnumber;
2216 break;
2217#endif
2218 case ISN_PUSHBLOB:
2219 blob_copy(iptr->isn_arg.blob, tv);
2220 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002221 case ISN_PUSHFUNC:
2222 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002223 if (iptr->isn_arg.string == NULL)
2224 tv->vval.v_string = NULL;
2225 else
2226 tv->vval.v_string =
2227 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002228 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002229 case ISN_PUSHCHANNEL:
2230#ifdef FEAT_JOB_CHANNEL
2231 tv->v_type = VAR_CHANNEL;
2232 tv->vval.v_channel = iptr->isn_arg.channel;
2233 if (tv->vval.v_channel != NULL)
2234 ++tv->vval.v_channel->ch_refcount;
2235#endif
2236 break;
2237 case ISN_PUSHJOB:
2238#ifdef FEAT_JOB_CHANNEL
2239 tv->v_type = VAR_JOB;
2240 tv->vval.v_job = iptr->isn_arg.job;
2241 if (tv->vval.v_job != NULL)
2242 ++tv->vval.v_job->jv_refcount;
2243#endif
2244 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 default:
2246 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002247 tv->vval.v_string = vim_strsave(
2248 iptr->isn_arg.string == NULL
2249 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 }
2251 break;
2252
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002253 case ISN_UNLET:
2254 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2255 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002256 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002257 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002258 case ISN_UNLETENV:
2259 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2260 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002261
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002262 case ISN_LOCKCONST:
2263 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2264 break;
2265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 // create a list from items on the stack; uses a single allocation
2267 // for the list header and the items
2268 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002269 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2270 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 break;
2272
2273 // create a dict from items on the stack
2274 case ISN_NEWDICT:
2275 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002276 int count = iptr->isn_arg.number;
2277 dict_T *dict = dict_alloc();
2278 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002279 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280
2281 if (dict == NULL)
2282 goto failed;
2283 for (idx = 0; idx < count; ++idx)
2284 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002285 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002287 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002288 key = tv->vval.v_string == NULL
2289 ? (char_u *)"" : tv->vval.v_string;
2290 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002291 if (item != NULL)
2292 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002293 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002294 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002295 dict_unref(dict);
2296 goto on_error;
2297 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002298 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 clear_tv(tv);
2300 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002301 {
2302 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002304 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2306 item->di_tv.v_lock = 0;
2307 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002308 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002309 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002310 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002312 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 }
2314
2315 if (count > 0)
2316 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002317 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 goto failed;
2319 else
2320 ++ectx.ec_stack.ga_len;
2321 tv = STACK_TV_BOT(-1);
2322 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002323 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 tv->vval.v_dict = dict;
2325 ++dict->dv_refcount;
2326 }
2327 break;
2328
2329 // call a :def function
2330 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002331 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002332 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 iptr->isn_arg.dfunc.cdf_argcount,
2334 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002335 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 break;
2337
2338 // call a builtin function
2339 case ISN_BCALL:
2340 SOURCING_LNUM = iptr->isn_lnum;
2341 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2342 iptr->isn_arg.bfunc.cbf_argcount,
2343 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002344 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 break;
2346
2347 // call a funcref or partial
2348 case ISN_PCALL:
2349 {
2350 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2351 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002352 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353
2354 SOURCING_LNUM = iptr->isn_lnum;
2355 if (pfunc->cpf_top)
2356 {
2357 // funcref is above the arguments
2358 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2359 }
2360 else
2361 {
2362 // Get the funcref from the stack.
2363 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002364 partial_tv = *STACK_TV_BOT(0);
2365 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 }
2367 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002368 if (tv == &partial_tv)
2369 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002371 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 }
2373 break;
2374
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002375 case ISN_PCALL_END:
2376 // PCALL finished, arguments have been consumed and replaced by
2377 // the return value. Now clear the funcref from the stack,
2378 // and move the return value in its place.
2379 --ectx.ec_stack.ga_len;
2380 clear_tv(STACK_TV_BOT(-1));
2381 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2382 break;
2383
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 // call a user defined function or funcref/partial
2385 case ISN_UCALL:
2386 {
2387 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2388
2389 SOURCING_LNUM = iptr->isn_lnum;
2390 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002391 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002392 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 }
2394 break;
2395
2396 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002397 case ISN_RETURN_ZERO:
2398 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2399 goto failed;
2400 tv = STACK_TV_BOT(0);
2401 ++ectx.ec_stack.ga_len;
2402 tv->v_type = VAR_NUMBER;
2403 tv->vval.v_number = 0;
2404 tv->v_lock = 0;
2405 // FALLTHROUGH
2406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 case ISN_RETURN:
2408 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002409 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002410 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002411
2412 if (trystack->ga_len > 0)
2413 trycmd = ((trycmd_T *)trystack->ga_data)
2414 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002415 if (trycmd != NULL
2416 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 && trycmd->tcd_finally_idx != 0)
2418 {
2419 // jump to ":finally"
2420 ectx.ec_iidx = trycmd->tcd_finally_idx;
2421 trycmd->tcd_return = TRUE;
2422 }
2423 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002424 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 }
2426 break;
2427
2428 // push a function reference to a compiled function
2429 case ISN_FUNCREF:
2430 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002431 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2432 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2433 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 if (pt == NULL)
2436 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002437 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002438 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002439 vim_free(pt);
2440 goto failed;
2441 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002442 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2443 &ectx) == FAIL)
2444 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002445
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002446 tv = STACK_TV_BOT(0);
2447 ++ectx.ec_stack.ga_len;
2448 tv->vval.v_partial = pt;
2449 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002450 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 }
2452 break;
2453
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002454 // Create a global function from a lambda.
2455 case ISN_NEWFUNC:
2456 {
2457 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2458
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002459 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002460 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002461 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002462 }
2463 break;
2464
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002465 // List functions
2466 case ISN_DEF:
2467 if (iptr->isn_arg.string == NULL)
2468 list_functions(NULL);
2469 else
2470 {
2471 exarg_T ea;
2472
2473 CLEAR_FIELD(ea);
2474 ea.cmd = ea.arg = iptr->isn_arg.string;
2475 define_function(&ea, NULL);
2476 }
2477 break;
2478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 // jump if a condition is met
2480 case ISN_JUMP:
2481 {
2482 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002483 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 int jump = TRUE;
2485
2486 if (when != JUMP_ALWAYS)
2487 {
2488 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002489 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002490 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002491 || when == JUMP_IF_COND_TRUE)
2492 {
2493 SOURCING_LNUM = iptr->isn_lnum;
2494 jump = tv_get_bool_chk(tv, &error);
2495 if (error)
2496 goto on_error;
2497 }
2498 else
2499 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002501 || when == JUMP_AND_KEEP_IF_FALSE
2502 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002504 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 {
2506 // drop the value from the stack
2507 clear_tv(tv);
2508 --ectx.ec_stack.ga_len;
2509 }
2510 }
2511 if (jump)
2512 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2513 }
2514 break;
2515
2516 // top of a for loop
2517 case ISN_FOR:
2518 {
2519 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2520 typval_T *idxtv =
2521 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2522
2523 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002524 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002526 ++idxtv->vval.v_number;
2527 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 // past the end of the list, jump to "endfor"
2529 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2530 else if (list->lv_first == &range_list_item)
2531 {
2532 // non-materialized range() list
2533 tv = STACK_TV_BOT(0);
2534 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002535 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 tv->vval.v_number = list_find_nr(
2537 list, idxtv->vval.v_number, NULL);
2538 ++ectx.ec_stack.ga_len;
2539 }
2540 else
2541 {
2542 listitem_T *li = list_find(list, idxtv->vval.v_number);
2543
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2545 ++ectx.ec_stack.ga_len;
2546 }
2547 }
2548 break;
2549
2550 // start of ":try" block
2551 case ISN_TRY:
2552 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002553 trycmd_T *trycmd = NULL;
2554
Bram Moolenaar270d0382020-05-15 21:42:53 +02002555 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002556 goto failed;
2557 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2558 + ectx.ec_trystack.ga_len;
2559 ++ectx.ec_trystack.ga_len;
2560 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002561 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2563 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002564 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002565 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 }
2567 break;
2568
2569 case ISN_PUSHEXC:
2570 if (current_exception == NULL)
2571 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002572 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 iemsg("Evaluating catch while current_exception is NULL");
2574 goto failed;
2575 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002576 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 goto failed;
2578 tv = STACK_TV_BOT(0);
2579 ++ectx.ec_stack.ga_len;
2580 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002581 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 tv->vval.v_string = vim_strsave(
2583 (char_u *)current_exception->value);
2584 break;
2585
2586 case ISN_CATCH:
2587 {
2588 garray_T *trystack = &ectx.ec_trystack;
2589
Bram Moolenaar20a76292020-12-25 19:47:24 +01002590 if (restore_cmdmod)
2591 {
2592 cmdmod.cmod_filter_regmatch.regprog = NULL;
2593 undo_cmdmod(&cmdmod);
2594 cmdmod = save_cmdmod;
2595 restore_cmdmod = FALSE;
2596 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 if (trystack->ga_len > 0)
2598 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002599 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 + trystack->ga_len - 1;
2601 trycmd->tcd_caught = TRUE;
2602 }
2603 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002604 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 catch_exception(current_exception);
2606 }
2607 break;
2608
2609 // end of ":try" block
2610 case ISN_ENDTRY:
2611 {
2612 garray_T *trystack = &ectx.ec_trystack;
2613
2614 if (trystack->ga_len > 0)
2615 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002616 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618 --trystack->ga_len;
2619 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002620 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 trycmd = ((trycmd_T *)trystack->ga_data)
2622 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002623 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 {
2625 // discard the exception
2626 if (caught_stack == current_exception)
2627 caught_stack = caught_stack->caught;
2628 discard_current_exception();
2629 }
2630
2631 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002632 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 }
2634 }
2635 break;
2636
2637 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002638 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002639 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002640
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002641 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2642 {
2643 // throwing an exception while using "silent!" causes
2644 // the function to abort but not display an error.
2645 tv = STACK_TV_BOT(-1);
2646 clear_tv(tv);
2647 tv->v_type = VAR_NUMBER;
2648 tv->vval.v_number = 0;
2649 goto done;
2650 }
2651 --ectx.ec_stack.ga_len;
2652 tv = STACK_TV_BOT(0);
2653 if (tv->vval.v_string == NULL
2654 || *skipwhite(tv->vval.v_string) == NUL)
2655 {
2656 vim_free(tv->vval.v_string);
2657 SOURCING_LNUM = iptr->isn_lnum;
2658 emsg(_(e_throw_with_empty_string));
2659 goto failed;
2660 }
2661
2662 // Inside a "catch" we need to first discard the caught
2663 // exception.
2664 if (trystack->ga_len > 0)
2665 {
2666 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2667 + trystack->ga_len - 1;
2668 if (trycmd->tcd_caught && current_exception != NULL)
2669 {
2670 // discard the exception
2671 if (caught_stack == current_exception)
2672 caught_stack = caught_stack->caught;
2673 discard_current_exception();
2674 trycmd->tcd_caught = FALSE;
2675 }
2676 }
2677
2678 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2679 == FAIL)
2680 {
2681 vim_free(tv->vval.v_string);
2682 goto failed;
2683 }
2684 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 break;
2687
2688 // compare with special values
2689 case ISN_COMPAREBOOL:
2690 case ISN_COMPARESPECIAL:
2691 {
2692 typval_T *tv1 = STACK_TV_BOT(-2);
2693 typval_T *tv2 = STACK_TV_BOT(-1);
2694 varnumber_T arg1 = tv1->vval.v_number;
2695 varnumber_T arg2 = tv2->vval.v_number;
2696 int res;
2697
2698 switch (iptr->isn_arg.op.op_type)
2699 {
2700 case EXPR_EQUAL: res = arg1 == arg2; break;
2701 case EXPR_NEQUAL: res = arg1 != arg2; break;
2702 default: res = 0; break;
2703 }
2704
2705 --ectx.ec_stack.ga_len;
2706 tv1->v_type = VAR_BOOL;
2707 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2708 }
2709 break;
2710
2711 // Operation with two number arguments
2712 case ISN_OPNR:
2713 case ISN_COMPARENR:
2714 {
2715 typval_T *tv1 = STACK_TV_BOT(-2);
2716 typval_T *tv2 = STACK_TV_BOT(-1);
2717 varnumber_T arg1 = tv1->vval.v_number;
2718 varnumber_T arg2 = tv2->vval.v_number;
2719 varnumber_T res;
2720
2721 switch (iptr->isn_arg.op.op_type)
2722 {
2723 case EXPR_MULT: res = arg1 * arg2; break;
2724 case EXPR_DIV: res = arg1 / arg2; break;
2725 case EXPR_REM: res = arg1 % arg2; break;
2726 case EXPR_SUB: res = arg1 - arg2; break;
2727 case EXPR_ADD: res = arg1 + arg2; break;
2728
2729 case EXPR_EQUAL: res = arg1 == arg2; break;
2730 case EXPR_NEQUAL: res = arg1 != arg2; break;
2731 case EXPR_GREATER: res = arg1 > arg2; break;
2732 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2733 case EXPR_SMALLER: res = arg1 < arg2; break;
2734 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2735 default: res = 0; break;
2736 }
2737
2738 --ectx.ec_stack.ga_len;
2739 if (iptr->isn_type == ISN_COMPARENR)
2740 {
2741 tv1->v_type = VAR_BOOL;
2742 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2743 }
2744 else
2745 tv1->vval.v_number = res;
2746 }
2747 break;
2748
2749 // Computation with two float arguments
2750 case ISN_OPFLOAT:
2751 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002752#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 {
2754 typval_T *tv1 = STACK_TV_BOT(-2);
2755 typval_T *tv2 = STACK_TV_BOT(-1);
2756 float_T arg1 = tv1->vval.v_float;
2757 float_T arg2 = tv2->vval.v_float;
2758 float_T res = 0;
2759 int cmp = FALSE;
2760
2761 switch (iptr->isn_arg.op.op_type)
2762 {
2763 case EXPR_MULT: res = arg1 * arg2; break;
2764 case EXPR_DIV: res = arg1 / arg2; break;
2765 case EXPR_SUB: res = arg1 - arg2; break;
2766 case EXPR_ADD: res = arg1 + arg2; break;
2767
2768 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2769 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2770 case EXPR_GREATER: cmp = arg1 > arg2; break;
2771 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2772 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2773 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2774 default: cmp = 0; break;
2775 }
2776 --ectx.ec_stack.ga_len;
2777 if (iptr->isn_type == ISN_COMPAREFLOAT)
2778 {
2779 tv1->v_type = VAR_BOOL;
2780 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2781 }
2782 else
2783 tv1->vval.v_float = res;
2784 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002785#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 break;
2787
2788 case ISN_COMPARELIST:
2789 {
2790 typval_T *tv1 = STACK_TV_BOT(-2);
2791 typval_T *tv2 = STACK_TV_BOT(-1);
2792 list_T *arg1 = tv1->vval.v_list;
2793 list_T *arg2 = tv2->vval.v_list;
2794 int cmp = FALSE;
2795 int ic = iptr->isn_arg.op.op_ic;
2796
2797 switch (iptr->isn_arg.op.op_type)
2798 {
2799 case EXPR_EQUAL: cmp =
2800 list_equal(arg1, arg2, ic, FALSE); break;
2801 case EXPR_NEQUAL: cmp =
2802 !list_equal(arg1, arg2, ic, FALSE); break;
2803 case EXPR_IS: cmp = arg1 == arg2; break;
2804 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2805 default: cmp = 0; break;
2806 }
2807 --ectx.ec_stack.ga_len;
2808 clear_tv(tv1);
2809 clear_tv(tv2);
2810 tv1->v_type = VAR_BOOL;
2811 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2812 }
2813 break;
2814
2815 case ISN_COMPAREBLOB:
2816 {
2817 typval_T *tv1 = STACK_TV_BOT(-2);
2818 typval_T *tv2 = STACK_TV_BOT(-1);
2819 blob_T *arg1 = tv1->vval.v_blob;
2820 blob_T *arg2 = tv2->vval.v_blob;
2821 int cmp = FALSE;
2822
2823 switch (iptr->isn_arg.op.op_type)
2824 {
2825 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2826 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2827 case EXPR_IS: cmp = arg1 == arg2; break;
2828 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2829 default: cmp = 0; break;
2830 }
2831 --ectx.ec_stack.ga_len;
2832 clear_tv(tv1);
2833 clear_tv(tv2);
2834 tv1->v_type = VAR_BOOL;
2835 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2836 }
2837 break;
2838
2839 // TODO: handle separately
2840 case ISN_COMPARESTRING:
2841 case ISN_COMPAREDICT:
2842 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 case ISN_COMPAREANY:
2844 {
2845 typval_T *tv1 = STACK_TV_BOT(-2);
2846 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002847 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 int ic = iptr->isn_arg.op.op_ic;
2849
Bram Moolenaareb26f432020-09-14 16:50:05 +02002850 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002851 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 --ectx.ec_stack.ga_len;
2854 }
2855 break;
2856
2857 case ISN_ADDLIST:
2858 case ISN_ADDBLOB:
2859 {
2860 typval_T *tv1 = STACK_TV_BOT(-2);
2861 typval_T *tv2 = STACK_TV_BOT(-1);
2862
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002863 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 if (iptr->isn_type == ISN_ADDLIST)
2865 eval_addlist(tv1, tv2);
2866 else
2867 eval_addblob(tv1, tv2);
2868 clear_tv(tv2);
2869 --ectx.ec_stack.ga_len;
2870 }
2871 break;
2872
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002873 case ISN_LISTAPPEND:
2874 {
2875 typval_T *tv1 = STACK_TV_BOT(-2);
2876 typval_T *tv2 = STACK_TV_BOT(-1);
2877 list_T *l = tv1->vval.v_list;
2878
2879 // add an item to a list
2880 if (l == NULL)
2881 {
2882 SOURCING_LNUM = iptr->isn_lnum;
2883 emsg(_(e_cannot_add_to_null_list));
2884 goto on_error;
2885 }
2886 if (list_append_tv(l, tv2) == FAIL)
2887 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002888 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002889 --ectx.ec_stack.ga_len;
2890 }
2891 break;
2892
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002893 case ISN_BLOBAPPEND:
2894 {
2895 typval_T *tv1 = STACK_TV_BOT(-2);
2896 typval_T *tv2 = STACK_TV_BOT(-1);
2897 blob_T *b = tv1->vval.v_blob;
2898 int error = FALSE;
2899 varnumber_T n;
2900
2901 // add a number to a blob
2902 if (b == NULL)
2903 {
2904 SOURCING_LNUM = iptr->isn_lnum;
2905 emsg(_(e_cannot_add_to_null_blob));
2906 goto on_error;
2907 }
2908 n = tv_get_number_chk(tv2, &error);
2909 if (error)
2910 goto on_error;
2911 ga_append(&b->bv_ga, (int)n);
2912 --ectx.ec_stack.ga_len;
2913 }
2914 break;
2915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 // Computation with two arguments of unknown type
2917 case ISN_OPANY:
2918 {
2919 typval_T *tv1 = STACK_TV_BOT(-2);
2920 typval_T *tv2 = STACK_TV_BOT(-1);
2921 varnumber_T n1, n2;
2922#ifdef FEAT_FLOAT
2923 float_T f1 = 0, f2 = 0;
2924#endif
2925 int error = FALSE;
2926
2927 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2928 {
2929 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2930 {
2931 eval_addlist(tv1, tv2);
2932 clear_tv(tv2);
2933 --ectx.ec_stack.ga_len;
2934 break;
2935 }
2936 else if (tv1->v_type == VAR_BLOB
2937 && tv2->v_type == VAR_BLOB)
2938 {
2939 eval_addblob(tv1, tv2);
2940 clear_tv(tv2);
2941 --ectx.ec_stack.ga_len;
2942 break;
2943 }
2944 }
2945#ifdef FEAT_FLOAT
2946 if (tv1->v_type == VAR_FLOAT)
2947 {
2948 f1 = tv1->vval.v_float;
2949 n1 = 0;
2950 }
2951 else
2952#endif
2953 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002954 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002955 n1 = tv_get_number_chk(tv1, &error);
2956 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002957 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958#ifdef FEAT_FLOAT
2959 if (tv2->v_type == VAR_FLOAT)
2960 f1 = n1;
2961#endif
2962 }
2963#ifdef FEAT_FLOAT
2964 if (tv2->v_type == VAR_FLOAT)
2965 {
2966 f2 = tv2->vval.v_float;
2967 n2 = 0;
2968 }
2969 else
2970#endif
2971 {
2972 n2 = tv_get_number_chk(tv2, &error);
2973 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002974 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002975#ifdef FEAT_FLOAT
2976 if (tv1->v_type == VAR_FLOAT)
2977 f2 = n2;
2978#endif
2979 }
2980#ifdef FEAT_FLOAT
2981 // if there is a float on either side the result is a float
2982 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2983 {
2984 switch (iptr->isn_arg.op.op_type)
2985 {
2986 case EXPR_MULT: f1 = f1 * f2; break;
2987 case EXPR_DIV: f1 = f1 / f2; break;
2988 case EXPR_SUB: f1 = f1 - f2; break;
2989 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002990 default: SOURCING_LNUM = iptr->isn_lnum;
2991 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002992 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 }
2994 clear_tv(tv1);
2995 clear_tv(tv2);
2996 tv1->v_type = VAR_FLOAT;
2997 tv1->vval.v_float = f1;
2998 --ectx.ec_stack.ga_len;
2999 }
3000 else
3001#endif
3002 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003003 int failed = FALSE;
3004
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 switch (iptr->isn_arg.op.op_type)
3006 {
3007 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003008 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3009 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003010 goto on_error;
3011 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003012 case EXPR_SUB: n1 = n1 - n2; break;
3013 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003014 default: n1 = num_modulus(n1, n2, &failed);
3015 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003016 goto on_error;
3017 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 }
3019 clear_tv(tv1);
3020 clear_tv(tv2);
3021 tv1->v_type = VAR_NUMBER;
3022 tv1->vval.v_number = n1;
3023 --ectx.ec_stack.ga_len;
3024 }
3025 }
3026 break;
3027
3028 case ISN_CONCAT:
3029 {
3030 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3031 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3032 char_u *res;
3033
3034 res = concat_str(str1, str2);
3035 clear_tv(STACK_TV_BOT(-2));
3036 clear_tv(STACK_TV_BOT(-1));
3037 --ectx.ec_stack.ga_len;
3038 STACK_TV_BOT(-1)->vval.v_string = res;
3039 }
3040 break;
3041
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003042 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003043 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003044 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003045 int is_slice = iptr->isn_type == ISN_STRSLICE;
3046 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003047 char_u *res;
3048
3049 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003050 // string slice: string is at stack-3, first index at
3051 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003052 if (is_slice)
3053 {
3054 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003055 n1 = tv->vval.v_number;
3056 }
3057
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003058 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003059 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003060
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003061 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003062 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003063 if (is_slice)
3064 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003065 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003066 else
3067 // Index: The resulting variable is a string of a
3068 // single character. If the index is too big or
3069 // negative the result is empty.
3070 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003071 vim_free(tv->vval.v_string);
3072 tv->vval.v_string = res;
3073 }
3074 break;
3075
3076 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003077 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 {
Bram Moolenaared591872020-08-15 22:14:53 +02003079 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003081 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082
3083 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003084 // list slice: list is at stack-3, indexes at stack-2 and
3085 // stack-1
3086 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003087 list = tv->vval.v_list;
3088
3089 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003090 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003092
3093 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003094 {
Bram Moolenaared591872020-08-15 22:14:53 +02003095 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003096 n1 = tv->vval.v_number;
3097 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003098 }
Bram Moolenaared591872020-08-15 22:14:53 +02003099
3100 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003101 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003102 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003103 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3104 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003105 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003106 }
3107 break;
3108
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003109 case ISN_ANYINDEX:
3110 case ISN_ANYSLICE:
3111 {
3112 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3113 typval_T *var1, *var2;
3114 int res;
3115
3116 // index: composite is at stack-2, index at stack-1
3117 // slice: composite is at stack-3, indexes at stack-2 and
3118 // stack-1
3119 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003120 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003121 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3122 goto on_error;
3123 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3124 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003125 res = eval_index_inner(tv, is_slice, var1, var2,
3126 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003127 clear_tv(var1);
3128 if (is_slice)
3129 clear_tv(var2);
3130 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3131 if (res == FAIL)
3132 goto on_error;
3133 }
3134 break;
3135
Bram Moolenaar9af78762020-06-16 11:34:42 +02003136 case ISN_SLICE:
3137 {
3138 list_T *list;
3139 int count = iptr->isn_arg.number;
3140
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003141 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003142 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003143 list = tv->vval.v_list;
3144
3145 // no error for short list, expect it to be checked earlier
3146 if (list != NULL && list->lv_len >= count)
3147 {
3148 list_T *newlist = list_slice(list,
3149 count, list->lv_len - 1);
3150
3151 if (newlist != NULL)
3152 {
3153 list_unref(list);
3154 tv->vval.v_list = newlist;
3155 ++newlist->lv_refcount;
3156 }
3157 }
3158 }
3159 break;
3160
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003161 case ISN_GETITEM:
3162 {
3163 listitem_T *li;
3164 int index = iptr->isn_arg.number;
3165
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003166 // Get list item: list is at stack-1, push item.
3167 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003168 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003169 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003170
3171 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3172 goto failed;
3173 ++ectx.ec_stack.ga_len;
3174 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3175 }
3176 break;
3177
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 case ISN_MEMBER:
3179 {
3180 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003181 char_u *key;
3182 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003183 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003184
3185 // dict member: dict is at stack-2, key at stack-1
3186 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003187 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003188 dict = tv->vval.v_dict;
3189
3190 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003191 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003192 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003193 if (key == NULL)
3194 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003195
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003196 if ((di = dict_find(dict, key, -1)) == NULL)
3197 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003199 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003200
3201 // If :silent! is used we will continue, make sure the
3202 // stack contents makes sense.
3203 clear_tv(tv);
3204 --ectx.ec_stack.ga_len;
3205 tv = STACK_TV_BOT(-1);
3206 clear_tv(tv);
3207 tv->v_type = VAR_NUMBER;
3208 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003209 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003210 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003211 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003212 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003213 // Clear the dict only after getting the item, to avoid
3214 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003215 tv = STACK_TV_BOT(-1);
3216 temp_tv = *tv;
3217 copy_tv(&di->di_tv, tv);
3218 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003219 }
3220 break;
3221
3222 // dict member with string key
3223 case ISN_STRINGMEMBER:
3224 {
3225 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003227 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003228
3229 tv = STACK_TV_BOT(-1);
3230 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3231 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003232 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003234 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235 }
3236 dict = tv->vval.v_dict;
3237
3238 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3239 == NULL)
3240 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003243 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003245 // Clear the dict after getting the item, to avoid that it
3246 // make the item invalid.
3247 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003249 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 }
3251 break;
3252
3253 case ISN_NEGATENR:
3254 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003255 if (tv->v_type != VAR_NUMBER
3256#ifdef FEAT_FLOAT
3257 && tv->v_type != VAR_FLOAT
3258#endif
3259 )
3260 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003262 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003263 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003264 }
3265#ifdef FEAT_FLOAT
3266 if (tv->v_type == VAR_FLOAT)
3267 tv->vval.v_float = -tv->vval.v_float;
3268 else
3269#endif
3270 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 break;
3272
3273 case ISN_CHECKNR:
3274 {
3275 int error = FALSE;
3276
3277 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003278 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003280 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 (void)tv_get_number_chk(tv, &error);
3282 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003283 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003284 }
3285 break;
3286
3287 case ISN_CHECKTYPE:
3288 {
3289 checktype_T *ct = &iptr->isn_arg.type;
3290
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003291 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003292 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare32e5162021-01-21 20:21:29 +01003293 if (check_typval_type(ct->ct_type, tv, ct->ct_arg_idx)
3294 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003295 goto on_error;
3296
3297 // number 0 is FALSE, number 1 is TRUE
3298 if (tv->v_type == VAR_NUMBER
3299 && ct->ct_type->tt_type == VAR_BOOL
3300 && (tv->vval.v_number == 0
3301 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003303 tv->v_type = VAR_BOOL;
3304 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003305 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 }
3307 }
3308 break;
3309
Bram Moolenaar9af78762020-06-16 11:34:42 +02003310 case ISN_CHECKLEN:
3311 {
3312 int min_len = iptr->isn_arg.checklen.cl_min_len;
3313 list_T *list = NULL;
3314
3315 tv = STACK_TV_BOT(-1);
3316 if (tv->v_type == VAR_LIST)
3317 list = tv->vval.v_list;
3318 if (list == NULL || list->lv_len < min_len
3319 || (list->lv_len > min_len
3320 && !iptr->isn_arg.checklen.cl_more_OK))
3321 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003322 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003323 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003324 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003325 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003326 }
3327 }
3328 break;
3329
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003330 case ISN_SETTYPE:
3331 {
3332 checktype_T *ct = &iptr->isn_arg.type;
3333
3334 tv = STACK_TV_BOT(-1);
3335 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3336 {
3337 free_type(tv->vval.v_dict->dv_type);
3338 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3339 }
3340 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3341 {
3342 free_type(tv->vval.v_list->lv_type);
3343 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3344 }
3345 }
3346 break;
3347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003349 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003350 {
3351 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003352 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
3354 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003355 if (iptr->isn_type == ISN_2BOOL)
3356 {
3357 n = tv2bool(tv);
3358 if (iptr->isn_arg.number) // invert
3359 n = !n;
3360 }
3361 else
3362 {
3363 SOURCING_LNUM = iptr->isn_lnum;
3364 n = tv_get_bool_chk(tv, &error);
3365 if (error)
3366 goto on_error;
3367 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 clear_tv(tv);
3369 tv->v_type = VAR_BOOL;
3370 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3371 }
3372 break;
3373
3374 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003375 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003376 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003377 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3378 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3379 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 break;
3381
Bram Moolenaar08597872020-12-10 19:43:40 +01003382 case ISN_RANGE:
3383 {
3384 exarg_T ea;
3385 char *errormsg;
3386
Bram Moolenaarece0b872021-01-08 20:40:45 +01003387 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003388 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003389 ea.addr_type = ADDR_LINES;
3390 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003391 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003392 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003393 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003394
3395 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3396 goto failed;
3397 ++ectx.ec_stack.ga_len;
3398 tv = STACK_TV_BOT(-1);
3399 tv->v_type = VAR_NUMBER;
3400 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003401 if (ea.addr_count == 0)
3402 tv->vval.v_number = curwin->w_cursor.lnum;
3403 else
3404 tv->vval.v_number = ea.line2;
3405 }
3406 break;
3407
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003408 case ISN_PUT:
3409 {
3410 int regname = iptr->isn_arg.put.put_regname;
3411 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3412 char_u *expr = NULL;
3413 int dir = FORWARD;
3414
Bram Moolenaar08597872020-12-10 19:43:40 +01003415 if (lnum < -2)
3416 {
3417 // line number was put on the stack by ISN_RANGE
3418 tv = STACK_TV_BOT(-1);
3419 curwin->w_cursor.lnum = tv->vval.v_number;
3420 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3421 dir = BACKWARD;
3422 --ectx.ec_stack.ga_len;
3423 }
3424 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003425 // :put! above cursor
3426 dir = BACKWARD;
3427 else if (lnum >= 0)
3428 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003429
3430 if (regname == '=')
3431 {
3432 tv = STACK_TV_BOT(-1);
3433 if (tv->v_type == VAR_STRING)
3434 expr = tv->vval.v_string;
3435 else
3436 {
3437 expr = typval2string(tv, TRUE); // allocates value
3438 clear_tv(tv);
3439 }
3440 --ectx.ec_stack.ga_len;
3441 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003442 check_cursor();
3443 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3444 vim_free(expr);
3445 }
3446 break;
3447
Bram Moolenaar02194d22020-10-24 23:08:38 +02003448 case ISN_CMDMOD:
3449 save_cmdmod = cmdmod;
3450 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003451 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003452 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3453 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003454 break;
3455
Bram Moolenaar02194d22020-10-24 23:08:38 +02003456 case ISN_CMDMOD_REV:
3457 // filter regprog is owned by the instruction, don't free it
3458 cmdmod.cmod_filter_regmatch.regprog = NULL;
3459 undo_cmdmod(&cmdmod);
3460 cmdmod = save_cmdmod;
3461 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003462 break;
3463
Bram Moolenaar792f7862020-11-23 08:31:18 +01003464 case ISN_UNPACK:
3465 {
3466 int count = iptr->isn_arg.unpack.unp_count;
3467 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3468 list_T *l;
3469 listitem_T *li;
3470 int i;
3471
3472 // Check there is a valid list to unpack.
3473 tv = STACK_TV_BOT(-1);
3474 if (tv->v_type != VAR_LIST)
3475 {
3476 SOURCING_LNUM = iptr->isn_lnum;
3477 emsg(_(e_for_argument_must_be_sequence_of_lists));
3478 goto on_error;
3479 }
3480 l = tv->vval.v_list;
3481 if (l == NULL
3482 || l->lv_len < (semicolon ? count - 1 : count))
3483 {
3484 SOURCING_LNUM = iptr->isn_lnum;
3485 emsg(_(e_list_value_does_not_have_enough_items));
3486 goto on_error;
3487 }
3488 else if (!semicolon && l->lv_len > count)
3489 {
3490 SOURCING_LNUM = iptr->isn_lnum;
3491 emsg(_(e_list_value_has_more_items_than_targets));
3492 goto on_error;
3493 }
3494
3495 CHECK_LIST_MATERIALIZE(l);
3496 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3497 goto failed;
3498 ectx.ec_stack.ga_len += count - 1;
3499
3500 // Variable after semicolon gets a list with the remaining
3501 // items.
3502 if (semicolon)
3503 {
3504 list_T *rem_list =
3505 list_alloc_with_items(l->lv_len - count + 1);
3506
3507 if (rem_list == NULL)
3508 goto failed;
3509 tv = STACK_TV_BOT(-count);
3510 tv->vval.v_list = rem_list;
3511 ++rem_list->lv_refcount;
3512 tv->v_lock = 0;
3513 li = l->lv_first;
3514 for (i = 0; i < count - 1; ++i)
3515 li = li->li_next;
3516 for (i = 0; li != NULL; ++i)
3517 {
3518 list_set_item(rem_list, i, &li->li_tv);
3519 li = li->li_next;
3520 }
3521 --count;
3522 }
3523
3524 // Produce the values in reverse order, first item last.
3525 li = l->lv_first;
3526 for (i = 0; i < count; ++i)
3527 {
3528 tv = STACK_TV_BOT(-i - 1);
3529 copy_tv(&li->li_tv, tv);
3530 li = li->li_next;
3531 }
3532
3533 list_unref(l);
3534 }
3535 break;
3536
Bram Moolenaarb2049902021-01-24 12:53:53 +01003537 case ISN_PROF_START:
3538 case ISN_PROF_END:
3539 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003540#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003541 funccall_T cookie;
3542 ufunc_T *cur_ufunc =
3543 (((dfunc_T *)def_functions.ga_data)
3544 + ectx.ec_dfunc_idx)->df_ufunc;
3545
3546 cookie.func = cur_ufunc;
3547 if (iptr->isn_type == ISN_PROF_START)
3548 {
3549 func_line_start(&cookie, iptr->isn_lnum);
3550 // if we get here the instruction is executed
3551 func_line_exec(&cookie);
3552 }
3553 else
3554 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003555#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003556 }
3557 break;
3558
Bram Moolenaar389df252020-07-09 21:20:47 +02003559 case ISN_SHUFFLE:
3560 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003561 typval_T tmp_tv;
3562 int item = iptr->isn_arg.shuffle.shfl_item;
3563 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003564
3565 tmp_tv = *STACK_TV_BOT(-item);
3566 for ( ; up > 0 && item > 1; --up)
3567 {
3568 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3569 --item;
3570 }
3571 *STACK_TV_BOT(-item) = tmp_tv;
3572 }
3573 break;
3574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 case ISN_DROP:
3576 --ectx.ec_stack.ga_len;
3577 clear_tv(STACK_TV_BOT(0));
3578 break;
3579 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003580 continue;
3581
Bram Moolenaard032f342020-07-18 18:13:02 +02003582func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003583 // Restore previous function. If the frame pointer is where we started
3584 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003585 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003586 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003587
Bram Moolenaard032f342020-07-18 18:13:02 +02003588 if (func_return(&ectx) == FAIL)
3589 // only fails when out of memory
3590 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003591 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003592
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003593on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003594 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003595 // If "emsg_silent" is set then ignore the error, unless it was set
3596 // when calling the function.
3597 if (did_emsg_cumul + did_emsg == did_emsg_before
3598 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003599 {
3600 // If a sequence of instructions causes an error while ":silent!"
3601 // was used, restore the stack length and jump ahead to restoring
3602 // the cmdmod.
3603 if (restore_cmdmod)
3604 {
3605 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3606 {
3607 --ectx.ec_stack.ga_len;
3608 clear_tv(STACK_TV_BOT(0));
3609 }
3610 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3611 ++ectx.ec_iidx;
3612 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003613 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003614 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003615on_fatal_error:
3616 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003617 // If we are not inside a try-catch started here, abort execution.
3618 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003619 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 }
3621
3622done:
3623 // function finished, get result from the stack.
3624 tv = STACK_TV_BOT(-1);
3625 *rettv = *tv;
3626 tv->v_type = VAR_UNKNOWN;
3627 ret = OK;
3628
3629failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003630 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003631 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003632 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003633
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003634 // Deal with any remaining closures, they may be in use somewhere.
3635 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003636 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003637 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003638 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3639 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003640
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003641 estack_pop();
3642 current_sctx = save_current_sctx;
3643
Bram Moolenaar352134b2020-10-17 22:04:08 +02003644 if (*msg_list != NULL && saved_msg_list != NULL)
3645 {
3646 msglist_T **plist = saved_msg_list;
3647
3648 // Append entries from the current msg_list (uncaught exceptions) to
3649 // the saved msg_list.
3650 while (*plist != NULL)
3651 plist = &(*plist)->next;
3652
3653 *plist = *msg_list;
3654 }
3655 msg_list = saved_msg_list;
3656
Bram Moolenaar02194d22020-10-24 23:08:38 +02003657 if (restore_cmdmod)
3658 {
3659 cmdmod.cmod_filter_regmatch.regprog = NULL;
3660 undo_cmdmod(&cmdmod);
3661 cmdmod = save_cmdmod;
3662 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003663 emsg_silent_def = save_emsg_silent_def;
3664 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003665
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003666failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003667 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003668 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3669 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003671 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003672 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003673
Bram Moolenaar0186e582021-01-10 18:33:11 +01003674 while (ectx.ec_outer != NULL)
3675 {
3676 outer_T *up = ectx.ec_outer->out_up_is_copy
3677 ? NULL : ectx.ec_outer->out_up;
3678
3679 vim_free(ectx.ec_outer);
3680 ectx.ec_outer = up;
3681 }
3682
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003683 // Not sure if this is necessary.
3684 suppress_errthrow = save_suppress_errthrow;
3685
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003686 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003687 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003688 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003689 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 return ret;
3691}
3692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003693/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003694 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003695 * We don't really need this at runtime, but we do have tests that require it,
3696 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003697 */
3698 void
3699ex_disassemble(exarg_T *eap)
3700{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003701 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003702 char_u *fname;
3703 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003704 dfunc_T *dfunc;
3705 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003706 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003707 int current;
3708 int line_idx = 0;
3709 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003710 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003712 if (STRNCMP(arg, "<lambda>", 8) == 0)
3713 {
3714 arg += 8;
3715 (void)getdigits(&arg);
3716 fname = vim_strnsave(eap->arg, arg - eap->arg);
3717 }
3718 else
3719 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003720 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003721 if (fname == NULL)
3722 {
3723 semsg(_(e_invarg2), eap->arg);
3724 return;
3725 }
3726
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003727 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003728 if (ufunc == NULL)
3729 {
3730 char_u *p = untrans_function_name(fname);
3731
3732 if (p != NULL)
3733 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003734 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003735 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003736 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 if (ufunc == NULL)
3738 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003739 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003740 return;
3741 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003742 if (func_needs_compiling(ufunc, eap->forceit)
3743 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003744 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003745 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003747 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003748 return;
3749 }
3750 if (ufunc->uf_name_exp != NULL)
3751 msg((char *)ufunc->uf_name_exp);
3752 else
3753 msg((char *)ufunc->uf_name);
3754
3755 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003756#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003757 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3758 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3759 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003760#else
3761 instr = dfunc->df_instr;
3762 instr_count = dfunc->df_instr_count;
3763#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003764 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 {
3766 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003767 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768
3769 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3770 {
3771 if (current > prev_current)
3772 {
3773 msg_puts("\n\n");
3774 prev_current = current;
3775 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003776 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3777 if (line != NULL)
3778 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003779 }
3780
3781 switch (iptr->isn_type)
3782 {
3783 case ISN_EXEC:
3784 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3785 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003786 case ISN_EXECCONCAT:
3787 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003788 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003789 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 case ISN_ECHO:
3791 {
3792 echo_T *echo = &iptr->isn_arg.echo;
3793
3794 smsg("%4d %s %d", current,
3795 echo->echo_with_white ? "ECHO" : "ECHON",
3796 echo->echo_count);
3797 }
3798 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003799 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003800 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003801 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003802 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003803 case ISN_ECHOMSG:
3804 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003805 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003806 break;
3807 case ISN_ECHOERR:
3808 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003809 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003810 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003811 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003812 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003813 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003814 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003815 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003816 + STACK_FRAME_SIZE));
3817 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003818 smsg("%4d LOAD $%lld", current,
3819 (varnumber_T)(iptr->isn_arg.number));
3820 }
3821 break;
3822 case ISN_LOADOUTER:
3823 {
3824 if (iptr->isn_arg.number < 0)
3825 smsg("%4d LOADOUTER level %d arg[%d]", current,
3826 iptr->isn_arg.outer.outer_depth,
3827 iptr->isn_arg.outer.outer_idx
3828 + STACK_FRAME_SIZE);
3829 else
3830 smsg("%4d LOADOUTER level %d $%d", current,
3831 iptr->isn_arg.outer.outer_depth,
3832 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003833 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 break;
3835 case ISN_LOADV:
3836 smsg("%4d LOADV v:%s", current,
3837 get_vim_var_name(iptr->isn_arg.number));
3838 break;
3839 case ISN_LOADSCRIPT:
3840 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003841 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3842 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003844 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003845
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003846 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3847 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003848 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003849 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 }
3851 break;
3852 case ISN_LOADS:
3853 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003854 scriptitem_T *si = SCRIPT_ITEM(
3855 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856
3857 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003858 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 }
3860 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003861 case ISN_LOADAUTO:
3862 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3863 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003864 case ISN_LOADG:
3865 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3866 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003867 case ISN_LOADB:
3868 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3869 break;
3870 case ISN_LOADW:
3871 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3872 break;
3873 case ISN_LOADT:
3874 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3875 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003876 case ISN_LOADGDICT:
3877 smsg("%4d LOAD g:", current);
3878 break;
3879 case ISN_LOADBDICT:
3880 smsg("%4d LOAD b:", current);
3881 break;
3882 case ISN_LOADWDICT:
3883 smsg("%4d LOAD w:", current);
3884 break;
3885 case ISN_LOADTDICT:
3886 smsg("%4d LOAD t:", current);
3887 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 case ISN_LOADOPT:
3889 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3890 break;
3891 case ISN_LOADENV:
3892 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3893 break;
3894 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003895 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 break;
3897
3898 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003899 if (iptr->isn_arg.number < 0)
3900 smsg("%4d STORE arg[%lld]", current,
3901 iptr->isn_arg.number + STACK_FRAME_SIZE);
3902 else
3903 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3904 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003905 case ISN_STOREOUTER:
3906 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003907 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003908 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3909 iptr->isn_arg.outer.outer_depth,
3910 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003911 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003912 smsg("%4d STOREOUTER level %d $%d", current,
3913 iptr->isn_arg.outer.outer_depth,
3914 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003917 case ISN_STOREV:
3918 smsg("%4d STOREV v:%s", current,
3919 get_vim_var_name(iptr->isn_arg.number));
3920 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003921 case ISN_STOREAUTO:
3922 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3923 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003925 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3926 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003927 case ISN_STOREB:
3928 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3929 break;
3930 case ISN_STOREW:
3931 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3932 break;
3933 case ISN_STORET:
3934 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3935 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003936 case ISN_STORES:
3937 {
3938 scriptitem_T *si = SCRIPT_ITEM(
3939 iptr->isn_arg.loadstore.ls_sid);
3940
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003941 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003942 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 break;
3945 case ISN_STORESCRIPT:
3946 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003947 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3948 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003950 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003951
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003952 smsg("%4d STORESCRIPT %s-%d in %s", current,
3953 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003954 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003955 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 }
3957 break;
3958 case ISN_STOREOPT:
3959 smsg("%4d STOREOPT &%s", current,
3960 iptr->isn_arg.storeopt.so_name);
3961 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003962 case ISN_STOREENV:
3963 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3964 break;
3965 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003966 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003967 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 case ISN_STORENR:
3969 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003970 iptr->isn_arg.storenr.stnr_val,
3971 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 break;
3973
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003974 case ISN_STOREINDEX:
3975 switch (iptr->isn_arg.vartype)
3976 {
3977 case VAR_LIST:
3978 smsg("%4d STORELIST", current);
3979 break;
3980 case VAR_DICT:
3981 smsg("%4d STOREDICT", current);
3982 break;
3983 case VAR_ANY:
3984 smsg("%4d STOREINDEX", current);
3985 break;
3986 default: break;
3987 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003988 break;
3989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003990 // constants
3991 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003992 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003993 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 break;
3995 case ISN_PUSHBOOL:
3996 case ISN_PUSHSPEC:
3997 smsg("%4d PUSH %s", current,
3998 get_var_special_name(iptr->isn_arg.number));
3999 break;
4000 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004001#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004003#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004004 break;
4005 case ISN_PUSHS:
4006 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4007 break;
4008 case ISN_PUSHBLOB:
4009 {
4010 char_u *r;
4011 char_u numbuf[NUMBUFLEN];
4012 char_u *tofree;
4013
4014 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004015 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 vim_free(tofree);
4017 }
4018 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004019 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004020 {
4021 char *name = (char *)iptr->isn_arg.string;
4022
4023 smsg("%4d PUSHFUNC \"%s\"", current,
4024 name == NULL ? "[none]" : name);
4025 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004026 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004027 case ISN_PUSHCHANNEL:
4028#ifdef FEAT_JOB_CHANNEL
4029 {
4030 channel_T *channel = iptr->isn_arg.channel;
4031
4032 smsg("%4d PUSHCHANNEL %d", current,
4033 channel == NULL ? 0 : channel->ch_id);
4034 }
4035#endif
4036 break;
4037 case ISN_PUSHJOB:
4038#ifdef FEAT_JOB_CHANNEL
4039 {
4040 typval_T tv;
4041 char_u *name;
4042
4043 tv.v_type = VAR_JOB;
4044 tv.vval.v_job = iptr->isn_arg.job;
4045 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004046 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004047 }
4048#endif
4049 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 case ISN_PUSHEXC:
4051 smsg("%4d PUSH v:exception", current);
4052 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004053 case ISN_UNLET:
4054 smsg("%4d UNLET%s %s", current,
4055 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4056 iptr->isn_arg.unlet.ul_name);
4057 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004058 case ISN_UNLETENV:
4059 smsg("%4d UNLETENV%s $%s", current,
4060 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4061 iptr->isn_arg.unlet.ul_name);
4062 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004063 case ISN_UNLETINDEX:
4064 smsg("%4d UNLETINDEX", current);
4065 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004066 case ISN_LOCKCONST:
4067 smsg("%4d LOCKCONST", current);
4068 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004069 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004070 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004071 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 break;
4073 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004074 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004075 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004076 break;
4077
4078 // function call
4079 case ISN_BCALL:
4080 {
4081 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4082
4083 smsg("%4d BCALL %s(argc %d)", current,
4084 internal_func_name(cbfunc->cbf_idx),
4085 cbfunc->cbf_argcount);
4086 }
4087 break;
4088 case ISN_DCALL:
4089 {
4090 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4091 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4092 + cdfunc->cdf_idx;
4093
4094 smsg("%4d DCALL %s(argc %d)", current,
4095 df->df_ufunc->uf_name_exp != NULL
4096 ? df->df_ufunc->uf_name_exp
4097 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4098 }
4099 break;
4100 case ISN_UCALL:
4101 {
4102 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4103
4104 smsg("%4d UCALL %s(argc %d)", current,
4105 cufunc->cuf_name, cufunc->cuf_argcount);
4106 }
4107 break;
4108 case ISN_PCALL:
4109 {
4110 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4111
4112 smsg("%4d PCALL%s (argc %d)", current,
4113 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4114 }
4115 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004116 case ISN_PCALL_END:
4117 smsg("%4d PCALL end", current);
4118 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004119 case ISN_RETURN:
4120 smsg("%4d RETURN", current);
4121 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004122 case ISN_RETURN_ZERO:
4123 smsg("%4d RETURN 0", current);
4124 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004125 case ISN_FUNCREF:
4126 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004127 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004129 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004131 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 }
4133 break;
4134
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004135 case ISN_NEWFUNC:
4136 {
4137 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4138
4139 smsg("%4d NEWFUNC %s %s", current,
4140 newfunc->nf_lambda, newfunc->nf_global);
4141 }
4142 break;
4143
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004144 case ISN_DEF:
4145 {
4146 char_u *name = iptr->isn_arg.string;
4147
4148 smsg("%4d DEF %s", current,
4149 name == NULL ? (char_u *)"" : name);
4150 }
4151 break;
4152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 case ISN_JUMP:
4154 {
4155 char *when = "?";
4156
4157 switch (iptr->isn_arg.jump.jump_when)
4158 {
4159 case JUMP_ALWAYS:
4160 when = "JUMP";
4161 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004162 case JUMP_AND_KEEP_IF_TRUE:
4163 when = "JUMP_AND_KEEP_IF_TRUE";
4164 break;
4165 case JUMP_IF_FALSE:
4166 when = "JUMP_IF_FALSE";
4167 break;
4168 case JUMP_AND_KEEP_IF_FALSE:
4169 when = "JUMP_AND_KEEP_IF_FALSE";
4170 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004171 case JUMP_IF_COND_FALSE:
4172 when = "JUMP_IF_COND_FALSE";
4173 break;
4174 case JUMP_IF_COND_TRUE:
4175 when = "JUMP_IF_COND_TRUE";
4176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004178 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 iptr->isn_arg.jump.jump_where);
4180 }
4181 break;
4182
4183 case ISN_FOR:
4184 {
4185 forloop_T *forloop = &iptr->isn_arg.forloop;
4186
4187 smsg("%4d FOR $%d -> %d", current,
4188 forloop->for_idx, forloop->for_end);
4189 }
4190 break;
4191
4192 case ISN_TRY:
4193 {
4194 try_T *try = &iptr->isn_arg.try;
4195
4196 smsg("%4d TRY catch -> %d, finally -> %d", current,
4197 try->try_catch, try->try_finally);
4198 }
4199 break;
4200 case ISN_CATCH:
4201 // TODO
4202 smsg("%4d CATCH", current);
4203 break;
4204 case ISN_ENDTRY:
4205 smsg("%4d ENDTRY", current);
4206 break;
4207 case ISN_THROW:
4208 smsg("%4d THROW", current);
4209 break;
4210
4211 // expression operations on number
4212 case ISN_OPNR:
4213 case ISN_OPFLOAT:
4214 case ISN_OPANY:
4215 {
4216 char *what;
4217 char *ins;
4218
4219 switch (iptr->isn_arg.op.op_type)
4220 {
4221 case EXPR_MULT: what = "*"; break;
4222 case EXPR_DIV: what = "/"; break;
4223 case EXPR_REM: what = "%"; break;
4224 case EXPR_SUB: what = "-"; break;
4225 case EXPR_ADD: what = "+"; break;
4226 default: what = "???"; break;
4227 }
4228 switch (iptr->isn_type)
4229 {
4230 case ISN_OPNR: ins = "OPNR"; break;
4231 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4232 case ISN_OPANY: ins = "OPANY"; break;
4233 default: ins = "???"; break;
4234 }
4235 smsg("%4d %s %s", current, ins, what);
4236 }
4237 break;
4238
4239 case ISN_COMPAREBOOL:
4240 case ISN_COMPARESPECIAL:
4241 case ISN_COMPARENR:
4242 case ISN_COMPAREFLOAT:
4243 case ISN_COMPARESTRING:
4244 case ISN_COMPAREBLOB:
4245 case ISN_COMPARELIST:
4246 case ISN_COMPAREDICT:
4247 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004248 case ISN_COMPAREANY:
4249 {
4250 char *p;
4251 char buf[10];
4252 char *type;
4253
4254 switch (iptr->isn_arg.op.op_type)
4255 {
4256 case EXPR_EQUAL: p = "=="; break;
4257 case EXPR_NEQUAL: p = "!="; break;
4258 case EXPR_GREATER: p = ">"; break;
4259 case EXPR_GEQUAL: p = ">="; break;
4260 case EXPR_SMALLER: p = "<"; break;
4261 case EXPR_SEQUAL: p = "<="; break;
4262 case EXPR_MATCH: p = "=~"; break;
4263 case EXPR_IS: p = "is"; break;
4264 case EXPR_ISNOT: p = "isnot"; break;
4265 case EXPR_NOMATCH: p = "!~"; break;
4266 default: p = "???"; break;
4267 }
4268 STRCPY(buf, p);
4269 if (iptr->isn_arg.op.op_ic == TRUE)
4270 strcat(buf, "?");
4271 switch(iptr->isn_type)
4272 {
4273 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4274 case ISN_COMPARESPECIAL:
4275 type = "COMPARESPECIAL"; break;
4276 case ISN_COMPARENR: type = "COMPARENR"; break;
4277 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4278 case ISN_COMPARESTRING:
4279 type = "COMPARESTRING"; break;
4280 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4281 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4282 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4283 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4285 default: type = "???"; break;
4286 }
4287
4288 smsg("%4d %s %s", current, type, buf);
4289 }
4290 break;
4291
4292 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4293 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4294
4295 // expression operations
4296 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004297 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004298 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004299 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004300 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004301 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004302 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004303 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4304 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004305 case ISN_SLICE: smsg("%4d SLICE %lld",
4306 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004307 case ISN_GETITEM: smsg("%4d ITEM %lld",
4308 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004309 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4310 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 iptr->isn_arg.string); break;
4312 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4313
4314 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004315 case ISN_CHECKTYPE:
4316 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004317 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004318 char *tofree;
4319
Bram Moolenaare32e5162021-01-21 20:21:29 +01004320 if (ct->ct_arg_idx == 0)
4321 smsg("%4d CHECKTYPE %s stack[%d]", current,
4322 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004323 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004324 else
4325 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4326 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004327 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004328 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004329 vim_free(tofree);
4330 break;
4331 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004332 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4333 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4334 iptr->isn_arg.checklen.cl_min_len);
4335 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004336 case ISN_SETTYPE:
4337 {
4338 char *tofree;
4339
4340 smsg("%4d SETTYPE %s", current,
4341 type_name(iptr->isn_arg.type.ct_type, &tofree));
4342 vim_free(tofree);
4343 break;
4344 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004345 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 case ISN_2BOOL: if (iptr->isn_arg.number)
4347 smsg("%4d INVERT (!val)", current);
4348 else
4349 smsg("%4d 2BOOL (!!val)", current);
4350 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004351 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004352 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004353 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004354 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004355 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004356 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004357 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4358 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004359 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004360 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4361 smsg("%4d PUT %c above range",
4362 current, iptr->isn_arg.put.put_regname);
4363 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4364 smsg("%4d PUT %c range",
4365 current, iptr->isn_arg.put.put_regname);
4366 else
4367 smsg("%4d PUT %c %ld", current,
4368 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004369 (long)iptr->isn_arg.put.put_lnum);
4370 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371
Bram Moolenaar02194d22020-10-24 23:08:38 +02004372 // TODO: summarize modifiers
4373 case ISN_CMDMOD:
4374 {
4375 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004376 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004377 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4378
4379 buf = alloc(len + 1);
4380 if (buf != NULL)
4381 {
4382 (void)produce_cmdmods(
4383 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4384 smsg("%4d CMDMOD %s", current, buf);
4385 vim_free(buf);
4386 }
4387 break;
4388 }
4389 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004390
Bram Moolenaarb2049902021-01-24 12:53:53 +01004391 case ISN_PROF_START:
4392 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4393 break;
4394
4395 case ISN_PROF_END:
4396 smsg("%4d PROFILE END", current);
4397 break;
4398
Bram Moolenaar792f7862020-11-23 08:31:18 +01004399 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4400 iptr->isn_arg.unpack.unp_count,
4401 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4402 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004403 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4404 iptr->isn_arg.shuffle.shfl_item,
4405 iptr->isn_arg.shuffle.shfl_up);
4406 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 case ISN_DROP: smsg("%4d DROP", current); break;
4408 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004409
4410 out_flush(); // output one line at a time
4411 ui_breakcheck();
4412 if (got_int)
4413 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004414 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004415}
4416
4417/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004418 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 * list, etc. Mostly like what JavaScript does, except that empty list and
4420 * empty dictionary are FALSE.
4421 */
4422 int
4423tv2bool(typval_T *tv)
4424{
4425 switch (tv->v_type)
4426 {
4427 case VAR_NUMBER:
4428 return tv->vval.v_number != 0;
4429 case VAR_FLOAT:
4430#ifdef FEAT_FLOAT
4431 return tv->vval.v_float != 0.0;
4432#else
4433 break;
4434#endif
4435 case VAR_PARTIAL:
4436 return tv->vval.v_partial != NULL;
4437 case VAR_FUNC:
4438 case VAR_STRING:
4439 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4440 case VAR_LIST:
4441 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4442 case VAR_DICT:
4443 return tv->vval.v_dict != NULL
4444 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4445 case VAR_BOOL:
4446 case VAR_SPECIAL:
4447 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4448 case VAR_JOB:
4449#ifdef FEAT_JOB_CHANNEL
4450 return tv->vval.v_job != NULL;
4451#else
4452 break;
4453#endif
4454 case VAR_CHANNEL:
4455#ifdef FEAT_JOB_CHANNEL
4456 return tv->vval.v_channel != NULL;
4457#else
4458 break;
4459#endif
4460 case VAR_BLOB:
4461 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4462 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004463 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004464 case VAR_VOID:
4465 break;
4466 }
4467 return FALSE;
4468}
4469
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004470 void
4471emsg_using_string_as(typval_T *tv, int as_number)
4472{
4473 semsg(_(as_number ? e_using_string_as_number_str
4474 : e_using_string_as_bool_str),
4475 tv->vval.v_string == NULL
4476 ? (char_u *)"" : tv->vval.v_string);
4477}
4478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004479/*
4480 * If "tv" is a string give an error and return FAIL.
4481 */
4482 int
4483check_not_string(typval_T *tv)
4484{
4485 if (tv->v_type == VAR_STRING)
4486 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004487 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004488 clear_tv(tv);
4489 return FAIL;
4490 }
4491 return OK;
4492}
4493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004494
4495#endif // FEAT_EVAL