blob: 232c84ed7d530c1ff3ee3c10f80179f922af2c48 [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 Moolenaarc620c052020-07-08 15:16:19 +0200297 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
298 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 Moolenaar8a7d6542020-01-26 15:56:19 +0100648
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200649 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200650 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
651 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200652 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100653 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100654 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100655 if (error != FCERR_UNKNOWN)
656 {
657 if (error == FCERR_TOOMANY)
658 semsg(_(e_toomanyarg), ufunc->uf_name);
659 else
660 semsg(_(e_toofewarg), ufunc->uf_name);
661 return FAIL;
662 }
663
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100664 // The function has been compiled, can call it quickly. For a function
665 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100666 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100667 if (iptr != NULL)
668 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100669 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100670 iptr->isn_type = ISN_DCALL;
671 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
672 iptr->isn_arg.dfunc.cdf_argcount = argcount;
673 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100674 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100675 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676
677 if (call_prepare(argcount, argvars, ectx) == FAIL)
678 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200679 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 funcexe.evaluate = TRUE;
681
682 // Call the user function. Result goes in last position on the stack.
683 // TODO: add selfdict if there is one
684 error = call_user_func_check(ufunc, argcount, argvars,
685 STACK_TV_BOT(-1), &funcexe, NULL);
686
687 // Clear the arguments.
688 for (idx = 0; idx < argcount; ++idx)
689 clear_tv(&argvars[idx]);
690
691 if (error != FCERR_NONE)
692 {
693 user_func_error(error, ufunc->uf_name);
694 return FAIL;
695 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100696 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200697 // Error other than from calling the function itself.
698 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699 return OK;
700}
701
702/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200703 * Return TRUE if an error was given or CTRL-C was pressed.
704 */
705 static int
706vim9_aborting(int prev_called_emsg)
707{
708 return called_emsg > prev_called_emsg || got_int || did_throw;
709}
710
711/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 * Execute a function by "name".
713 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100714 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 * Returns FAIL if not found without an error message.
716 */
717 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100718call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100719{
720 ufunc_T *ufunc;
721
722 if (builtin_function(name, -1))
723 {
724 int func_idx = find_internal_func(name);
725
726 if (func_idx < 0)
727 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200728 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729 return FAIL;
730 return call_bfunc(func_idx, argcount, ectx);
731 }
732
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200733 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200734
735 if (ufunc == NULL)
736 {
737 int called_emsg_before = called_emsg;
738
739 if (script_autoload(name, TRUE))
740 // loaded a package, search for the function again
741 ufunc = find_func(name, FALSE, NULL);
742 if (vim9_aborting(called_emsg_before))
743 return FAIL; // bail out if loading the script caused an error
744 }
745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100747 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748
749 return FAIL;
750}
751
752 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200753call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200755 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200756 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100758 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759
760 if (tv->v_type == VAR_PARTIAL)
761 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200762 partial_T *pt = tv->vval.v_partial;
763 int i;
764
765 if (pt->pt_argc > 0)
766 {
767 // Make space for arguments from the partial, shift the "argcount"
768 // arguments up.
769 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
770 return FAIL;
771 for (i = 1; i <= argcount; ++i)
772 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
773 ectx->ec_stack.ga_len += pt->pt_argc;
774 argcount += pt->pt_argc;
775
776 // copy the arguments from the partial onto the stack
777 for (i = 0; i < pt->pt_argc; ++i)
778 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780
781 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100782 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 name = pt->pt_name;
785 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200786 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200788 if (name != NULL)
789 {
790 char_u fname_buf[FLEN_FIXED + 1];
791 char_u *tofree = NULL;
792 int error = FCERR_NONE;
793 char_u *fname;
794
795 // May need to translate <SNR>123_ to K_SNR.
796 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
797 if (error != FCERR_NONE)
798 res = FAIL;
799 else
800 res = call_by_name(fname, argcount, ectx, NULL);
801 vim_free(tofree);
802 }
803
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100804 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 {
806 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200807 semsg(_(e_unknownfunc),
808 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 return FAIL;
810 }
811 return OK;
812}
813
814/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200815 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
816 * TRUE.
817 */
818 static int
819error_if_locked(int lock, char *error)
820{
821 if (lock & (VAR_LOCKED | VAR_FIXED))
822 {
823 emsg(_(error));
824 return TRUE;
825 }
826 return FALSE;
827}
828
829/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100830 * Store "tv" in variable "name".
831 * This is for s: and g: variables.
832 */
833 static void
834store_var(char_u *name, typval_T *tv)
835{
836 funccal_entry_T entry;
837
838 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100839 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100840 restore_funccal();
841}
842
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100843/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100844 * Convert "tv" to a string.
845 * Return FAIL if not allowed.
846 */
847 static int
848do_2string(typval_T *tv, int is_2string_any)
849{
850 if (tv->v_type != VAR_STRING)
851 {
852 char_u *str;
853
854 if (is_2string_any)
855 {
856 switch (tv->v_type)
857 {
858 case VAR_SPECIAL:
859 case VAR_BOOL:
860 case VAR_NUMBER:
861 case VAR_FLOAT:
862 case VAR_BLOB: break;
863 default: to_string_error(tv->v_type);
864 return FAIL;
865 }
866 }
867 str = typval_tostring(tv);
868 clear_tv(tv);
869 tv->v_type = VAR_STRING;
870 tv->vval.v_string = str;
871 }
872 return OK;
873}
874
875/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100876 * When the value of "sv" is a null list of dict, allocate it.
877 */
878 static void
879allocate_if_null(typval_T *tv)
880{
881 switch (tv->v_type)
882 {
883 case VAR_LIST:
884 if (tv->vval.v_list == NULL)
885 rettv_list_alloc(tv);
886 break;
887 case VAR_DICT:
888 if (tv->vval.v_dict == NULL)
889 rettv_dict_alloc(tv);
890 break;
891 default:
892 break;
893 }
894}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200895
Bram Moolenaare7525c52021-01-09 13:20:37 +0100896/*
897 * Return the character "str[index]" where "index" is the character index. If
898 * "index" is out of range NULL is returned.
899 */
900 char_u *
901char_from_string(char_u *str, varnumber_T index)
902{
903 size_t nbyte = 0;
904 varnumber_T nchar = index;
905 size_t slen;
906
907 if (str == NULL)
908 return NULL;
909 slen = STRLEN(str);
910
911 // do the same as for a list: a negative index counts from the end
912 if (index < 0)
913 {
914 int clen = 0;
915
916 for (nbyte = 0; nbyte < slen; ++clen)
917 nbyte += MB_CPTR2LEN(str + nbyte);
918 nchar = clen + index;
919 if (nchar < 0)
920 // unlike list: index out of range results in empty string
921 return NULL;
922 }
923
924 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
925 nbyte += MB_CPTR2LEN(str + nbyte);
926 if (nbyte >= slen)
927 return NULL;
928 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
929}
930
931/*
932 * Get the byte index for character index "idx" in string "str" with length
933 * "str_len".
934 * If going over the end return "str_len".
935 * If "idx" is negative count from the end, -1 is the last character.
936 * When going over the start return -1.
937 */
938 static long
939char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
940{
941 varnumber_T nchar = idx;
942 size_t nbyte = 0;
943
944 if (nchar >= 0)
945 {
946 while (nchar > 0 && nbyte < str_len)
947 {
948 nbyte += MB_CPTR2LEN(str + nbyte);
949 --nchar;
950 }
951 }
952 else
953 {
954 nbyte = str_len;
955 while (nchar < 0 && nbyte > 0)
956 {
957 --nbyte;
958 nbyte -= mb_head_off(str, str + nbyte);
959 ++nchar;
960 }
961 if (nchar < 0)
962 return -1;
963 }
964 return (long)nbyte;
965}
966
967/*
968 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100969 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100970 * Return NULL when the result is empty.
971 */
972 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100973string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100974{
975 long start_byte, end_byte;
976 size_t slen;
977
978 if (str == NULL)
979 return NULL;
980 slen = STRLEN(str);
981 start_byte = char_idx2byte(str, slen, first);
982 if (start_byte < 0)
983 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100984 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100985 end_byte = (long)slen;
986 else
987 {
988 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100989 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100990 // end index is inclusive
991 end_byte += MB_CPTR2LEN(str + end_byte);
992 }
993
994 if (start_byte >= (long)slen || end_byte <= start_byte)
995 return NULL;
996 return vim_strnsave(str + start_byte, end_byte - start_byte);
997}
998
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100999 static svar_T *
1000get_script_svar(scriptref_T *sref, ectx_T *ectx)
1001{
1002 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1003 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1004 + ectx->ec_dfunc_idx;
1005 svar_T *sv;
1006
1007 if (sref->sref_seq != si->sn_script_seq)
1008 {
1009 // The script was reloaded after the function was
1010 // compiled, the script_idx may not be valid.
1011 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1012 dfunc->df_ufunc->uf_name_exp);
1013 return NULL;
1014 }
1015 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1016 if (!equal_type(sv->sv_type, sref->sref_type))
1017 {
1018 emsg(_(e_script_variable_type_changed));
1019 return NULL;
1020 }
1021 return sv;
1022}
1023
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001024/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 * Execute a function by "name".
1026 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001027 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 */
1029 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001030call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031{
Bram Moolenaared677f52020-08-12 16:38:10 +02001032 int called_emsg_before = called_emsg;
1033 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034
Bram Moolenaared677f52020-08-12 16:38:10 +02001035 res = call_by_name(name, argcount, ectx, iptr);
1036 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001037 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001038 dictitem_T *v;
1039
1040 v = find_var(name, NULL, FALSE);
1041 if (v == NULL)
1042 {
1043 semsg(_(e_unknownfunc), name);
1044 return FAIL;
1045 }
1046 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1047 {
1048 semsg(_(e_unknownfunc), name);
1049 return FAIL;
1050 }
1051 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001053 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054}
1055
1056/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001057 * When a function reference is used, fill a partial with the information
1058 * needed, especially when it is used as a closure.
1059 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001060 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001061fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1062{
1063 pt->pt_func = ufunc;
1064 pt->pt_refcount = 1;
1065
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001066 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001067 {
1068 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1069 + ectx->ec_dfunc_idx;
1070
1071 // The closure needs to find arguments and local
1072 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001073 pt->pt_outer.out_stack = &ectx->ec_stack;
1074 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1075 pt->pt_outer.out_up = ectx->ec_outer;
1076 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001077
1078 // If this function returns and the closure is still
1079 // being used, we need to make a copy of the context
1080 // (arguments and local variables). Store a reference
1081 // to the partial so we can handle that.
1082 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1083 {
1084 vim_free(pt);
1085 return FAIL;
1086 }
1087 // Extra variable keeps the count of closures created
1088 // in the current function call.
1089 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1090 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1091
1092 ((partial_T **)ectx->ec_funcrefs.ga_data)
1093 [ectx->ec_funcrefs.ga_len] = pt;
1094 ++pt->pt_refcount;
1095 ++ectx->ec_funcrefs.ga_len;
1096 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001097 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001098 return OK;
1099}
1100
1101/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 * Call a "def" function from old Vim script.
1103 * Return OK or FAIL.
1104 */
1105 int
1106call_def_function(
1107 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001108 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001110 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001111 typval_T *rettv) // return value
1112{
1113 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001114 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001115 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 typval_T *tv;
1117 int idx;
1118 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001119 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001120 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001121 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001122 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001123 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001124 msglist_T **saved_msg_list = NULL;
1125 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001126 cmdmod_T save_cmdmod;
1127 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001128 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001129 int save_emsg_silent_def = emsg_silent_def;
1130 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001131 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001132 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133
1134// Get pointer to item in the stack.
1135#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1136
1137// Get pointer to item at the bottom of the stack, -1 is the bottom.
1138#undef STACK_TV_BOT
1139#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1140
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001141// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001142#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 +01001143
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001144 if (ufunc->uf_def_status == UF_NOT_COMPILED
1145 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001146 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001147 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001148 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001149 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001150 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001151 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001152 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001153
Bram Moolenaar09689a02020-05-09 22:50:08 +02001154 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001155 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001156 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1157 + ufunc->uf_dfunc_idx;
1158 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001159 {
1160 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001161 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001162 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001163 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001165 // If depth of calling is getting too high, don't execute the function.
1166 orig_funcdepth = funcdepth_get();
1167 if (funcdepth_increment() == FAIL)
1168 return FAIL;
1169
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001170 CLEAR_FIELD(ectx);
1171 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1172 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1173 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001174 {
1175 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001176 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001179 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001181 // Put arguments on the stack, but no more than what the function expects.
1182 // A lambda can be called with more arguments than it uses.
1183 for (idx = 0; idx < argc
1184 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1185 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001187 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001188 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1189 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001190 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 copy_tv(&argv[idx], STACK_TV_BOT(0));
1192 ++ectx.ec_stack.ga_len;
1193 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001194
1195 // Turn varargs into a list. Empty list if no args.
1196 if (ufunc->uf_va_name != NULL)
1197 {
1198 int vararg_count = argc - ufunc->uf_args.ga_len;
1199
1200 if (vararg_count < 0)
1201 vararg_count = 0;
1202 else
1203 argc -= vararg_count;
1204 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001205 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001206
1207 // Check the type of the list items.
1208 tv = STACK_TV_BOT(-1);
1209 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001210 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001211 && ufunc->uf_va_type->tt_member != &t_any
1212 && tv->vval.v_list != NULL)
1213 {
1214 type_T *expected = ufunc->uf_va_type->tt_member;
1215 listitem_T *li = tv->vval.v_list->lv_first;
1216
1217 for (idx = 0; idx < vararg_count; ++idx)
1218 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001219 if (check_typval_type(expected, &li->li_tv,
1220 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001221 goto failed_early;
1222 li = li->li_next;
1223 }
1224 }
1225
Bram Moolenaar23e03252020-04-12 22:22:31 +02001226 if (defcount > 0)
1227 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001228 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001229 --ectx.ec_stack.ga_len;
1230 }
1231
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001232 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001233 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001234 if (defcount > 0)
1235 for (idx = 0; idx < defcount; ++idx)
1236 {
1237 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1238 ++ectx.ec_stack.ga_len;
1239 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001240 if (ufunc->uf_va_name != NULL)
1241 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242
1243 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001244 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1245 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001247 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001248 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1249 + ufunc->uf_dfunc_idx;
1250 ufunc_T *base_ufunc = dfunc->df_ufunc;
1251
1252 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1253 // by copy_func().
1254 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001255 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001256 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1257 if (ectx.ec_outer == NULL)
1258 goto failed_early;
1259 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001260 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001261 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1262 {
1263 if (current_ectx->ec_outer != NULL)
1264 *ectx.ec_outer = *current_ectx->ec_outer;
1265 }
1266 else
1267 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001268 }
1269 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001270 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1271 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001272 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001273 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001274
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 // dummy frame entries
1276 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1277 {
1278 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1279 ++ectx.ec_stack.ga_len;
1280 }
1281
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001282 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001283 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001284 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1285 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001287 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001288 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001289 ectx.ec_stack.ga_len += dfunc->df_varcount;
1290 if (dfunc->df_has_closure)
1291 {
1292 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1293 STACK_TV_VAR(idx)->vval.v_number = 0;
1294 ++ectx.ec_stack.ga_len;
1295 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001296
1297 ectx.ec_instr = dfunc->df_instr;
1298 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001299
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001300 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001301 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001302 estack_push_ufunc(ufunc, 1);
1303 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001304 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1305
Bram Moolenaar352134b2020-10-17 22:04:08 +02001306 // Use a specific location for storing error messages to be converted to an
1307 // exception.
1308 saved_msg_list = msg_list;
1309 msg_list = &private_msg_list;
1310
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001311 // Do turn errors into exceptions.
1312 suppress_errthrow = FALSE;
1313
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001314 // When ":silent!" was used before calling then we still abort the
1315 // function. If ":silent!" is used in the function then we don't.
1316 emsg_silent_def = emsg_silent;
1317 did_emsg_def = 0;
1318
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001319 // Decide where to start execution, handles optional arguments.
1320 init_instr_idx(ufunc, argc, &ectx);
1321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 for (;;)
1323 {
1324 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001325
Bram Moolenaar270d0382020-05-15 21:42:53 +02001326 if (++breakcheck_count >= 100)
1327 {
1328 line_breakcheck();
1329 breakcheck_count = 0;
1330 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 if (got_int)
1332 {
1333 // Turn CTRL-C into an exception.
1334 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001335 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001336 goto failed;
1337 did_throw = TRUE;
1338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339
Bram Moolenaara26b9702020-04-18 19:53:28 +02001340 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1341 {
1342 // Turn an error message into an exception.
1343 did_emsg = FALSE;
1344 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1345 goto failed;
1346 did_throw = TRUE;
1347 *msg_list = NULL;
1348 }
1349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 if (did_throw && !ectx.ec_in_catch)
1351 {
1352 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001353 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354
1355 // An exception jumps to the first catch, finally, or returns from
1356 // the current function.
1357 if (trystack->ga_len > 0)
1358 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001359 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 {
1361 // jump to ":catch" or ":finally"
1362 ectx.ec_in_catch = TRUE;
1363 ectx.ec_iidx = trycmd->tcd_catch_idx;
1364 }
1365 else
1366 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001367 // Not inside try or need to return from current functions.
1368 // Push a dummy return value.
1369 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1370 goto failed;
1371 tv = STACK_TV_BOT(0);
1372 tv->v_type = VAR_NUMBER;
1373 tv->vval.v_number = 0;
1374 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001375 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001377 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001378 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001379 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1380 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 goto done;
1382 }
1383
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001384 if (func_return(&ectx) == FAIL)
1385 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 }
1387 continue;
1388 }
1389
1390 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1391 switch (iptr->isn_type)
1392 {
1393 // execute Ex command line
1394 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001395 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001396 source_cookie_T cookie;
1397
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001399 // Pass getsourceline to get an error for a missing ":end"
1400 // command.
1401 CLEAR_FIELD(cookie);
1402 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1403 if (do_cmdline(iptr->isn_arg.string,
1404 getsourceline, &cookie,
1405 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1406 == FAIL
1407 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001408 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001409 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 break;
1411
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001412 // execute Ex command from pieces on the stack
1413 case ISN_EXECCONCAT:
1414 {
1415 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001416 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001417 int pass;
1418 int i;
1419 char_u *cmd = NULL;
1420 char_u *str;
1421
1422 for (pass = 1; pass <= 2; ++pass)
1423 {
1424 for (i = 0; i < count; ++i)
1425 {
1426 tv = STACK_TV_BOT(i - count);
1427 str = tv->vval.v_string;
1428 if (str != NULL && *str != NUL)
1429 {
1430 if (pass == 2)
1431 STRCPY(cmd + len, str);
1432 len += STRLEN(str);
1433 }
1434 if (pass == 2)
1435 clear_tv(tv);
1436 }
1437 if (pass == 1)
1438 {
1439 cmd = alloc(len + 1);
1440 if (cmd == NULL)
1441 goto failed;
1442 len = 0;
1443 }
1444 }
1445
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001446 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001447 do_cmdline_cmd(cmd);
1448 vim_free(cmd);
1449 }
1450 break;
1451
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001452 // execute :echo {string} ...
1453 case ISN_ECHO:
1454 {
1455 int count = iptr->isn_arg.echo.echo_count;
1456 int atstart = TRUE;
1457 int needclr = TRUE;
1458
1459 for (idx = 0; idx < count; ++idx)
1460 {
1461 tv = STACK_TV_BOT(idx - count);
1462 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1463 &atstart, &needclr);
1464 clear_tv(tv);
1465 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001466 if (needclr)
1467 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 ectx.ec_stack.ga_len -= count;
1469 }
1470 break;
1471
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001472 // :execute {string} ...
1473 // :echomsg {string} ...
1474 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001475 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001476 case ISN_ECHOMSG:
1477 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001478 {
1479 int count = iptr->isn_arg.number;
1480 garray_T ga;
1481 char_u buf[NUMBUFLEN];
1482 char_u *p;
1483 int len;
1484 int failed = FALSE;
1485
1486 ga_init2(&ga, 1, 80);
1487 for (idx = 0; idx < count; ++idx)
1488 {
1489 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001490 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001491 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001492 if (tv->v_type == VAR_CHANNEL
1493 || tv->v_type == VAR_JOB)
1494 {
1495 SOURCING_LNUM = iptr->isn_lnum;
1496 emsg(_(e_inval_string));
1497 break;
1498 }
1499 else
1500 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001501 }
1502 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001503 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001504
1505 len = (int)STRLEN(p);
1506 if (ga_grow(&ga, len + 2) == FAIL)
1507 failed = TRUE;
1508 else
1509 {
1510 if (ga.ga_len > 0)
1511 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1512 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1513 ga.ga_len += len;
1514 }
1515 clear_tv(tv);
1516 }
1517 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001518 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001519 {
1520 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001521 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001522 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001523
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001524 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001525 {
1526 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001527 {
1528 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001529 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001530 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001531 {
1532 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001533 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001534 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001535 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001536 else
1537 {
1538 msg_sb_eol();
1539 if (iptr->isn_type == ISN_ECHOMSG)
1540 {
1541 msg_attr(ga.ga_data, echo_attr);
1542 out_flush();
1543 }
1544 else
1545 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001546 SOURCING_LNUM = iptr->isn_lnum;
1547 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001548 }
1549 }
1550 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001551 ga_clear(&ga);
1552 }
1553 break;
1554
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 // load local variable or argument
1556 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001557 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 goto failed;
1559 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1560 ++ectx.ec_stack.ga_len;
1561 break;
1562
1563 // load v: variable
1564 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001565 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 goto failed;
1567 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1568 ++ectx.ec_stack.ga_len;
1569 break;
1570
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001571 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 case ISN_LOADSCRIPT:
1573 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001574 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 svar_T *sv;
1576
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001577 sv = get_script_svar(sref, &ectx);
1578 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001579 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001580 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001581 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 goto failed;
1583 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1584 ++ectx.ec_stack.ga_len;
1585 }
1586 break;
1587
1588 // load s: variable in old script
1589 case ISN_LOADS:
1590 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001591 hashtab_T *ht = &SCRIPT_VARS(
1592 iptr->isn_arg.loadstore.ls_sid);
1593 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 if (di == NULL)
1597 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001598 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001599 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001600 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 }
1602 else
1603 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001604 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 goto failed;
1606 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1607 ++ectx.ec_stack.ga_len;
1608 }
1609 }
1610 break;
1611
Bram Moolenaard3aac292020-04-19 14:32:17 +02001612 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001614 case ISN_LOADB:
1615 case ISN_LOADW:
1616 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001618 dictitem_T *di = NULL;
1619 hashtab_T *ht = NULL;
1620 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001621
Bram Moolenaard3aac292020-04-19 14:32:17 +02001622 switch (iptr->isn_type)
1623 {
1624 case ISN_LOADG:
1625 ht = get_globvar_ht();
1626 namespace = 'g';
1627 break;
1628 case ISN_LOADB:
1629 ht = &curbuf->b_vars->dv_hashtab;
1630 namespace = 'b';
1631 break;
1632 case ISN_LOADW:
1633 ht = &curwin->w_vars->dv_hashtab;
1634 namespace = 'w';
1635 break;
1636 case ISN_LOADT:
1637 ht = &curtab->tp_vars->dv_hashtab;
1638 namespace = 't';
1639 break;
1640 default: // Cannot reach here
1641 goto failed;
1642 }
1643 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 if (di == NULL)
1646 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001647 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001648 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001649 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001650 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 }
1652 else
1653 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001654 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 goto failed;
1656 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1657 ++ectx.ec_stack.ga_len;
1658 }
1659 }
1660 break;
1661
Bram Moolenaar03290b82020-12-19 16:30:44 +01001662 // load autoload variable
1663 case ISN_LOADAUTO:
1664 {
1665 char_u *name = iptr->isn_arg.string;
1666
1667 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1668 goto failed;
1669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001670 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001671 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1672 goto on_error;
1673 ++ectx.ec_stack.ga_len;
1674 }
1675 break;
1676
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001677 // load g:/b:/w:/t: namespace
1678 case ISN_LOADGDICT:
1679 case ISN_LOADBDICT:
1680 case ISN_LOADWDICT:
1681 case ISN_LOADTDICT:
1682 {
1683 dict_T *d = NULL;
1684
1685 switch (iptr->isn_type)
1686 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001687 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1688 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1689 case ISN_LOADWDICT: d = curwin->w_vars; break;
1690 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001691 default: // Cannot reach here
1692 goto failed;
1693 }
1694 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1695 goto failed;
1696 tv = STACK_TV_BOT(0);
1697 tv->v_type = VAR_DICT;
1698 tv->v_lock = 0;
1699 tv->vval.v_dict = d;
1700 ++ectx.ec_stack.ga_len;
1701 }
1702 break;
1703
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 // load &option
1705 case ISN_LOADOPT:
1706 {
1707 typval_T optval;
1708 char_u *name = iptr->isn_arg.string;
1709
Bram Moolenaara8c17702020-04-01 21:17:24 +02001710 // This is not expected to fail, name is checked during
1711 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001712 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001714 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001715 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 *STACK_TV_BOT(0) = optval;
1717 ++ectx.ec_stack.ga_len;
1718 }
1719 break;
1720
1721 // load $ENV
1722 case ISN_LOADENV:
1723 {
1724 typval_T optval;
1725 char_u *name = iptr->isn_arg.string;
1726
Bram Moolenaar270d0382020-05-15 21:42:53 +02001727 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001729 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001730 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001731 *STACK_TV_BOT(0) = optval;
1732 ++ectx.ec_stack.ga_len;
1733 }
1734 break;
1735
1736 // load @register
1737 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001738 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 goto failed;
1740 tv = STACK_TV_BOT(0);
1741 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001742 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001743 // This may result in NULL, which should be equivalent to an
1744 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 tv->vval.v_string = get_reg_contents(
1746 iptr->isn_arg.number, GREG_EXPR_SRC);
1747 ++ectx.ec_stack.ga_len;
1748 break;
1749
1750 // store local variable
1751 case ISN_STORE:
1752 --ectx.ec_stack.ga_len;
1753 tv = STACK_TV_VAR(iptr->isn_arg.number);
1754 clear_tv(tv);
1755 *tv = *STACK_TV_BOT(0);
1756 break;
1757
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001758 // store s: variable in old script
1759 case ISN_STORES:
1760 {
1761 hashtab_T *ht = &SCRIPT_VARS(
1762 iptr->isn_arg.loadstore.ls_sid);
1763 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001764 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001765
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001766 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001767 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001768 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001769 else
1770 {
1771 clear_tv(&di->di_tv);
1772 di->di_tv = *STACK_TV_BOT(0);
1773 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001774 }
1775 break;
1776
1777 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 case ISN_STORESCRIPT:
1779 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001780 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1781 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001783 sv = get_script_svar(sref, &ectx);
1784 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001785 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001786 --ectx.ec_stack.ga_len;
1787 clear_tv(sv->sv_tv);
1788 *sv->sv_tv = *STACK_TV_BOT(0);
1789 }
1790 break;
1791
1792 // store option
1793 case ISN_STOREOPT:
1794 {
1795 long n = 0;
1796 char_u *s = NULL;
1797 char *msg;
1798
1799 --ectx.ec_stack.ga_len;
1800 tv = STACK_TV_BOT(0);
1801 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001802 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001804 if (s == NULL)
1805 s = (char_u *)"";
1806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001808 // must be VAR_NUMBER, CHECKTYPE makes sure
1809 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1811 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001812 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 if (msg != NULL)
1814 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001815 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001817 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 }
1820 break;
1821
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001822 // store $ENV
1823 case ISN_STOREENV:
1824 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001825 tv = STACK_TV_BOT(0);
1826 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1827 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001828 break;
1829
1830 // store @r
1831 case ISN_STOREREG:
1832 {
1833 int reg = iptr->isn_arg.number;
1834
1835 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001836 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001837 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001838 tv_get_string(tv), -1, FALSE);
1839 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001840 }
1841 break;
1842
1843 // store v: variable
1844 case ISN_STOREV:
1845 --ectx.ec_stack.ga_len;
1846 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1847 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001848 // should not happen, type is checked when compiling
1849 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001850 break;
1851
Bram Moolenaard3aac292020-04-19 14:32:17 +02001852 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001854 case ISN_STOREB:
1855 case ISN_STOREW:
1856 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001858 dictitem_T *di;
1859 hashtab_T *ht;
1860 char_u *name = iptr->isn_arg.string + 2;
1861
Bram Moolenaard3aac292020-04-19 14:32:17 +02001862 switch (iptr->isn_type)
1863 {
1864 case ISN_STOREG:
1865 ht = get_globvar_ht();
1866 break;
1867 case ISN_STOREB:
1868 ht = &curbuf->b_vars->dv_hashtab;
1869 break;
1870 case ISN_STOREW:
1871 ht = &curwin->w_vars->dv_hashtab;
1872 break;
1873 case ISN_STORET:
1874 ht = &curtab->tp_vars->dv_hashtab;
1875 break;
1876 default: // Cannot reach here
1877 goto failed;
1878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879
1880 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001881 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001883 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 else
1885 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001886 SOURCING_LNUM = iptr->isn_lnum;
1887 if (var_check_permission(di, name) == FAIL)
1888 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889 clear_tv(&di->di_tv);
1890 di->di_tv = *STACK_TV_BOT(0);
1891 }
1892 }
1893 break;
1894
Bram Moolenaar03290b82020-12-19 16:30:44 +01001895 // store an autoload variable
1896 case ISN_STOREAUTO:
1897 SOURCING_LNUM = iptr->isn_lnum;
1898 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1899 clear_tv(STACK_TV_BOT(-1));
1900 --ectx.ec_stack.ga_len;
1901 break;
1902
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 // store number in local variable
1904 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001905 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 clear_tv(tv);
1907 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001908 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 break;
1910
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001911 // store value in list or dict variable
1912 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001913 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001914 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001915 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001916 typval_T *tv_dest = STACK_TV_BOT(-1);
1917 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001918
Bram Moolenaar752fc692021-01-04 21:57:11 +01001919 // Stack contains:
1920 // -3 value to be stored
1921 // -2 index
1922 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001923 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001924 SOURCING_LNUM = iptr->isn_lnum;
1925 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001926 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001927 dest_type = tv_dest->v_type;
1928 if (dest_type == VAR_DICT)
1929 status = do_2string(tv_idx, TRUE);
1930 else if (dest_type == VAR_LIST
1931 && tv_idx->v_type != VAR_NUMBER)
1932 {
1933 emsg(_(e_number_exp));
1934 status = FAIL;
1935 }
1936 }
1937 else if (dest_type != tv_dest->v_type)
1938 {
1939 // just in case, should be OK
1940 semsg(_(e_expected_str_but_got_str),
1941 vartype_name(dest_type),
1942 vartype_name(tv_dest->v_type));
1943 status = FAIL;
1944 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001945
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001946 if (status == OK && dest_type == VAR_LIST)
1947 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001948 long lidx = (long)tv_idx->vval.v_number;
1949 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001950
1951 if (list == NULL)
1952 {
1953 emsg(_(e_list_not_set));
1954 goto on_error;
1955 }
1956 if (lidx < 0 && list->lv_len + lidx >= 0)
1957 // negative index is relative to the end
1958 lidx = list->lv_len + lidx;
1959 if (lidx < 0 || lidx > list->lv_len)
1960 {
1961 semsg(_(e_listidx), lidx);
1962 goto on_error;
1963 }
1964 if (lidx < list->lv_len)
1965 {
1966 listitem_T *li = list_find(list, lidx);
1967
1968 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001969 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001970 goto on_error;
1971 // overwrite existing list item
1972 clear_tv(&li->li_tv);
1973 li->li_tv = *tv;
1974 }
1975 else
1976 {
1977 if (error_if_locked(list->lv_lock,
1978 e_cannot_change_list))
1979 goto on_error;
1980 // append to list, only fails when out of memory
1981 if (list_append_tv(list, tv) == FAIL)
1982 goto failed;
1983 clear_tv(tv);
1984 }
1985 }
1986 else if (status == OK && dest_type == VAR_DICT)
1987 {
1988 char_u *key = tv_idx->vval.v_string;
1989 dict_T *dict = tv_dest->vval.v_dict;
1990 dictitem_T *di;
1991
1992 SOURCING_LNUM = iptr->isn_lnum;
1993 if (dict == NULL)
1994 {
1995 emsg(_(e_dictionary_not_set));
1996 goto on_error;
1997 }
1998 if (key == NULL)
1999 key = (char_u *)"";
2000 di = dict_find(dict, key, -1);
2001 if (di != NULL)
2002 {
2003 if (error_if_locked(di->di_tv.v_lock,
2004 e_cannot_change_dict_item))
2005 goto on_error;
2006 // overwrite existing value
2007 clear_tv(&di->di_tv);
2008 di->di_tv = *tv;
2009 }
2010 else
2011 {
2012 if (error_if_locked(dict->dv_lock,
2013 e_cannot_change_dict))
2014 goto on_error;
2015 // add to dict, only fails when out of memory
2016 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2017 goto failed;
2018 clear_tv(tv);
2019 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002020 }
2021 else
2022 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002023 status = FAIL;
2024 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002025 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002026
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002027 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002028 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002029 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002030 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002031 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002032 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002033 goto on_error;
2034 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002035 }
2036 break;
2037
Bram Moolenaar0186e582021-01-10 18:33:11 +01002038 // load or store variable or argument from outer scope
2039 case ISN_LOADOUTER:
2040 case ISN_STOREOUTER:
2041 {
2042 int depth = iptr->isn_arg.outer.outer_depth;
2043 outer_T *outer = ectx.ec_outer;
2044
2045 while (depth > 1 && outer != NULL)
2046 {
2047 outer = outer->out_up;
2048 --depth;
2049 }
2050 if (outer == NULL)
2051 {
2052 SOURCING_LNUM = iptr->isn_lnum;
2053 iemsg("LOADOUTER depth more than scope levels");
2054 goto failed;
2055 }
2056 tv = ((typval_T *)outer->out_stack->ga_data)
2057 + outer->out_frame_idx + STACK_FRAME_SIZE
2058 + iptr->isn_arg.outer.outer_idx;
2059 if (iptr->isn_type == ISN_LOADOUTER)
2060 {
2061 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2062 goto failed;
2063 copy_tv(tv, STACK_TV_BOT(0));
2064 ++ectx.ec_stack.ga_len;
2065 }
2066 else
2067 {
2068 --ectx.ec_stack.ga_len;
2069 clear_tv(tv);
2070 *tv = *STACK_TV_BOT(0);
2071 }
2072 }
2073 break;
2074
Bram Moolenaar752fc692021-01-04 21:57:11 +01002075 // unlet item in list or dict variable
2076 case ISN_UNLETINDEX:
2077 {
2078 typval_T *tv_idx = STACK_TV_BOT(-2);
2079 typval_T *tv_dest = STACK_TV_BOT(-1);
2080 int status = OK;
2081
2082 // Stack contains:
2083 // -2 index
2084 // -1 dict or list
2085 if (tv_dest->v_type == VAR_DICT)
2086 {
2087 // unlet a dict item, index must be a string
2088 if (tv_idx->v_type != VAR_STRING)
2089 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002090 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002091 semsg(_(e_expected_str_but_got_str),
2092 vartype_name(VAR_STRING),
2093 vartype_name(tv_idx->v_type));
2094 status = FAIL;
2095 }
2096 else
2097 {
2098 dict_T *d = tv_dest->vval.v_dict;
2099 char_u *key = tv_idx->vval.v_string;
2100 dictitem_T *di = NULL;
2101
2102 if (key == NULL)
2103 key = (char_u *)"";
2104 if (d != NULL)
2105 di = dict_find(d, key, (int)STRLEN(key));
2106 if (di == NULL)
2107 {
2108 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002109 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002110 semsg(_(e_dictkey), key);
2111 status = FAIL;
2112 }
2113 else
2114 {
2115 // TODO: check for dict or item locked
2116 dictitem_remove(d, di);
2117 }
2118 }
2119 }
2120 else if (tv_dest->v_type == VAR_LIST)
2121 {
2122 // unlet a List item, index must be a number
2123 if (tv_idx->v_type != VAR_NUMBER)
2124 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002126 semsg(_(e_expected_str_but_got_str),
2127 vartype_name(VAR_NUMBER),
2128 vartype_name(tv_idx->v_type));
2129 status = FAIL;
2130 }
2131 else
2132 {
2133 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002134 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002135 listitem_T *li = NULL;
2136
2137 li = list_find(l, n);
2138 if (li == NULL)
2139 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002141 semsg(_(e_listidx), n);
2142 status = FAIL;
2143 }
2144 else
2145 // TODO: check for list or item locked
2146 listitem_remove(l, li);
2147 }
2148 }
2149 else
2150 {
2151 status = FAIL;
2152 semsg(_(e_cannot_index_str),
2153 vartype_name(tv_dest->v_type));
2154 }
2155
2156 clear_tv(tv_idx);
2157 clear_tv(tv_dest);
2158 ectx.ec_stack.ga_len -= 2;
2159 if (status == FAIL)
2160 goto on_error;
2161 }
2162 break;
2163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 // push constant
2165 case ISN_PUSHNR:
2166 case ISN_PUSHBOOL:
2167 case ISN_PUSHSPEC:
2168 case ISN_PUSHF:
2169 case ISN_PUSHS:
2170 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002171 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002172 case ISN_PUSHCHANNEL:
2173 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002174 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 goto failed;
2176 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002177 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002178 ++ectx.ec_stack.ga_len;
2179 switch (iptr->isn_type)
2180 {
2181 case ISN_PUSHNR:
2182 tv->v_type = VAR_NUMBER;
2183 tv->vval.v_number = iptr->isn_arg.number;
2184 break;
2185 case ISN_PUSHBOOL:
2186 tv->v_type = VAR_BOOL;
2187 tv->vval.v_number = iptr->isn_arg.number;
2188 break;
2189 case ISN_PUSHSPEC:
2190 tv->v_type = VAR_SPECIAL;
2191 tv->vval.v_number = iptr->isn_arg.number;
2192 break;
2193#ifdef FEAT_FLOAT
2194 case ISN_PUSHF:
2195 tv->v_type = VAR_FLOAT;
2196 tv->vval.v_float = iptr->isn_arg.fnumber;
2197 break;
2198#endif
2199 case ISN_PUSHBLOB:
2200 blob_copy(iptr->isn_arg.blob, tv);
2201 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002202 case ISN_PUSHFUNC:
2203 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002204 if (iptr->isn_arg.string == NULL)
2205 tv->vval.v_string = NULL;
2206 else
2207 tv->vval.v_string =
2208 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002209 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002210 case ISN_PUSHCHANNEL:
2211#ifdef FEAT_JOB_CHANNEL
2212 tv->v_type = VAR_CHANNEL;
2213 tv->vval.v_channel = iptr->isn_arg.channel;
2214 if (tv->vval.v_channel != NULL)
2215 ++tv->vval.v_channel->ch_refcount;
2216#endif
2217 break;
2218 case ISN_PUSHJOB:
2219#ifdef FEAT_JOB_CHANNEL
2220 tv->v_type = VAR_JOB;
2221 tv->vval.v_job = iptr->isn_arg.job;
2222 if (tv->vval.v_job != NULL)
2223 ++tv->vval.v_job->jv_refcount;
2224#endif
2225 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 default:
2227 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002228 tv->vval.v_string = vim_strsave(
2229 iptr->isn_arg.string == NULL
2230 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 }
2232 break;
2233
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002234 case ISN_UNLET:
2235 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2236 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002237 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002238 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002239 case ISN_UNLETENV:
2240 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2241 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002242
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002243 case ISN_LOCKCONST:
2244 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2245 break;
2246
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 // create a list from items on the stack; uses a single allocation
2248 // for the list header and the items
2249 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002250 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2251 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 break;
2253
2254 // create a dict from items on the stack
2255 case ISN_NEWDICT:
2256 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002257 int count = iptr->isn_arg.number;
2258 dict_T *dict = dict_alloc();
2259 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002260 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261
2262 if (dict == NULL)
2263 goto failed;
2264 for (idx = 0; idx < count; ++idx)
2265 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002266 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002268 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002269 key = tv->vval.v_string == NULL
2270 ? (char_u *)"" : tv->vval.v_string;
2271 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002272 if (item != NULL)
2273 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002274 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002275 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002276 dict_unref(dict);
2277 goto on_error;
2278 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002279 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 clear_tv(tv);
2281 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002282 {
2283 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002285 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2287 item->di_tv.v_lock = 0;
2288 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002289 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002290 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002291 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002292 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002293 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 }
2295
2296 if (count > 0)
2297 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002298 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 goto failed;
2300 else
2301 ++ectx.ec_stack.ga_len;
2302 tv = STACK_TV_BOT(-1);
2303 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002304 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 tv->vval.v_dict = dict;
2306 ++dict->dv_refcount;
2307 }
2308 break;
2309
2310 // call a :def function
2311 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002312 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002313 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 iptr->isn_arg.dfunc.cdf_argcount,
2315 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002316 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 break;
2318
2319 // call a builtin function
2320 case ISN_BCALL:
2321 SOURCING_LNUM = iptr->isn_lnum;
2322 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2323 iptr->isn_arg.bfunc.cbf_argcount,
2324 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002325 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 break;
2327
2328 // call a funcref or partial
2329 case ISN_PCALL:
2330 {
2331 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2332 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002333 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334
2335 SOURCING_LNUM = iptr->isn_lnum;
2336 if (pfunc->cpf_top)
2337 {
2338 // funcref is above the arguments
2339 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2340 }
2341 else
2342 {
2343 // Get the funcref from the stack.
2344 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002345 partial_tv = *STACK_TV_BOT(0);
2346 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 }
2348 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002349 if (tv == &partial_tv)
2350 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002352 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 }
2354 break;
2355
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002356 case ISN_PCALL_END:
2357 // PCALL finished, arguments have been consumed and replaced by
2358 // the return value. Now clear the funcref from the stack,
2359 // and move the return value in its place.
2360 --ectx.ec_stack.ga_len;
2361 clear_tv(STACK_TV_BOT(-1));
2362 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2363 break;
2364
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 // call a user defined function or funcref/partial
2366 case ISN_UCALL:
2367 {
2368 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2369
2370 SOURCING_LNUM = iptr->isn_lnum;
2371 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002372 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002373 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 }
2375 break;
2376
2377 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002378 case ISN_RETURN_ZERO:
2379 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2380 goto failed;
2381 tv = STACK_TV_BOT(0);
2382 ++ectx.ec_stack.ga_len;
2383 tv->v_type = VAR_NUMBER;
2384 tv->vval.v_number = 0;
2385 tv->v_lock = 0;
2386 // FALLTHROUGH
2387
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 case ISN_RETURN:
2389 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002390 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002391 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002392
2393 if (trystack->ga_len > 0)
2394 trycmd = ((trycmd_T *)trystack->ga_data)
2395 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002396 if (trycmd != NULL
2397 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 && trycmd->tcd_finally_idx != 0)
2399 {
2400 // jump to ":finally"
2401 ectx.ec_iidx = trycmd->tcd_finally_idx;
2402 trycmd->tcd_return = TRUE;
2403 }
2404 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002405 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 }
2407 break;
2408
2409 // push a function reference to a compiled function
2410 case ISN_FUNCREF:
2411 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002412 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2413 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2414 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 if (pt == NULL)
2417 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002418 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002419 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002420 vim_free(pt);
2421 goto failed;
2422 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002423 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2424 &ectx) == FAIL)
2425 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002426
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 tv = STACK_TV_BOT(0);
2428 ++ectx.ec_stack.ga_len;
2429 tv->vval.v_partial = pt;
2430 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002431 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 }
2433 break;
2434
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002435 // Create a global function from a lambda.
2436 case ISN_NEWFUNC:
2437 {
2438 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2439
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002440 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002441 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002442 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002443 }
2444 break;
2445
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002446 // List functions
2447 case ISN_DEF:
2448 if (iptr->isn_arg.string == NULL)
2449 list_functions(NULL);
2450 else
2451 {
2452 exarg_T ea;
2453
2454 CLEAR_FIELD(ea);
2455 ea.cmd = ea.arg = iptr->isn_arg.string;
2456 define_function(&ea, NULL);
2457 }
2458 break;
2459
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002460 // jump if a condition is met
2461 case ISN_JUMP:
2462 {
2463 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002464 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002465 int jump = TRUE;
2466
2467 if (when != JUMP_ALWAYS)
2468 {
2469 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002470 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002471 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002472 || when == JUMP_IF_COND_TRUE)
2473 {
2474 SOURCING_LNUM = iptr->isn_lnum;
2475 jump = tv_get_bool_chk(tv, &error);
2476 if (error)
2477 goto on_error;
2478 }
2479 else
2480 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002482 || when == JUMP_AND_KEEP_IF_FALSE
2483 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002485 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 {
2487 // drop the value from the stack
2488 clear_tv(tv);
2489 --ectx.ec_stack.ga_len;
2490 }
2491 }
2492 if (jump)
2493 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2494 }
2495 break;
2496
2497 // top of a for loop
2498 case ISN_FOR:
2499 {
2500 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2501 typval_T *idxtv =
2502 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2503
2504 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002505 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002507 ++idxtv->vval.v_number;
2508 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 // past the end of the list, jump to "endfor"
2510 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2511 else if (list->lv_first == &range_list_item)
2512 {
2513 // non-materialized range() list
2514 tv = STACK_TV_BOT(0);
2515 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002516 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 tv->vval.v_number = list_find_nr(
2518 list, idxtv->vval.v_number, NULL);
2519 ++ectx.ec_stack.ga_len;
2520 }
2521 else
2522 {
2523 listitem_T *li = list_find(list, idxtv->vval.v_number);
2524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2526 ++ectx.ec_stack.ga_len;
2527 }
2528 }
2529 break;
2530
2531 // start of ":try" block
2532 case ISN_TRY:
2533 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002534 trycmd_T *trycmd = NULL;
2535
Bram Moolenaar270d0382020-05-15 21:42:53 +02002536 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 goto failed;
2538 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2539 + ectx.ec_trystack.ga_len;
2540 ++ectx.ec_trystack.ga_len;
2541 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002542 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2544 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002545 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002546 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 }
2548 break;
2549
2550 case ISN_PUSHEXC:
2551 if (current_exception == NULL)
2552 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 iemsg("Evaluating catch while current_exception is NULL");
2555 goto failed;
2556 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002557 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 goto failed;
2559 tv = STACK_TV_BOT(0);
2560 ++ectx.ec_stack.ga_len;
2561 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002562 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563 tv->vval.v_string = vim_strsave(
2564 (char_u *)current_exception->value);
2565 break;
2566
2567 case ISN_CATCH:
2568 {
2569 garray_T *trystack = &ectx.ec_trystack;
2570
Bram Moolenaar20a76292020-12-25 19:47:24 +01002571 if (restore_cmdmod)
2572 {
2573 cmdmod.cmod_filter_regmatch.regprog = NULL;
2574 undo_cmdmod(&cmdmod);
2575 cmdmod = save_cmdmod;
2576 restore_cmdmod = FALSE;
2577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 if (trystack->ga_len > 0)
2579 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002580 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 + trystack->ga_len - 1;
2582 trycmd->tcd_caught = TRUE;
2583 }
2584 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002585 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 catch_exception(current_exception);
2587 }
2588 break;
2589
2590 // end of ":try" block
2591 case ISN_ENDTRY:
2592 {
2593 garray_T *trystack = &ectx.ec_trystack;
2594
2595 if (trystack->ga_len > 0)
2596 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002597 trycmd_T *trycmd = NULL;
2598
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 --trystack->ga_len;
2600 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002601 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 trycmd = ((trycmd_T *)trystack->ga_data)
2603 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002604 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 {
2606 // discard the exception
2607 if (caught_stack == current_exception)
2608 caught_stack = caught_stack->caught;
2609 discard_current_exception();
2610 }
2611
2612 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002613 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 }
2615 }
2616 break;
2617
2618 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002619 if (ectx.ec_trystack.ga_len == 0 && trylevel == 0
2620 && emsg_silent)
2621 {
2622 // throwing an exception while using "silent!" causes the
2623 // function to abort but not display an error.
2624 tv = STACK_TV_BOT(-1);
2625 clear_tv(tv);
2626 tv->v_type = VAR_NUMBER;
2627 tv->vval.v_number = 0;
2628 goto done;
2629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 --ectx.ec_stack.ga_len;
2631 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002632 if (tv->vval.v_string == NULL
2633 || *skipwhite(tv->vval.v_string) == NUL)
2634 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002635 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002636 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002637 emsg(_(e_throw_with_empty_string));
2638 goto failed;
2639 }
2640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2642 {
2643 vim_free(tv->vval.v_string);
2644 goto failed;
2645 }
2646 did_throw = TRUE;
2647 break;
2648
2649 // compare with special values
2650 case ISN_COMPAREBOOL:
2651 case ISN_COMPARESPECIAL:
2652 {
2653 typval_T *tv1 = STACK_TV_BOT(-2);
2654 typval_T *tv2 = STACK_TV_BOT(-1);
2655 varnumber_T arg1 = tv1->vval.v_number;
2656 varnumber_T arg2 = tv2->vval.v_number;
2657 int res;
2658
2659 switch (iptr->isn_arg.op.op_type)
2660 {
2661 case EXPR_EQUAL: res = arg1 == arg2; break;
2662 case EXPR_NEQUAL: res = arg1 != arg2; break;
2663 default: res = 0; break;
2664 }
2665
2666 --ectx.ec_stack.ga_len;
2667 tv1->v_type = VAR_BOOL;
2668 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2669 }
2670 break;
2671
2672 // Operation with two number arguments
2673 case ISN_OPNR:
2674 case ISN_COMPARENR:
2675 {
2676 typval_T *tv1 = STACK_TV_BOT(-2);
2677 typval_T *tv2 = STACK_TV_BOT(-1);
2678 varnumber_T arg1 = tv1->vval.v_number;
2679 varnumber_T arg2 = tv2->vval.v_number;
2680 varnumber_T res;
2681
2682 switch (iptr->isn_arg.op.op_type)
2683 {
2684 case EXPR_MULT: res = arg1 * arg2; break;
2685 case EXPR_DIV: res = arg1 / arg2; break;
2686 case EXPR_REM: res = arg1 % arg2; break;
2687 case EXPR_SUB: res = arg1 - arg2; break;
2688 case EXPR_ADD: res = arg1 + arg2; break;
2689
2690 case EXPR_EQUAL: res = arg1 == arg2; break;
2691 case EXPR_NEQUAL: res = arg1 != arg2; break;
2692 case EXPR_GREATER: res = arg1 > arg2; break;
2693 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2694 case EXPR_SMALLER: res = arg1 < arg2; break;
2695 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2696 default: res = 0; break;
2697 }
2698
2699 --ectx.ec_stack.ga_len;
2700 if (iptr->isn_type == ISN_COMPARENR)
2701 {
2702 tv1->v_type = VAR_BOOL;
2703 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2704 }
2705 else
2706 tv1->vval.v_number = res;
2707 }
2708 break;
2709
2710 // Computation with two float arguments
2711 case ISN_OPFLOAT:
2712 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002713#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 {
2715 typval_T *tv1 = STACK_TV_BOT(-2);
2716 typval_T *tv2 = STACK_TV_BOT(-1);
2717 float_T arg1 = tv1->vval.v_float;
2718 float_T arg2 = tv2->vval.v_float;
2719 float_T res = 0;
2720 int cmp = FALSE;
2721
2722 switch (iptr->isn_arg.op.op_type)
2723 {
2724 case EXPR_MULT: res = arg1 * arg2; break;
2725 case EXPR_DIV: 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: cmp = arg1 == arg2; break;
2730 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2731 case EXPR_GREATER: cmp = arg1 > arg2; break;
2732 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2733 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2734 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2735 default: cmp = 0; break;
2736 }
2737 --ectx.ec_stack.ga_len;
2738 if (iptr->isn_type == ISN_COMPAREFLOAT)
2739 {
2740 tv1->v_type = VAR_BOOL;
2741 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2742 }
2743 else
2744 tv1->vval.v_float = res;
2745 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002746#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 break;
2748
2749 case ISN_COMPARELIST:
2750 {
2751 typval_T *tv1 = STACK_TV_BOT(-2);
2752 typval_T *tv2 = STACK_TV_BOT(-1);
2753 list_T *arg1 = tv1->vval.v_list;
2754 list_T *arg2 = tv2->vval.v_list;
2755 int cmp = FALSE;
2756 int ic = iptr->isn_arg.op.op_ic;
2757
2758 switch (iptr->isn_arg.op.op_type)
2759 {
2760 case EXPR_EQUAL: cmp =
2761 list_equal(arg1, arg2, ic, FALSE); break;
2762 case EXPR_NEQUAL: cmp =
2763 !list_equal(arg1, arg2, ic, FALSE); break;
2764 case EXPR_IS: cmp = arg1 == arg2; break;
2765 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2766 default: cmp = 0; break;
2767 }
2768 --ectx.ec_stack.ga_len;
2769 clear_tv(tv1);
2770 clear_tv(tv2);
2771 tv1->v_type = VAR_BOOL;
2772 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2773 }
2774 break;
2775
2776 case ISN_COMPAREBLOB:
2777 {
2778 typval_T *tv1 = STACK_TV_BOT(-2);
2779 typval_T *tv2 = STACK_TV_BOT(-1);
2780 blob_T *arg1 = tv1->vval.v_blob;
2781 blob_T *arg2 = tv2->vval.v_blob;
2782 int cmp = FALSE;
2783
2784 switch (iptr->isn_arg.op.op_type)
2785 {
2786 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2787 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2788 case EXPR_IS: cmp = arg1 == arg2; break;
2789 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2790 default: cmp = 0; break;
2791 }
2792 --ectx.ec_stack.ga_len;
2793 clear_tv(tv1);
2794 clear_tv(tv2);
2795 tv1->v_type = VAR_BOOL;
2796 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2797 }
2798 break;
2799
2800 // TODO: handle separately
2801 case ISN_COMPARESTRING:
2802 case ISN_COMPAREDICT:
2803 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 case ISN_COMPAREANY:
2805 {
2806 typval_T *tv1 = STACK_TV_BOT(-2);
2807 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002808 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 int ic = iptr->isn_arg.op.op_ic;
2810
Bram Moolenaareb26f432020-09-14 16:50:05 +02002811 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002812 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002814 --ectx.ec_stack.ga_len;
2815 }
2816 break;
2817
2818 case ISN_ADDLIST:
2819 case ISN_ADDBLOB:
2820 {
2821 typval_T *tv1 = STACK_TV_BOT(-2);
2822 typval_T *tv2 = STACK_TV_BOT(-1);
2823
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002824 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 if (iptr->isn_type == ISN_ADDLIST)
2826 eval_addlist(tv1, tv2);
2827 else
2828 eval_addblob(tv1, tv2);
2829 clear_tv(tv2);
2830 --ectx.ec_stack.ga_len;
2831 }
2832 break;
2833
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002834 case ISN_LISTAPPEND:
2835 {
2836 typval_T *tv1 = STACK_TV_BOT(-2);
2837 typval_T *tv2 = STACK_TV_BOT(-1);
2838 list_T *l = tv1->vval.v_list;
2839
2840 // add an item to a list
2841 if (l == NULL)
2842 {
2843 SOURCING_LNUM = iptr->isn_lnum;
2844 emsg(_(e_cannot_add_to_null_list));
2845 goto on_error;
2846 }
2847 if (list_append_tv(l, tv2) == FAIL)
2848 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002849 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002850 --ectx.ec_stack.ga_len;
2851 }
2852 break;
2853
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002854 case ISN_BLOBAPPEND:
2855 {
2856 typval_T *tv1 = STACK_TV_BOT(-2);
2857 typval_T *tv2 = STACK_TV_BOT(-1);
2858 blob_T *b = tv1->vval.v_blob;
2859 int error = FALSE;
2860 varnumber_T n;
2861
2862 // add a number to a blob
2863 if (b == NULL)
2864 {
2865 SOURCING_LNUM = iptr->isn_lnum;
2866 emsg(_(e_cannot_add_to_null_blob));
2867 goto on_error;
2868 }
2869 n = tv_get_number_chk(tv2, &error);
2870 if (error)
2871 goto on_error;
2872 ga_append(&b->bv_ga, (int)n);
2873 --ectx.ec_stack.ga_len;
2874 }
2875 break;
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 // Computation with two arguments of unknown type
2878 case ISN_OPANY:
2879 {
2880 typval_T *tv1 = STACK_TV_BOT(-2);
2881 typval_T *tv2 = STACK_TV_BOT(-1);
2882 varnumber_T n1, n2;
2883#ifdef FEAT_FLOAT
2884 float_T f1 = 0, f2 = 0;
2885#endif
2886 int error = FALSE;
2887
2888 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2889 {
2890 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2891 {
2892 eval_addlist(tv1, tv2);
2893 clear_tv(tv2);
2894 --ectx.ec_stack.ga_len;
2895 break;
2896 }
2897 else if (tv1->v_type == VAR_BLOB
2898 && tv2->v_type == VAR_BLOB)
2899 {
2900 eval_addblob(tv1, tv2);
2901 clear_tv(tv2);
2902 --ectx.ec_stack.ga_len;
2903 break;
2904 }
2905 }
2906#ifdef FEAT_FLOAT
2907 if (tv1->v_type == VAR_FLOAT)
2908 {
2909 f1 = tv1->vval.v_float;
2910 n1 = 0;
2911 }
2912 else
2913#endif
2914 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 n1 = tv_get_number_chk(tv1, &error);
2917 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002918 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919#ifdef FEAT_FLOAT
2920 if (tv2->v_type == VAR_FLOAT)
2921 f1 = n1;
2922#endif
2923 }
2924#ifdef FEAT_FLOAT
2925 if (tv2->v_type == VAR_FLOAT)
2926 {
2927 f2 = tv2->vval.v_float;
2928 n2 = 0;
2929 }
2930 else
2931#endif
2932 {
2933 n2 = tv_get_number_chk(tv2, &error);
2934 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002935 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936#ifdef FEAT_FLOAT
2937 if (tv1->v_type == VAR_FLOAT)
2938 f2 = n2;
2939#endif
2940 }
2941#ifdef FEAT_FLOAT
2942 // if there is a float on either side the result is a float
2943 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2944 {
2945 switch (iptr->isn_arg.op.op_type)
2946 {
2947 case EXPR_MULT: f1 = f1 * f2; break;
2948 case EXPR_DIV: f1 = f1 / f2; break;
2949 case EXPR_SUB: f1 = f1 - f2; break;
2950 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002951 default: SOURCING_LNUM = iptr->isn_lnum;
2952 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002953 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002954 }
2955 clear_tv(tv1);
2956 clear_tv(tv2);
2957 tv1->v_type = VAR_FLOAT;
2958 tv1->vval.v_float = f1;
2959 --ectx.ec_stack.ga_len;
2960 }
2961 else
2962#endif
2963 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002964 int failed = FALSE;
2965
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002966 switch (iptr->isn_arg.op.op_type)
2967 {
2968 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002969 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
2970 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002971 goto on_error;
2972 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case EXPR_SUB: n1 = n1 - n2; break;
2974 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002975 default: n1 = num_modulus(n1, n2, &failed);
2976 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002977 goto on_error;
2978 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 }
2980 clear_tv(tv1);
2981 clear_tv(tv2);
2982 tv1->v_type = VAR_NUMBER;
2983 tv1->vval.v_number = n1;
2984 --ectx.ec_stack.ga_len;
2985 }
2986 }
2987 break;
2988
2989 case ISN_CONCAT:
2990 {
2991 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2992 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2993 char_u *res;
2994
2995 res = concat_str(str1, str2);
2996 clear_tv(STACK_TV_BOT(-2));
2997 clear_tv(STACK_TV_BOT(-1));
2998 --ectx.ec_stack.ga_len;
2999 STACK_TV_BOT(-1)->vval.v_string = res;
3000 }
3001 break;
3002
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003003 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003004 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003005 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003006 int is_slice = iptr->isn_type == ISN_STRSLICE;
3007 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003008 char_u *res;
3009
3010 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003011 // string slice: string is at stack-3, first index at
3012 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003013 if (is_slice)
3014 {
3015 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003016 n1 = tv->vval.v_number;
3017 }
3018
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003019 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003020 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003021
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003022 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003023 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003024 if (is_slice)
3025 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003026 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003027 else
3028 // Index: The resulting variable is a string of a
3029 // single character. If the index is too big or
3030 // negative the result is empty.
3031 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003032 vim_free(tv->vval.v_string);
3033 tv->vval.v_string = res;
3034 }
3035 break;
3036
3037 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003038 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 {
Bram Moolenaared591872020-08-15 22:14:53 +02003040 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003042 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043
3044 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003045 // list slice: list is at stack-3, indexes at stack-2 and
3046 // stack-1
3047 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 list = tv->vval.v_list;
3049
3050 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003051 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003052 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003053
3054 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 {
Bram Moolenaared591872020-08-15 22:14:53 +02003056 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003057 n1 = tv->vval.v_number;
3058 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 }
Bram Moolenaared591872020-08-15 22:14:53 +02003060
3061 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003062 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003063 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003064 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3065 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003066 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 }
3068 break;
3069
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003070 case ISN_ANYINDEX:
3071 case ISN_ANYSLICE:
3072 {
3073 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3074 typval_T *var1, *var2;
3075 int res;
3076
3077 // index: composite is at stack-2, index at stack-1
3078 // slice: composite is at stack-3, indexes at stack-2 and
3079 // stack-1
3080 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003081 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003082 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3083 goto on_error;
3084 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3085 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003086 res = eval_index_inner(tv, is_slice, var1, var2,
3087 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003088 clear_tv(var1);
3089 if (is_slice)
3090 clear_tv(var2);
3091 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3092 if (res == FAIL)
3093 goto on_error;
3094 }
3095 break;
3096
Bram Moolenaar9af78762020-06-16 11:34:42 +02003097 case ISN_SLICE:
3098 {
3099 list_T *list;
3100 int count = iptr->isn_arg.number;
3101
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003102 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003103 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003104 list = tv->vval.v_list;
3105
3106 // no error for short list, expect it to be checked earlier
3107 if (list != NULL && list->lv_len >= count)
3108 {
3109 list_T *newlist = list_slice(list,
3110 count, list->lv_len - 1);
3111
3112 if (newlist != NULL)
3113 {
3114 list_unref(list);
3115 tv->vval.v_list = newlist;
3116 ++newlist->lv_refcount;
3117 }
3118 }
3119 }
3120 break;
3121
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003122 case ISN_GETITEM:
3123 {
3124 listitem_T *li;
3125 int index = iptr->isn_arg.number;
3126
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003127 // Get list item: list is at stack-1, push item.
3128 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003129 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003130 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003131
3132 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3133 goto failed;
3134 ++ectx.ec_stack.ga_len;
3135 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3136 }
3137 break;
3138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139 case ISN_MEMBER:
3140 {
3141 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003142 char_u *key;
3143 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003144 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003145
3146 // dict member: dict is at stack-2, key at stack-1
3147 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003148 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003149 dict = tv->vval.v_dict;
3150
3151 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003152 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003153 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003154 if (key == NULL)
3155 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003156
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003157 if ((di = dict_find(dict, key, -1)) == NULL)
3158 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003159 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003160 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003161
3162 // If :silent! is used we will continue, make sure the
3163 // stack contents makes sense.
3164 clear_tv(tv);
3165 --ectx.ec_stack.ga_len;
3166 tv = STACK_TV_BOT(-1);
3167 clear_tv(tv);
3168 tv->v_type = VAR_NUMBER;
3169 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003170 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003171 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003172 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003173 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003174 // Clear the dict only after getting the item, to avoid
3175 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003176 tv = STACK_TV_BOT(-1);
3177 temp_tv = *tv;
3178 copy_tv(&di->di_tv, tv);
3179 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003180 }
3181 break;
3182
3183 // dict member with string key
3184 case ISN_STRINGMEMBER:
3185 {
3186 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003188 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189
3190 tv = STACK_TV_BOT(-1);
3191 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3192 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003193 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003195 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
3197 dict = tv->vval.v_dict;
3198
3199 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3200 == NULL)
3201 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003204 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003206 // Clear the dict after getting the item, to avoid that it
3207 // make the item invalid.
3208 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003210 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 }
3212 break;
3213
3214 case ISN_NEGATENR:
3215 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003216 if (tv->v_type != VAR_NUMBER
3217#ifdef FEAT_FLOAT
3218 && tv->v_type != VAR_FLOAT
3219#endif
3220 )
3221 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003223 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003224 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003225 }
3226#ifdef FEAT_FLOAT
3227 if (tv->v_type == VAR_FLOAT)
3228 tv->vval.v_float = -tv->vval.v_float;
3229 else
3230#endif
3231 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 break;
3233
3234 case ISN_CHECKNR:
3235 {
3236 int error = FALSE;
3237
3238 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003241 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 (void)tv_get_number_chk(tv, &error);
3243 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003244 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003245 }
3246 break;
3247
3248 case ISN_CHECKTYPE:
3249 {
3250 checktype_T *ct = &iptr->isn_arg.type;
3251
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003252 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003253 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare32e5162021-01-21 20:21:29 +01003254 if (check_typval_type(ct->ct_type, tv, ct->ct_arg_idx)
3255 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003256 goto on_error;
3257
3258 // number 0 is FALSE, number 1 is TRUE
3259 if (tv->v_type == VAR_NUMBER
3260 && ct->ct_type->tt_type == VAR_BOOL
3261 && (tv->vval.v_number == 0
3262 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003264 tv->v_type = VAR_BOOL;
3265 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003266 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 }
3268 }
3269 break;
3270
Bram Moolenaar9af78762020-06-16 11:34:42 +02003271 case ISN_CHECKLEN:
3272 {
3273 int min_len = iptr->isn_arg.checklen.cl_min_len;
3274 list_T *list = NULL;
3275
3276 tv = STACK_TV_BOT(-1);
3277 if (tv->v_type == VAR_LIST)
3278 list = tv->vval.v_list;
3279 if (list == NULL || list->lv_len < min_len
3280 || (list->lv_len > min_len
3281 && !iptr->isn_arg.checklen.cl_more_OK))
3282 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003283 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003284 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003285 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003286 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003287 }
3288 }
3289 break;
3290
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003291 case ISN_SETTYPE:
3292 {
3293 checktype_T *ct = &iptr->isn_arg.type;
3294
3295 tv = STACK_TV_BOT(-1);
3296 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3297 {
3298 free_type(tv->vval.v_dict->dv_type);
3299 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3300 }
3301 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3302 {
3303 free_type(tv->vval.v_list->lv_type);
3304 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3305 }
3306 }
3307 break;
3308
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003309 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003310 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003311 {
3312 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003313 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314
3315 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003316 if (iptr->isn_type == ISN_2BOOL)
3317 {
3318 n = tv2bool(tv);
3319 if (iptr->isn_arg.number) // invert
3320 n = !n;
3321 }
3322 else
3323 {
3324 SOURCING_LNUM = iptr->isn_lnum;
3325 n = tv_get_bool_chk(tv, &error);
3326 if (error)
3327 goto on_error;
3328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003329 clear_tv(tv);
3330 tv->v_type = VAR_BOOL;
3331 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3332 }
3333 break;
3334
3335 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003336 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003337 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003338 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3339 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3340 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 break;
3342
Bram Moolenaar08597872020-12-10 19:43:40 +01003343 case ISN_RANGE:
3344 {
3345 exarg_T ea;
3346 char *errormsg;
3347
Bram Moolenaarece0b872021-01-08 20:40:45 +01003348 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003349 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003350 ea.addr_type = ADDR_LINES;
3351 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003352 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003353 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003354 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003355
3356 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3357 goto failed;
3358 ++ectx.ec_stack.ga_len;
3359 tv = STACK_TV_BOT(-1);
3360 tv->v_type = VAR_NUMBER;
3361 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003362 if (ea.addr_count == 0)
3363 tv->vval.v_number = curwin->w_cursor.lnum;
3364 else
3365 tv->vval.v_number = ea.line2;
3366 }
3367 break;
3368
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003369 case ISN_PUT:
3370 {
3371 int regname = iptr->isn_arg.put.put_regname;
3372 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3373 char_u *expr = NULL;
3374 int dir = FORWARD;
3375
Bram Moolenaar08597872020-12-10 19:43:40 +01003376 if (lnum < -2)
3377 {
3378 // line number was put on the stack by ISN_RANGE
3379 tv = STACK_TV_BOT(-1);
3380 curwin->w_cursor.lnum = tv->vval.v_number;
3381 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3382 dir = BACKWARD;
3383 --ectx.ec_stack.ga_len;
3384 }
3385 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003386 // :put! above cursor
3387 dir = BACKWARD;
3388 else if (lnum >= 0)
3389 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003390
3391 if (regname == '=')
3392 {
3393 tv = STACK_TV_BOT(-1);
3394 if (tv->v_type == VAR_STRING)
3395 expr = tv->vval.v_string;
3396 else
3397 {
3398 expr = typval2string(tv, TRUE); // allocates value
3399 clear_tv(tv);
3400 }
3401 --ectx.ec_stack.ga_len;
3402 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003403 check_cursor();
3404 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3405 vim_free(expr);
3406 }
3407 break;
3408
Bram Moolenaar02194d22020-10-24 23:08:38 +02003409 case ISN_CMDMOD:
3410 save_cmdmod = cmdmod;
3411 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003412 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003413 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3414 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003415 break;
3416
Bram Moolenaar02194d22020-10-24 23:08:38 +02003417 case ISN_CMDMOD_REV:
3418 // filter regprog is owned by the instruction, don't free it
3419 cmdmod.cmod_filter_regmatch.regprog = NULL;
3420 undo_cmdmod(&cmdmod);
3421 cmdmod = save_cmdmod;
3422 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003423 break;
3424
Bram Moolenaar792f7862020-11-23 08:31:18 +01003425 case ISN_UNPACK:
3426 {
3427 int count = iptr->isn_arg.unpack.unp_count;
3428 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3429 list_T *l;
3430 listitem_T *li;
3431 int i;
3432
3433 // Check there is a valid list to unpack.
3434 tv = STACK_TV_BOT(-1);
3435 if (tv->v_type != VAR_LIST)
3436 {
3437 SOURCING_LNUM = iptr->isn_lnum;
3438 emsg(_(e_for_argument_must_be_sequence_of_lists));
3439 goto on_error;
3440 }
3441 l = tv->vval.v_list;
3442 if (l == NULL
3443 || l->lv_len < (semicolon ? count - 1 : count))
3444 {
3445 SOURCING_LNUM = iptr->isn_lnum;
3446 emsg(_(e_list_value_does_not_have_enough_items));
3447 goto on_error;
3448 }
3449 else if (!semicolon && l->lv_len > count)
3450 {
3451 SOURCING_LNUM = iptr->isn_lnum;
3452 emsg(_(e_list_value_has_more_items_than_targets));
3453 goto on_error;
3454 }
3455
3456 CHECK_LIST_MATERIALIZE(l);
3457 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3458 goto failed;
3459 ectx.ec_stack.ga_len += count - 1;
3460
3461 // Variable after semicolon gets a list with the remaining
3462 // items.
3463 if (semicolon)
3464 {
3465 list_T *rem_list =
3466 list_alloc_with_items(l->lv_len - count + 1);
3467
3468 if (rem_list == NULL)
3469 goto failed;
3470 tv = STACK_TV_BOT(-count);
3471 tv->vval.v_list = rem_list;
3472 ++rem_list->lv_refcount;
3473 tv->v_lock = 0;
3474 li = l->lv_first;
3475 for (i = 0; i < count - 1; ++i)
3476 li = li->li_next;
3477 for (i = 0; li != NULL; ++i)
3478 {
3479 list_set_item(rem_list, i, &li->li_tv);
3480 li = li->li_next;
3481 }
3482 --count;
3483 }
3484
3485 // Produce the values in reverse order, first item last.
3486 li = l->lv_first;
3487 for (i = 0; i < count; ++i)
3488 {
3489 tv = STACK_TV_BOT(-i - 1);
3490 copy_tv(&li->li_tv, tv);
3491 li = li->li_next;
3492 }
3493
3494 list_unref(l);
3495 }
3496 break;
3497
Bram Moolenaar389df252020-07-09 21:20:47 +02003498 case ISN_SHUFFLE:
3499 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003500 typval_T tmp_tv;
3501 int item = iptr->isn_arg.shuffle.shfl_item;
3502 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003503
3504 tmp_tv = *STACK_TV_BOT(-item);
3505 for ( ; up > 0 && item > 1; --up)
3506 {
3507 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3508 --item;
3509 }
3510 *STACK_TV_BOT(-item) = tmp_tv;
3511 }
3512 break;
3513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 case ISN_DROP:
3515 --ectx.ec_stack.ga_len;
3516 clear_tv(STACK_TV_BOT(0));
3517 break;
3518 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003519 continue;
3520
Bram Moolenaard032f342020-07-18 18:13:02 +02003521func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003522 // Restore previous function. If the frame pointer is where we started
3523 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003524 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003525 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003526
Bram Moolenaard032f342020-07-18 18:13:02 +02003527 if (func_return(&ectx) == FAIL)
3528 // only fails when out of memory
3529 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003530 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003531
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003532on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003533 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003534 // If "emsg_silent" is set then ignore the error, unless it was set
3535 // when calling the function.
3536 if (did_emsg_cumul + did_emsg == did_emsg_before
3537 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003538 {
3539 // If a sequence of instructions causes an error while ":silent!"
3540 // was used, restore the stack length and jump ahead to restoring
3541 // the cmdmod.
3542 if (restore_cmdmod)
3543 {
3544 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3545 {
3546 --ectx.ec_stack.ga_len;
3547 clear_tv(STACK_TV_BOT(0));
3548 }
3549 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3550 ++ectx.ec_iidx;
3551 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003552 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003553 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003554on_fatal_error:
3555 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003556 // If we are not inside a try-catch started here, abort execution.
3557 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003558 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 }
3560
3561done:
3562 // function finished, get result from the stack.
3563 tv = STACK_TV_BOT(-1);
3564 *rettv = *tv;
3565 tv->v_type = VAR_UNKNOWN;
3566 ret = OK;
3567
3568failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003569 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003570 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003571 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003572
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003573 // Deal with any remaining closures, they may be in use somewhere.
3574 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003575 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003576 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003577 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3578 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003579
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003580 estack_pop();
3581 current_sctx = save_current_sctx;
3582
Bram Moolenaar352134b2020-10-17 22:04:08 +02003583 if (*msg_list != NULL && saved_msg_list != NULL)
3584 {
3585 msglist_T **plist = saved_msg_list;
3586
3587 // Append entries from the current msg_list (uncaught exceptions) to
3588 // the saved msg_list.
3589 while (*plist != NULL)
3590 plist = &(*plist)->next;
3591
3592 *plist = *msg_list;
3593 }
3594 msg_list = saved_msg_list;
3595
Bram Moolenaar02194d22020-10-24 23:08:38 +02003596 if (restore_cmdmod)
3597 {
3598 cmdmod.cmod_filter_regmatch.regprog = NULL;
3599 undo_cmdmod(&cmdmod);
3600 cmdmod = save_cmdmod;
3601 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003602 emsg_silent_def = save_emsg_silent_def;
3603 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003604
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003605failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003606 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3608 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003609
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003611 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003612
Bram Moolenaar0186e582021-01-10 18:33:11 +01003613 while (ectx.ec_outer != NULL)
3614 {
3615 outer_T *up = ectx.ec_outer->out_up_is_copy
3616 ? NULL : ectx.ec_outer->out_up;
3617
3618 vim_free(ectx.ec_outer);
3619 ectx.ec_outer = up;
3620 }
3621
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003622 // Not sure if this is necessary.
3623 suppress_errthrow = save_suppress_errthrow;
3624
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003625 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003626 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003627 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003628 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 return ret;
3630}
3631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003633 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003634 * We don't really need this at runtime, but we do have tests that require it,
3635 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003636 */
3637 void
3638ex_disassemble(exarg_T *eap)
3639{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003640 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003641 char_u *fname;
3642 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003643 dfunc_T *dfunc;
3644 isn_T *instr;
3645 int current;
3646 int line_idx = 0;
3647 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003648 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003649
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003650 if (STRNCMP(arg, "<lambda>", 8) == 0)
3651 {
3652 arg += 8;
3653 (void)getdigits(&arg);
3654 fname = vim_strnsave(eap->arg, arg - eap->arg);
3655 }
3656 else
3657 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003658 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003659 if (fname == NULL)
3660 {
3661 semsg(_(e_invarg2), eap->arg);
3662 return;
3663 }
3664
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003665 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003666 if (ufunc == NULL)
3667 {
3668 char_u *p = untrans_function_name(fname);
3669
3670 if (p != NULL)
3671 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003672 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003673 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003674 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 if (ufunc == NULL)
3676 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003677 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003678 return;
3679 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003680 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003681 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3682 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003683 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003684 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003685 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 return;
3687 }
3688 if (ufunc->uf_name_exp != NULL)
3689 msg((char *)ufunc->uf_name_exp);
3690 else
3691 msg((char *)ufunc->uf_name);
3692
3693 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3694 instr = dfunc->df_instr;
3695 for (current = 0; current < dfunc->df_instr_count; ++current)
3696 {
3697 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003698 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003699
3700 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3701 {
3702 if (current > prev_current)
3703 {
3704 msg_puts("\n\n");
3705 prev_current = current;
3706 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003707 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3708 if (line != NULL)
3709 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 }
3711
3712 switch (iptr->isn_type)
3713 {
3714 case ISN_EXEC:
3715 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3716 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003717 case ISN_EXECCONCAT:
3718 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003719 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003720 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003721 case ISN_ECHO:
3722 {
3723 echo_T *echo = &iptr->isn_arg.echo;
3724
3725 smsg("%4d %s %d", current,
3726 echo->echo_with_white ? "ECHO" : "ECHON",
3727 echo->echo_count);
3728 }
3729 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003730 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003731 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003732 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003733 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003734 case ISN_ECHOMSG:
3735 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003736 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003737 break;
3738 case ISN_ECHOERR:
3739 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003740 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003741 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003742 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003743 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003744 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003745 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003746 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003747 + STACK_FRAME_SIZE));
3748 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003749 smsg("%4d LOAD $%lld", current,
3750 (varnumber_T)(iptr->isn_arg.number));
3751 }
3752 break;
3753 case ISN_LOADOUTER:
3754 {
3755 if (iptr->isn_arg.number < 0)
3756 smsg("%4d LOADOUTER level %d arg[%d]", current,
3757 iptr->isn_arg.outer.outer_depth,
3758 iptr->isn_arg.outer.outer_idx
3759 + STACK_FRAME_SIZE);
3760 else
3761 smsg("%4d LOADOUTER level %d $%d", current,
3762 iptr->isn_arg.outer.outer_depth,
3763 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003764 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 break;
3766 case ISN_LOADV:
3767 smsg("%4d LOADV v:%s", current,
3768 get_vim_var_name(iptr->isn_arg.number));
3769 break;
3770 case ISN_LOADSCRIPT:
3771 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003772 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3773 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003774 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003775 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003777 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3778 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003779 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003780 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 }
3782 break;
3783 case ISN_LOADS:
3784 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003785 scriptitem_T *si = SCRIPT_ITEM(
3786 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003787
3788 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003789 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003790 }
3791 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003792 case ISN_LOADAUTO:
3793 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3794 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003795 case ISN_LOADG:
3796 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3797 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003798 case ISN_LOADB:
3799 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3800 break;
3801 case ISN_LOADW:
3802 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3803 break;
3804 case ISN_LOADT:
3805 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3806 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003807 case ISN_LOADGDICT:
3808 smsg("%4d LOAD g:", current);
3809 break;
3810 case ISN_LOADBDICT:
3811 smsg("%4d LOAD b:", current);
3812 break;
3813 case ISN_LOADWDICT:
3814 smsg("%4d LOAD w:", current);
3815 break;
3816 case ISN_LOADTDICT:
3817 smsg("%4d LOAD t:", current);
3818 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003819 case ISN_LOADOPT:
3820 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3821 break;
3822 case ISN_LOADENV:
3823 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3824 break;
3825 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003826 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003827 break;
3828
3829 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003830 if (iptr->isn_arg.number < 0)
3831 smsg("%4d STORE arg[%lld]", current,
3832 iptr->isn_arg.number + STACK_FRAME_SIZE);
3833 else
3834 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3835 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003836 case ISN_STOREOUTER:
3837 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003838 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003839 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3840 iptr->isn_arg.outer.outer_depth,
3841 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003842 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003843 smsg("%4d STOREOUTER level %d $%d", current,
3844 iptr->isn_arg.outer.outer_depth,
3845 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003846 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003847 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003848 case ISN_STOREV:
3849 smsg("%4d STOREV v:%s", current,
3850 get_vim_var_name(iptr->isn_arg.number));
3851 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003852 case ISN_STOREAUTO:
3853 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3854 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003856 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3857 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003858 case ISN_STOREB:
3859 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3860 break;
3861 case ISN_STOREW:
3862 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3863 break;
3864 case ISN_STORET:
3865 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3866 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003867 case ISN_STORES:
3868 {
3869 scriptitem_T *si = SCRIPT_ITEM(
3870 iptr->isn_arg.loadstore.ls_sid);
3871
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003872 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003873 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003874 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003875 break;
3876 case ISN_STORESCRIPT:
3877 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003878 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3879 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003880 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003881 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003883 smsg("%4d STORESCRIPT %s-%d in %s", current,
3884 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003885 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003886 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 }
3888 break;
3889 case ISN_STOREOPT:
3890 smsg("%4d STOREOPT &%s", current,
3891 iptr->isn_arg.storeopt.so_name);
3892 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003893 case ISN_STOREENV:
3894 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3895 break;
3896 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003897 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003899 case ISN_STORENR:
3900 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003901 iptr->isn_arg.storenr.stnr_val,
3902 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 break;
3904
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003905 case ISN_STOREINDEX:
3906 switch (iptr->isn_arg.vartype)
3907 {
3908 case VAR_LIST:
3909 smsg("%4d STORELIST", current);
3910 break;
3911 case VAR_DICT:
3912 smsg("%4d STOREDICT", current);
3913 break;
3914 case VAR_ANY:
3915 smsg("%4d STOREINDEX", current);
3916 break;
3917 default: break;
3918 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003919 break;
3920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003921 // constants
3922 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003923 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003924 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003925 break;
3926 case ISN_PUSHBOOL:
3927 case ISN_PUSHSPEC:
3928 smsg("%4d PUSH %s", current,
3929 get_var_special_name(iptr->isn_arg.number));
3930 break;
3931 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003932#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003934#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 break;
3936 case ISN_PUSHS:
3937 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3938 break;
3939 case ISN_PUSHBLOB:
3940 {
3941 char_u *r;
3942 char_u numbuf[NUMBUFLEN];
3943 char_u *tofree;
3944
3945 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003946 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 vim_free(tofree);
3948 }
3949 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003950 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003951 {
3952 char *name = (char *)iptr->isn_arg.string;
3953
3954 smsg("%4d PUSHFUNC \"%s\"", current,
3955 name == NULL ? "[none]" : name);
3956 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003957 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003958 case ISN_PUSHCHANNEL:
3959#ifdef FEAT_JOB_CHANNEL
3960 {
3961 channel_T *channel = iptr->isn_arg.channel;
3962
3963 smsg("%4d PUSHCHANNEL %d", current,
3964 channel == NULL ? 0 : channel->ch_id);
3965 }
3966#endif
3967 break;
3968 case ISN_PUSHJOB:
3969#ifdef FEAT_JOB_CHANNEL
3970 {
3971 typval_T tv;
3972 char_u *name;
3973
3974 tv.v_type = VAR_JOB;
3975 tv.vval.v_job = iptr->isn_arg.job;
3976 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003977 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003978 }
3979#endif
3980 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003981 case ISN_PUSHEXC:
3982 smsg("%4d PUSH v:exception", current);
3983 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003984 case ISN_UNLET:
3985 smsg("%4d UNLET%s %s", current,
3986 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3987 iptr->isn_arg.unlet.ul_name);
3988 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003989 case ISN_UNLETENV:
3990 smsg("%4d UNLETENV%s $%s", current,
3991 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3992 iptr->isn_arg.unlet.ul_name);
3993 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003994 case ISN_UNLETINDEX:
3995 smsg("%4d UNLETINDEX", current);
3996 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003997 case ISN_LOCKCONST:
3998 smsg("%4d LOCKCONST", current);
3999 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004001 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004002 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004003 break;
4004 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004005 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004006 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004007 break;
4008
4009 // function call
4010 case ISN_BCALL:
4011 {
4012 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4013
4014 smsg("%4d BCALL %s(argc %d)", current,
4015 internal_func_name(cbfunc->cbf_idx),
4016 cbfunc->cbf_argcount);
4017 }
4018 break;
4019 case ISN_DCALL:
4020 {
4021 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4022 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4023 + cdfunc->cdf_idx;
4024
4025 smsg("%4d DCALL %s(argc %d)", current,
4026 df->df_ufunc->uf_name_exp != NULL
4027 ? df->df_ufunc->uf_name_exp
4028 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4029 }
4030 break;
4031 case ISN_UCALL:
4032 {
4033 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4034
4035 smsg("%4d UCALL %s(argc %d)", current,
4036 cufunc->cuf_name, cufunc->cuf_argcount);
4037 }
4038 break;
4039 case ISN_PCALL:
4040 {
4041 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4042
4043 smsg("%4d PCALL%s (argc %d)", current,
4044 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4045 }
4046 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004047 case ISN_PCALL_END:
4048 smsg("%4d PCALL end", current);
4049 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 case ISN_RETURN:
4051 smsg("%4d RETURN", current);
4052 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004053 case ISN_RETURN_ZERO:
4054 smsg("%4d RETURN 0", current);
4055 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 case ISN_FUNCREF:
4057 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004058 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004060 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004062 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004063 }
4064 break;
4065
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004066 case ISN_NEWFUNC:
4067 {
4068 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4069
4070 smsg("%4d NEWFUNC %s %s", current,
4071 newfunc->nf_lambda, newfunc->nf_global);
4072 }
4073 break;
4074
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004075 case ISN_DEF:
4076 {
4077 char_u *name = iptr->isn_arg.string;
4078
4079 smsg("%4d DEF %s", current,
4080 name == NULL ? (char_u *)"" : name);
4081 }
4082 break;
4083
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 case ISN_JUMP:
4085 {
4086 char *when = "?";
4087
4088 switch (iptr->isn_arg.jump.jump_when)
4089 {
4090 case JUMP_ALWAYS:
4091 when = "JUMP";
4092 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 case JUMP_AND_KEEP_IF_TRUE:
4094 when = "JUMP_AND_KEEP_IF_TRUE";
4095 break;
4096 case JUMP_IF_FALSE:
4097 when = "JUMP_IF_FALSE";
4098 break;
4099 case JUMP_AND_KEEP_IF_FALSE:
4100 when = "JUMP_AND_KEEP_IF_FALSE";
4101 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004102 case JUMP_IF_COND_FALSE:
4103 when = "JUMP_IF_COND_FALSE";
4104 break;
4105 case JUMP_IF_COND_TRUE:
4106 when = "JUMP_IF_COND_TRUE";
4107 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004108 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004109 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 iptr->isn_arg.jump.jump_where);
4111 }
4112 break;
4113
4114 case ISN_FOR:
4115 {
4116 forloop_T *forloop = &iptr->isn_arg.forloop;
4117
4118 smsg("%4d FOR $%d -> %d", current,
4119 forloop->for_idx, forloop->for_end);
4120 }
4121 break;
4122
4123 case ISN_TRY:
4124 {
4125 try_T *try = &iptr->isn_arg.try;
4126
4127 smsg("%4d TRY catch -> %d, finally -> %d", current,
4128 try->try_catch, try->try_finally);
4129 }
4130 break;
4131 case ISN_CATCH:
4132 // TODO
4133 smsg("%4d CATCH", current);
4134 break;
4135 case ISN_ENDTRY:
4136 smsg("%4d ENDTRY", current);
4137 break;
4138 case ISN_THROW:
4139 smsg("%4d THROW", current);
4140 break;
4141
4142 // expression operations on number
4143 case ISN_OPNR:
4144 case ISN_OPFLOAT:
4145 case ISN_OPANY:
4146 {
4147 char *what;
4148 char *ins;
4149
4150 switch (iptr->isn_arg.op.op_type)
4151 {
4152 case EXPR_MULT: what = "*"; break;
4153 case EXPR_DIV: what = "/"; break;
4154 case EXPR_REM: what = "%"; break;
4155 case EXPR_SUB: what = "-"; break;
4156 case EXPR_ADD: what = "+"; break;
4157 default: what = "???"; break;
4158 }
4159 switch (iptr->isn_type)
4160 {
4161 case ISN_OPNR: ins = "OPNR"; break;
4162 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4163 case ISN_OPANY: ins = "OPANY"; break;
4164 default: ins = "???"; break;
4165 }
4166 smsg("%4d %s %s", current, ins, what);
4167 }
4168 break;
4169
4170 case ISN_COMPAREBOOL:
4171 case ISN_COMPARESPECIAL:
4172 case ISN_COMPARENR:
4173 case ISN_COMPAREFLOAT:
4174 case ISN_COMPARESTRING:
4175 case ISN_COMPAREBLOB:
4176 case ISN_COMPARELIST:
4177 case ISN_COMPAREDICT:
4178 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 case ISN_COMPAREANY:
4180 {
4181 char *p;
4182 char buf[10];
4183 char *type;
4184
4185 switch (iptr->isn_arg.op.op_type)
4186 {
4187 case EXPR_EQUAL: p = "=="; break;
4188 case EXPR_NEQUAL: p = "!="; break;
4189 case EXPR_GREATER: p = ">"; break;
4190 case EXPR_GEQUAL: p = ">="; break;
4191 case EXPR_SMALLER: p = "<"; break;
4192 case EXPR_SEQUAL: p = "<="; break;
4193 case EXPR_MATCH: p = "=~"; break;
4194 case EXPR_IS: p = "is"; break;
4195 case EXPR_ISNOT: p = "isnot"; break;
4196 case EXPR_NOMATCH: p = "!~"; break;
4197 default: p = "???"; break;
4198 }
4199 STRCPY(buf, p);
4200 if (iptr->isn_arg.op.op_ic == TRUE)
4201 strcat(buf, "?");
4202 switch(iptr->isn_type)
4203 {
4204 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4205 case ISN_COMPARESPECIAL:
4206 type = "COMPARESPECIAL"; break;
4207 case ISN_COMPARENR: type = "COMPARENR"; break;
4208 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4209 case ISN_COMPARESTRING:
4210 type = "COMPARESTRING"; break;
4211 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4212 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4213 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4214 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004215 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4216 default: type = "???"; break;
4217 }
4218
4219 smsg("%4d %s %s", current, type, buf);
4220 }
4221 break;
4222
4223 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4224 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4225
4226 // expression operations
4227 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004228 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004229 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004230 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004231 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004232 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004233 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004234 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4235 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004236 case ISN_SLICE: smsg("%4d SLICE %lld",
4237 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004238 case ISN_GETITEM: smsg("%4d ITEM %lld",
4239 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004240 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4241 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004242 iptr->isn_arg.string); break;
4243 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4244
4245 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004246 case ISN_CHECKTYPE:
4247 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004248 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004249 char *tofree;
4250
Bram Moolenaare32e5162021-01-21 20:21:29 +01004251 if (ct->ct_arg_idx == 0)
4252 smsg("%4d CHECKTYPE %s stack[%d]", current,
4253 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004254 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004255 else
4256 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4257 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004258 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004259 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004260 vim_free(tofree);
4261 break;
4262 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004263 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4264 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4265 iptr->isn_arg.checklen.cl_min_len);
4266 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004267 case ISN_SETTYPE:
4268 {
4269 char *tofree;
4270
4271 smsg("%4d SETTYPE %s", current,
4272 type_name(iptr->isn_arg.type.ct_type, &tofree));
4273 vim_free(tofree);
4274 break;
4275 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004276 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004277 case ISN_2BOOL: if (iptr->isn_arg.number)
4278 smsg("%4d INVERT (!val)", current);
4279 else
4280 smsg("%4d 2BOOL (!!val)", current);
4281 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004282 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004283 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004284 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004285 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004286 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004287 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004288 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4289 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004290 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004291 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4292 smsg("%4d PUT %c above range",
4293 current, iptr->isn_arg.put.put_regname);
4294 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4295 smsg("%4d PUT %c range",
4296 current, iptr->isn_arg.put.put_regname);
4297 else
4298 smsg("%4d PUT %c %ld", current,
4299 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004300 (long)iptr->isn_arg.put.put_lnum);
4301 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004302
Bram Moolenaar02194d22020-10-24 23:08:38 +02004303 // TODO: summarize modifiers
4304 case ISN_CMDMOD:
4305 {
4306 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004307 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004308 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4309
4310 buf = alloc(len + 1);
4311 if (buf != NULL)
4312 {
4313 (void)produce_cmdmods(
4314 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4315 smsg("%4d CMDMOD %s", current, buf);
4316 vim_free(buf);
4317 }
4318 break;
4319 }
4320 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004321
Bram Moolenaar792f7862020-11-23 08:31:18 +01004322 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4323 iptr->isn_arg.unpack.unp_count,
4324 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4325 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004326 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4327 iptr->isn_arg.shuffle.shfl_item,
4328 iptr->isn_arg.shuffle.shfl_up);
4329 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 case ISN_DROP: smsg("%4d DROP", current); break;
4331 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004332
4333 out_flush(); // output one line at a time
4334 ui_breakcheck();
4335 if (got_int)
4336 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004337 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004338}
4339
4340/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004341 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004342 * list, etc. Mostly like what JavaScript does, except that empty list and
4343 * empty dictionary are FALSE.
4344 */
4345 int
4346tv2bool(typval_T *tv)
4347{
4348 switch (tv->v_type)
4349 {
4350 case VAR_NUMBER:
4351 return tv->vval.v_number != 0;
4352 case VAR_FLOAT:
4353#ifdef FEAT_FLOAT
4354 return tv->vval.v_float != 0.0;
4355#else
4356 break;
4357#endif
4358 case VAR_PARTIAL:
4359 return tv->vval.v_partial != NULL;
4360 case VAR_FUNC:
4361 case VAR_STRING:
4362 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4363 case VAR_LIST:
4364 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4365 case VAR_DICT:
4366 return tv->vval.v_dict != NULL
4367 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4368 case VAR_BOOL:
4369 case VAR_SPECIAL:
4370 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4371 case VAR_JOB:
4372#ifdef FEAT_JOB_CHANNEL
4373 return tv->vval.v_job != NULL;
4374#else
4375 break;
4376#endif
4377 case VAR_CHANNEL:
4378#ifdef FEAT_JOB_CHANNEL
4379 return tv->vval.v_channel != NULL;
4380#else
4381 break;
4382#endif
4383 case VAR_BLOB:
4384 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4385 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004386 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004387 case VAR_VOID:
4388 break;
4389 }
4390 return FALSE;
4391}
4392
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004393 void
4394emsg_using_string_as(typval_T *tv, int as_number)
4395{
4396 semsg(_(as_number ? e_using_string_as_number_str
4397 : e_using_string_as_bool_str),
4398 tv->vval.v_string == NULL
4399 ? (char_u *)"" : tv->vval.v_string);
4400}
4401
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402/*
4403 * If "tv" is a string give an error and return FAIL.
4404 */
4405 int
4406check_not_string(typval_T *tv)
4407{
4408 if (tv->v_type == VAR_STRING)
4409 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004410 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004411 clear_tv(tv);
4412 return FAIL;
4413 }
4414 return OK;
4415}
4416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417
4418#endif // FEAT_EVAL