blob: 938fc2e191e8c6fd857160c6ab910912cdb0eef2 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010057struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaar0186e582021-01-10 18:33:11 +010061 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 garray_T ec_trystack; // stack of trycmd_T values
64 int ec_in_catch; // when TRUE in catch or finally block
65
66 int ec_dfunc_idx; // current function index
67 isn_T *ec_instr; // array with instructions
68 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020069
70 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010071};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
73// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020074#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar418f1df2020-08-12 21:34:49 +020076 void
77to_string_error(vartype_T vartype)
78{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020079 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020080}
81
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010083 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 */
85 static int
86ufunc_argcount(ufunc_T *ufunc)
87{
88 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
89}
90
91/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010092 * Set the instruction index, depending on omitted arguments, where the default
93 * values are to be computed. If all optional arguments are present, start
94 * with the function body.
95 * The expression evaluation is at the start of the instructions:
96 * 0 -> EVAL default1
97 * STORE arg[-2]
98 * 1 -> EVAL default2
99 * STORE arg[-1]
100 * 2 -> function body
101 */
102 static void
103init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
104{
105 if (ufunc->uf_def_args.ga_len == 0)
106 ectx->ec_iidx = 0;
107 else
108 {
109 int defcount = ufunc->uf_args.ga_len - argcount;
110
111 // If there is a varargs argument defcount can be negative, no defaults
112 // to evaluate then.
113 if (defcount < 0)
114 defcount = 0;
115 ectx->ec_iidx = ufunc->uf_def_arg_idx[
116 ufunc->uf_def_args.ga_len - defcount];
117 }
118}
119
120/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200121 * Create a new list from "count" items at the bottom of the stack.
122 * When "count" is zero an empty list is added to the stack.
123 */
124 static int
125exe_newlist(int count, ectx_T *ectx)
126{
127 list_T *list = list_alloc_with_items(count);
128 int idx;
129 typval_T *tv;
130
131 if (list == NULL)
132 return FAIL;
133 for (idx = 0; idx < count; ++idx)
134 list_set_item(list, idx, STACK_TV_BOT(idx - count));
135
136 if (count > 0)
137 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200138 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200139 return FAIL;
140 else
141 ++ectx->ec_stack.ga_len;
142 tv = STACK_TV_BOT(-1);
143 tv->v_type = VAR_LIST;
144 tv->vval.v_list = list;
145 ++list->lv_refcount;
146 return OK;
147}
148
149/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100151 * This adds a stack frame and sets the instruction pointer to the start of the
152 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100153 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154 *
155 * Stack has:
156 * - current arguments (already there)
157 * - omitted optional argument (default values) added here
158 * - stack frame:
159 * - pointer to calling function
160 * - Index of next instruction in calling function
161 * - previous frame pointer
162 * - reserved space for local variables
163 */
164 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100165call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200167 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
169 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 int arg_to_add;
171 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200172 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200174 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175
176 if (dfunc->df_deleted)
177 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100178 // don't use ufunc->uf_name, it may have been freed
179 emsg_funcname(e_func_deleted,
180 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 return FAIL;
182 }
183
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 if (ufunc->uf_va_name != NULL)
185 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200186 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200187 // Stack at time of call with 2 varargs:
188 // normal_arg
189 // optional_arg
190 // vararg_1
191 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 // After creating the list:
193 // normal_arg
194 // optional_arg
195 // vararg-list
196 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 // After creating the list
199 // normal_arg
200 // (space for optional_arg)
201 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200202 vararg_count = argcount - ufunc->uf_args.ga_len;
203 if (vararg_count < 0)
204 vararg_count = 0;
205 else
206 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200207 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200208 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209
210 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200211 }
212
Bram Moolenaarfe270812020-04-11 22:31:27 +0200213 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 if (arg_to_add < 0)
215 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200216 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200217 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200218 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 return FAIL;
221 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200222
223 // Reserve space for:
224 // - missing arguments
225 // - stack frame
226 // - local variables
227 // - if needed: a counter for number of closures created in
228 // ectx->ec_funcrefs.
229 varcount = dfunc->df_varcount + dfunc->df_has_closure;
230 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
231 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 return FAIL;
233
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100234 // If depth of calling is getting too high, don't execute the function.
235 if (funcdepth_increment() == FAIL)
236 return FAIL;
237
Bram Moolenaarfe270812020-04-11 22:31:27 +0200238 // Move the vararg-list to below the missing optional arguments.
239 if (vararg_count > 0 && arg_to_add > 0)
240 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100241
242 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200244 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
247 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100248 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
249 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100250 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
251 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200252 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
254 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200255 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200257 if (dfunc->df_has_closure)
258 {
259 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
260
261 tv->v_type = VAR_NUMBER;
262 tv->vval.v_number = 0;
263 }
264 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265
Bram Moolenaar0186e582021-01-10 18:33:11 +0100266 if (pt != NULL || ufunc->uf_partial != NULL || ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100267 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100268 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
269
270 if (outer == NULL)
271 return FAIL;
272 if (pt != NULL)
273 {
274 *outer = pt->pt_outer;
275 outer->out_up_is_copy = TRUE;
276 }
277 else if (ufunc->uf_partial != NULL)
278 {
279 *outer = ufunc->uf_partial->pt_outer;
280 outer->out_up_is_copy = TRUE;
281 }
282 else
283 {
284 outer->out_stack = &ectx->ec_stack;
285 outer->out_frame_idx = ectx->ec_frame_idx;
286 outer->out_up = ectx->ec_outer;
287 }
288 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100289 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100290 else
291 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 // Set execution state to the start of the called function.
294 ectx->ec_dfunc_idx = cdf_idx;
295 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200296 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
297 if (entry != NULL)
298 {
299 // Set the script context to the script where the function was defined.
300 // TODO: save more than the SID?
301 entry->es_save_sid = current_sctx.sc_sid;
302 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
303 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100304
305 // Decide where to start execution, handles optional arguments.
306 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307
308 return OK;
309}
310
311// Get pointer to item in the stack.
312#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
313
314/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200315 * Used when returning from a function: Check if any closure is still
316 * referenced. If so then move the arguments and variables to a separate piece
317 * of stack to be used when the closure is called.
318 * When "free_arguments" is TRUE the arguments are to be freed.
319 * Returns FAIL when out of memory.
320 */
321 static int
322handle_closure_in_use(ectx_T *ectx, int free_arguments)
323{
324 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
325 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200326 int argcount;
327 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 int idx;
329 typval_T *tv;
330 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200331 garray_T *gap = &ectx->ec_funcrefs;
332 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200333
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200334 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 return OK; // function was freed
336 if (dfunc->df_has_closure == 0)
337 return OK; // no closures
338 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
339 closure_count = tv->vval.v_number;
340 if (closure_count == 0)
341 return OK; // no funcrefs created
342
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200343 argcount = ufunc_argcount(dfunc->df_ufunc);
344 top = ectx->ec_frame_idx - argcount;
345
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200346 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200347 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200349 partial_T *pt;
350 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200351
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200352 if (off < 0)
353 continue; // count is off or already done
354 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200355 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200356 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200357 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200358 int i;
359
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200360 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200361 // unreferenced on return.
362 for (i = 0; i < dfunc->df_varcount; ++i)
363 {
364 typval_T *stv = STACK_TV(ectx->ec_frame_idx
365 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200366 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200367 --refcount;
368 }
369 if (refcount > 1)
370 {
371 closure_in_use = TRUE;
372 break;
373 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 }
376
377 if (closure_in_use)
378 {
379 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
380 typval_T *stack;
381
382 // A closure is using the arguments and/or local variables.
383 // Move them to the called function.
384 if (funcstack == NULL)
385 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200386 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
387 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
389 funcstack->fs_ga.ga_data = stack;
390 if (stack == NULL)
391 {
392 vim_free(funcstack);
393 return FAIL;
394 }
395
396 // Move or copy the arguments.
397 for (idx = 0; idx < argcount; ++idx)
398 {
399 tv = STACK_TV(top + idx);
400 if (free_arguments)
401 {
402 *(stack + idx) = *tv;
403 tv->v_type = VAR_UNKNOWN;
404 }
405 else
406 copy_tv(tv, stack + idx);
407 }
408 // Move the local variables.
409 for (idx = 0; idx < dfunc->df_varcount; ++idx)
410 {
411 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200412
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200413 // A partial created for a local function, that is also used as a
414 // local variable, has a reference count for the variable, thus
415 // will never go down to zero. When all these refcounts are one
416 // then the funcstack is unused. We need to count how many we have
417 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200418 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
419 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200420 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200421
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
424 gap->ga_len - closure_count + i])
425 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200426 }
427
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200428 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200429 tv->v_type = VAR_UNKNOWN;
430 }
431
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200433 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200434 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
435 - closure_count + idx];
436 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200438 ++funcstack->fs_refcount;
439 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100440 pt->pt_outer.out_stack = &funcstack->fs_ga;
441 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
442 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200443 }
444 }
445 }
446
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200447 for (idx = 0; idx < closure_count; ++idx)
448 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
449 - closure_count + idx]);
450 gap->ga_len -= closure_count;
451 if (gap->ga_len == 0)
452 ga_clear(gap);
453
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200454 return OK;
455}
456
457/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200458 * Called when a partial is freed or its reference count goes down to one. The
459 * funcstack may be the only reference to the partials in the local variables.
460 * Go over all of them, the funcref and can be freed if all partials
461 * referencing the funcstack have a reference count of one.
462 */
463 void
464funcstack_check_refcount(funcstack_T *funcstack)
465{
466 int i;
467 garray_T *gap = &funcstack->fs_ga;
468 int done = 0;
469
470 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
471 return;
472 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
473 {
474 typval_T *tv = ((typval_T *)gap->ga_data) + i;
475
476 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
477 && tv->vval.v_partial->pt_funcstack == funcstack
478 && tv->vval.v_partial->pt_refcount == 1)
479 ++done;
480 }
481 if (done == funcstack->fs_min_refcount)
482 {
483 typval_T *stack = gap->ga_data;
484
485 // All partials referencing the funcstack have a reference count of
486 // one, thus the funcstack is no longer of use.
487 for (i = 0; i < gap->ga_len; ++i)
488 clear_tv(stack + i);
489 vim_free(stack);
490 vim_free(funcstack);
491 }
492}
493
494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 * Return from the current function.
496 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498func_return(ectx_T *ectx)
499{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100501 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200502 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
503 + ectx->ec_dfunc_idx;
504 int argcount = ufunc_argcount(dfunc->df_ufunc);
505 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200506 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507
508 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200509 entry = estack_pop();
510 if (entry != NULL)
511 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 if (handle_closure_in_use(ectx, TRUE) == FAIL)
514 return FAIL;
515
516 // Clear the arguments.
517 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
518 clear_tv(STACK_TV(idx));
519
520 // Clear local variables and temp values, but not the return value.
521 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100522 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100524
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100525 // The return value should be on top of the stack. However, when aborting
526 // it may not be there and ec_frame_idx is the top of the stack.
527 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100529 ret_idx = 0;
530
Bram Moolenaar0186e582021-01-10 18:33:11 +0100531 vim_free(ectx->ec_outer);
532
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100533 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100534 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
535 + STACK_FRAME_FUNC_OFF)->vval.v_number;
536 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
537 + STACK_FRAME_IIDX_OFF)->vval.v_number;
538 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
539 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200540 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
542 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
544 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100545
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100546 if (ret_idx > 0)
547 {
548 // Reset the stack to the position before the call, with a spot for the
549 // return value, moved there from above the frame.
550 ectx->ec_stack.ga_len = top + 1;
551 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
552 }
553 else
554 // Reset the stack to the position before the call.
555 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200556
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200558 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559}
560
561#undef STACK_TV
562
563/*
564 * Prepare arguments and rettv for calling a builtin or user function.
565 */
566 static int
567call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
568{
569 int idx;
570 typval_T *tv;
571
572 // Move arguments from bottom of the stack to argvars[] and add terminator.
573 for (idx = 0; idx < argcount; ++idx)
574 argvars[idx] = *STACK_TV_BOT(idx - argcount);
575 argvars[argcount].v_type = VAR_UNKNOWN;
576
577 // Result replaces the arguments on the stack.
578 if (argcount > 0)
579 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200580 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 return FAIL;
582 else
583 ++ectx->ec_stack.ga_len;
584
585 // Default return value is zero.
586 tv = STACK_TV_BOT(-1);
587 tv->v_type = VAR_NUMBER;
588 tv->vval.v_number = 0;
589
590 return OK;
591}
592
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200593// Ugly global to avoid passing the execution context around through many
594// layers.
595static ectx_T *current_ectx = NULL;
596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597/*
598 * Call a builtin function by index.
599 */
600 static int
601call_bfunc(int func_idx, int argcount, ectx_T *ectx)
602{
603 typval_T argvars[MAX_FUNC_ARGS];
604 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100605 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200606 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 if (call_prepare(argcount, argvars, ectx) == FAIL)
609 return FAIL;
610
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200611 // Call the builtin function. Set "current_ectx" so that when it
612 // recursively invokes call_def_function() a closure context can be set.
613 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200615 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
617 // Clear the arguments.
618 for (idx = 0; idx < argcount; ++idx)
619 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200620
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100621 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 return OK;
624}
625
626/*
627 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100628 * If the function is compiled this will add a stack frame and set the
629 * instruction pointer at the start of the function.
630 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100631 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100632 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 */
634 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635call_ufunc(
636 ufunc_T *ufunc,
637 partial_T *pt,
638 int argcount,
639 ectx_T *ectx,
640 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641{
642 typval_T argvars[MAX_FUNC_ARGS];
643 funcexe_T funcexe;
644 int error;
645 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100646 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200648 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200649 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
650 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200651 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100652 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100653 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100654 if (error != FCERR_UNKNOWN)
655 {
656 if (error == FCERR_TOOMANY)
657 semsg(_(e_toomanyarg), ufunc->uf_name);
658 else
659 semsg(_(e_toofewarg), ufunc->uf_name);
660 return FAIL;
661 }
662
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100663 // The function has been compiled, can call it quickly. For a function
664 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100665 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100666 if (iptr != NULL)
667 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100668 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 iptr->isn_type = ISN_DCALL;
670 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
671 iptr->isn_arg.dfunc.cdf_argcount = argcount;
672 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100673 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (call_prepare(argcount, argvars, ectx) == FAIL)
677 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200678 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 funcexe.evaluate = TRUE;
680
681 // Call the user function. Result goes in last position on the stack.
682 // TODO: add selfdict if there is one
683 error = call_user_func_check(ufunc, argcount, argvars,
684 STACK_TV_BOT(-1), &funcexe, NULL);
685
686 // Clear the arguments.
687 for (idx = 0; idx < argcount; ++idx)
688 clear_tv(&argvars[idx]);
689
690 if (error != FCERR_NONE)
691 {
692 user_func_error(error, ufunc->uf_name);
693 return FAIL;
694 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200696 // Error other than from calling the function itself.
697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 return OK;
699}
700
701/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200702 * Return TRUE if an error was given or CTRL-C was pressed.
703 */
704 static int
705vim9_aborting(int prev_called_emsg)
706{
707 return called_emsg > prev_called_emsg || got_int || did_throw;
708}
709
710/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 * Execute a function by "name".
712 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100713 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 * Returns FAIL if not found without an error message.
715 */
716 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718{
719 ufunc_T *ufunc;
720
721 if (builtin_function(name, -1))
722 {
723 int func_idx = find_internal_func(name);
724
725 if (func_idx < 0)
726 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200727 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 return FAIL;
729 return call_bfunc(func_idx, argcount, ectx);
730 }
731
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200732 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200733
734 if (ufunc == NULL)
735 {
736 int called_emsg_before = called_emsg;
737
738 if (script_autoload(name, TRUE))
739 // loaded a package, search for the function again
740 ufunc = find_func(name, FALSE, NULL);
741 if (vim9_aborting(called_emsg_before))
742 return FAIL; // bail out if loading the script caused an error
743 }
744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100746 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
748 return FAIL;
749}
750
751 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200752call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200754 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200755 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100757 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if (tv->v_type == VAR_PARTIAL)
760 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200761 partial_T *pt = tv->vval.v_partial;
762 int i;
763
764 if (pt->pt_argc > 0)
765 {
766 // Make space for arguments from the partial, shift the "argcount"
767 // arguments up.
768 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
769 return FAIL;
770 for (i = 1; i <= argcount; ++i)
771 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
772 ectx->ec_stack.ga_len += pt->pt_argc;
773 argcount += pt->pt_argc;
774
775 // copy the arguments from the partial onto the stack
776 for (i = 0; i < pt->pt_argc; ++i)
777 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
780 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 name = pt->pt_name;
784 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200785 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200787 if (name != NULL)
788 {
789 char_u fname_buf[FLEN_FIXED + 1];
790 char_u *tofree = NULL;
791 int error = FCERR_NONE;
792 char_u *fname;
793
794 // May need to translate <SNR>123_ to K_SNR.
795 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
796 if (error != FCERR_NONE)
797 res = FAIL;
798 else
799 res = call_by_name(fname, argcount, ectx, NULL);
800 vim_free(tofree);
801 }
802
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100803 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 {
805 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200806 semsg(_(e_unknownfunc),
807 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 return FAIL;
809 }
810 return OK;
811}
812
813/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200814 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
815 * TRUE.
816 */
817 static int
818error_if_locked(int lock, char *error)
819{
820 if (lock & (VAR_LOCKED | VAR_FIXED))
821 {
822 emsg(_(error));
823 return TRUE;
824 }
825 return FALSE;
826}
827
828/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100829 * Store "tv" in variable "name".
830 * This is for s: and g: variables.
831 */
832 static void
833store_var(char_u *name, typval_T *tv)
834{
835 funccal_entry_T entry;
836
837 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100838 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100839 restore_funccal();
840}
841
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100842/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100843 * Convert "tv" to a string.
844 * Return FAIL if not allowed.
845 */
846 static int
847do_2string(typval_T *tv, int is_2string_any)
848{
849 if (tv->v_type != VAR_STRING)
850 {
851 char_u *str;
852
853 if (is_2string_any)
854 {
855 switch (tv->v_type)
856 {
857 case VAR_SPECIAL:
858 case VAR_BOOL:
859 case VAR_NUMBER:
860 case VAR_FLOAT:
861 case VAR_BLOB: break;
862 default: to_string_error(tv->v_type);
863 return FAIL;
864 }
865 }
866 str = typval_tostring(tv);
867 clear_tv(tv);
868 tv->v_type = VAR_STRING;
869 tv->vval.v_string = str;
870 }
871 return OK;
872}
873
874/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100875 * When the value of "sv" is a null list of dict, allocate it.
876 */
877 static void
878allocate_if_null(typval_T *tv)
879{
880 switch (tv->v_type)
881 {
882 case VAR_LIST:
883 if (tv->vval.v_list == NULL)
884 rettv_list_alloc(tv);
885 break;
886 case VAR_DICT:
887 if (tv->vval.v_dict == NULL)
888 rettv_dict_alloc(tv);
889 break;
890 default:
891 break;
892 }
893}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200894
Bram Moolenaare7525c52021-01-09 13:20:37 +0100895/*
896 * Return the character "str[index]" where "index" is the character index. If
897 * "index" is out of range NULL is returned.
898 */
899 char_u *
900char_from_string(char_u *str, varnumber_T index)
901{
902 size_t nbyte = 0;
903 varnumber_T nchar = index;
904 size_t slen;
905
906 if (str == NULL)
907 return NULL;
908 slen = STRLEN(str);
909
910 // do the same as for a list: a negative index counts from the end
911 if (index < 0)
912 {
913 int clen = 0;
914
915 for (nbyte = 0; nbyte < slen; ++clen)
916 nbyte += MB_CPTR2LEN(str + nbyte);
917 nchar = clen + index;
918 if (nchar < 0)
919 // unlike list: index out of range results in empty string
920 return NULL;
921 }
922
923 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
924 nbyte += MB_CPTR2LEN(str + nbyte);
925 if (nbyte >= slen)
926 return NULL;
927 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
928}
929
930/*
931 * Get the byte index for character index "idx" in string "str" with length
932 * "str_len".
933 * If going over the end return "str_len".
934 * If "idx" is negative count from the end, -1 is the last character.
935 * When going over the start return -1.
936 */
937 static long
938char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
939{
940 varnumber_T nchar = idx;
941 size_t nbyte = 0;
942
943 if (nchar >= 0)
944 {
945 while (nchar > 0 && nbyte < str_len)
946 {
947 nbyte += MB_CPTR2LEN(str + nbyte);
948 --nchar;
949 }
950 }
951 else
952 {
953 nbyte = str_len;
954 while (nchar < 0 && nbyte > 0)
955 {
956 --nbyte;
957 nbyte -= mb_head_off(str, str + nbyte);
958 ++nchar;
959 }
960 if (nchar < 0)
961 return -1;
962 }
963 return (long)nbyte;
964}
965
966/*
967 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100968 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100969 * Return NULL when the result is empty.
970 */
971 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100972string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100973{
974 long start_byte, end_byte;
975 size_t slen;
976
977 if (str == NULL)
978 return NULL;
979 slen = STRLEN(str);
980 start_byte = char_idx2byte(str, slen, first);
981 if (start_byte < 0)
982 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100983 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100984 end_byte = (long)slen;
985 else
986 {
987 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100988 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100989 // end index is inclusive
990 end_byte += MB_CPTR2LEN(str + end_byte);
991 }
992
993 if (start_byte >= (long)slen || end_byte <= start_byte)
994 return NULL;
995 return vim_strnsave(str + start_byte, end_byte - start_byte);
996}
997
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100998 static svar_T *
999get_script_svar(scriptref_T *sref, ectx_T *ectx)
1000{
1001 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1002 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1003 + ectx->ec_dfunc_idx;
1004 svar_T *sv;
1005
1006 if (sref->sref_seq != si->sn_script_seq)
1007 {
1008 // The script was reloaded after the function was
1009 // compiled, the script_idx may not be valid.
1010 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1011 dfunc->df_ufunc->uf_name_exp);
1012 return NULL;
1013 }
1014 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1015 if (!equal_type(sv->sv_type, sref->sref_type))
1016 {
1017 emsg(_(e_script_variable_type_changed));
1018 return NULL;
1019 }
1020 return sv;
1021}
1022
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 * Execute a function by "name".
1025 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001026 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 */
1028 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001029call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030{
Bram Moolenaared677f52020-08-12 16:38:10 +02001031 int called_emsg_before = called_emsg;
1032 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
Bram Moolenaared677f52020-08-12 16:38:10 +02001034 res = call_by_name(name, argcount, ectx, iptr);
1035 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001037 dictitem_T *v;
1038
1039 v = find_var(name, NULL, FALSE);
1040 if (v == NULL)
1041 {
1042 semsg(_(e_unknownfunc), name);
1043 return FAIL;
1044 }
1045 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1046 {
1047 semsg(_(e_unknownfunc), name);
1048 return FAIL;
1049 }
1050 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001052 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053}
1054
1055/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001056 * When a function reference is used, fill a partial with the information
1057 * needed, especially when it is used as a closure.
1058 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001059 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001060fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1061{
1062 pt->pt_func = ufunc;
1063 pt->pt_refcount = 1;
1064
1065 if (pt->pt_func->uf_flags & FC_CLOSURE)
1066 {
1067 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1068 + ectx->ec_dfunc_idx;
1069
1070 // The closure needs to find arguments and local
1071 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001072 pt->pt_outer.out_stack = &ectx->ec_stack;
1073 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1074 pt->pt_outer.out_up = ectx->ec_outer;
1075 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001076
1077 // If this function returns and the closure is still
1078 // being used, we need to make a copy of the context
1079 // (arguments and local variables). Store a reference
1080 // to the partial so we can handle that.
1081 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1082 {
1083 vim_free(pt);
1084 return FAIL;
1085 }
1086 // Extra variable keeps the count of closures created
1087 // in the current function call.
1088 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1089 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1090
1091 ((partial_T **)ectx->ec_funcrefs.ga_data)
1092 [ectx->ec_funcrefs.ga_len] = pt;
1093 ++pt->pt_refcount;
1094 ++ectx->ec_funcrefs.ga_len;
1095 }
1096 ++pt->pt_func->uf_refcount;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 * Call a "def" function from old Vim script.
1102 * Return OK or FAIL.
1103 */
1104 int
1105call_def_function(
1106 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001107 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001109 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 typval_T *rettv) // return value
1111{
1112 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001113 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001114 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 typval_T *tv;
1116 int idx;
1117 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001118 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001119 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001120 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001121 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001122 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001123 msglist_T **saved_msg_list = NULL;
1124 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001125 cmdmod_T save_cmdmod;
1126 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001127 int save_emsg_silent_def = emsg_silent_def;
1128 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001129 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001130 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131
1132// Get pointer to item in the stack.
1133#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1134
1135// Get pointer to item at the bottom of the stack, -1 is the bottom.
1136#undef STACK_TV_BOT
1137#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1138
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001139// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001140#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 +01001141
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001142 if (ufunc->uf_def_status == UF_NOT_COMPILED
1143 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001144 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001145 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001146 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001147 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001148 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001149 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001150 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001151
Bram Moolenaar09689a02020-05-09 22:50:08 +02001152 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001153 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001154 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1155 + ufunc->uf_dfunc_idx;
1156 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001157 {
1158 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001159 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001160 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001163 // If depth of calling is getting too high, don't execute the function.
1164 orig_funcdepth = funcdepth_get();
1165 if (funcdepth_increment() == FAIL)
1166 return FAIL;
1167
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001168 CLEAR_FIELD(ectx);
1169 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1170 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1171 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001172 {
1173 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001174 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001175 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001177 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001179 // Put arguments on the stack, but no more than what the function expects.
1180 // A lambda can be called with more arguments than it uses.
1181 for (idx = 0; idx < argc
1182 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1183 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001185 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001186 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1187 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001188 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 copy_tv(&argv[idx], STACK_TV_BOT(0));
1190 ++ectx.ec_stack.ga_len;
1191 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001192
1193 // Turn varargs into a list. Empty list if no args.
1194 if (ufunc->uf_va_name != NULL)
1195 {
1196 int vararg_count = argc - ufunc->uf_args.ga_len;
1197
1198 if (vararg_count < 0)
1199 vararg_count = 0;
1200 else
1201 argc -= vararg_count;
1202 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001203 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001204
1205 // Check the type of the list items.
1206 tv = STACK_TV_BOT(-1);
1207 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001208 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001209 && ufunc->uf_va_type->tt_member != &t_any
1210 && tv->vval.v_list != NULL)
1211 {
1212 type_T *expected = ufunc->uf_va_type->tt_member;
1213 listitem_T *li = tv->vval.v_list->lv_first;
1214
1215 for (idx = 0; idx < vararg_count; ++idx)
1216 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001217 if (check_typval_type(expected, &li->li_tv,
1218 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001219 goto failed_early;
1220 li = li->li_next;
1221 }
1222 }
1223
Bram Moolenaar23e03252020-04-12 22:22:31 +02001224 if (defcount > 0)
1225 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001226 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001227 --ectx.ec_stack.ga_len;
1228 }
1229
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001230 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001231 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001232 if (defcount > 0)
1233 for (idx = 0; idx < defcount; ++idx)
1234 {
1235 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1236 ++ectx.ec_stack.ga_len;
1237 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001238 if (ufunc->uf_va_name != NULL)
1239 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240
1241 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001242 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1243 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244
Bram Moolenaar0186e582021-01-10 18:33:11 +01001245 if (partial != NULL || ufunc->uf_partial != NULL)
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001246 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001247 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1248 if (ectx.ec_outer == NULL)
1249 goto failed_early;
1250 if (partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001251 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001252 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1253 {
1254 if (current_ectx->ec_outer != NULL)
1255 *ectx.ec_outer = *current_ectx->ec_outer;
1256 }
1257 else
1258 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001259 }
1260 else
Bram Moolenaar0186e582021-01-10 18:33:11 +01001261 *ectx.ec_outer = ufunc->uf_partial->pt_outer;
1262 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001263 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 // dummy frame entries
1266 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1267 {
1268 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1269 ++ectx.ec_stack.ga_len;
1270 }
1271
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001272 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001273 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001274 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1275 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001277 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001278 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001279 ectx.ec_stack.ga_len += dfunc->df_varcount;
1280 if (dfunc->df_has_closure)
1281 {
1282 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1283 STACK_TV_VAR(idx)->vval.v_number = 0;
1284 ++ectx.ec_stack.ga_len;
1285 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001286
1287 ectx.ec_instr = dfunc->df_instr;
1288 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001289
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001290 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001291 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001292 estack_push_ufunc(ufunc, 1);
1293 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001294 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1295
Bram Moolenaar352134b2020-10-17 22:04:08 +02001296 // Use a specific location for storing error messages to be converted to an
1297 // exception.
1298 saved_msg_list = msg_list;
1299 msg_list = &private_msg_list;
1300
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001301 // Do turn errors into exceptions.
1302 suppress_errthrow = FALSE;
1303
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001304 // When ":silent!" was used before calling then we still abort the
1305 // function. If ":silent!" is used in the function then we don't.
1306 emsg_silent_def = emsg_silent;
1307 did_emsg_def = 0;
1308
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001309 // Decide where to start execution, handles optional arguments.
1310 init_instr_idx(ufunc, argc, &ectx);
1311
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 for (;;)
1313 {
1314 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001315
Bram Moolenaar270d0382020-05-15 21:42:53 +02001316 if (++breakcheck_count >= 100)
1317 {
1318 line_breakcheck();
1319 breakcheck_count = 0;
1320 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001321 if (got_int)
1322 {
1323 // Turn CTRL-C into an exception.
1324 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001325 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001326 goto failed;
1327 did_throw = TRUE;
1328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329
Bram Moolenaara26b9702020-04-18 19:53:28 +02001330 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1331 {
1332 // Turn an error message into an exception.
1333 did_emsg = FALSE;
1334 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1335 goto failed;
1336 did_throw = TRUE;
1337 *msg_list = NULL;
1338 }
1339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 if (did_throw && !ectx.ec_in_catch)
1341 {
1342 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001343 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344
1345 // An exception jumps to the first catch, finally, or returns from
1346 // the current function.
1347 if (trystack->ga_len > 0)
1348 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001349 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 {
1351 // jump to ":catch" or ":finally"
1352 ectx.ec_in_catch = TRUE;
1353 ectx.ec_iidx = trycmd->tcd_catch_idx;
1354 }
1355 else
1356 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001357 // Not inside try or need to return from current functions.
1358 // Push a dummy return value.
1359 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1360 goto failed;
1361 tv = STACK_TV_BOT(0);
1362 tv->v_type = VAR_NUMBER;
1363 tv->vval.v_number = 0;
1364 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001365 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001367 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001368 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001369 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1370 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 goto done;
1372 }
1373
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001374 if (func_return(&ectx) == FAIL)
1375 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 }
1377 continue;
1378 }
1379
1380 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1381 switch (iptr->isn_type)
1382 {
1383 // execute Ex command line
1384 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001385 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001386 source_cookie_T cookie;
1387
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001388 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001389 // Pass getsourceline to get an error for a missing ":end"
1390 // command.
1391 CLEAR_FIELD(cookie);
1392 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1393 if (do_cmdline(iptr->isn_arg.string,
1394 getsourceline, &cookie,
1395 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1396 == FAIL
1397 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001398 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001399 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 break;
1401
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001402 // execute Ex command from pieces on the stack
1403 case ISN_EXECCONCAT:
1404 {
1405 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001406 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001407 int pass;
1408 int i;
1409 char_u *cmd = NULL;
1410 char_u *str;
1411
1412 for (pass = 1; pass <= 2; ++pass)
1413 {
1414 for (i = 0; i < count; ++i)
1415 {
1416 tv = STACK_TV_BOT(i - count);
1417 str = tv->vval.v_string;
1418 if (str != NULL && *str != NUL)
1419 {
1420 if (pass == 2)
1421 STRCPY(cmd + len, str);
1422 len += STRLEN(str);
1423 }
1424 if (pass == 2)
1425 clear_tv(tv);
1426 }
1427 if (pass == 1)
1428 {
1429 cmd = alloc(len + 1);
1430 if (cmd == NULL)
1431 goto failed;
1432 len = 0;
1433 }
1434 }
1435
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001436 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001437 do_cmdline_cmd(cmd);
1438 vim_free(cmd);
1439 }
1440 break;
1441
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 // execute :echo {string} ...
1443 case ISN_ECHO:
1444 {
1445 int count = iptr->isn_arg.echo.echo_count;
1446 int atstart = TRUE;
1447 int needclr = TRUE;
1448
1449 for (idx = 0; idx < count; ++idx)
1450 {
1451 tv = STACK_TV_BOT(idx - count);
1452 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1453 &atstart, &needclr);
1454 clear_tv(tv);
1455 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001456 if (needclr)
1457 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 ectx.ec_stack.ga_len -= count;
1459 }
1460 break;
1461
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001462 // :execute {string} ...
1463 // :echomsg {string} ...
1464 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001465 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001466 case ISN_ECHOMSG:
1467 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001468 {
1469 int count = iptr->isn_arg.number;
1470 garray_T ga;
1471 char_u buf[NUMBUFLEN];
1472 char_u *p;
1473 int len;
1474 int failed = FALSE;
1475
1476 ga_init2(&ga, 1, 80);
1477 for (idx = 0; idx < count; ++idx)
1478 {
1479 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001480 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001481 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001482 if (tv->v_type == VAR_CHANNEL
1483 || tv->v_type == VAR_JOB)
1484 {
1485 SOURCING_LNUM = iptr->isn_lnum;
1486 emsg(_(e_inval_string));
1487 break;
1488 }
1489 else
1490 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001491 }
1492 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001493 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001494
1495 len = (int)STRLEN(p);
1496 if (ga_grow(&ga, len + 2) == FAIL)
1497 failed = TRUE;
1498 else
1499 {
1500 if (ga.ga_len > 0)
1501 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1502 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1503 ga.ga_len += len;
1504 }
1505 clear_tv(tv);
1506 }
1507 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001508 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001509 {
1510 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001511 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001512 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001513
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001514 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001515 {
1516 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001517 {
1518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001519 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001520 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001521 {
1522 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001523 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001524 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001525 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001526 else
1527 {
1528 msg_sb_eol();
1529 if (iptr->isn_type == ISN_ECHOMSG)
1530 {
1531 msg_attr(ga.ga_data, echo_attr);
1532 out_flush();
1533 }
1534 else
1535 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001536 SOURCING_LNUM = iptr->isn_lnum;
1537 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001538 }
1539 }
1540 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001541 ga_clear(&ga);
1542 }
1543 break;
1544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 // load local variable or argument
1546 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001547 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 goto failed;
1549 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1550 ++ectx.ec_stack.ga_len;
1551 break;
1552
1553 // load v: variable
1554 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001555 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 goto failed;
1557 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1558 ++ectx.ec_stack.ga_len;
1559 break;
1560
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001561 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 case ISN_LOADSCRIPT:
1563 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001564 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 svar_T *sv;
1566
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001567 sv = get_script_svar(sref, &ectx);
1568 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001569 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001570 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001571 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 goto failed;
1573 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1574 ++ectx.ec_stack.ga_len;
1575 }
1576 break;
1577
1578 // load s: variable in old script
1579 case ISN_LOADS:
1580 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001581 hashtab_T *ht = &SCRIPT_VARS(
1582 iptr->isn_arg.loadstore.ls_sid);
1583 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 if (di == NULL)
1587 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001588 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001589 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001590 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 }
1592 else
1593 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001594 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001595 goto failed;
1596 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1597 ++ectx.ec_stack.ga_len;
1598 }
1599 }
1600 break;
1601
Bram Moolenaard3aac292020-04-19 14:32:17 +02001602 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001604 case ISN_LOADB:
1605 case ISN_LOADW:
1606 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001608 dictitem_T *di = NULL;
1609 hashtab_T *ht = NULL;
1610 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001611
Bram Moolenaard3aac292020-04-19 14:32:17 +02001612 switch (iptr->isn_type)
1613 {
1614 case ISN_LOADG:
1615 ht = get_globvar_ht();
1616 namespace = 'g';
1617 break;
1618 case ISN_LOADB:
1619 ht = &curbuf->b_vars->dv_hashtab;
1620 namespace = 'b';
1621 break;
1622 case ISN_LOADW:
1623 ht = &curwin->w_vars->dv_hashtab;
1624 namespace = 'w';
1625 break;
1626 case ISN_LOADT:
1627 ht = &curtab->tp_vars->dv_hashtab;
1628 namespace = 't';
1629 break;
1630 default: // Cannot reach here
1631 goto failed;
1632 }
1633 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001634
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 if (di == NULL)
1636 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001637 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001638 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001639 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001640 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001641 }
1642 else
1643 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001644 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 goto failed;
1646 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1647 ++ectx.ec_stack.ga_len;
1648 }
1649 }
1650 break;
1651
Bram Moolenaar03290b82020-12-19 16:30:44 +01001652 // load autoload variable
1653 case ISN_LOADAUTO:
1654 {
1655 char_u *name = iptr->isn_arg.string;
1656
1657 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1658 goto failed;
1659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001660 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001661 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1662 goto on_error;
1663 ++ectx.ec_stack.ga_len;
1664 }
1665 break;
1666
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001667 // load g:/b:/w:/t: namespace
1668 case ISN_LOADGDICT:
1669 case ISN_LOADBDICT:
1670 case ISN_LOADWDICT:
1671 case ISN_LOADTDICT:
1672 {
1673 dict_T *d = NULL;
1674
1675 switch (iptr->isn_type)
1676 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001677 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1678 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1679 case ISN_LOADWDICT: d = curwin->w_vars; break;
1680 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001681 default: // Cannot reach here
1682 goto failed;
1683 }
1684 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1685 goto failed;
1686 tv = STACK_TV_BOT(0);
1687 tv->v_type = VAR_DICT;
1688 tv->v_lock = 0;
1689 tv->vval.v_dict = d;
1690 ++ectx.ec_stack.ga_len;
1691 }
1692 break;
1693
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 // load &option
1695 case ISN_LOADOPT:
1696 {
1697 typval_T optval;
1698 char_u *name = iptr->isn_arg.string;
1699
Bram Moolenaara8c17702020-04-01 21:17:24 +02001700 // This is not expected to fail, name is checked during
1701 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001702 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001704 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001705 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706 *STACK_TV_BOT(0) = optval;
1707 ++ectx.ec_stack.ga_len;
1708 }
1709 break;
1710
1711 // load $ENV
1712 case ISN_LOADENV:
1713 {
1714 typval_T optval;
1715 char_u *name = iptr->isn_arg.string;
1716
Bram Moolenaar270d0382020-05-15 21:42:53 +02001717 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001719 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001720 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 *STACK_TV_BOT(0) = optval;
1722 ++ectx.ec_stack.ga_len;
1723 }
1724 break;
1725
1726 // load @register
1727 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001728 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 goto failed;
1730 tv = STACK_TV_BOT(0);
1731 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001732 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001733 // This may result in NULL, which should be equivalent to an
1734 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 tv->vval.v_string = get_reg_contents(
1736 iptr->isn_arg.number, GREG_EXPR_SRC);
1737 ++ectx.ec_stack.ga_len;
1738 break;
1739
1740 // store local variable
1741 case ISN_STORE:
1742 --ectx.ec_stack.ga_len;
1743 tv = STACK_TV_VAR(iptr->isn_arg.number);
1744 clear_tv(tv);
1745 *tv = *STACK_TV_BOT(0);
1746 break;
1747
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001748 // store s: variable in old script
1749 case ISN_STORES:
1750 {
1751 hashtab_T *ht = &SCRIPT_VARS(
1752 iptr->isn_arg.loadstore.ls_sid);
1753 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001754 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001755
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001756 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001757 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001758 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001759 else
1760 {
1761 clear_tv(&di->di_tv);
1762 di->di_tv = *STACK_TV_BOT(0);
1763 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001764 }
1765 break;
1766
1767 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 case ISN_STORESCRIPT:
1769 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001770 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1771 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001773 sv = get_script_svar(sref, &ectx);
1774 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001775 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 --ectx.ec_stack.ga_len;
1777 clear_tv(sv->sv_tv);
1778 *sv->sv_tv = *STACK_TV_BOT(0);
1779 }
1780 break;
1781
1782 // store option
1783 case ISN_STOREOPT:
1784 {
1785 long n = 0;
1786 char_u *s = NULL;
1787 char *msg;
1788
1789 --ectx.ec_stack.ga_len;
1790 tv = STACK_TV_BOT(0);
1791 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001792 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001794 if (s == NULL)
1795 s = (char_u *)"";
1796 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001797 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001798 // must be VAR_NUMBER, CHECKTYPE makes sure
1799 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1801 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001802 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 if (msg != NULL)
1804 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001805 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 }
1810 break;
1811
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001812 // store $ENV
1813 case ISN_STOREENV:
1814 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001815 tv = STACK_TV_BOT(0);
1816 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1817 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001818 break;
1819
1820 // store @r
1821 case ISN_STOREREG:
1822 {
1823 int reg = iptr->isn_arg.number;
1824
1825 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001826 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001827 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001828 tv_get_string(tv), -1, FALSE);
1829 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001830 }
1831 break;
1832
1833 // store v: variable
1834 case ISN_STOREV:
1835 --ectx.ec_stack.ga_len;
1836 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1837 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001838 // should not happen, type is checked when compiling
1839 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001840 break;
1841
Bram Moolenaard3aac292020-04-19 14:32:17 +02001842 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001844 case ISN_STOREB:
1845 case ISN_STOREW:
1846 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001848 dictitem_T *di;
1849 hashtab_T *ht;
1850 char_u *name = iptr->isn_arg.string + 2;
1851
Bram Moolenaard3aac292020-04-19 14:32:17 +02001852 switch (iptr->isn_type)
1853 {
1854 case ISN_STOREG:
1855 ht = get_globvar_ht();
1856 break;
1857 case ISN_STOREB:
1858 ht = &curbuf->b_vars->dv_hashtab;
1859 break;
1860 case ISN_STOREW:
1861 ht = &curwin->w_vars->dv_hashtab;
1862 break;
1863 case ISN_STORET:
1864 ht = &curtab->tp_vars->dv_hashtab;
1865 break;
1866 default: // Cannot reach here
1867 goto failed;
1868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869
1870 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001871 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001873 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 else
1875 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001876 SOURCING_LNUM = iptr->isn_lnum;
1877 if (var_check_permission(di, name) == FAIL)
1878 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 clear_tv(&di->di_tv);
1880 di->di_tv = *STACK_TV_BOT(0);
1881 }
1882 }
1883 break;
1884
Bram Moolenaar03290b82020-12-19 16:30:44 +01001885 // store an autoload variable
1886 case ISN_STOREAUTO:
1887 SOURCING_LNUM = iptr->isn_lnum;
1888 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1889 clear_tv(STACK_TV_BOT(-1));
1890 --ectx.ec_stack.ga_len;
1891 break;
1892
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 // store number in local variable
1894 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001895 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 clear_tv(tv);
1897 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001898 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 break;
1900
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001901 // store value in list or dict variable
1902 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001903 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001904 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001905 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001906 typval_T *tv_dest = STACK_TV_BOT(-1);
1907 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001908
Bram Moolenaar752fc692021-01-04 21:57:11 +01001909 // Stack contains:
1910 // -3 value to be stored
1911 // -2 index
1912 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001913 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001914 SOURCING_LNUM = iptr->isn_lnum;
1915 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001916 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001917 dest_type = tv_dest->v_type;
1918 if (dest_type == VAR_DICT)
1919 status = do_2string(tv_idx, TRUE);
1920 else if (dest_type == VAR_LIST
1921 && tv_idx->v_type != VAR_NUMBER)
1922 {
1923 emsg(_(e_number_exp));
1924 status = FAIL;
1925 }
1926 }
1927 else if (dest_type != tv_dest->v_type)
1928 {
1929 // just in case, should be OK
1930 semsg(_(e_expected_str_but_got_str),
1931 vartype_name(dest_type),
1932 vartype_name(tv_dest->v_type));
1933 status = FAIL;
1934 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001935
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001936 if (status == OK && dest_type == VAR_LIST)
1937 {
1938 varnumber_T lidx = tv_idx->vval.v_number;
1939 list_T *list = tv_dest->vval.v_list;
1940
1941 if (list == NULL)
1942 {
1943 emsg(_(e_list_not_set));
1944 goto on_error;
1945 }
1946 if (lidx < 0 && list->lv_len + lidx >= 0)
1947 // negative index is relative to the end
1948 lidx = list->lv_len + lidx;
1949 if (lidx < 0 || lidx > list->lv_len)
1950 {
1951 semsg(_(e_listidx), lidx);
1952 goto on_error;
1953 }
1954 if (lidx < list->lv_len)
1955 {
1956 listitem_T *li = list_find(list, lidx);
1957
1958 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001959 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001960 goto on_error;
1961 // overwrite existing list item
1962 clear_tv(&li->li_tv);
1963 li->li_tv = *tv;
1964 }
1965 else
1966 {
1967 if (error_if_locked(list->lv_lock,
1968 e_cannot_change_list))
1969 goto on_error;
1970 // append to list, only fails when out of memory
1971 if (list_append_tv(list, tv) == FAIL)
1972 goto failed;
1973 clear_tv(tv);
1974 }
1975 }
1976 else if (status == OK && dest_type == VAR_DICT)
1977 {
1978 char_u *key = tv_idx->vval.v_string;
1979 dict_T *dict = tv_dest->vval.v_dict;
1980 dictitem_T *di;
1981
1982 SOURCING_LNUM = iptr->isn_lnum;
1983 if (dict == NULL)
1984 {
1985 emsg(_(e_dictionary_not_set));
1986 goto on_error;
1987 }
1988 if (key == NULL)
1989 key = (char_u *)"";
1990 di = dict_find(dict, key, -1);
1991 if (di != NULL)
1992 {
1993 if (error_if_locked(di->di_tv.v_lock,
1994 e_cannot_change_dict_item))
1995 goto on_error;
1996 // overwrite existing value
1997 clear_tv(&di->di_tv);
1998 di->di_tv = *tv;
1999 }
2000 else
2001 {
2002 if (error_if_locked(dict->dv_lock,
2003 e_cannot_change_dict))
2004 goto on_error;
2005 // add to dict, only fails when out of memory
2006 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2007 goto failed;
2008 clear_tv(tv);
2009 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002010 }
2011 else
2012 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002013 status = FAIL;
2014 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002015 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002016
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002017 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002018 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002019 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002020 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002021 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002022 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002023 goto on_error;
2024 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002025 }
2026 break;
2027
Bram Moolenaar0186e582021-01-10 18:33:11 +01002028 // load or store variable or argument from outer scope
2029 case ISN_LOADOUTER:
2030 case ISN_STOREOUTER:
2031 {
2032 int depth = iptr->isn_arg.outer.outer_depth;
2033 outer_T *outer = ectx.ec_outer;
2034
2035 while (depth > 1 && outer != NULL)
2036 {
2037 outer = outer->out_up;
2038 --depth;
2039 }
2040 if (outer == NULL)
2041 {
2042 SOURCING_LNUM = iptr->isn_lnum;
2043 iemsg("LOADOUTER depth more than scope levels");
2044 goto failed;
2045 }
2046 tv = ((typval_T *)outer->out_stack->ga_data)
2047 + outer->out_frame_idx + STACK_FRAME_SIZE
2048 + iptr->isn_arg.outer.outer_idx;
2049 if (iptr->isn_type == ISN_LOADOUTER)
2050 {
2051 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2052 goto failed;
2053 copy_tv(tv, STACK_TV_BOT(0));
2054 ++ectx.ec_stack.ga_len;
2055 }
2056 else
2057 {
2058 --ectx.ec_stack.ga_len;
2059 clear_tv(tv);
2060 *tv = *STACK_TV_BOT(0);
2061 }
2062 }
2063 break;
2064
Bram Moolenaar752fc692021-01-04 21:57:11 +01002065 // unlet item in list or dict variable
2066 case ISN_UNLETINDEX:
2067 {
2068 typval_T *tv_idx = STACK_TV_BOT(-2);
2069 typval_T *tv_dest = STACK_TV_BOT(-1);
2070 int status = OK;
2071
2072 // Stack contains:
2073 // -2 index
2074 // -1 dict or list
2075 if (tv_dest->v_type == VAR_DICT)
2076 {
2077 // unlet a dict item, index must be a string
2078 if (tv_idx->v_type != VAR_STRING)
2079 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002080 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002081 semsg(_(e_expected_str_but_got_str),
2082 vartype_name(VAR_STRING),
2083 vartype_name(tv_idx->v_type));
2084 status = FAIL;
2085 }
2086 else
2087 {
2088 dict_T *d = tv_dest->vval.v_dict;
2089 char_u *key = tv_idx->vval.v_string;
2090 dictitem_T *di = NULL;
2091
2092 if (key == NULL)
2093 key = (char_u *)"";
2094 if (d != NULL)
2095 di = dict_find(d, key, (int)STRLEN(key));
2096 if (di == NULL)
2097 {
2098 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002099 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002100 semsg(_(e_dictkey), key);
2101 status = FAIL;
2102 }
2103 else
2104 {
2105 // TODO: check for dict or item locked
2106 dictitem_remove(d, di);
2107 }
2108 }
2109 }
2110 else if (tv_dest->v_type == VAR_LIST)
2111 {
2112 // unlet a List item, index must be a number
2113 if (tv_idx->v_type != VAR_NUMBER)
2114 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002115 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002116 semsg(_(e_expected_str_but_got_str),
2117 vartype_name(VAR_NUMBER),
2118 vartype_name(tv_idx->v_type));
2119 status = FAIL;
2120 }
2121 else
2122 {
2123 list_T *l = tv_dest->vval.v_list;
2124 varnumber_T n = tv_idx->vval.v_number;
2125 listitem_T *li = NULL;
2126
2127 li = list_find(l, n);
2128 if (li == NULL)
2129 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002131 semsg(_(e_listidx), n);
2132 status = FAIL;
2133 }
2134 else
2135 // TODO: check for list or item locked
2136 listitem_remove(l, li);
2137 }
2138 }
2139 else
2140 {
2141 status = FAIL;
2142 semsg(_(e_cannot_index_str),
2143 vartype_name(tv_dest->v_type));
2144 }
2145
2146 clear_tv(tv_idx);
2147 clear_tv(tv_dest);
2148 ectx.ec_stack.ga_len -= 2;
2149 if (status == FAIL)
2150 goto on_error;
2151 }
2152 break;
2153
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 // push constant
2155 case ISN_PUSHNR:
2156 case ISN_PUSHBOOL:
2157 case ISN_PUSHSPEC:
2158 case ISN_PUSHF:
2159 case ISN_PUSHS:
2160 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002161 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002162 case ISN_PUSHCHANNEL:
2163 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002164 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002165 goto failed;
2166 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002167 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 ++ectx.ec_stack.ga_len;
2169 switch (iptr->isn_type)
2170 {
2171 case ISN_PUSHNR:
2172 tv->v_type = VAR_NUMBER;
2173 tv->vval.v_number = iptr->isn_arg.number;
2174 break;
2175 case ISN_PUSHBOOL:
2176 tv->v_type = VAR_BOOL;
2177 tv->vval.v_number = iptr->isn_arg.number;
2178 break;
2179 case ISN_PUSHSPEC:
2180 tv->v_type = VAR_SPECIAL;
2181 tv->vval.v_number = iptr->isn_arg.number;
2182 break;
2183#ifdef FEAT_FLOAT
2184 case ISN_PUSHF:
2185 tv->v_type = VAR_FLOAT;
2186 tv->vval.v_float = iptr->isn_arg.fnumber;
2187 break;
2188#endif
2189 case ISN_PUSHBLOB:
2190 blob_copy(iptr->isn_arg.blob, tv);
2191 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002192 case ISN_PUSHFUNC:
2193 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002194 if (iptr->isn_arg.string == NULL)
2195 tv->vval.v_string = NULL;
2196 else
2197 tv->vval.v_string =
2198 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002199 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002200 case ISN_PUSHCHANNEL:
2201#ifdef FEAT_JOB_CHANNEL
2202 tv->v_type = VAR_CHANNEL;
2203 tv->vval.v_channel = iptr->isn_arg.channel;
2204 if (tv->vval.v_channel != NULL)
2205 ++tv->vval.v_channel->ch_refcount;
2206#endif
2207 break;
2208 case ISN_PUSHJOB:
2209#ifdef FEAT_JOB_CHANNEL
2210 tv->v_type = VAR_JOB;
2211 tv->vval.v_job = iptr->isn_arg.job;
2212 if (tv->vval.v_job != NULL)
2213 ++tv->vval.v_job->jv_refcount;
2214#endif
2215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 default:
2217 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002218 tv->vval.v_string = vim_strsave(
2219 iptr->isn_arg.string == NULL
2220 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 }
2222 break;
2223
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002224 case ISN_UNLET:
2225 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2226 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002227 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002228 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002229 case ISN_UNLETENV:
2230 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2231 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002232
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002233 case ISN_LOCKCONST:
2234 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2235 break;
2236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 // create a list from items on the stack; uses a single allocation
2238 // for the list header and the items
2239 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002240 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2241 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 break;
2243
2244 // create a dict from items on the stack
2245 case ISN_NEWDICT:
2246 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002247 int count = iptr->isn_arg.number;
2248 dict_T *dict = dict_alloc();
2249 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002250 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251
2252 if (dict == NULL)
2253 goto failed;
2254 for (idx = 0; idx < count; ++idx)
2255 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002256 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002258 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002259 key = tv->vval.v_string == NULL
2260 ? (char_u *)"" : tv->vval.v_string;
2261 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002262 if (item != NULL)
2263 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002265 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002266 dict_unref(dict);
2267 goto on_error;
2268 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002269 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 clear_tv(tv);
2271 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002272 {
2273 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002275 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2277 item->di_tv.v_lock = 0;
2278 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002279 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002280 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002281 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 }
2285
2286 if (count > 0)
2287 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002288 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 goto failed;
2290 else
2291 ++ectx.ec_stack.ga_len;
2292 tv = STACK_TV_BOT(-1);
2293 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002294 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 tv->vval.v_dict = dict;
2296 ++dict->dv_refcount;
2297 }
2298 break;
2299
2300 // call a :def function
2301 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002302 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002303 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002304 iptr->isn_arg.dfunc.cdf_argcount,
2305 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002306 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 break;
2308
2309 // call a builtin function
2310 case ISN_BCALL:
2311 SOURCING_LNUM = iptr->isn_lnum;
2312 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2313 iptr->isn_arg.bfunc.cbf_argcount,
2314 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002315 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 break;
2317
2318 // call a funcref or partial
2319 case ISN_PCALL:
2320 {
2321 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2322 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002323 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324
2325 SOURCING_LNUM = iptr->isn_lnum;
2326 if (pfunc->cpf_top)
2327 {
2328 // funcref is above the arguments
2329 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2330 }
2331 else
2332 {
2333 // Get the funcref from the stack.
2334 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002335 partial_tv = *STACK_TV_BOT(0);
2336 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 }
2338 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002339 if (tv == &partial_tv)
2340 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002342 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 }
2344 break;
2345
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002346 case ISN_PCALL_END:
2347 // PCALL finished, arguments have been consumed and replaced by
2348 // the return value. Now clear the funcref from the stack,
2349 // and move the return value in its place.
2350 --ectx.ec_stack.ga_len;
2351 clear_tv(STACK_TV_BOT(-1));
2352 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2353 break;
2354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 // call a user defined function or funcref/partial
2356 case ISN_UCALL:
2357 {
2358 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2359
2360 SOURCING_LNUM = iptr->isn_lnum;
2361 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002362 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002363 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 }
2365 break;
2366
2367 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002368 case ISN_RETURN_ZERO:
2369 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2370 goto failed;
2371 tv = STACK_TV_BOT(0);
2372 ++ectx.ec_stack.ga_len;
2373 tv->v_type = VAR_NUMBER;
2374 tv->vval.v_number = 0;
2375 tv->v_lock = 0;
2376 // FALLTHROUGH
2377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002378 case ISN_RETURN:
2379 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002380 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002381 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002382
2383 if (trystack->ga_len > 0)
2384 trycmd = ((trycmd_T *)trystack->ga_data)
2385 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002386 if (trycmd != NULL
2387 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 && trycmd->tcd_finally_idx != 0)
2389 {
2390 // jump to ":finally"
2391 ectx.ec_iidx = trycmd->tcd_finally_idx;
2392 trycmd->tcd_return = TRUE;
2393 }
2394 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002395 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397 break;
2398
2399 // push a function reference to a compiled function
2400 case ISN_FUNCREF:
2401 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002402 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2403 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2404 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406 if (pt == NULL)
2407 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002408 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002409 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002410 vim_free(pt);
2411 goto failed;
2412 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002413 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2414 &ectx) == FAIL)
2415 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002416
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 tv = STACK_TV_BOT(0);
2418 ++ectx.ec_stack.ga_len;
2419 tv->vval.v_partial = pt;
2420 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002421 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 }
2423 break;
2424
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002425 // Create a global function from a lambda.
2426 case ISN_NEWFUNC:
2427 {
2428 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2429
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002430 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002431 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002432 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002433 }
2434 break;
2435
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002436 // List functions
2437 case ISN_DEF:
2438 if (iptr->isn_arg.string == NULL)
2439 list_functions(NULL);
2440 else
2441 {
2442 exarg_T ea;
2443
2444 CLEAR_FIELD(ea);
2445 ea.cmd = ea.arg = iptr->isn_arg.string;
2446 define_function(&ea, NULL);
2447 }
2448 break;
2449
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 // jump if a condition is met
2451 case ISN_JUMP:
2452 {
2453 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002454 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 int jump = TRUE;
2456
2457 if (when != JUMP_ALWAYS)
2458 {
2459 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002460 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002461 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002462 || when == JUMP_IF_COND_TRUE)
2463 {
2464 SOURCING_LNUM = iptr->isn_lnum;
2465 jump = tv_get_bool_chk(tv, &error);
2466 if (error)
2467 goto on_error;
2468 }
2469 else
2470 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002472 || when == JUMP_AND_KEEP_IF_FALSE
2473 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002475 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 {
2477 // drop the value from the stack
2478 clear_tv(tv);
2479 --ectx.ec_stack.ga_len;
2480 }
2481 }
2482 if (jump)
2483 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2484 }
2485 break;
2486
2487 // top of a for loop
2488 case ISN_FOR:
2489 {
2490 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2491 typval_T *idxtv =
2492 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2493
2494 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002495 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002497 ++idxtv->vval.v_number;
2498 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 // past the end of the list, jump to "endfor"
2500 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2501 else if (list->lv_first == &range_list_item)
2502 {
2503 // non-materialized range() list
2504 tv = STACK_TV_BOT(0);
2505 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002506 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 tv->vval.v_number = list_find_nr(
2508 list, idxtv->vval.v_number, NULL);
2509 ++ectx.ec_stack.ga_len;
2510 }
2511 else
2512 {
2513 listitem_T *li = list_find(list, idxtv->vval.v_number);
2514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2516 ++ectx.ec_stack.ga_len;
2517 }
2518 }
2519 break;
2520
2521 // start of ":try" block
2522 case ISN_TRY:
2523 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002524 trycmd_T *trycmd = NULL;
2525
Bram Moolenaar270d0382020-05-15 21:42:53 +02002526 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 goto failed;
2528 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2529 + ectx.ec_trystack.ga_len;
2530 ++ectx.ec_trystack.ga_len;
2531 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002532 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2534 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002535 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002536 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 }
2538 break;
2539
2540 case ISN_PUSHEXC:
2541 if (current_exception == NULL)
2542 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 iemsg("Evaluating catch while current_exception is NULL");
2545 goto failed;
2546 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002547 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 goto failed;
2549 tv = STACK_TV_BOT(0);
2550 ++ectx.ec_stack.ga_len;
2551 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002552 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 tv->vval.v_string = vim_strsave(
2554 (char_u *)current_exception->value);
2555 break;
2556
2557 case ISN_CATCH:
2558 {
2559 garray_T *trystack = &ectx.ec_trystack;
2560
Bram Moolenaar20a76292020-12-25 19:47:24 +01002561 if (restore_cmdmod)
2562 {
2563 cmdmod.cmod_filter_regmatch.regprog = NULL;
2564 undo_cmdmod(&cmdmod);
2565 cmdmod = save_cmdmod;
2566 restore_cmdmod = FALSE;
2567 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 if (trystack->ga_len > 0)
2569 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002570 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 + trystack->ga_len - 1;
2572 trycmd->tcd_caught = TRUE;
2573 }
2574 did_emsg = got_int = did_throw = FALSE;
2575 catch_exception(current_exception);
2576 }
2577 break;
2578
2579 // end of ":try" block
2580 case ISN_ENDTRY:
2581 {
2582 garray_T *trystack = &ectx.ec_trystack;
2583
2584 if (trystack->ga_len > 0)
2585 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002586 trycmd_T *trycmd = NULL;
2587
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 --trystack->ga_len;
2589 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002590 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 trycmd = ((trycmd_T *)trystack->ga_data)
2592 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002593 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 {
2595 // discard the exception
2596 if (caught_stack == current_exception)
2597 caught_stack = caught_stack->caught;
2598 discard_current_exception();
2599 }
2600
2601 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002602 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 }
2604 }
2605 break;
2606
2607 case ISN_THROW:
2608 --ectx.ec_stack.ga_len;
2609 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002610 if (tv->vval.v_string == NULL
2611 || *skipwhite(tv->vval.v_string) == NUL)
2612 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002613 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002614 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002615 emsg(_(e_throw_with_empty_string));
2616 goto failed;
2617 }
2618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2620 {
2621 vim_free(tv->vval.v_string);
2622 goto failed;
2623 }
2624 did_throw = TRUE;
2625 break;
2626
2627 // compare with special values
2628 case ISN_COMPAREBOOL:
2629 case ISN_COMPARESPECIAL:
2630 {
2631 typval_T *tv1 = STACK_TV_BOT(-2);
2632 typval_T *tv2 = STACK_TV_BOT(-1);
2633 varnumber_T arg1 = tv1->vval.v_number;
2634 varnumber_T arg2 = tv2->vval.v_number;
2635 int res;
2636
2637 switch (iptr->isn_arg.op.op_type)
2638 {
2639 case EXPR_EQUAL: res = arg1 == arg2; break;
2640 case EXPR_NEQUAL: res = arg1 != arg2; break;
2641 default: res = 0; break;
2642 }
2643
2644 --ectx.ec_stack.ga_len;
2645 tv1->v_type = VAR_BOOL;
2646 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2647 }
2648 break;
2649
2650 // Operation with two number arguments
2651 case ISN_OPNR:
2652 case ISN_COMPARENR:
2653 {
2654 typval_T *tv1 = STACK_TV_BOT(-2);
2655 typval_T *tv2 = STACK_TV_BOT(-1);
2656 varnumber_T arg1 = tv1->vval.v_number;
2657 varnumber_T arg2 = tv2->vval.v_number;
2658 varnumber_T res;
2659
2660 switch (iptr->isn_arg.op.op_type)
2661 {
2662 case EXPR_MULT: res = arg1 * arg2; break;
2663 case EXPR_DIV: res = arg1 / arg2; break;
2664 case EXPR_REM: res = arg1 % arg2; break;
2665 case EXPR_SUB: res = arg1 - arg2; break;
2666 case EXPR_ADD: res = arg1 + arg2; break;
2667
2668 case EXPR_EQUAL: res = arg1 == arg2; break;
2669 case EXPR_NEQUAL: res = arg1 != arg2; break;
2670 case EXPR_GREATER: res = arg1 > arg2; break;
2671 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2672 case EXPR_SMALLER: res = arg1 < arg2; break;
2673 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2674 default: res = 0; break;
2675 }
2676
2677 --ectx.ec_stack.ga_len;
2678 if (iptr->isn_type == ISN_COMPARENR)
2679 {
2680 tv1->v_type = VAR_BOOL;
2681 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2682 }
2683 else
2684 tv1->vval.v_number = res;
2685 }
2686 break;
2687
2688 // Computation with two float arguments
2689 case ISN_OPFLOAT:
2690 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002691#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 {
2693 typval_T *tv1 = STACK_TV_BOT(-2);
2694 typval_T *tv2 = STACK_TV_BOT(-1);
2695 float_T arg1 = tv1->vval.v_float;
2696 float_T arg2 = tv2->vval.v_float;
2697 float_T res = 0;
2698 int cmp = FALSE;
2699
2700 switch (iptr->isn_arg.op.op_type)
2701 {
2702 case EXPR_MULT: res = arg1 * arg2; break;
2703 case EXPR_DIV: res = arg1 / arg2; break;
2704 case EXPR_SUB: res = arg1 - arg2; break;
2705 case EXPR_ADD: res = arg1 + arg2; break;
2706
2707 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2708 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2709 case EXPR_GREATER: cmp = arg1 > arg2; break;
2710 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2711 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2712 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2713 default: cmp = 0; break;
2714 }
2715 --ectx.ec_stack.ga_len;
2716 if (iptr->isn_type == ISN_COMPAREFLOAT)
2717 {
2718 tv1->v_type = VAR_BOOL;
2719 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2720 }
2721 else
2722 tv1->vval.v_float = res;
2723 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002724#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 break;
2726
2727 case ISN_COMPARELIST:
2728 {
2729 typval_T *tv1 = STACK_TV_BOT(-2);
2730 typval_T *tv2 = STACK_TV_BOT(-1);
2731 list_T *arg1 = tv1->vval.v_list;
2732 list_T *arg2 = tv2->vval.v_list;
2733 int cmp = FALSE;
2734 int ic = iptr->isn_arg.op.op_ic;
2735
2736 switch (iptr->isn_arg.op.op_type)
2737 {
2738 case EXPR_EQUAL: cmp =
2739 list_equal(arg1, arg2, ic, FALSE); break;
2740 case EXPR_NEQUAL: cmp =
2741 !list_equal(arg1, arg2, ic, FALSE); break;
2742 case EXPR_IS: cmp = arg1 == arg2; break;
2743 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2744 default: cmp = 0; break;
2745 }
2746 --ectx.ec_stack.ga_len;
2747 clear_tv(tv1);
2748 clear_tv(tv2);
2749 tv1->v_type = VAR_BOOL;
2750 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2751 }
2752 break;
2753
2754 case ISN_COMPAREBLOB:
2755 {
2756 typval_T *tv1 = STACK_TV_BOT(-2);
2757 typval_T *tv2 = STACK_TV_BOT(-1);
2758 blob_T *arg1 = tv1->vval.v_blob;
2759 blob_T *arg2 = tv2->vval.v_blob;
2760 int cmp = FALSE;
2761
2762 switch (iptr->isn_arg.op.op_type)
2763 {
2764 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2765 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2766 case EXPR_IS: cmp = arg1 == arg2; break;
2767 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2768 default: cmp = 0; break;
2769 }
2770 --ectx.ec_stack.ga_len;
2771 clear_tv(tv1);
2772 clear_tv(tv2);
2773 tv1->v_type = VAR_BOOL;
2774 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2775 }
2776 break;
2777
2778 // TODO: handle separately
2779 case ISN_COMPARESTRING:
2780 case ISN_COMPAREDICT:
2781 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 case ISN_COMPAREANY:
2783 {
2784 typval_T *tv1 = STACK_TV_BOT(-2);
2785 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002786 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 int ic = iptr->isn_arg.op.op_ic;
2788
Bram Moolenaareb26f432020-09-14 16:50:05 +02002789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002790 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 --ectx.ec_stack.ga_len;
2793 }
2794 break;
2795
2796 case ISN_ADDLIST:
2797 case ISN_ADDBLOB:
2798 {
2799 typval_T *tv1 = STACK_TV_BOT(-2);
2800 typval_T *tv2 = STACK_TV_BOT(-1);
2801
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002802 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 if (iptr->isn_type == ISN_ADDLIST)
2804 eval_addlist(tv1, tv2);
2805 else
2806 eval_addblob(tv1, tv2);
2807 clear_tv(tv2);
2808 --ectx.ec_stack.ga_len;
2809 }
2810 break;
2811
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002812 case ISN_LISTAPPEND:
2813 {
2814 typval_T *tv1 = STACK_TV_BOT(-2);
2815 typval_T *tv2 = STACK_TV_BOT(-1);
2816 list_T *l = tv1->vval.v_list;
2817
2818 // add an item to a list
2819 if (l == NULL)
2820 {
2821 SOURCING_LNUM = iptr->isn_lnum;
2822 emsg(_(e_cannot_add_to_null_list));
2823 goto on_error;
2824 }
2825 if (list_append_tv(l, tv2) == FAIL)
2826 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002827 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002828 --ectx.ec_stack.ga_len;
2829 }
2830 break;
2831
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002832 case ISN_BLOBAPPEND:
2833 {
2834 typval_T *tv1 = STACK_TV_BOT(-2);
2835 typval_T *tv2 = STACK_TV_BOT(-1);
2836 blob_T *b = tv1->vval.v_blob;
2837 int error = FALSE;
2838 varnumber_T n;
2839
2840 // add a number to a blob
2841 if (b == NULL)
2842 {
2843 SOURCING_LNUM = iptr->isn_lnum;
2844 emsg(_(e_cannot_add_to_null_blob));
2845 goto on_error;
2846 }
2847 n = tv_get_number_chk(tv2, &error);
2848 if (error)
2849 goto on_error;
2850 ga_append(&b->bv_ga, (int)n);
2851 --ectx.ec_stack.ga_len;
2852 }
2853 break;
2854
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 // Computation with two arguments of unknown type
2856 case ISN_OPANY:
2857 {
2858 typval_T *tv1 = STACK_TV_BOT(-2);
2859 typval_T *tv2 = STACK_TV_BOT(-1);
2860 varnumber_T n1, n2;
2861#ifdef FEAT_FLOAT
2862 float_T f1 = 0, f2 = 0;
2863#endif
2864 int error = FALSE;
2865
2866 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2867 {
2868 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2869 {
2870 eval_addlist(tv1, tv2);
2871 clear_tv(tv2);
2872 --ectx.ec_stack.ga_len;
2873 break;
2874 }
2875 else if (tv1->v_type == VAR_BLOB
2876 && tv2->v_type == VAR_BLOB)
2877 {
2878 eval_addblob(tv1, tv2);
2879 clear_tv(tv2);
2880 --ectx.ec_stack.ga_len;
2881 break;
2882 }
2883 }
2884#ifdef FEAT_FLOAT
2885 if (tv1->v_type == VAR_FLOAT)
2886 {
2887 f1 = tv1->vval.v_float;
2888 n1 = 0;
2889 }
2890 else
2891#endif
2892 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002893 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 n1 = tv_get_number_chk(tv1, &error);
2895 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002896 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897#ifdef FEAT_FLOAT
2898 if (tv2->v_type == VAR_FLOAT)
2899 f1 = n1;
2900#endif
2901 }
2902#ifdef FEAT_FLOAT
2903 if (tv2->v_type == VAR_FLOAT)
2904 {
2905 f2 = tv2->vval.v_float;
2906 n2 = 0;
2907 }
2908 else
2909#endif
2910 {
2911 n2 = tv_get_number_chk(tv2, &error);
2912 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002913 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914#ifdef FEAT_FLOAT
2915 if (tv1->v_type == VAR_FLOAT)
2916 f2 = n2;
2917#endif
2918 }
2919#ifdef FEAT_FLOAT
2920 // if there is a float on either side the result is a float
2921 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2922 {
2923 switch (iptr->isn_arg.op.op_type)
2924 {
2925 case EXPR_MULT: f1 = f1 * f2; break;
2926 case EXPR_DIV: f1 = f1 / f2; break;
2927 case EXPR_SUB: f1 = f1 - f2; break;
2928 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002929 default: SOURCING_LNUM = iptr->isn_lnum;
2930 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002931 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 }
2933 clear_tv(tv1);
2934 clear_tv(tv2);
2935 tv1->v_type = VAR_FLOAT;
2936 tv1->vval.v_float = f1;
2937 --ectx.ec_stack.ga_len;
2938 }
2939 else
2940#endif
2941 {
2942 switch (iptr->isn_arg.op.op_type)
2943 {
2944 case EXPR_MULT: n1 = n1 * n2; break;
2945 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2946 case EXPR_SUB: n1 = n1 - n2; break;
2947 case EXPR_ADD: n1 = n1 + n2; break;
2948 default: n1 = num_modulus(n1, n2); break;
2949 }
2950 clear_tv(tv1);
2951 clear_tv(tv2);
2952 tv1->v_type = VAR_NUMBER;
2953 tv1->vval.v_number = n1;
2954 --ectx.ec_stack.ga_len;
2955 }
2956 }
2957 break;
2958
2959 case ISN_CONCAT:
2960 {
2961 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2962 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2963 char_u *res;
2964
2965 res = concat_str(str1, str2);
2966 clear_tv(STACK_TV_BOT(-2));
2967 clear_tv(STACK_TV_BOT(-1));
2968 --ectx.ec_stack.ga_len;
2969 STACK_TV_BOT(-1)->vval.v_string = res;
2970 }
2971 break;
2972
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002973 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002974 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002975 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002976 int is_slice = iptr->isn_type == ISN_STRSLICE;
2977 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002978 char_u *res;
2979
2980 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002981 // string slice: string is at stack-3, first index at
2982 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002983 if (is_slice)
2984 {
2985 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002986 n1 = tv->vval.v_number;
2987 }
2988
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002989 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002990 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002991
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002992 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002993 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002994 if (is_slice)
2995 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01002996 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002997 else
2998 // Index: The resulting variable is a string of a
2999 // single character. If the index is too big or
3000 // negative the result is empty.
3001 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003002 vim_free(tv->vval.v_string);
3003 tv->vval.v_string = res;
3004 }
3005 break;
3006
3007 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003008 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 {
Bram Moolenaared591872020-08-15 22:14:53 +02003010 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003012 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013
3014 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003015 // list slice: list is at stack-3, indexes at stack-2 and
3016 // stack-1
3017 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003018 list = tv->vval.v_list;
3019
3020 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003021 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003023
3024 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 {
Bram Moolenaared591872020-08-15 22:14:53 +02003026 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003027 n1 = tv->vval.v_number;
3028 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 }
Bram Moolenaared591872020-08-15 22:14:53 +02003030
3031 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003032 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003034 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3035 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003036 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 }
3038 break;
3039
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003040 case ISN_ANYINDEX:
3041 case ISN_ANYSLICE:
3042 {
3043 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3044 typval_T *var1, *var2;
3045 int res;
3046
3047 // index: composite is at stack-2, index at stack-1
3048 // slice: composite is at stack-3, indexes at stack-2 and
3049 // stack-1
3050 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003051 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003052 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3053 goto on_error;
3054 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3055 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003056 res = eval_index_inner(tv, is_slice, var1, var2,
3057 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003058 clear_tv(var1);
3059 if (is_slice)
3060 clear_tv(var2);
3061 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3062 if (res == FAIL)
3063 goto on_error;
3064 }
3065 break;
3066
Bram Moolenaar9af78762020-06-16 11:34:42 +02003067 case ISN_SLICE:
3068 {
3069 list_T *list;
3070 int count = iptr->isn_arg.number;
3071
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003072 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003073 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003074 list = tv->vval.v_list;
3075
3076 // no error for short list, expect it to be checked earlier
3077 if (list != NULL && list->lv_len >= count)
3078 {
3079 list_T *newlist = list_slice(list,
3080 count, list->lv_len - 1);
3081
3082 if (newlist != NULL)
3083 {
3084 list_unref(list);
3085 tv->vval.v_list = newlist;
3086 ++newlist->lv_refcount;
3087 }
3088 }
3089 }
3090 break;
3091
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003092 case ISN_GETITEM:
3093 {
3094 listitem_T *li;
3095 int index = iptr->isn_arg.number;
3096
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003097 // Get list item: list is at stack-1, push item.
3098 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003099 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003100 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003101
3102 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3103 goto failed;
3104 ++ectx.ec_stack.ga_len;
3105 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3106 }
3107 break;
3108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 case ISN_MEMBER:
3110 {
3111 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003112 char_u *key;
3113 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003114 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003115
3116 // dict member: dict is at stack-2, key at stack-1
3117 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003118 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003119 dict = tv->vval.v_dict;
3120
3121 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003122 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003123 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003124 if (key == NULL)
3125 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003126
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003127 if ((di = dict_find(dict, key, -1)) == NULL)
3128 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003129 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003130 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003131
3132 // If :silent! is used we will continue, make sure the
3133 // stack contents makes sense.
3134 clear_tv(tv);
3135 --ectx.ec_stack.ga_len;
3136 tv = STACK_TV_BOT(-1);
3137 clear_tv(tv);
3138 tv->v_type = VAR_NUMBER;
3139 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003140 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003141 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003142 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003143 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003144 // Clear the dict only after getting the item, to avoid
3145 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003146 tv = STACK_TV_BOT(-1);
3147 temp_tv = *tv;
3148 copy_tv(&di->di_tv, tv);
3149 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003150 }
3151 break;
3152
3153 // dict member with string key
3154 case ISN_STRINGMEMBER:
3155 {
3156 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003157 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003158 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159
3160 tv = STACK_TV_BOT(-1);
3161 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3162 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003163 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003165 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 }
3167 dict = tv->vval.v_dict;
3168
3169 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3170 == NULL)
3171 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003172 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003174 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003176 // Clear the dict after getting the item, to avoid that it
3177 // make the item invalid.
3178 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003180 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 }
3182 break;
3183
3184 case ISN_NEGATENR:
3185 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003186 if (tv->v_type != VAR_NUMBER
3187#ifdef FEAT_FLOAT
3188 && tv->v_type != VAR_FLOAT
3189#endif
3190 )
3191 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003192 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003193 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003194 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003195 }
3196#ifdef FEAT_FLOAT
3197 if (tv->v_type == VAR_FLOAT)
3198 tv->vval.v_float = -tv->vval.v_float;
3199 else
3200#endif
3201 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 break;
3203
3204 case ISN_CHECKNR:
3205 {
3206 int error = FALSE;
3207
3208 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003211 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 (void)tv_get_number_chk(tv, &error);
3213 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003214 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215 }
3216 break;
3217
3218 case ISN_CHECKTYPE:
3219 {
3220 checktype_T *ct = &iptr->isn_arg.type;
3221
3222 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003223 SOURCING_LNUM = iptr->isn_lnum;
3224 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
3225 goto on_error;
3226
3227 // number 0 is FALSE, number 1 is TRUE
3228 if (tv->v_type == VAR_NUMBER
3229 && ct->ct_type->tt_type == VAR_BOOL
3230 && (tv->vval.v_number == 0
3231 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003233 tv->v_type = VAR_BOOL;
3234 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003235 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 }
3237 }
3238 break;
3239
Bram Moolenaar9af78762020-06-16 11:34:42 +02003240 case ISN_CHECKLEN:
3241 {
3242 int min_len = iptr->isn_arg.checklen.cl_min_len;
3243 list_T *list = NULL;
3244
3245 tv = STACK_TV_BOT(-1);
3246 if (tv->v_type == VAR_LIST)
3247 list = tv->vval.v_list;
3248 if (list == NULL || list->lv_len < min_len
3249 || (list->lv_len > min_len
3250 && !iptr->isn_arg.checklen.cl_more_OK))
3251 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003253 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003254 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003255 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003256 }
3257 }
3258 break;
3259
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003260 case ISN_SETTYPE:
3261 {
3262 checktype_T *ct = &iptr->isn_arg.type;
3263
3264 tv = STACK_TV_BOT(-1);
3265 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3266 {
3267 free_type(tv->vval.v_dict->dv_type);
3268 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3269 }
3270 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3271 {
3272 free_type(tv->vval.v_list->lv_type);
3273 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3274 }
3275 }
3276 break;
3277
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003279 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003280 {
3281 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003282 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003283
3284 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003285 if (iptr->isn_type == ISN_2BOOL)
3286 {
3287 n = tv2bool(tv);
3288 if (iptr->isn_arg.number) // invert
3289 n = !n;
3290 }
3291 else
3292 {
3293 SOURCING_LNUM = iptr->isn_lnum;
3294 n = tv_get_bool_chk(tv, &error);
3295 if (error)
3296 goto on_error;
3297 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 clear_tv(tv);
3299 tv->v_type = VAR_BOOL;
3300 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3301 }
3302 break;
3303
3304 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003305 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003306 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003307 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3308 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3309 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003310 break;
3311
Bram Moolenaar08597872020-12-10 19:43:40 +01003312 case ISN_RANGE:
3313 {
3314 exarg_T ea;
3315 char *errormsg;
3316
3317 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3318 goto failed;
3319 ++ectx.ec_stack.ga_len;
3320 tv = STACK_TV_BOT(-1);
Bram Moolenaarece0b872021-01-08 20:40:45 +01003321 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003322 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003323 ea.addr_type = ADDR_LINES;
3324 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003325 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003326 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003327 goto on_error;
Bram Moolenaar08597872020-12-10 19:43:40 +01003328 if (ea.addr_count == 0)
3329 tv->vval.v_number = curwin->w_cursor.lnum;
3330 else
3331 tv->vval.v_number = ea.line2;
3332 }
3333 break;
3334
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003335 case ISN_PUT:
3336 {
3337 int regname = iptr->isn_arg.put.put_regname;
3338 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3339 char_u *expr = NULL;
3340 int dir = FORWARD;
3341
3342 if (regname == '=')
3343 {
3344 tv = STACK_TV_BOT(-1);
3345 if (tv->v_type == VAR_STRING)
3346 expr = tv->vval.v_string;
3347 else
3348 {
3349 expr = typval_tostring(tv); // allocates value
3350 clear_tv(tv);
3351 }
3352 --ectx.ec_stack.ga_len;
3353 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003354 if (lnum < -2)
3355 {
3356 // line number was put on the stack by ISN_RANGE
3357 tv = STACK_TV_BOT(-1);
3358 curwin->w_cursor.lnum = tv->vval.v_number;
3359 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3360 dir = BACKWARD;
3361 --ectx.ec_stack.ga_len;
3362 }
3363 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003364 // :put! above cursor
3365 dir = BACKWARD;
3366 else if (lnum >= 0)
3367 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3368 check_cursor();
3369 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3370 vim_free(expr);
3371 }
3372 break;
3373
Bram Moolenaar02194d22020-10-24 23:08:38 +02003374 case ISN_CMDMOD:
3375 save_cmdmod = cmdmod;
3376 restore_cmdmod = TRUE;
3377 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3378 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003379 break;
3380
Bram Moolenaar02194d22020-10-24 23:08:38 +02003381 case ISN_CMDMOD_REV:
3382 // filter regprog is owned by the instruction, don't free it
3383 cmdmod.cmod_filter_regmatch.regprog = NULL;
3384 undo_cmdmod(&cmdmod);
3385 cmdmod = save_cmdmod;
3386 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003387 break;
3388
Bram Moolenaar792f7862020-11-23 08:31:18 +01003389 case ISN_UNPACK:
3390 {
3391 int count = iptr->isn_arg.unpack.unp_count;
3392 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3393 list_T *l;
3394 listitem_T *li;
3395 int i;
3396
3397 // Check there is a valid list to unpack.
3398 tv = STACK_TV_BOT(-1);
3399 if (tv->v_type != VAR_LIST)
3400 {
3401 SOURCING_LNUM = iptr->isn_lnum;
3402 emsg(_(e_for_argument_must_be_sequence_of_lists));
3403 goto on_error;
3404 }
3405 l = tv->vval.v_list;
3406 if (l == NULL
3407 || l->lv_len < (semicolon ? count - 1 : count))
3408 {
3409 SOURCING_LNUM = iptr->isn_lnum;
3410 emsg(_(e_list_value_does_not_have_enough_items));
3411 goto on_error;
3412 }
3413 else if (!semicolon && l->lv_len > count)
3414 {
3415 SOURCING_LNUM = iptr->isn_lnum;
3416 emsg(_(e_list_value_has_more_items_than_targets));
3417 goto on_error;
3418 }
3419
3420 CHECK_LIST_MATERIALIZE(l);
3421 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3422 goto failed;
3423 ectx.ec_stack.ga_len += count - 1;
3424
3425 // Variable after semicolon gets a list with the remaining
3426 // items.
3427 if (semicolon)
3428 {
3429 list_T *rem_list =
3430 list_alloc_with_items(l->lv_len - count + 1);
3431
3432 if (rem_list == NULL)
3433 goto failed;
3434 tv = STACK_TV_BOT(-count);
3435 tv->vval.v_list = rem_list;
3436 ++rem_list->lv_refcount;
3437 tv->v_lock = 0;
3438 li = l->lv_first;
3439 for (i = 0; i < count - 1; ++i)
3440 li = li->li_next;
3441 for (i = 0; li != NULL; ++i)
3442 {
3443 list_set_item(rem_list, i, &li->li_tv);
3444 li = li->li_next;
3445 }
3446 --count;
3447 }
3448
3449 // Produce the values in reverse order, first item last.
3450 li = l->lv_first;
3451 for (i = 0; i < count; ++i)
3452 {
3453 tv = STACK_TV_BOT(-i - 1);
3454 copy_tv(&li->li_tv, tv);
3455 li = li->li_next;
3456 }
3457
3458 list_unref(l);
3459 }
3460 break;
3461
Bram Moolenaar389df252020-07-09 21:20:47 +02003462 case ISN_SHUFFLE:
3463 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003464 typval_T tmp_tv;
3465 int item = iptr->isn_arg.shuffle.shfl_item;
3466 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003467
3468 tmp_tv = *STACK_TV_BOT(-item);
3469 for ( ; up > 0 && item > 1; --up)
3470 {
3471 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3472 --item;
3473 }
3474 *STACK_TV_BOT(-item) = tmp_tv;
3475 }
3476 break;
3477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 case ISN_DROP:
3479 --ectx.ec_stack.ga_len;
3480 clear_tv(STACK_TV_BOT(0));
3481 break;
3482 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003483 continue;
3484
Bram Moolenaard032f342020-07-18 18:13:02 +02003485func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003486 // Restore previous function. If the frame pointer is where we started
3487 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003488 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003489 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003490
Bram Moolenaard032f342020-07-18 18:13:02 +02003491 if (func_return(&ectx) == FAIL)
3492 // only fails when out of memory
3493 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003494 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003495
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003496on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003497 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003498 // If "emsg_silent" is set then ignore the error, unless it was set
3499 // when calling the function.
3500 if (did_emsg_cumul + did_emsg == did_emsg_before
3501 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003502 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003503on_fatal_error:
3504 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003505 // If we are not inside a try-catch started here, abort execution.
3506 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003507 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 }
3509
3510done:
3511 // function finished, get result from the stack.
3512 tv = STACK_TV_BOT(-1);
3513 *rettv = *tv;
3514 tv->v_type = VAR_UNKNOWN;
3515 ret = OK;
3516
3517failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003518 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003519 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003520 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003521
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003522 // Deal with any remaining closures, they may be in use somewhere.
3523 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003524 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003525 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003526 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3527 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003528
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003529 estack_pop();
3530 current_sctx = save_current_sctx;
3531
Bram Moolenaar352134b2020-10-17 22:04:08 +02003532 if (*msg_list != NULL && saved_msg_list != NULL)
3533 {
3534 msglist_T **plist = saved_msg_list;
3535
3536 // Append entries from the current msg_list (uncaught exceptions) to
3537 // the saved msg_list.
3538 while (*plist != NULL)
3539 plist = &(*plist)->next;
3540
3541 *plist = *msg_list;
3542 }
3543 msg_list = saved_msg_list;
3544
Bram Moolenaar02194d22020-10-24 23:08:38 +02003545 if (restore_cmdmod)
3546 {
3547 cmdmod.cmod_filter_regmatch.regprog = NULL;
3548 undo_cmdmod(&cmdmod);
3549 cmdmod = save_cmdmod;
3550 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003551 emsg_silent_def = save_emsg_silent_def;
3552 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003553
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003554failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003555 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3557 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003559 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003560 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003561
Bram Moolenaar0186e582021-01-10 18:33:11 +01003562 while (ectx.ec_outer != NULL)
3563 {
3564 outer_T *up = ectx.ec_outer->out_up_is_copy
3565 ? NULL : ectx.ec_outer->out_up;
3566
3567 vim_free(ectx.ec_outer);
3568 ectx.ec_outer = up;
3569 }
3570
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003571 // Not sure if this is necessary.
3572 suppress_errthrow = save_suppress_errthrow;
3573
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003574 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003575 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003576 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003577 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578 return ret;
3579}
3580
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003581/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003582 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003583 * We don't really need this at runtime, but we do have tests that require it,
3584 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 */
3586 void
3587ex_disassemble(exarg_T *eap)
3588{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003589 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003590 char_u *fname;
3591 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003592 dfunc_T *dfunc;
3593 isn_T *instr;
3594 int current;
3595 int line_idx = 0;
3596 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003597 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003599 if (STRNCMP(arg, "<lambda>", 8) == 0)
3600 {
3601 arg += 8;
3602 (void)getdigits(&arg);
3603 fname = vim_strnsave(eap->arg, arg - eap->arg);
3604 }
3605 else
3606 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003607 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003608 if (fname == NULL)
3609 {
3610 semsg(_(e_invarg2), eap->arg);
3611 return;
3612 }
3613
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003614 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003615 if (ufunc == NULL)
3616 {
3617 char_u *p = untrans_function_name(fname);
3618
3619 if (p != NULL)
3620 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003621 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003622 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003623 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003624 if (ufunc == NULL)
3625 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003626 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 return;
3628 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003629 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003630 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3631 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003632 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003634 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003635 return;
3636 }
3637 if (ufunc->uf_name_exp != NULL)
3638 msg((char *)ufunc->uf_name_exp);
3639 else
3640 msg((char *)ufunc->uf_name);
3641
3642 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3643 instr = dfunc->df_instr;
3644 for (current = 0; current < dfunc->df_instr_count; ++current)
3645 {
3646 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003647 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648
3649 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3650 {
3651 if (current > prev_current)
3652 {
3653 msg_puts("\n\n");
3654 prev_current = current;
3655 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003656 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3657 if (line != NULL)
3658 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659 }
3660
3661 switch (iptr->isn_type)
3662 {
3663 case ISN_EXEC:
3664 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3665 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003666 case ISN_EXECCONCAT:
3667 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003668 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003669 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003670 case ISN_ECHO:
3671 {
3672 echo_T *echo = &iptr->isn_arg.echo;
3673
3674 smsg("%4d %s %d", current,
3675 echo->echo_with_white ? "ECHO" : "ECHON",
3676 echo->echo_count);
3677 }
3678 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003679 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003680 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003681 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003682 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003683 case ISN_ECHOMSG:
3684 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003685 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003686 break;
3687 case ISN_ECHOERR:
3688 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003689 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003690 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003692 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003693 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003694 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003695 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003696 + STACK_FRAME_SIZE));
3697 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003698 smsg("%4d LOAD $%lld", current,
3699 (varnumber_T)(iptr->isn_arg.number));
3700 }
3701 break;
3702 case ISN_LOADOUTER:
3703 {
3704 if (iptr->isn_arg.number < 0)
3705 smsg("%4d LOADOUTER level %d arg[%d]", current,
3706 iptr->isn_arg.outer.outer_depth,
3707 iptr->isn_arg.outer.outer_idx
3708 + STACK_FRAME_SIZE);
3709 else
3710 smsg("%4d LOADOUTER level %d $%d", current,
3711 iptr->isn_arg.outer.outer_depth,
3712 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003713 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003714 break;
3715 case ISN_LOADV:
3716 smsg("%4d LOADV v:%s", current,
3717 get_vim_var_name(iptr->isn_arg.number));
3718 break;
3719 case ISN_LOADSCRIPT:
3720 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003721 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3722 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003724 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003726 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3727 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003728 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003729 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003730 }
3731 break;
3732 case ISN_LOADS:
3733 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003734 scriptitem_T *si = SCRIPT_ITEM(
3735 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003736
3737 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003738 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 }
3740 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003741 case ISN_LOADAUTO:
3742 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3743 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 case ISN_LOADG:
3745 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3746 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003747 case ISN_LOADB:
3748 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3749 break;
3750 case ISN_LOADW:
3751 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3752 break;
3753 case ISN_LOADT:
3754 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3755 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003756 case ISN_LOADGDICT:
3757 smsg("%4d LOAD g:", current);
3758 break;
3759 case ISN_LOADBDICT:
3760 smsg("%4d LOAD b:", current);
3761 break;
3762 case ISN_LOADWDICT:
3763 smsg("%4d LOAD w:", current);
3764 break;
3765 case ISN_LOADTDICT:
3766 smsg("%4d LOAD t:", current);
3767 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 case ISN_LOADOPT:
3769 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3770 break;
3771 case ISN_LOADENV:
3772 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3773 break;
3774 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003775 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003776 break;
3777
3778 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003779 if (iptr->isn_arg.number < 0)
3780 smsg("%4d STORE arg[%lld]", current,
3781 iptr->isn_arg.number + STACK_FRAME_SIZE);
3782 else
3783 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3784 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003785 case ISN_STOREOUTER:
3786 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003787 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003788 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3789 iptr->isn_arg.outer.outer_depth,
3790 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003791 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003792 smsg("%4d STOREOUTER level %d $%d", current,
3793 iptr->isn_arg.outer.outer_depth,
3794 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003796 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003797 case ISN_STOREV:
3798 smsg("%4d STOREV v:%s", current,
3799 get_vim_var_name(iptr->isn_arg.number));
3800 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003801 case ISN_STOREAUTO:
3802 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3803 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003804 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003805 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3806 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003807 case ISN_STOREB:
3808 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3809 break;
3810 case ISN_STOREW:
3811 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3812 break;
3813 case ISN_STORET:
3814 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3815 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003816 case ISN_STORES:
3817 {
3818 scriptitem_T *si = SCRIPT_ITEM(
3819 iptr->isn_arg.loadstore.ls_sid);
3820
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003821 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003822 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003824 break;
3825 case ISN_STORESCRIPT:
3826 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003827 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3828 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003829 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003830 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003832 smsg("%4d STORESCRIPT %s-%d in %s", current,
3833 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003834 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003835 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003836 }
3837 break;
3838 case ISN_STOREOPT:
3839 smsg("%4d STOREOPT &%s", current,
3840 iptr->isn_arg.storeopt.so_name);
3841 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003842 case ISN_STOREENV:
3843 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3844 break;
3845 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003846 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003847 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 case ISN_STORENR:
3849 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003850 iptr->isn_arg.storenr.stnr_val,
3851 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003852 break;
3853
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003854 case ISN_STOREINDEX:
3855 switch (iptr->isn_arg.vartype)
3856 {
3857 case VAR_LIST:
3858 smsg("%4d STORELIST", current);
3859 break;
3860 case VAR_DICT:
3861 smsg("%4d STOREDICT", current);
3862 break;
3863 case VAR_ANY:
3864 smsg("%4d STOREINDEX", current);
3865 break;
3866 default: break;
3867 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003868 break;
3869
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 // constants
3871 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003872 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003873 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003874 break;
3875 case ISN_PUSHBOOL:
3876 case ISN_PUSHSPEC:
3877 smsg("%4d PUSH %s", current,
3878 get_var_special_name(iptr->isn_arg.number));
3879 break;
3880 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003881#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003883#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 break;
3885 case ISN_PUSHS:
3886 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3887 break;
3888 case ISN_PUSHBLOB:
3889 {
3890 char_u *r;
3891 char_u numbuf[NUMBUFLEN];
3892 char_u *tofree;
3893
3894 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003895 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 vim_free(tofree);
3897 }
3898 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003899 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003900 {
3901 char *name = (char *)iptr->isn_arg.string;
3902
3903 smsg("%4d PUSHFUNC \"%s\"", current,
3904 name == NULL ? "[none]" : name);
3905 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003906 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003907 case ISN_PUSHCHANNEL:
3908#ifdef FEAT_JOB_CHANNEL
3909 {
3910 channel_T *channel = iptr->isn_arg.channel;
3911
3912 smsg("%4d PUSHCHANNEL %d", current,
3913 channel == NULL ? 0 : channel->ch_id);
3914 }
3915#endif
3916 break;
3917 case ISN_PUSHJOB:
3918#ifdef FEAT_JOB_CHANNEL
3919 {
3920 typval_T tv;
3921 char_u *name;
3922
3923 tv.v_type = VAR_JOB;
3924 tv.vval.v_job = iptr->isn_arg.job;
3925 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003926 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003927 }
3928#endif
3929 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003930 case ISN_PUSHEXC:
3931 smsg("%4d PUSH v:exception", current);
3932 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003933 case ISN_UNLET:
3934 smsg("%4d UNLET%s %s", current,
3935 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3936 iptr->isn_arg.unlet.ul_name);
3937 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003938 case ISN_UNLETENV:
3939 smsg("%4d UNLETENV%s $%s", current,
3940 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3941 iptr->isn_arg.unlet.ul_name);
3942 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003943 case ISN_UNLETINDEX:
3944 smsg("%4d UNLETINDEX", current);
3945 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003946 case ISN_LOCKCONST:
3947 smsg("%4d LOCKCONST", current);
3948 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003950 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003951 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003952 break;
3953 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003954 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003955 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 break;
3957
3958 // function call
3959 case ISN_BCALL:
3960 {
3961 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3962
3963 smsg("%4d BCALL %s(argc %d)", current,
3964 internal_func_name(cbfunc->cbf_idx),
3965 cbfunc->cbf_argcount);
3966 }
3967 break;
3968 case ISN_DCALL:
3969 {
3970 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3971 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3972 + cdfunc->cdf_idx;
3973
3974 smsg("%4d DCALL %s(argc %d)", current,
3975 df->df_ufunc->uf_name_exp != NULL
3976 ? df->df_ufunc->uf_name_exp
3977 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3978 }
3979 break;
3980 case ISN_UCALL:
3981 {
3982 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3983
3984 smsg("%4d UCALL %s(argc %d)", current,
3985 cufunc->cuf_name, cufunc->cuf_argcount);
3986 }
3987 break;
3988 case ISN_PCALL:
3989 {
3990 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3991
3992 smsg("%4d PCALL%s (argc %d)", current,
3993 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3994 }
3995 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003996 case ISN_PCALL_END:
3997 smsg("%4d PCALL end", current);
3998 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999 case ISN_RETURN:
4000 smsg("%4d RETURN", current);
4001 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004002 case ISN_RETURN_ZERO:
4003 smsg("%4d RETURN 0", current);
4004 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004005 case ISN_FUNCREF:
4006 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004007 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004009 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004011 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004012 }
4013 break;
4014
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004015 case ISN_NEWFUNC:
4016 {
4017 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4018
4019 smsg("%4d NEWFUNC %s %s", current,
4020 newfunc->nf_lambda, newfunc->nf_global);
4021 }
4022 break;
4023
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004024 case ISN_DEF:
4025 {
4026 char_u *name = iptr->isn_arg.string;
4027
4028 smsg("%4d DEF %s", current,
4029 name == NULL ? (char_u *)"" : name);
4030 }
4031 break;
4032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004033 case ISN_JUMP:
4034 {
4035 char *when = "?";
4036
4037 switch (iptr->isn_arg.jump.jump_when)
4038 {
4039 case JUMP_ALWAYS:
4040 when = "JUMP";
4041 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 case JUMP_AND_KEEP_IF_TRUE:
4043 when = "JUMP_AND_KEEP_IF_TRUE";
4044 break;
4045 case JUMP_IF_FALSE:
4046 when = "JUMP_IF_FALSE";
4047 break;
4048 case JUMP_AND_KEEP_IF_FALSE:
4049 when = "JUMP_AND_KEEP_IF_FALSE";
4050 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004051 case JUMP_IF_COND_FALSE:
4052 when = "JUMP_IF_COND_FALSE";
4053 break;
4054 case JUMP_IF_COND_TRUE:
4055 when = "JUMP_IF_COND_TRUE";
4056 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004057 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004058 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004059 iptr->isn_arg.jump.jump_where);
4060 }
4061 break;
4062
4063 case ISN_FOR:
4064 {
4065 forloop_T *forloop = &iptr->isn_arg.forloop;
4066
4067 smsg("%4d FOR $%d -> %d", current,
4068 forloop->for_idx, forloop->for_end);
4069 }
4070 break;
4071
4072 case ISN_TRY:
4073 {
4074 try_T *try = &iptr->isn_arg.try;
4075
4076 smsg("%4d TRY catch -> %d, finally -> %d", current,
4077 try->try_catch, try->try_finally);
4078 }
4079 break;
4080 case ISN_CATCH:
4081 // TODO
4082 smsg("%4d CATCH", current);
4083 break;
4084 case ISN_ENDTRY:
4085 smsg("%4d ENDTRY", current);
4086 break;
4087 case ISN_THROW:
4088 smsg("%4d THROW", current);
4089 break;
4090
4091 // expression operations on number
4092 case ISN_OPNR:
4093 case ISN_OPFLOAT:
4094 case ISN_OPANY:
4095 {
4096 char *what;
4097 char *ins;
4098
4099 switch (iptr->isn_arg.op.op_type)
4100 {
4101 case EXPR_MULT: what = "*"; break;
4102 case EXPR_DIV: what = "/"; break;
4103 case EXPR_REM: what = "%"; break;
4104 case EXPR_SUB: what = "-"; break;
4105 case EXPR_ADD: what = "+"; break;
4106 default: what = "???"; break;
4107 }
4108 switch (iptr->isn_type)
4109 {
4110 case ISN_OPNR: ins = "OPNR"; break;
4111 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4112 case ISN_OPANY: ins = "OPANY"; break;
4113 default: ins = "???"; break;
4114 }
4115 smsg("%4d %s %s", current, ins, what);
4116 }
4117 break;
4118
4119 case ISN_COMPAREBOOL:
4120 case ISN_COMPARESPECIAL:
4121 case ISN_COMPARENR:
4122 case ISN_COMPAREFLOAT:
4123 case ISN_COMPARESTRING:
4124 case ISN_COMPAREBLOB:
4125 case ISN_COMPARELIST:
4126 case ISN_COMPAREDICT:
4127 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128 case ISN_COMPAREANY:
4129 {
4130 char *p;
4131 char buf[10];
4132 char *type;
4133
4134 switch (iptr->isn_arg.op.op_type)
4135 {
4136 case EXPR_EQUAL: p = "=="; break;
4137 case EXPR_NEQUAL: p = "!="; break;
4138 case EXPR_GREATER: p = ">"; break;
4139 case EXPR_GEQUAL: p = ">="; break;
4140 case EXPR_SMALLER: p = "<"; break;
4141 case EXPR_SEQUAL: p = "<="; break;
4142 case EXPR_MATCH: p = "=~"; break;
4143 case EXPR_IS: p = "is"; break;
4144 case EXPR_ISNOT: p = "isnot"; break;
4145 case EXPR_NOMATCH: p = "!~"; break;
4146 default: p = "???"; break;
4147 }
4148 STRCPY(buf, p);
4149 if (iptr->isn_arg.op.op_ic == TRUE)
4150 strcat(buf, "?");
4151 switch(iptr->isn_type)
4152 {
4153 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4154 case ISN_COMPARESPECIAL:
4155 type = "COMPARESPECIAL"; break;
4156 case ISN_COMPARENR: type = "COMPARENR"; break;
4157 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4158 case ISN_COMPARESTRING:
4159 type = "COMPARESTRING"; break;
4160 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4161 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4162 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4163 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4165 default: type = "???"; break;
4166 }
4167
4168 smsg("%4d %s %s", current, type, buf);
4169 }
4170 break;
4171
4172 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4173 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4174
4175 // expression operations
4176 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004177 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004178 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004179 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004180 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004181 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004182 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004183 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4184 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004185 case ISN_SLICE: smsg("%4d SLICE %lld",
4186 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004187 case ISN_GETITEM: smsg("%4d ITEM %lld",
4188 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004189 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4190 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 iptr->isn_arg.string); break;
4192 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4193
4194 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004195 case ISN_CHECKTYPE:
4196 {
4197 char *tofree;
4198
4199 smsg("%4d CHECKTYPE %s stack[%d]", current,
4200 type_name(iptr->isn_arg.type.ct_type, &tofree),
4201 iptr->isn_arg.type.ct_off);
4202 vim_free(tofree);
4203 break;
4204 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004205 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4206 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4207 iptr->isn_arg.checklen.cl_min_len);
4208 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004209 case ISN_SETTYPE:
4210 {
4211 char *tofree;
4212
4213 smsg("%4d SETTYPE %s", current,
4214 type_name(iptr->isn_arg.type.ct_type, &tofree));
4215 vim_free(tofree);
4216 break;
4217 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004218 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004219 case ISN_2BOOL: if (iptr->isn_arg.number)
4220 smsg("%4d INVERT (!val)", current);
4221 else
4222 smsg("%4d 2BOOL (!!val)", current);
4223 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004224 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004225 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004226 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004227 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004228 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004229 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004230 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4231 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004232 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004233 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4234 smsg("%4d PUT %c above range",
4235 current, iptr->isn_arg.put.put_regname);
4236 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4237 smsg("%4d PUT %c range",
4238 current, iptr->isn_arg.put.put_regname);
4239 else
4240 smsg("%4d PUT %c %ld", current,
4241 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004242 (long)iptr->isn_arg.put.put_lnum);
4243 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004244
Bram Moolenaar02194d22020-10-24 23:08:38 +02004245 // TODO: summarize modifiers
4246 case ISN_CMDMOD:
4247 {
4248 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004249 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004250 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4251
4252 buf = alloc(len + 1);
4253 if (buf != NULL)
4254 {
4255 (void)produce_cmdmods(
4256 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4257 smsg("%4d CMDMOD %s", current, buf);
4258 vim_free(buf);
4259 }
4260 break;
4261 }
4262 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004263
Bram Moolenaar792f7862020-11-23 08:31:18 +01004264 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4265 iptr->isn_arg.unpack.unp_count,
4266 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4267 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004268 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4269 iptr->isn_arg.shuffle.shfl_item,
4270 iptr->isn_arg.shuffle.shfl_up);
4271 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 case ISN_DROP: smsg("%4d DROP", current); break;
4273 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004274
4275 out_flush(); // output one line at a time
4276 ui_breakcheck();
4277 if (got_int)
4278 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004279 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280}
4281
4282/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004283 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004284 * list, etc. Mostly like what JavaScript does, except that empty list and
4285 * empty dictionary are FALSE.
4286 */
4287 int
4288tv2bool(typval_T *tv)
4289{
4290 switch (tv->v_type)
4291 {
4292 case VAR_NUMBER:
4293 return tv->vval.v_number != 0;
4294 case VAR_FLOAT:
4295#ifdef FEAT_FLOAT
4296 return tv->vval.v_float != 0.0;
4297#else
4298 break;
4299#endif
4300 case VAR_PARTIAL:
4301 return tv->vval.v_partial != NULL;
4302 case VAR_FUNC:
4303 case VAR_STRING:
4304 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4305 case VAR_LIST:
4306 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4307 case VAR_DICT:
4308 return tv->vval.v_dict != NULL
4309 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4310 case VAR_BOOL:
4311 case VAR_SPECIAL:
4312 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4313 case VAR_JOB:
4314#ifdef FEAT_JOB_CHANNEL
4315 return tv->vval.v_job != NULL;
4316#else
4317 break;
4318#endif
4319 case VAR_CHANNEL:
4320#ifdef FEAT_JOB_CHANNEL
4321 return tv->vval.v_channel != NULL;
4322#else
4323 break;
4324#endif
4325 case VAR_BLOB:
4326 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4327 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004328 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329 case VAR_VOID:
4330 break;
4331 }
4332 return FALSE;
4333}
4334
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004335 void
4336emsg_using_string_as(typval_T *tv, int as_number)
4337{
4338 semsg(_(as_number ? e_using_string_as_number_str
4339 : e_using_string_as_bool_str),
4340 tv->vval.v_string == NULL
4341 ? (char_u *)"" : tv->vval.v_string);
4342}
4343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344/*
4345 * If "tv" is a string give an error and return FAIL.
4346 */
4347 int
4348check_not_string(typval_T *tv)
4349{
4350 if (tv->v_type == VAR_STRING)
4351 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004352 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 clear_tv(tv);
4354 return FAIL;
4355 }
4356 return OK;
4357}
4358
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004359
4360#endif // FEAT_EVAL