blob: abf88b1aef9b3f4532b65130dfbbc902550f4a81 [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 */
57typedef struct {
58 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 Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#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 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // Move the vararg-list to below the missing optional arguments.
231 if (vararg_count > 0 && arg_to_add > 0)
232 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100233
234 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200235 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200237 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100238
239 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100240 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
241 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200242 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
243 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
244 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200245 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
247 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200248 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100249 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200250 if (dfunc->df_has_closure)
251 {
252 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
253
254 tv->v_type = VAR_NUMBER;
255 tv->vval.v_number = 0;
256 }
257 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258
259 // Set execution state to the start of the called function.
260 ectx->ec_dfunc_idx = cdf_idx;
261 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200262 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
263 if (entry != NULL)
264 {
265 // Set the script context to the script where the function was defined.
266 // TODO: save more than the SID?
267 entry->es_save_sid = current_sctx.sc_sid;
268 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
269 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100270
271 // Decide where to start execution, handles optional arguments.
272 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100273
274 return OK;
275}
276
277// Get pointer to item in the stack.
278#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
279
280/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200281 * Used when returning from a function: Check if any closure is still
282 * referenced. If so then move the arguments and variables to a separate piece
283 * of stack to be used when the closure is called.
284 * When "free_arguments" is TRUE the arguments are to be freed.
285 * Returns FAIL when out of memory.
286 */
287 static int
288handle_closure_in_use(ectx_T *ectx, int free_arguments)
289{
290 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
291 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200292 int argcount;
293 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200294 int idx;
295 typval_T *tv;
296 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200297 garray_T *gap = &ectx->ec_funcrefs;
298 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200299
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200300 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 return OK; // function was freed
302 if (dfunc->df_has_closure == 0)
303 return OK; // no closures
304 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
305 closure_count = tv->vval.v_number;
306 if (closure_count == 0)
307 return OK; // no funcrefs created
308
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200309 argcount = ufunc_argcount(dfunc->df_ufunc);
310 top = ectx->ec_frame_idx - argcount;
311
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200312 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200314 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200315 partial_T *pt;
316 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200318 if (off < 0)
319 continue; // count is off or already done
320 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200322 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200323 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200324 int i;
325
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200326 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200327 // unreferenced on return.
328 for (i = 0; i < dfunc->df_varcount; ++i)
329 {
330 typval_T *stv = STACK_TV(ectx->ec_frame_idx
331 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200332 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200333 --refcount;
334 }
335 if (refcount > 1)
336 {
337 closure_in_use = TRUE;
338 break;
339 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200340 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200341 }
342
343 if (closure_in_use)
344 {
345 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
346 typval_T *stack;
347
348 // A closure is using the arguments and/or local variables.
349 // Move them to the called function.
350 if (funcstack == NULL)
351 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200352 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
353 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200354 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
355 funcstack->fs_ga.ga_data = stack;
356 if (stack == NULL)
357 {
358 vim_free(funcstack);
359 return FAIL;
360 }
361
362 // Move or copy the arguments.
363 for (idx = 0; idx < argcount; ++idx)
364 {
365 tv = STACK_TV(top + idx);
366 if (free_arguments)
367 {
368 *(stack + idx) = *tv;
369 tv->v_type = VAR_UNKNOWN;
370 }
371 else
372 copy_tv(tv, stack + idx);
373 }
374 // Move the local variables.
375 for (idx = 0; idx < dfunc->df_varcount; ++idx)
376 {
377 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200378
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200379 // A partial created for a local function, that is also used as a
380 // local variable, has a reference count for the variable, thus
381 // will never go down to zero. When all these refcounts are one
382 // then the funcstack is unused. We need to count how many we have
383 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200384 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
385 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200386 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200387
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200389 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
390 gap->ga_len - closure_count + i])
391 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 }
393
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200394 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200395 tv->v_type = VAR_UNKNOWN;
396 }
397
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200400 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
401 - closure_count + idx];
402 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 ++funcstack->fs_refcount;
405 pt->pt_funcstack = funcstack;
406 pt->pt_ectx_stack = &funcstack->fs_ga;
407 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409 }
410 }
411
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 for (idx = 0; idx < closure_count; ++idx)
413 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
414 - closure_count + idx]);
415 gap->ga_len -= closure_count;
416 if (gap->ga_len == 0)
417 ga_clear(gap);
418
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 return OK;
420}
421
422/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 * Called when a partial is freed or its reference count goes down to one. The
424 * funcstack may be the only reference to the partials in the local variables.
425 * Go over all of them, the funcref and can be freed if all partials
426 * referencing the funcstack have a reference count of one.
427 */
428 void
429funcstack_check_refcount(funcstack_T *funcstack)
430{
431 int i;
432 garray_T *gap = &funcstack->fs_ga;
433 int done = 0;
434
435 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
436 return;
437 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
438 {
439 typval_T *tv = ((typval_T *)gap->ga_data) + i;
440
441 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
442 && tv->vval.v_partial->pt_funcstack == funcstack
443 && tv->vval.v_partial->pt_refcount == 1)
444 ++done;
445 }
446 if (done == funcstack->fs_min_refcount)
447 {
448 typval_T *stack = gap->ga_data;
449
450 // All partials referencing the funcstack have a reference count of
451 // one, thus the funcstack is no longer of use.
452 for (i = 0; i < gap->ga_len; ++i)
453 clear_tv(stack + i);
454 vim_free(stack);
455 vim_free(funcstack);
456 }
457}
458
459/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 * Return from the current function.
461 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200462 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100463func_return(ectx_T *ectx)
464{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
467 + ectx->ec_dfunc_idx;
468 int argcount = ufunc_argcount(dfunc->df_ufunc);
469 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200470 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471
472 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200473 entry = estack_pop();
474 if (entry != NULL)
475 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200477 if (handle_closure_in_use(ectx, TRUE) == FAIL)
478 return FAIL;
479
480 // Clear the arguments.
481 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
482 clear_tv(STACK_TV(idx));
483
484 // Clear local variables and temp values, but not the return value.
485 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100486 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100488
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100489 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
491 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200492 ectx->ec_outer_stack =
493 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
494 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
495 // restoring ec_frame_idx must be last
496 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
498 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100499
500 // Reset the stack to the position before the call, move the return value
501 // to the top of the stack.
502 idx = ectx->ec_stack.ga_len - 1;
503 ectx->ec_stack.ga_len = top + 1;
504 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200505
506 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507}
508
509#undef STACK_TV
510
511/*
512 * Prepare arguments and rettv for calling a builtin or user function.
513 */
514 static int
515call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
516{
517 int idx;
518 typval_T *tv;
519
520 // Move arguments from bottom of the stack to argvars[] and add terminator.
521 for (idx = 0; idx < argcount; ++idx)
522 argvars[idx] = *STACK_TV_BOT(idx - argcount);
523 argvars[argcount].v_type = VAR_UNKNOWN;
524
525 // Result replaces the arguments on the stack.
526 if (argcount > 0)
527 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200528 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 return FAIL;
530 else
531 ++ectx->ec_stack.ga_len;
532
533 // Default return value is zero.
534 tv = STACK_TV_BOT(-1);
535 tv->v_type = VAR_NUMBER;
536 tv->vval.v_number = 0;
537
538 return OK;
539}
540
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200541// Ugly global to avoid passing the execution context around through many
542// layers.
543static ectx_T *current_ectx = NULL;
544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545/*
546 * Call a builtin function by index.
547 */
548 static int
549call_bfunc(int func_idx, int argcount, ectx_T *ectx)
550{
551 typval_T argvars[MAX_FUNC_ARGS];
552 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100553 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200554 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555
556 if (call_prepare(argcount, argvars, ectx) == FAIL)
557 return FAIL;
558
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200559 // Call the builtin function. Set "current_ectx" so that when it
560 // recursively invokes call_def_function() a closure context can be set.
561 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200563 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564
565 // Clear the arguments.
566 for (idx = 0; idx < argcount; ++idx)
567 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200568
Bram Moolenaar171fb922020-10-28 16:54:47 +0100569 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200570 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return OK;
572}
573
574/*
575 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100576 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 */
578 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100579call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580{
581 typval_T argvars[MAX_FUNC_ARGS];
582 funcexe_T funcexe;
583 int error;
584 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100585 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200587 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200588 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
589 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200590 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100591 {
592 // The function has been compiled, can call it quickly. For a function
593 // that was defined later: we can call it directly next time.
594 if (iptr != NULL)
595 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100596 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100597 iptr->isn_type = ISN_DCALL;
598 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
599 iptr->isn_arg.dfunc.cdf_argcount = argcount;
600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
604 if (call_prepare(argcount, argvars, ectx) == FAIL)
605 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200606 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 funcexe.evaluate = TRUE;
608
609 // Call the user function. Result goes in last position on the stack.
610 // TODO: add selfdict if there is one
611 error = call_user_func_check(ufunc, argcount, argvars,
612 STACK_TV_BOT(-1), &funcexe, NULL);
613
614 // Clear the arguments.
615 for (idx = 0; idx < argcount; ++idx)
616 clear_tv(&argvars[idx]);
617
618 if (error != FCERR_NONE)
619 {
620 user_func_error(error, ufunc->uf_name);
621 return FAIL;
622 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100623 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200624 // Error other than from calling the function itself.
625 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return OK;
627}
628
629/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200630 * Return TRUE if an error was given or CTRL-C was pressed.
631 */
632 static int
633vim9_aborting(int prev_called_emsg)
634{
635 return called_emsg > prev_called_emsg || got_int || did_throw;
636}
637
638/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 * Execute a function by "name".
640 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100641 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 * Returns FAIL if not found without an error message.
643 */
644 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100645call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646{
647 ufunc_T *ufunc;
648
649 if (builtin_function(name, -1))
650 {
651 int func_idx = find_internal_func(name);
652
653 if (func_idx < 0)
654 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200655 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 return FAIL;
657 return call_bfunc(func_idx, argcount, ectx);
658 }
659
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200660 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200661
662 if (ufunc == NULL)
663 {
664 int called_emsg_before = called_emsg;
665
666 if (script_autoload(name, TRUE))
667 // loaded a package, search for the function again
668 ufunc = find_func(name, FALSE, NULL);
669 if (vim9_aborting(called_emsg_before))
670 return FAIL; // bail out if loading the script caused an error
671 }
672
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100674 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 return FAIL;
677}
678
679 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200680call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200682 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200683 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200685 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686
687 if (tv->v_type == VAR_PARTIAL)
688 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200689 partial_T *pt = tv->vval.v_partial;
690 int i;
691
692 if (pt->pt_argc > 0)
693 {
694 // Make space for arguments from the partial, shift the "argcount"
695 // arguments up.
696 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
697 return FAIL;
698 for (i = 1; i <= argcount; ++i)
699 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
700 ectx->ec_stack.ga_len += pt->pt_argc;
701 argcount += pt->pt_argc;
702
703 // copy the arguments from the partial onto the stack
704 for (i = 0; i < pt->pt_argc; ++i)
705 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707
708 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200709 {
710 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
711
712 // closure may need the function context where it was defined
713 ectx->ec_outer_stack = pt->pt_ectx_stack;
714 ectx->ec_outer_frame = pt->pt_ectx_frame;
715
716 return ret;
717 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 name = pt->pt_name;
719 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200720 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200722 if (name != NULL)
723 {
724 char_u fname_buf[FLEN_FIXED + 1];
725 char_u *tofree = NULL;
726 int error = FCERR_NONE;
727 char_u *fname;
728
729 // May need to translate <SNR>123_ to K_SNR.
730 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
731 if (error != FCERR_NONE)
732 res = FAIL;
733 else
734 res = call_by_name(fname, argcount, ectx, NULL);
735 vim_free(tofree);
736 }
737
738 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 {
740 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200741 semsg(_(e_unknownfunc),
742 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 return FAIL;
744 }
745 return OK;
746}
747
748/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200749 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
750 * TRUE.
751 */
752 static int
753error_if_locked(int lock, char *error)
754{
755 if (lock & (VAR_LOCKED | VAR_FIXED))
756 {
757 emsg(_(error));
758 return TRUE;
759 }
760 return FALSE;
761}
762
763/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100764 * Store "tv" in variable "name".
765 * This is for s: and g: variables.
766 */
767 static void
768store_var(char_u *name, typval_T *tv)
769{
770 funccal_entry_T entry;
771
772 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200773 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100774 restore_funccal();
775}
776
Bram Moolenaard3aac292020-04-19 14:32:17 +0200777
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100778/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 * Execute a function by "name".
780 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100781 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 */
783 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100784call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100785{
Bram Moolenaared677f52020-08-12 16:38:10 +0200786 int called_emsg_before = called_emsg;
787 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaared677f52020-08-12 16:38:10 +0200789 res = call_by_name(name, argcount, ectx, iptr);
790 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200792 dictitem_T *v;
793
794 v = find_var(name, NULL, FALSE);
795 if (v == NULL)
796 {
797 semsg(_(e_unknownfunc), name);
798 return FAIL;
799 }
800 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
801 {
802 semsg(_(e_unknownfunc), name);
803 return FAIL;
804 }
805 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200807 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808}
809
810/*
811 * Call a "def" function from old Vim script.
812 * Return OK or FAIL.
813 */
814 int
815call_def_function(
816 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200817 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200819 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 typval_T *rettv) // return value
821{
822 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200823 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200824 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 typval_T *tv;
826 int idx;
827 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100828 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200829 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200830 int breakcheck_count = 0;
Bram Moolenaard66960b2020-10-30 20:46:26 +0100831 int did_emsg_before = did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200832 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200833 msglist_T **saved_msg_list = NULL;
834 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200835 cmdmod_T save_cmdmod;
836 int restore_cmdmod = FALSE;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100837 int trylevel_at_start = trylevel;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838
839// Get pointer to item in the stack.
840#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
841
842// Get pointer to item at the bottom of the stack, -1 is the bottom.
843#undef STACK_TV_BOT
844#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
845
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200846// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200847#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 +0100848
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200849// Like STACK_TV_VAR but use the outer scope
850#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
851
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200852 if (ufunc->uf_def_status == UF_NOT_COMPILED
853 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200854 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200855 {
Bram Moolenaard66960b2020-10-30 20:46:26 +0100856 if (did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200857 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200858 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200859 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200860 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200861
Bram Moolenaar09689a02020-05-09 22:50:08 +0200862 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200863 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200864 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
865 + ufunc->uf_dfunc_idx;
866 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200867 {
868 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200869 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200870 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100872
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200873 CLEAR_FIELD(ectx);
874 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
875 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
876 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
877 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200879 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880
881 // Put arguments on the stack.
882 for (idx = 0; idx < argc; ++idx)
883 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200884 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200885 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
886 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200887 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 copy_tv(&argv[idx], STACK_TV_BOT(0));
889 ++ectx.ec_stack.ga_len;
890 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200891
892 // Turn varargs into a list. Empty list if no args.
893 if (ufunc->uf_va_name != NULL)
894 {
895 int vararg_count = argc - ufunc->uf_args.ga_len;
896
897 if (vararg_count < 0)
898 vararg_count = 0;
899 else
900 argc -= vararg_count;
901 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200902 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200903
904 // Check the type of the list items.
905 tv = STACK_TV_BOT(-1);
906 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200907 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200908 && ufunc->uf_va_type->tt_member != &t_any
909 && tv->vval.v_list != NULL)
910 {
911 type_T *expected = ufunc->uf_va_type->tt_member;
912 listitem_T *li = tv->vval.v_list->lv_first;
913
914 for (idx = 0; idx < vararg_count; ++idx)
915 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200916 if (check_typval_type(expected, &li->li_tv,
917 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200918 goto failed_early;
919 li = li->li_next;
920 }
921 }
922
Bram Moolenaar23e03252020-04-12 22:22:31 +0200923 if (defcount > 0)
924 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200925 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200926 --ectx.ec_stack.ga_len;
927 }
928
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100929 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200930 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100931 if (defcount > 0)
932 for (idx = 0; idx < defcount; ++idx)
933 {
934 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
935 ++ectx.ec_stack.ga_len;
936 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200937 if (ufunc->uf_va_name != NULL)
938 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100939
940 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200941 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
942 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200944 if (partial != NULL)
945 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200946 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
947 {
948 // TODO: is this always the right way?
949 ectx.ec_outer_stack = &current_ectx->ec_stack;
950 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
951 }
952 else
953 {
954 ectx.ec_outer_stack = partial->pt_ectx_stack;
955 ectx.ec_outer_frame = partial->pt_ectx_frame;
956 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200957 }
958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 // dummy frame entries
960 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
961 {
962 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
963 ++ectx.ec_stack.ga_len;
964 }
965
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200966 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200967 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200968 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
969 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200971 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200972 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200973 ectx.ec_stack.ga_len += dfunc->df_varcount;
974 if (dfunc->df_has_closure)
975 {
976 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
977 STACK_TV_VAR(idx)->vval.v_number = 0;
978 ++ectx.ec_stack.ga_len;
979 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200980
981 ectx.ec_instr = dfunc->df_instr;
982 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100983
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200984 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200985 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200986 estack_push_ufunc(ufunc, 1);
987 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200988 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
989
Bram Moolenaar352134b2020-10-17 22:04:08 +0200990 // Use a specific location for storing error messages to be converted to an
991 // exception.
992 saved_msg_list = msg_list;
993 msg_list = &private_msg_list;
994
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200995 // Do turn errors into exceptions.
996 suppress_errthrow = FALSE;
997
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100998 // Decide where to start execution, handles optional arguments.
999 init_instr_idx(ufunc, argc, &ectx);
1000
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 for (;;)
1002 {
1003 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001004
Bram Moolenaar270d0382020-05-15 21:42:53 +02001005 if (++breakcheck_count >= 100)
1006 {
1007 line_breakcheck();
1008 breakcheck_count = 0;
1009 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001010 if (got_int)
1011 {
1012 // Turn CTRL-C into an exception.
1013 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001014 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001015 goto failed;
1016 did_throw = TRUE;
1017 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018
Bram Moolenaara26b9702020-04-18 19:53:28 +02001019 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1020 {
1021 // Turn an error message into an exception.
1022 did_emsg = FALSE;
1023 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1024 goto failed;
1025 did_throw = TRUE;
1026 *msg_list = NULL;
1027 }
1028
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 if (did_throw && !ectx.ec_in_catch)
1030 {
1031 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001032 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
1034 // An exception jumps to the first catch, finally, or returns from
1035 // the current function.
1036 if (trystack->ga_len > 0)
1037 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001038 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 {
1040 // jump to ":catch" or ":finally"
1041 ectx.ec_in_catch = TRUE;
1042 ectx.ec_iidx = trycmd->tcd_catch_idx;
1043 }
1044 else
1045 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001046 // Not inside try or need to return from current functions.
1047 // Push a dummy return value.
1048 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1049 goto failed;
1050 tv = STACK_TV_BOT(0);
1051 tv->v_type = VAR_NUMBER;
1052 tv->vval.v_number = 0;
1053 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001054 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001056 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001057 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001058 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1059 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 goto done;
1061 }
1062
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001063 if (func_return(&ectx) == FAIL)
1064 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 }
1066 continue;
1067 }
1068
1069 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1070 switch (iptr->isn_type)
1071 {
1072 // execute Ex command line
1073 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001074 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 do_cmdline_cmd(iptr->isn_arg.string);
1076 break;
1077
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001078 // execute Ex command from pieces on the stack
1079 case ISN_EXECCONCAT:
1080 {
1081 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001082 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001083 int pass;
1084 int i;
1085 char_u *cmd = NULL;
1086 char_u *str;
1087
1088 for (pass = 1; pass <= 2; ++pass)
1089 {
1090 for (i = 0; i < count; ++i)
1091 {
1092 tv = STACK_TV_BOT(i - count);
1093 str = tv->vval.v_string;
1094 if (str != NULL && *str != NUL)
1095 {
1096 if (pass == 2)
1097 STRCPY(cmd + len, str);
1098 len += STRLEN(str);
1099 }
1100 if (pass == 2)
1101 clear_tv(tv);
1102 }
1103 if (pass == 1)
1104 {
1105 cmd = alloc(len + 1);
1106 if (cmd == NULL)
1107 goto failed;
1108 len = 0;
1109 }
1110 }
1111
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001113 do_cmdline_cmd(cmd);
1114 vim_free(cmd);
1115 }
1116 break;
1117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 // execute :echo {string} ...
1119 case ISN_ECHO:
1120 {
1121 int count = iptr->isn_arg.echo.echo_count;
1122 int atstart = TRUE;
1123 int needclr = TRUE;
1124
1125 for (idx = 0; idx < count; ++idx)
1126 {
1127 tv = STACK_TV_BOT(idx - count);
1128 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1129 &atstart, &needclr);
1130 clear_tv(tv);
1131 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001132 if (needclr)
1133 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 ectx.ec_stack.ga_len -= count;
1135 }
1136 break;
1137
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001138 // :execute {string} ...
1139 // :echomsg {string} ...
1140 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001141 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001142 case ISN_ECHOMSG:
1143 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001144 {
1145 int count = iptr->isn_arg.number;
1146 garray_T ga;
1147 char_u buf[NUMBUFLEN];
1148 char_u *p;
1149 int len;
1150 int failed = FALSE;
1151
1152 ga_init2(&ga, 1, 80);
1153 for (idx = 0; idx < count; ++idx)
1154 {
1155 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001156 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001157 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001158 if (tv->v_type == VAR_CHANNEL
1159 || tv->v_type == VAR_JOB)
1160 {
1161 SOURCING_LNUM = iptr->isn_lnum;
1162 emsg(_(e_inval_string));
1163 break;
1164 }
1165 else
1166 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001167 }
1168 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001169 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001170
1171 len = (int)STRLEN(p);
1172 if (ga_grow(&ga, len + 2) == FAIL)
1173 failed = TRUE;
1174 else
1175 {
1176 if (ga.ga_len > 0)
1177 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1178 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1179 ga.ga_len += len;
1180 }
1181 clear_tv(tv);
1182 }
1183 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001184 if (failed)
1185 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001186
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001187 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001188 {
1189 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001190 {
1191 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001192 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001193 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001194 else
1195 {
1196 msg_sb_eol();
1197 if (iptr->isn_type == ISN_ECHOMSG)
1198 {
1199 msg_attr(ga.ga_data, echo_attr);
1200 out_flush();
1201 }
1202 else
1203 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001204 SOURCING_LNUM = iptr->isn_lnum;
1205 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001206 }
1207 }
1208 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001209 ga_clear(&ga);
1210 }
1211 break;
1212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 // load local variable or argument
1214 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001215 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 goto failed;
1217 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1218 ++ectx.ec_stack.ga_len;
1219 break;
1220
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001221 // load variable or argument from outer scope
1222 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001223 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001224 goto failed;
1225 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1226 STACK_TV_BOT(0));
1227 ++ectx.ec_stack.ga_len;
1228 break;
1229
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 // load v: variable
1231 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001232 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 goto failed;
1234 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1235 ++ectx.ec_stack.ga_len;
1236 break;
1237
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001238 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 case ISN_LOADSCRIPT:
1240 {
1241 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001242 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 svar_T *sv;
1244
1245 sv = ((svar_T *)si->sn_var_vals.ga_data)
1246 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001247 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 goto failed;
1249 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1250 ++ectx.ec_stack.ga_len;
1251 }
1252 break;
1253
1254 // load s: variable in old script
1255 case ISN_LOADS:
1256 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001257 hashtab_T *ht = &SCRIPT_VARS(
1258 iptr->isn_arg.loadstore.ls_sid);
1259 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001261
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 if (di == NULL)
1263 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001264 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001265 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001266 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 }
1268 else
1269 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001270 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 goto failed;
1272 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1273 ++ectx.ec_stack.ga_len;
1274 }
1275 }
1276 break;
1277
Bram Moolenaard3aac292020-04-19 14:32:17 +02001278 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001280 case ISN_LOADB:
1281 case ISN_LOADW:
1282 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001284 dictitem_T *di = NULL;
1285 hashtab_T *ht = NULL;
1286 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001287
Bram Moolenaard3aac292020-04-19 14:32:17 +02001288 switch (iptr->isn_type)
1289 {
1290 case ISN_LOADG:
1291 ht = get_globvar_ht();
1292 namespace = 'g';
1293 break;
1294 case ISN_LOADB:
1295 ht = &curbuf->b_vars->dv_hashtab;
1296 namespace = 'b';
1297 break;
1298 case ISN_LOADW:
1299 ht = &curwin->w_vars->dv_hashtab;
1300 namespace = 'w';
1301 break;
1302 case ISN_LOADT:
1303 ht = &curtab->tp_vars->dv_hashtab;
1304 namespace = 't';
1305 break;
1306 default: // Cannot reach here
1307 goto failed;
1308 }
1309 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001310
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001311 if (di == NULL)
1312 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001313 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001314 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001315 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001316 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 }
1318 else
1319 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001320 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 goto failed;
1322 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1323 ++ectx.ec_stack.ga_len;
1324 }
1325 }
1326 break;
1327
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001328 // load g:/b:/w:/t: namespace
1329 case ISN_LOADGDICT:
1330 case ISN_LOADBDICT:
1331 case ISN_LOADWDICT:
1332 case ISN_LOADTDICT:
1333 {
1334 dict_T *d = NULL;
1335
1336 switch (iptr->isn_type)
1337 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001338 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1339 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1340 case ISN_LOADWDICT: d = curwin->w_vars; break;
1341 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001342 default: // Cannot reach here
1343 goto failed;
1344 }
1345 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1346 goto failed;
1347 tv = STACK_TV_BOT(0);
1348 tv->v_type = VAR_DICT;
1349 tv->v_lock = 0;
1350 tv->vval.v_dict = d;
1351 ++ectx.ec_stack.ga_len;
1352 }
1353 break;
1354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 // load &option
1356 case ISN_LOADOPT:
1357 {
1358 typval_T optval;
1359 char_u *name = iptr->isn_arg.string;
1360
Bram Moolenaara8c17702020-04-01 21:17:24 +02001361 // This is not expected to fail, name is checked during
1362 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001363 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001365 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001366 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 *STACK_TV_BOT(0) = optval;
1368 ++ectx.ec_stack.ga_len;
1369 }
1370 break;
1371
1372 // load $ENV
1373 case ISN_LOADENV:
1374 {
1375 typval_T optval;
1376 char_u *name = iptr->isn_arg.string;
1377
Bram Moolenaar270d0382020-05-15 21:42:53 +02001378 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001380 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001381 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 *STACK_TV_BOT(0) = optval;
1383 ++ectx.ec_stack.ga_len;
1384 }
1385 break;
1386
1387 // load @register
1388 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001389 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 goto failed;
1391 tv = STACK_TV_BOT(0);
1392 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001393 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001394 // This may result in NULL, which should be equivalent to an
1395 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 tv->vval.v_string = get_reg_contents(
1397 iptr->isn_arg.number, GREG_EXPR_SRC);
1398 ++ectx.ec_stack.ga_len;
1399 break;
1400
1401 // store local variable
1402 case ISN_STORE:
1403 --ectx.ec_stack.ga_len;
1404 tv = STACK_TV_VAR(iptr->isn_arg.number);
1405 clear_tv(tv);
1406 *tv = *STACK_TV_BOT(0);
1407 break;
1408
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001409 // store variable or argument in outer scope
1410 case ISN_STOREOUTER:
1411 --ectx.ec_stack.ga_len;
1412 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1413 clear_tv(tv);
1414 *tv = *STACK_TV_BOT(0);
1415 break;
1416
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001417 // store s: variable in old script
1418 case ISN_STORES:
1419 {
1420 hashtab_T *ht = &SCRIPT_VARS(
1421 iptr->isn_arg.loadstore.ls_sid);
1422 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001423 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001424
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001425 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001426 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001427 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001428 else
1429 {
1430 clear_tv(&di->di_tv);
1431 di->di_tv = *STACK_TV_BOT(0);
1432 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001433 }
1434 break;
1435
1436 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 case ISN_STORESCRIPT:
1438 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001439 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001440 iptr->isn_arg.script.script_sid);
1441 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1442 + iptr->isn_arg.script.script_idx;
1443
1444 --ectx.ec_stack.ga_len;
1445 clear_tv(sv->sv_tv);
1446 *sv->sv_tv = *STACK_TV_BOT(0);
1447 }
1448 break;
1449
1450 // store option
1451 case ISN_STOREOPT:
1452 {
1453 long n = 0;
1454 char_u *s = NULL;
1455 char *msg;
1456
1457 --ectx.ec_stack.ga_len;
1458 tv = STACK_TV_BOT(0);
1459 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001460 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001462 if (s == NULL)
1463 s = (char_u *)"";
1464 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001466 // must be VAR_NUMBER, CHECKTYPE makes sure
1467 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1469 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001470 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 if (msg != NULL)
1472 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001473 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 }
1478 break;
1479
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001480 // store $ENV
1481 case ISN_STOREENV:
1482 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001483 tv = STACK_TV_BOT(0);
1484 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1485 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001486 break;
1487
1488 // store @r
1489 case ISN_STOREREG:
1490 {
1491 int reg = iptr->isn_arg.number;
1492
1493 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001494 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001496 tv_get_string(tv), -1, FALSE);
1497 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001498 }
1499 break;
1500
1501 // store v: variable
1502 case ISN_STOREV:
1503 --ectx.ec_stack.ga_len;
1504 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1505 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001506 // should not happen, type is checked when compiling
1507 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001508 break;
1509
Bram Moolenaard3aac292020-04-19 14:32:17 +02001510 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001512 case ISN_STOREB:
1513 case ISN_STOREW:
1514 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 {
1516 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001517 hashtab_T *ht;
1518 switch (iptr->isn_type)
1519 {
1520 case ISN_STOREG:
1521 ht = get_globvar_ht();
1522 break;
1523 case ISN_STOREB:
1524 ht = &curbuf->b_vars->dv_hashtab;
1525 break;
1526 case ISN_STOREW:
1527 ht = &curwin->w_vars->dv_hashtab;
1528 break;
1529 case ISN_STORET:
1530 ht = &curtab->tp_vars->dv_hashtab;
1531 break;
1532 default: // Cannot reach here
1533 goto failed;
1534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535
1536 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001537 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001539 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540 else
1541 {
1542 clear_tv(&di->di_tv);
1543 di->di_tv = *STACK_TV_BOT(0);
1544 }
1545 }
1546 break;
1547
1548 // store number in local variable
1549 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001550 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 clear_tv(tv);
1552 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001553 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 break;
1555
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001556 // store value in list variable
1557 case ISN_STORELIST:
1558 {
1559 typval_T *tv_idx = STACK_TV_BOT(-2);
1560 varnumber_T lidx = tv_idx->vval.v_number;
1561 typval_T *tv_list = STACK_TV_BOT(-1);
1562 list_T *list = tv_list->vval.v_list;
1563
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001564 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001565 if (lidx < 0 && list->lv_len + lidx >= 0)
1566 // negative index is relative to the end
1567 lidx = list->lv_len + lidx;
1568 if (lidx < 0 || lidx > list->lv_len)
1569 {
1570 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001571 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001572 }
1573 tv = STACK_TV_BOT(-3);
1574 if (lidx < list->lv_len)
1575 {
1576 listitem_T *li = list_find(list, lidx);
1577
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 if (error_if_locked(li->li_tv.v_lock,
1579 e_cannot_change_list_item))
1580 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001581 // overwrite existing list item
1582 clear_tv(&li->li_tv);
1583 li->li_tv = *tv;
1584 }
1585 else
1586 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001587 if (error_if_locked(list->lv_lock,
1588 e_cannot_change_list))
1589 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001590 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001591 if (list_append_tv(list, tv) == FAIL)
1592 goto failed;
1593 clear_tv(tv);
1594 }
1595 clear_tv(tv_idx);
1596 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001597 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001598 }
1599 break;
1600
1601 // store value in dict variable
1602 case ISN_STOREDICT:
1603 {
1604 typval_T *tv_key = STACK_TV_BOT(-2);
1605 char_u *key = tv_key->vval.v_string;
1606 typval_T *tv_dict = STACK_TV_BOT(-1);
1607 dict_T *dict = tv_dict->vval.v_dict;
1608 dictitem_T *di;
1609
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001610 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001611 if (dict == NULL)
1612 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001613 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001614 goto on_error;
1615 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001616 if (key == NULL)
1617 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001618 tv = STACK_TV_BOT(-3);
1619 di = dict_find(dict, key, -1);
1620 if (di != NULL)
1621 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001622 if (error_if_locked(di->di_tv.v_lock,
1623 e_cannot_change_dict_item))
1624 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001625 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001626 clear_tv(&di->di_tv);
1627 di->di_tv = *tv;
1628 }
1629 else
1630 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001631 if (error_if_locked(dict->dv_lock,
1632 e_cannot_change_dict))
1633 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001634 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001635 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1636 goto failed;
1637 clear_tv(tv);
1638 }
1639 clear_tv(tv_key);
1640 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001641 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001642 }
1643 break;
1644
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 // push constant
1646 case ISN_PUSHNR:
1647 case ISN_PUSHBOOL:
1648 case ISN_PUSHSPEC:
1649 case ISN_PUSHF:
1650 case ISN_PUSHS:
1651 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001652 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001653 case ISN_PUSHCHANNEL:
1654 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001655 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 goto failed;
1657 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001658 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 ++ectx.ec_stack.ga_len;
1660 switch (iptr->isn_type)
1661 {
1662 case ISN_PUSHNR:
1663 tv->v_type = VAR_NUMBER;
1664 tv->vval.v_number = iptr->isn_arg.number;
1665 break;
1666 case ISN_PUSHBOOL:
1667 tv->v_type = VAR_BOOL;
1668 tv->vval.v_number = iptr->isn_arg.number;
1669 break;
1670 case ISN_PUSHSPEC:
1671 tv->v_type = VAR_SPECIAL;
1672 tv->vval.v_number = iptr->isn_arg.number;
1673 break;
1674#ifdef FEAT_FLOAT
1675 case ISN_PUSHF:
1676 tv->v_type = VAR_FLOAT;
1677 tv->vval.v_float = iptr->isn_arg.fnumber;
1678 break;
1679#endif
1680 case ISN_PUSHBLOB:
1681 blob_copy(iptr->isn_arg.blob, tv);
1682 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001683 case ISN_PUSHFUNC:
1684 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001685 if (iptr->isn_arg.string == NULL)
1686 tv->vval.v_string = NULL;
1687 else
1688 tv->vval.v_string =
1689 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001690 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001691 case ISN_PUSHCHANNEL:
1692#ifdef FEAT_JOB_CHANNEL
1693 tv->v_type = VAR_CHANNEL;
1694 tv->vval.v_channel = iptr->isn_arg.channel;
1695 if (tv->vval.v_channel != NULL)
1696 ++tv->vval.v_channel->ch_refcount;
1697#endif
1698 break;
1699 case ISN_PUSHJOB:
1700#ifdef FEAT_JOB_CHANNEL
1701 tv->v_type = VAR_JOB;
1702 tv->vval.v_job = iptr->isn_arg.job;
1703 if (tv->vval.v_job != NULL)
1704 ++tv->vval.v_job->jv_refcount;
1705#endif
1706 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 default:
1708 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001709 tv->vval.v_string = vim_strsave(
1710 iptr->isn_arg.string == NULL
1711 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 }
1713 break;
1714
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001715 case ISN_UNLET:
1716 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1717 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001718 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001719 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001720 case ISN_UNLETENV:
1721 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1722 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001723
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001724 case ISN_LOCKCONST:
1725 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1726 break;
1727
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 // create a list from items on the stack; uses a single allocation
1729 // for the list header and the items
1730 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001731 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1732 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 break;
1734
1735 // create a dict from items on the stack
1736 case ISN_NEWDICT:
1737 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001738 int count = iptr->isn_arg.number;
1739 dict_T *dict = dict_alloc();
1740 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741
1742 if (dict == NULL)
1743 goto failed;
1744 for (idx = 0; idx < count; ++idx)
1745 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001746 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001748 // check key is unique
1749 item = dict_find(dict, tv->vval.v_string, -1);
1750 if (item != NULL)
1751 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001753 semsg(_(e_duplicate_key), tv->vval.v_string);
1754 dict_unref(dict);
1755 goto on_error;
1756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 item = dictitem_alloc(tv->vval.v_string);
1758 clear_tv(tv);
1759 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001760 {
1761 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001763 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1765 item->di_tv.v_lock = 0;
1766 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001767 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001768 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001769 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001771 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 }
1773
1774 if (count > 0)
1775 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001776 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 goto failed;
1778 else
1779 ++ectx.ec_stack.ga_len;
1780 tv = STACK_TV_BOT(-1);
1781 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001782 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 tv->vval.v_dict = dict;
1784 ++dict->dv_refcount;
1785 }
1786 break;
1787
1788 // call a :def function
1789 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001790 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001791 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1792 iptr->isn_arg.dfunc.cdf_argcount,
1793 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001794 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 break;
1796
1797 // call a builtin function
1798 case ISN_BCALL:
1799 SOURCING_LNUM = iptr->isn_lnum;
1800 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1801 iptr->isn_arg.bfunc.cbf_argcount,
1802 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001803 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 break;
1805
1806 // call a funcref or partial
1807 case ISN_PCALL:
1808 {
1809 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1810 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001811 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812
1813 SOURCING_LNUM = iptr->isn_lnum;
1814 if (pfunc->cpf_top)
1815 {
1816 // funcref is above the arguments
1817 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1818 }
1819 else
1820 {
1821 // Get the funcref from the stack.
1822 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001823 partial_tv = *STACK_TV_BOT(0);
1824 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 }
1826 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001827 if (tv == &partial_tv)
1828 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001830 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 }
1832 break;
1833
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001834 case ISN_PCALL_END:
1835 // PCALL finished, arguments have been consumed and replaced by
1836 // the return value. Now clear the funcref from the stack,
1837 // and move the return value in its place.
1838 --ectx.ec_stack.ga_len;
1839 clear_tv(STACK_TV_BOT(-1));
1840 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1841 break;
1842
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 // call a user defined function or funcref/partial
1844 case ISN_UCALL:
1845 {
1846 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1847
1848 SOURCING_LNUM = iptr->isn_lnum;
1849 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001850 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001851 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 }
1853 break;
1854
1855 // return from a :def function call
1856 case ISN_RETURN:
1857 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001858 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001859 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001860
1861 if (trystack->ga_len > 0)
1862 trycmd = ((trycmd_T *)trystack->ga_data)
1863 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001864 if (trycmd != NULL
1865 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001866 && trycmd->tcd_finally_idx != 0)
1867 {
1868 // jump to ":finally"
1869 ectx.ec_iidx = trycmd->tcd_finally_idx;
1870 trycmd->tcd_return = TRUE;
1871 }
1872 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001873 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 }
1875 break;
1876
1877 // push a function reference to a compiled function
1878 case ISN_FUNCREF:
1879 {
1880 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001881 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882
1883 pt = ALLOC_CLEAR_ONE(partial_T);
1884 if (pt == NULL)
1885 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001886 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001887 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001888 vim_free(pt);
1889 goto failed;
1890 }
1891 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1892 + iptr->isn_arg.funcref.fr_func;
1893 pt->pt_func = pt_dfunc->df_ufunc;
1894 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001895
1896 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1897 {
1898 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1899 + ectx.ec_dfunc_idx;
1900
1901 // The closure needs to find arguments and local
1902 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001903 pt->pt_ectx_stack = &ectx.ec_stack;
1904 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001905
1906 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001907 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001908 // (arguments and local variables). Store a reference
1909 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001910 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001911 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001912 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001913 goto failed;
1914 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001915 // Extra variable keeps the count of closures created
1916 // in the current function call.
1917 tv = STACK_TV_VAR(dfunc->df_varcount);
1918 ++tv->vval.v_number;
1919
1920 ((partial_T **)ectx.ec_funcrefs.ga_data)
1921 [ectx.ec_funcrefs.ga_len] = pt;
1922 ++pt->pt_refcount;
1923 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001924 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001925 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 tv = STACK_TV_BOT(0);
1928 ++ectx.ec_stack.ga_len;
1929 tv->vval.v_partial = pt;
1930 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001931 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001932 }
1933 break;
1934
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001935 // Create a global function from a lambda.
1936 case ISN_NEWFUNC:
1937 {
1938 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1939
1940 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1941 }
1942 break;
1943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 // jump if a condition is met
1945 case ISN_JUMP:
1946 {
1947 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001948 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 int jump = TRUE;
1950
1951 if (when != JUMP_ALWAYS)
1952 {
1953 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001954 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001955 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001956 || when == JUMP_IF_COND_TRUE)
1957 {
1958 SOURCING_LNUM = iptr->isn_lnum;
1959 jump = tv_get_bool_chk(tv, &error);
1960 if (error)
1961 goto on_error;
1962 }
1963 else
1964 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001966 || when == JUMP_AND_KEEP_IF_FALSE
1967 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001968 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001969 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001970 {
1971 // drop the value from the stack
1972 clear_tv(tv);
1973 --ectx.ec_stack.ga_len;
1974 }
1975 }
1976 if (jump)
1977 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1978 }
1979 break;
1980
1981 // top of a for loop
1982 case ISN_FOR:
1983 {
1984 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1985 typval_T *idxtv =
1986 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1987
1988 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001989 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001991 ++idxtv->vval.v_number;
1992 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 // past the end of the list, jump to "endfor"
1994 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1995 else if (list->lv_first == &range_list_item)
1996 {
1997 // non-materialized range() list
1998 tv = STACK_TV_BOT(0);
1999 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002000 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 tv->vval.v_number = list_find_nr(
2002 list, idxtv->vval.v_number, NULL);
2003 ++ectx.ec_stack.ga_len;
2004 }
2005 else
2006 {
2007 listitem_T *li = list_find(list, idxtv->vval.v_number);
2008
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2010 ++ectx.ec_stack.ga_len;
2011 }
2012 }
2013 break;
2014
2015 // start of ":try" block
2016 case ISN_TRY:
2017 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002018 trycmd_T *trycmd = NULL;
2019
Bram Moolenaar270d0382020-05-15 21:42:53 +02002020 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002021 goto failed;
2022 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2023 + ectx.ec_trystack.ga_len;
2024 ++ectx.ec_trystack.ga_len;
2025 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002026 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002027 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2028 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002029 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002030 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032 break;
2033
2034 case ISN_PUSHEXC:
2035 if (current_exception == NULL)
2036 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002037 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 iemsg("Evaluating catch while current_exception is NULL");
2039 goto failed;
2040 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002041 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 goto failed;
2043 tv = STACK_TV_BOT(0);
2044 ++ectx.ec_stack.ga_len;
2045 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002046 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 tv->vval.v_string = vim_strsave(
2048 (char_u *)current_exception->value);
2049 break;
2050
2051 case ISN_CATCH:
2052 {
2053 garray_T *trystack = &ectx.ec_trystack;
2054
2055 if (trystack->ga_len > 0)
2056 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002057 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 + trystack->ga_len - 1;
2059 trycmd->tcd_caught = TRUE;
2060 }
2061 did_emsg = got_int = did_throw = FALSE;
2062 catch_exception(current_exception);
2063 }
2064 break;
2065
2066 // end of ":try" block
2067 case ISN_ENDTRY:
2068 {
2069 garray_T *trystack = &ectx.ec_trystack;
2070
2071 if (trystack->ga_len > 0)
2072 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002073 trycmd_T *trycmd = NULL;
2074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 --trystack->ga_len;
2076 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002077 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 trycmd = ((trycmd_T *)trystack->ga_data)
2079 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002080 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002081 {
2082 // discard the exception
2083 if (caught_stack == current_exception)
2084 caught_stack = caught_stack->caught;
2085 discard_current_exception();
2086 }
2087
2088 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002089 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 }
2091 }
2092 break;
2093
2094 case ISN_THROW:
2095 --ectx.ec_stack.ga_len;
2096 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002097 if (tv->vval.v_string == NULL
2098 || *skipwhite(tv->vval.v_string) == NUL)
2099 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002100 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002101 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002102 emsg(_(e_throw_with_empty_string));
2103 goto failed;
2104 }
2105
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002106 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2107 {
2108 vim_free(tv->vval.v_string);
2109 goto failed;
2110 }
2111 did_throw = TRUE;
2112 break;
2113
2114 // compare with special values
2115 case ISN_COMPAREBOOL:
2116 case ISN_COMPARESPECIAL:
2117 {
2118 typval_T *tv1 = STACK_TV_BOT(-2);
2119 typval_T *tv2 = STACK_TV_BOT(-1);
2120 varnumber_T arg1 = tv1->vval.v_number;
2121 varnumber_T arg2 = tv2->vval.v_number;
2122 int res;
2123
2124 switch (iptr->isn_arg.op.op_type)
2125 {
2126 case EXPR_EQUAL: res = arg1 == arg2; break;
2127 case EXPR_NEQUAL: res = arg1 != arg2; break;
2128 default: res = 0; break;
2129 }
2130
2131 --ectx.ec_stack.ga_len;
2132 tv1->v_type = VAR_BOOL;
2133 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2134 }
2135 break;
2136
2137 // Operation with two number arguments
2138 case ISN_OPNR:
2139 case ISN_COMPARENR:
2140 {
2141 typval_T *tv1 = STACK_TV_BOT(-2);
2142 typval_T *tv2 = STACK_TV_BOT(-1);
2143 varnumber_T arg1 = tv1->vval.v_number;
2144 varnumber_T arg2 = tv2->vval.v_number;
2145 varnumber_T res;
2146
2147 switch (iptr->isn_arg.op.op_type)
2148 {
2149 case EXPR_MULT: res = arg1 * arg2; break;
2150 case EXPR_DIV: res = arg1 / arg2; break;
2151 case EXPR_REM: res = arg1 % arg2; break;
2152 case EXPR_SUB: res = arg1 - arg2; break;
2153 case EXPR_ADD: res = arg1 + arg2; break;
2154
2155 case EXPR_EQUAL: res = arg1 == arg2; break;
2156 case EXPR_NEQUAL: res = arg1 != arg2; break;
2157 case EXPR_GREATER: res = arg1 > arg2; break;
2158 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2159 case EXPR_SMALLER: res = arg1 < arg2; break;
2160 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2161 default: res = 0; break;
2162 }
2163
2164 --ectx.ec_stack.ga_len;
2165 if (iptr->isn_type == ISN_COMPARENR)
2166 {
2167 tv1->v_type = VAR_BOOL;
2168 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2169 }
2170 else
2171 tv1->vval.v_number = res;
2172 }
2173 break;
2174
2175 // Computation with two float arguments
2176 case ISN_OPFLOAT:
2177 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002178#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 {
2180 typval_T *tv1 = STACK_TV_BOT(-2);
2181 typval_T *tv2 = STACK_TV_BOT(-1);
2182 float_T arg1 = tv1->vval.v_float;
2183 float_T arg2 = tv2->vval.v_float;
2184 float_T res = 0;
2185 int cmp = FALSE;
2186
2187 switch (iptr->isn_arg.op.op_type)
2188 {
2189 case EXPR_MULT: res = arg1 * arg2; break;
2190 case EXPR_DIV: res = arg1 / arg2; break;
2191 case EXPR_SUB: res = arg1 - arg2; break;
2192 case EXPR_ADD: res = arg1 + arg2; break;
2193
2194 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2195 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2196 case EXPR_GREATER: cmp = arg1 > arg2; break;
2197 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2198 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2199 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2200 default: cmp = 0; break;
2201 }
2202 --ectx.ec_stack.ga_len;
2203 if (iptr->isn_type == ISN_COMPAREFLOAT)
2204 {
2205 tv1->v_type = VAR_BOOL;
2206 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2207 }
2208 else
2209 tv1->vval.v_float = res;
2210 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002211#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002212 break;
2213
2214 case ISN_COMPARELIST:
2215 {
2216 typval_T *tv1 = STACK_TV_BOT(-2);
2217 typval_T *tv2 = STACK_TV_BOT(-1);
2218 list_T *arg1 = tv1->vval.v_list;
2219 list_T *arg2 = tv2->vval.v_list;
2220 int cmp = FALSE;
2221 int ic = iptr->isn_arg.op.op_ic;
2222
2223 switch (iptr->isn_arg.op.op_type)
2224 {
2225 case EXPR_EQUAL: cmp =
2226 list_equal(arg1, arg2, ic, FALSE); break;
2227 case EXPR_NEQUAL: cmp =
2228 !list_equal(arg1, arg2, ic, FALSE); break;
2229 case EXPR_IS: cmp = arg1 == arg2; break;
2230 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2231 default: cmp = 0; break;
2232 }
2233 --ectx.ec_stack.ga_len;
2234 clear_tv(tv1);
2235 clear_tv(tv2);
2236 tv1->v_type = VAR_BOOL;
2237 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2238 }
2239 break;
2240
2241 case ISN_COMPAREBLOB:
2242 {
2243 typval_T *tv1 = STACK_TV_BOT(-2);
2244 typval_T *tv2 = STACK_TV_BOT(-1);
2245 blob_T *arg1 = tv1->vval.v_blob;
2246 blob_T *arg2 = tv2->vval.v_blob;
2247 int cmp = FALSE;
2248
2249 switch (iptr->isn_arg.op.op_type)
2250 {
2251 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2252 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2253 case EXPR_IS: cmp = arg1 == arg2; break;
2254 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2255 default: cmp = 0; break;
2256 }
2257 --ectx.ec_stack.ga_len;
2258 clear_tv(tv1);
2259 clear_tv(tv2);
2260 tv1->v_type = VAR_BOOL;
2261 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2262 }
2263 break;
2264
2265 // TODO: handle separately
2266 case ISN_COMPARESTRING:
2267 case ISN_COMPAREDICT:
2268 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 case ISN_COMPAREANY:
2270 {
2271 typval_T *tv1 = STACK_TV_BOT(-2);
2272 typval_T *tv2 = STACK_TV_BOT(-1);
2273 exptype_T exptype = iptr->isn_arg.op.op_type;
2274 int ic = iptr->isn_arg.op.op_ic;
2275
Bram Moolenaareb26f432020-09-14 16:50:05 +02002276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 typval_compare(tv1, tv2, exptype, ic);
2278 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 --ectx.ec_stack.ga_len;
2280 }
2281 break;
2282
2283 case ISN_ADDLIST:
2284 case ISN_ADDBLOB:
2285 {
2286 typval_T *tv1 = STACK_TV_BOT(-2);
2287 typval_T *tv2 = STACK_TV_BOT(-1);
2288
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002289 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 if (iptr->isn_type == ISN_ADDLIST)
2291 eval_addlist(tv1, tv2);
2292 else
2293 eval_addblob(tv1, tv2);
2294 clear_tv(tv2);
2295 --ectx.ec_stack.ga_len;
2296 }
2297 break;
2298
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002299 case ISN_LISTAPPEND:
2300 {
2301 typval_T *tv1 = STACK_TV_BOT(-2);
2302 typval_T *tv2 = STACK_TV_BOT(-1);
2303 list_T *l = tv1->vval.v_list;
2304
2305 // add an item to a list
2306 if (l == NULL)
2307 {
2308 SOURCING_LNUM = iptr->isn_lnum;
2309 emsg(_(e_cannot_add_to_null_list));
2310 goto on_error;
2311 }
2312 if (list_append_tv(l, tv2) == FAIL)
2313 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002314 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002315 --ectx.ec_stack.ga_len;
2316 }
2317 break;
2318
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002319 case ISN_BLOBAPPEND:
2320 {
2321 typval_T *tv1 = STACK_TV_BOT(-2);
2322 typval_T *tv2 = STACK_TV_BOT(-1);
2323 blob_T *b = tv1->vval.v_blob;
2324 int error = FALSE;
2325 varnumber_T n;
2326
2327 // add a number to a blob
2328 if (b == NULL)
2329 {
2330 SOURCING_LNUM = iptr->isn_lnum;
2331 emsg(_(e_cannot_add_to_null_blob));
2332 goto on_error;
2333 }
2334 n = tv_get_number_chk(tv2, &error);
2335 if (error)
2336 goto on_error;
2337 ga_append(&b->bv_ga, (int)n);
2338 --ectx.ec_stack.ga_len;
2339 }
2340 break;
2341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 // Computation with two arguments of unknown type
2343 case ISN_OPANY:
2344 {
2345 typval_T *tv1 = STACK_TV_BOT(-2);
2346 typval_T *tv2 = STACK_TV_BOT(-1);
2347 varnumber_T n1, n2;
2348#ifdef FEAT_FLOAT
2349 float_T f1 = 0, f2 = 0;
2350#endif
2351 int error = FALSE;
2352
2353 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2354 {
2355 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2356 {
2357 eval_addlist(tv1, tv2);
2358 clear_tv(tv2);
2359 --ectx.ec_stack.ga_len;
2360 break;
2361 }
2362 else if (tv1->v_type == VAR_BLOB
2363 && tv2->v_type == VAR_BLOB)
2364 {
2365 eval_addblob(tv1, tv2);
2366 clear_tv(tv2);
2367 --ectx.ec_stack.ga_len;
2368 break;
2369 }
2370 }
2371#ifdef FEAT_FLOAT
2372 if (tv1->v_type == VAR_FLOAT)
2373 {
2374 f1 = tv1->vval.v_float;
2375 n1 = 0;
2376 }
2377 else
2378#endif
2379 {
2380 n1 = tv_get_number_chk(tv1, &error);
2381 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002382 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383#ifdef FEAT_FLOAT
2384 if (tv2->v_type == VAR_FLOAT)
2385 f1 = n1;
2386#endif
2387 }
2388#ifdef FEAT_FLOAT
2389 if (tv2->v_type == VAR_FLOAT)
2390 {
2391 f2 = tv2->vval.v_float;
2392 n2 = 0;
2393 }
2394 else
2395#endif
2396 {
2397 n2 = tv_get_number_chk(tv2, &error);
2398 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002399 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400#ifdef FEAT_FLOAT
2401 if (tv1->v_type == VAR_FLOAT)
2402 f2 = n2;
2403#endif
2404 }
2405#ifdef FEAT_FLOAT
2406 // if there is a float on either side the result is a float
2407 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2408 {
2409 switch (iptr->isn_arg.op.op_type)
2410 {
2411 case EXPR_MULT: f1 = f1 * f2; break;
2412 case EXPR_DIV: f1 = f1 / f2; break;
2413 case EXPR_SUB: f1 = f1 - f2; break;
2414 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002415 default: SOURCING_LNUM = iptr->isn_lnum;
2416 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002417 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
2419 clear_tv(tv1);
2420 clear_tv(tv2);
2421 tv1->v_type = VAR_FLOAT;
2422 tv1->vval.v_float = f1;
2423 --ectx.ec_stack.ga_len;
2424 }
2425 else
2426#endif
2427 {
2428 switch (iptr->isn_arg.op.op_type)
2429 {
2430 case EXPR_MULT: n1 = n1 * n2; break;
2431 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2432 case EXPR_SUB: n1 = n1 - n2; break;
2433 case EXPR_ADD: n1 = n1 + n2; break;
2434 default: n1 = num_modulus(n1, n2); break;
2435 }
2436 clear_tv(tv1);
2437 clear_tv(tv2);
2438 tv1->v_type = VAR_NUMBER;
2439 tv1->vval.v_number = n1;
2440 --ectx.ec_stack.ga_len;
2441 }
2442 }
2443 break;
2444
2445 case ISN_CONCAT:
2446 {
2447 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2448 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2449 char_u *res;
2450
2451 res = concat_str(str1, str2);
2452 clear_tv(STACK_TV_BOT(-2));
2453 clear_tv(STACK_TV_BOT(-1));
2454 --ectx.ec_stack.ga_len;
2455 STACK_TV_BOT(-1)->vval.v_string = res;
2456 }
2457 break;
2458
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002459 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002460 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002461 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002462 int is_slice = iptr->isn_type == ISN_STRSLICE;
2463 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002464 char_u *res;
2465
2466 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002467 // string slice: string is at stack-3, first index at
2468 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002469 if (is_slice)
2470 {
2471 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002472 n1 = tv->vval.v_number;
2473 }
2474
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002475 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002476 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002477
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002478 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002479 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002480 if (is_slice)
2481 // Slice: Select the characters from the string
2482 res = string_slice(tv->vval.v_string, n1, n2);
2483 else
2484 // Index: The resulting variable is a string of a
2485 // single character. If the index is too big or
2486 // negative the result is empty.
2487 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002488 vim_free(tv->vval.v_string);
2489 tv->vval.v_string = res;
2490 }
2491 break;
2492
2493 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002494 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 {
Bram Moolenaared591872020-08-15 22:14:53 +02002496 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002498 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499
2500 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002501 // list slice: list is at stack-3, indexes at stack-2 and
2502 // stack-1
2503 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 list = tv->vval.v_list;
2505
2506 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002507 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002509
2510 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 {
Bram Moolenaared591872020-08-15 22:14:53 +02002512 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002513 n1 = tv->vval.v_number;
2514 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 }
Bram Moolenaared591872020-08-15 22:14:53 +02002516
2517 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002518 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002520 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2521 == FAIL)
2522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 }
2524 break;
2525
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002526 case ISN_ANYINDEX:
2527 case ISN_ANYSLICE:
2528 {
2529 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2530 typval_T *var1, *var2;
2531 int res;
2532
2533 // index: composite is at stack-2, index at stack-1
2534 // slice: composite is at stack-3, indexes at stack-2 and
2535 // stack-1
2536 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002537 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002538 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2539 goto on_error;
2540 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2541 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2542 res = eval_index_inner(tv, is_slice,
2543 var1, var2, NULL, -1, TRUE);
2544 clear_tv(var1);
2545 if (is_slice)
2546 clear_tv(var2);
2547 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2548 if (res == FAIL)
2549 goto on_error;
2550 }
2551 break;
2552
Bram Moolenaar9af78762020-06-16 11:34:42 +02002553 case ISN_SLICE:
2554 {
2555 list_T *list;
2556 int count = iptr->isn_arg.number;
2557
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002558 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002559 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002560 list = tv->vval.v_list;
2561
2562 // no error for short list, expect it to be checked earlier
2563 if (list != NULL && list->lv_len >= count)
2564 {
2565 list_T *newlist = list_slice(list,
2566 count, list->lv_len - 1);
2567
2568 if (newlist != NULL)
2569 {
2570 list_unref(list);
2571 tv->vval.v_list = newlist;
2572 ++newlist->lv_refcount;
2573 }
2574 }
2575 }
2576 break;
2577
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002578 case ISN_GETITEM:
2579 {
2580 listitem_T *li;
2581 int index = iptr->isn_arg.number;
2582
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002583 // Get list item: list is at stack-1, push item.
2584 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002585 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002586 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002587
2588 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2589 goto failed;
2590 ++ectx.ec_stack.ga_len;
2591 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2592 }
2593 break;
2594
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 case ISN_MEMBER:
2596 {
2597 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002598 char_u *key;
2599 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002600 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002601
2602 // dict member: dict is at stack-2, key at stack-1
2603 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002604 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002605 dict = tv->vval.v_dict;
2606
2607 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002608 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002609 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002610 if (key == NULL)
2611 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002612
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002613 if ((di = dict_find(dict, key, -1)) == NULL)
2614 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002615 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002616 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002617 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002618 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002619 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002620 --ectx.ec_stack.ga_len;
2621 // Clear the dict after getting the item, to avoid that it
2622 // make the item invalid.
2623 tv = STACK_TV_BOT(-1);
2624 temp_tv = *tv;
2625 copy_tv(&di->di_tv, tv);
2626 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002627 }
2628 break;
2629
2630 // dict member with string key
2631 case ISN_STRINGMEMBER:
2632 {
2633 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002635 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636
2637 tv = STACK_TV_BOT(-1);
2638 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2639 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002640 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002642 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644 dict = tv->vval.v_dict;
2645
2646 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2647 == NULL)
2648 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002649 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002651 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002653 // Clear the dict after getting the item, to avoid that it
2654 // make the item invalid.
2655 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002656 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002657 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 }
2659 break;
2660
2661 case ISN_NEGATENR:
2662 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002663 if (tv->v_type != VAR_NUMBER
2664#ifdef FEAT_FLOAT
2665 && tv->v_type != VAR_FLOAT
2666#endif
2667 )
2668 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002670 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002671 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002672 }
2673#ifdef FEAT_FLOAT
2674 if (tv->v_type == VAR_FLOAT)
2675 tv->vval.v_float = -tv->vval.v_float;
2676 else
2677#endif
2678 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002679 break;
2680
2681 case ISN_CHECKNR:
2682 {
2683 int error = FALSE;
2684
2685 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002688 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 (void)tv_get_number_chk(tv, &error);
2690 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002691 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002692 }
2693 break;
2694
2695 case ISN_CHECKTYPE:
2696 {
2697 checktype_T *ct = &iptr->isn_arg.type;
2698
2699 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002700 SOURCING_LNUM = iptr->isn_lnum;
2701 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2702 goto on_error;
2703
2704 // number 0 is FALSE, number 1 is TRUE
2705 if (tv->v_type == VAR_NUMBER
2706 && ct->ct_type->tt_type == VAR_BOOL
2707 && (tv->vval.v_number == 0
2708 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002710 tv->v_type = VAR_BOOL;
2711 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002712 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 }
2714 }
2715 break;
2716
Bram Moolenaar9af78762020-06-16 11:34:42 +02002717 case ISN_CHECKLEN:
2718 {
2719 int min_len = iptr->isn_arg.checklen.cl_min_len;
2720 list_T *list = NULL;
2721
2722 tv = STACK_TV_BOT(-1);
2723 if (tv->v_type == VAR_LIST)
2724 list = tv->vval.v_list;
2725 if (list == NULL || list->lv_len < min_len
2726 || (list->lv_len > min_len
2727 && !iptr->isn_arg.checklen.cl_more_OK))
2728 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002729 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002730 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002731 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002732 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002733 }
2734 }
2735 break;
2736
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002738 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 {
2740 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002741 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742
2743 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002744 if (iptr->isn_type == ISN_2BOOL)
2745 {
2746 n = tv2bool(tv);
2747 if (iptr->isn_arg.number) // invert
2748 n = !n;
2749 }
2750 else
2751 {
2752 SOURCING_LNUM = iptr->isn_lnum;
2753 n = tv_get_bool_chk(tv, &error);
2754 if (error)
2755 goto on_error;
2756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 clear_tv(tv);
2758 tv->v_type = VAR_BOOL;
2759 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2760 }
2761 break;
2762
2763 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002764 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 {
2766 char_u *str;
2767
2768 tv = STACK_TV_BOT(iptr->isn_arg.number);
2769 if (tv->v_type != VAR_STRING)
2770 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002771 if (iptr->isn_type == ISN_2STRING_ANY)
2772 {
2773 switch (tv->v_type)
2774 {
2775 case VAR_SPECIAL:
2776 case VAR_BOOL:
2777 case VAR_NUMBER:
2778 case VAR_FLOAT:
2779 case VAR_BLOB: break;
2780 default: to_string_error(tv->v_type);
2781 goto on_error;
2782 }
2783 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 str = typval_tostring(tv);
2785 clear_tv(tv);
2786 tv->v_type = VAR_STRING;
2787 tv->vval.v_string = str;
2788 }
2789 }
2790 break;
2791
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002792 case ISN_PUT:
2793 {
2794 int regname = iptr->isn_arg.put.put_regname;
2795 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2796 char_u *expr = NULL;
2797 int dir = FORWARD;
2798
2799 if (regname == '=')
2800 {
2801 tv = STACK_TV_BOT(-1);
2802 if (tv->v_type == VAR_STRING)
2803 expr = tv->vval.v_string;
2804 else
2805 {
2806 expr = typval_tostring(tv); // allocates value
2807 clear_tv(tv);
2808 }
2809 --ectx.ec_stack.ga_len;
2810 }
2811 if (lnum == -2)
2812 // :put! above cursor
2813 dir = BACKWARD;
2814 else if (lnum >= 0)
2815 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2816 check_cursor();
2817 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2818 vim_free(expr);
2819 }
2820 break;
2821
Bram Moolenaar02194d22020-10-24 23:08:38 +02002822 case ISN_CMDMOD:
2823 save_cmdmod = cmdmod;
2824 restore_cmdmod = TRUE;
2825 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2826 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002827 break;
2828
Bram Moolenaar02194d22020-10-24 23:08:38 +02002829 case ISN_CMDMOD_REV:
2830 // filter regprog is owned by the instruction, don't free it
2831 cmdmod.cmod_filter_regmatch.regprog = NULL;
2832 undo_cmdmod(&cmdmod);
2833 cmdmod = save_cmdmod;
2834 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002835 break;
2836
Bram Moolenaar389df252020-07-09 21:20:47 +02002837 case ISN_SHUFFLE:
2838 {
2839 typval_T tmp_tv;
2840 int item = iptr->isn_arg.shuffle.shfl_item;
2841 int up = iptr->isn_arg.shuffle.shfl_up;
2842
2843 tmp_tv = *STACK_TV_BOT(-item);
2844 for ( ; up > 0 && item > 1; --up)
2845 {
2846 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2847 --item;
2848 }
2849 *STACK_TV_BOT(-item) = tmp_tv;
2850 }
2851 break;
2852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 case ISN_DROP:
2854 --ectx.ec_stack.ga_len;
2855 clear_tv(STACK_TV_BOT(0));
2856 break;
2857 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002858 continue;
2859
Bram Moolenaard032f342020-07-18 18:13:02 +02002860func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002861 // Restore previous function. If the frame pointer is where we started
2862 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002863 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002864 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002865
Bram Moolenaard032f342020-07-18 18:13:02 +02002866 if (func_return(&ectx) == FAIL)
2867 // only fails when out of memory
2868 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002869 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002870
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002871on_error:
Bram Moolenaar171fb922020-10-28 16:54:47 +01002872 // If we are not inside a try-catch started here, abort execution.
2873 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002874 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 }
2876
2877done:
2878 // function finished, get result from the stack.
2879 tv = STACK_TV_BOT(-1);
2880 *rettv = *tv;
2881 tv->v_type = VAR_UNKNOWN;
2882 ret = OK;
2883
2884failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002885 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002886 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002887 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002888
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002889 // Deal with any remaining closures, they may be in use somewhere.
2890 if (ectx.ec_funcrefs.ga_len > 0)
2891 handle_closure_in_use(&ectx, FALSE);
2892
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002893 estack_pop();
2894 current_sctx = save_current_sctx;
2895
Bram Moolenaar352134b2020-10-17 22:04:08 +02002896 if (*msg_list != NULL && saved_msg_list != NULL)
2897 {
2898 msglist_T **plist = saved_msg_list;
2899
2900 // Append entries from the current msg_list (uncaught exceptions) to
2901 // the saved msg_list.
2902 while (*plist != NULL)
2903 plist = &(*plist)->next;
2904
2905 *plist = *msg_list;
2906 }
2907 msg_list = saved_msg_list;
2908
Bram Moolenaar02194d22020-10-24 23:08:38 +02002909 if (restore_cmdmod)
2910 {
2911 cmdmod.cmod_filter_regmatch.regprog = NULL;
2912 undo_cmdmod(&cmdmod);
2913 cmdmod = save_cmdmod;
2914 }
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002915
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002916failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002917 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2919 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002920
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002922 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002923
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002924 // Not sure if this is necessary.
2925 suppress_errthrow = save_suppress_errthrow;
2926
Bram Moolenaard66960b2020-10-30 20:46:26 +01002927 if (ret != OK && did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002928 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002929 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 return ret;
2931}
2932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933/*
2934 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002935 * We don't really need this at runtime, but we do have tests that require it,
2936 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 */
2938 void
2939ex_disassemble(exarg_T *eap)
2940{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002941 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002942 char_u *fname;
2943 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 dfunc_T *dfunc;
2945 isn_T *instr;
2946 int current;
2947 int line_idx = 0;
2948 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002949 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002950
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002951 if (STRNCMP(arg, "<lambda>", 8) == 0)
2952 {
2953 arg += 8;
2954 (void)getdigits(&arg);
2955 fname = vim_strnsave(eap->arg, arg - eap->arg);
2956 }
2957 else
2958 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002959 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002960 if (fname == NULL)
2961 {
2962 semsg(_(e_invarg2), eap->arg);
2963 return;
2964 }
2965
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002966 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002967 if (ufunc == NULL)
2968 {
2969 char_u *p = untrans_function_name(fname);
2970
2971 if (p != NULL)
2972 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002973 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002974 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002975 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002976 if (ufunc == NULL)
2977 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002978 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 return;
2980 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002981 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002982 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2983 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002984 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002986 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002987 return;
2988 }
2989 if (ufunc->uf_name_exp != NULL)
2990 msg((char *)ufunc->uf_name_exp);
2991 else
2992 msg((char *)ufunc->uf_name);
2993
2994 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2995 instr = dfunc->df_instr;
2996 for (current = 0; current < dfunc->df_instr_count; ++current)
2997 {
2998 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002999 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003000
3001 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3002 {
3003 if (current > prev_current)
3004 {
3005 msg_puts("\n\n");
3006 prev_current = current;
3007 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003008 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3009 if (line != NULL)
3010 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 }
3012
3013 switch (iptr->isn_type)
3014 {
3015 case ISN_EXEC:
3016 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3017 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003018 case ISN_EXECCONCAT:
3019 smsg("%4d EXECCONCAT %lld", current,
3020 (long long)iptr->isn_arg.number);
3021 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 case ISN_ECHO:
3023 {
3024 echo_T *echo = &iptr->isn_arg.echo;
3025
3026 smsg("%4d %s %d", current,
3027 echo->echo_with_white ? "ECHO" : "ECHON",
3028 echo->echo_count);
3029 }
3030 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003031 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003032 smsg("%4d EXECUTE %lld", current,
3033 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003034 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003035 case ISN_ECHOMSG:
3036 smsg("%4d ECHOMSG %lld", current,
3037 (long long)(iptr->isn_arg.number));
3038 break;
3039 case ISN_ECHOERR:
3040 smsg("%4d ECHOERR %lld", current,
3041 (long long)(iptr->isn_arg.number));
3042 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003044 case ISN_LOADOUTER:
3045 {
3046 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3047
3048 if (iptr->isn_arg.number < 0)
3049 smsg("%4d LOAD%s arg[%lld]", current, add,
3050 (long long)(iptr->isn_arg.number
3051 + STACK_FRAME_SIZE));
3052 else
3053 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003054 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003055 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003056 break;
3057 case ISN_LOADV:
3058 smsg("%4d LOADV v:%s", current,
3059 get_vim_var_name(iptr->isn_arg.number));
3060 break;
3061 case ISN_LOADSCRIPT:
3062 {
3063 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003064 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3066 + iptr->isn_arg.script.script_idx;
3067
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003068 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3069 sv->sv_name,
3070 iptr->isn_arg.script.script_idx,
3071 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 }
3073 break;
3074 case ISN_LOADS:
3075 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003076 scriptitem_T *si = SCRIPT_ITEM(
3077 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078
3079 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003080 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 }
3082 break;
3083 case ISN_LOADG:
3084 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3085 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003086 case ISN_LOADB:
3087 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3088 break;
3089 case ISN_LOADW:
3090 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3091 break;
3092 case ISN_LOADT:
3093 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3094 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003095 case ISN_LOADGDICT:
3096 smsg("%4d LOAD g:", current);
3097 break;
3098 case ISN_LOADBDICT:
3099 smsg("%4d LOAD b:", current);
3100 break;
3101 case ISN_LOADWDICT:
3102 smsg("%4d LOAD w:", current);
3103 break;
3104 case ISN_LOADTDICT:
3105 smsg("%4d LOAD t:", current);
3106 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 case ISN_LOADOPT:
3108 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3109 break;
3110 case ISN_LOADENV:
3111 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3112 break;
3113 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003114 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 break;
3116
3117 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003118 case ISN_STOREOUTER:
3119 {
3120 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3121
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003122 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003123 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003124 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003125 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003126 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003127 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003130 case ISN_STOREV:
3131 smsg("%4d STOREV v:%s", current,
3132 get_vim_var_name(iptr->isn_arg.number));
3133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003135 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3136 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003137 case ISN_STOREB:
3138 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3139 break;
3140 case ISN_STOREW:
3141 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3142 break;
3143 case ISN_STORET:
3144 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3145 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003146 case ISN_STORES:
3147 {
3148 scriptitem_T *si = SCRIPT_ITEM(
3149 iptr->isn_arg.loadstore.ls_sid);
3150
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003151 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003152 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003153 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 break;
3155 case ISN_STORESCRIPT:
3156 {
3157 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003158 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3160 + iptr->isn_arg.script.script_idx;
3161
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003162 smsg("%4d STORESCRIPT %s-%d in %s", current,
3163 sv->sv_name,
3164 iptr->isn_arg.script.script_idx,
3165 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 }
3167 break;
3168 case ISN_STOREOPT:
3169 smsg("%4d STOREOPT &%s", current,
3170 iptr->isn_arg.storeopt.so_name);
3171 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003172 case ISN_STOREENV:
3173 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3174 break;
3175 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003176 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003177 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 case ISN_STORENR:
3179 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003180 iptr->isn_arg.storenr.stnr_val,
3181 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 break;
3183
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003184 case ISN_STORELIST:
3185 smsg("%4d STORELIST", current);
3186 break;
3187
3188 case ISN_STOREDICT:
3189 smsg("%4d STOREDICT", current);
3190 break;
3191
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 // constants
3193 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003194 smsg("%4d PUSHNR %lld", current,
3195 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 break;
3197 case ISN_PUSHBOOL:
3198 case ISN_PUSHSPEC:
3199 smsg("%4d PUSH %s", current,
3200 get_var_special_name(iptr->isn_arg.number));
3201 break;
3202 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003203#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003205#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 break;
3207 case ISN_PUSHS:
3208 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3209 break;
3210 case ISN_PUSHBLOB:
3211 {
3212 char_u *r;
3213 char_u numbuf[NUMBUFLEN];
3214 char_u *tofree;
3215
3216 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003217 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 vim_free(tofree);
3219 }
3220 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003221 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003222 {
3223 char *name = (char *)iptr->isn_arg.string;
3224
3225 smsg("%4d PUSHFUNC \"%s\"", current,
3226 name == NULL ? "[none]" : name);
3227 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003228 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003229 case ISN_PUSHCHANNEL:
3230#ifdef FEAT_JOB_CHANNEL
3231 {
3232 channel_T *channel = iptr->isn_arg.channel;
3233
3234 smsg("%4d PUSHCHANNEL %d", current,
3235 channel == NULL ? 0 : channel->ch_id);
3236 }
3237#endif
3238 break;
3239 case ISN_PUSHJOB:
3240#ifdef FEAT_JOB_CHANNEL
3241 {
3242 typval_T tv;
3243 char_u *name;
3244
3245 tv.v_type = VAR_JOB;
3246 tv.vval.v_job = iptr->isn_arg.job;
3247 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003248 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003249 }
3250#endif
3251 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 case ISN_PUSHEXC:
3253 smsg("%4d PUSH v:exception", current);
3254 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003255 case ISN_UNLET:
3256 smsg("%4d UNLET%s %s", current,
3257 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3258 iptr->isn_arg.unlet.ul_name);
3259 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003260 case ISN_UNLETENV:
3261 smsg("%4d UNLETENV%s $%s", current,
3262 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3263 iptr->isn_arg.unlet.ul_name);
3264 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003265 case ISN_LOCKCONST:
3266 smsg("%4d LOCKCONST", current);
3267 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003269 smsg("%4d NEWLIST size %lld", current,
3270 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003271 break;
3272 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003273 smsg("%4d NEWDICT size %lld", current,
3274 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 break;
3276
3277 // function call
3278 case ISN_BCALL:
3279 {
3280 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3281
3282 smsg("%4d BCALL %s(argc %d)", current,
3283 internal_func_name(cbfunc->cbf_idx),
3284 cbfunc->cbf_argcount);
3285 }
3286 break;
3287 case ISN_DCALL:
3288 {
3289 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3290 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3291 + cdfunc->cdf_idx;
3292
3293 smsg("%4d DCALL %s(argc %d)", current,
3294 df->df_ufunc->uf_name_exp != NULL
3295 ? df->df_ufunc->uf_name_exp
3296 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3297 }
3298 break;
3299 case ISN_UCALL:
3300 {
3301 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3302
3303 smsg("%4d UCALL %s(argc %d)", current,
3304 cufunc->cuf_name, cufunc->cuf_argcount);
3305 }
3306 break;
3307 case ISN_PCALL:
3308 {
3309 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3310
3311 smsg("%4d PCALL%s (argc %d)", current,
3312 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3313 }
3314 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003315 case ISN_PCALL_END:
3316 smsg("%4d PCALL end", current);
3317 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 case ISN_RETURN:
3319 smsg("%4d RETURN", current);
3320 break;
3321 case ISN_FUNCREF:
3322 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003323 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003325 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003326
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003327 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003328 }
3329 break;
3330
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003331 case ISN_NEWFUNC:
3332 {
3333 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3334
3335 smsg("%4d NEWFUNC %s %s", current,
3336 newfunc->nf_lambda, newfunc->nf_global);
3337 }
3338 break;
3339
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 case ISN_JUMP:
3341 {
3342 char *when = "?";
3343
3344 switch (iptr->isn_arg.jump.jump_when)
3345 {
3346 case JUMP_ALWAYS:
3347 when = "JUMP";
3348 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003349 case JUMP_AND_KEEP_IF_TRUE:
3350 when = "JUMP_AND_KEEP_IF_TRUE";
3351 break;
3352 case JUMP_IF_FALSE:
3353 when = "JUMP_IF_FALSE";
3354 break;
3355 case JUMP_AND_KEEP_IF_FALSE:
3356 when = "JUMP_AND_KEEP_IF_FALSE";
3357 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003358 case JUMP_IF_COND_FALSE:
3359 when = "JUMP_IF_COND_FALSE";
3360 break;
3361 case JUMP_IF_COND_TRUE:
3362 when = "JUMP_IF_COND_TRUE";
3363 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003365 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 iptr->isn_arg.jump.jump_where);
3367 }
3368 break;
3369
3370 case ISN_FOR:
3371 {
3372 forloop_T *forloop = &iptr->isn_arg.forloop;
3373
3374 smsg("%4d FOR $%d -> %d", current,
3375 forloop->for_idx, forloop->for_end);
3376 }
3377 break;
3378
3379 case ISN_TRY:
3380 {
3381 try_T *try = &iptr->isn_arg.try;
3382
3383 smsg("%4d TRY catch -> %d, finally -> %d", current,
3384 try->try_catch, try->try_finally);
3385 }
3386 break;
3387 case ISN_CATCH:
3388 // TODO
3389 smsg("%4d CATCH", current);
3390 break;
3391 case ISN_ENDTRY:
3392 smsg("%4d ENDTRY", current);
3393 break;
3394 case ISN_THROW:
3395 smsg("%4d THROW", current);
3396 break;
3397
3398 // expression operations on number
3399 case ISN_OPNR:
3400 case ISN_OPFLOAT:
3401 case ISN_OPANY:
3402 {
3403 char *what;
3404 char *ins;
3405
3406 switch (iptr->isn_arg.op.op_type)
3407 {
3408 case EXPR_MULT: what = "*"; break;
3409 case EXPR_DIV: what = "/"; break;
3410 case EXPR_REM: what = "%"; break;
3411 case EXPR_SUB: what = "-"; break;
3412 case EXPR_ADD: what = "+"; break;
3413 default: what = "???"; break;
3414 }
3415 switch (iptr->isn_type)
3416 {
3417 case ISN_OPNR: ins = "OPNR"; break;
3418 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3419 case ISN_OPANY: ins = "OPANY"; break;
3420 default: ins = "???"; break;
3421 }
3422 smsg("%4d %s %s", current, ins, what);
3423 }
3424 break;
3425
3426 case ISN_COMPAREBOOL:
3427 case ISN_COMPARESPECIAL:
3428 case ISN_COMPARENR:
3429 case ISN_COMPAREFLOAT:
3430 case ISN_COMPARESTRING:
3431 case ISN_COMPAREBLOB:
3432 case ISN_COMPARELIST:
3433 case ISN_COMPAREDICT:
3434 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 case ISN_COMPAREANY:
3436 {
3437 char *p;
3438 char buf[10];
3439 char *type;
3440
3441 switch (iptr->isn_arg.op.op_type)
3442 {
3443 case EXPR_EQUAL: p = "=="; break;
3444 case EXPR_NEQUAL: p = "!="; break;
3445 case EXPR_GREATER: p = ">"; break;
3446 case EXPR_GEQUAL: p = ">="; break;
3447 case EXPR_SMALLER: p = "<"; break;
3448 case EXPR_SEQUAL: p = "<="; break;
3449 case EXPR_MATCH: p = "=~"; break;
3450 case EXPR_IS: p = "is"; break;
3451 case EXPR_ISNOT: p = "isnot"; break;
3452 case EXPR_NOMATCH: p = "!~"; break;
3453 default: p = "???"; break;
3454 }
3455 STRCPY(buf, p);
3456 if (iptr->isn_arg.op.op_ic == TRUE)
3457 strcat(buf, "?");
3458 switch(iptr->isn_type)
3459 {
3460 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3461 case ISN_COMPARESPECIAL:
3462 type = "COMPARESPECIAL"; break;
3463 case ISN_COMPARENR: type = "COMPARENR"; break;
3464 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3465 case ISN_COMPARESTRING:
3466 type = "COMPARESTRING"; break;
3467 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3468 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3469 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3470 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3472 default: type = "???"; break;
3473 }
3474
3475 smsg("%4d %s %s", current, type, buf);
3476 }
3477 break;
3478
3479 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3480 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3481
3482 // expression operations
3483 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003484 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003485 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003486 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003487 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003488 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003489 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003490 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3491 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003492 case ISN_SLICE: smsg("%4d SLICE %lld",
3493 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003494 case ISN_GETITEM: smsg("%4d ITEM %lld",
3495 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003496 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3497 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003498 iptr->isn_arg.string); break;
3499 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3500
3501 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003502 case ISN_CHECKTYPE:
3503 {
3504 char *tofree;
3505
3506 smsg("%4d CHECKTYPE %s stack[%d]", current,
3507 type_name(iptr->isn_arg.type.ct_type, &tofree),
3508 iptr->isn_arg.type.ct_off);
3509 vim_free(tofree);
3510 break;
3511 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003512 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3513 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3514 iptr->isn_arg.checklen.cl_min_len);
3515 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003516 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517 case ISN_2BOOL: if (iptr->isn_arg.number)
3518 smsg("%4d INVERT (!val)", current);
3519 else
3520 smsg("%4d 2BOOL (!!val)", current);
3521 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003522 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3523 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003524 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003525 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3526 (long long)(iptr->isn_arg.number));
3527 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003528 case ISN_PUT:
3529 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3530 (long)iptr->isn_arg.put.put_lnum);
3531 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532
Bram Moolenaar02194d22020-10-24 23:08:38 +02003533 // TODO: summarize modifiers
3534 case ISN_CMDMOD:
3535 {
3536 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003537 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003538 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3539
3540 buf = alloc(len + 1);
3541 if (buf != NULL)
3542 {
3543 (void)produce_cmdmods(
3544 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3545 smsg("%4d CMDMOD %s", current, buf);
3546 vim_free(buf);
3547 }
3548 break;
3549 }
3550 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003551
Bram Moolenaar389df252020-07-09 21:20:47 +02003552 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3553 iptr->isn_arg.shuffle.shfl_item,
3554 iptr->isn_arg.shuffle.shfl_up);
3555 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 case ISN_DROP: smsg("%4d DROP", current); break;
3557 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003558
3559 out_flush(); // output one line at a time
3560 ui_breakcheck();
3561 if (got_int)
3562 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003564}
3565
3566/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003567 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 * list, etc. Mostly like what JavaScript does, except that empty list and
3569 * empty dictionary are FALSE.
3570 */
3571 int
3572tv2bool(typval_T *tv)
3573{
3574 switch (tv->v_type)
3575 {
3576 case VAR_NUMBER:
3577 return tv->vval.v_number != 0;
3578 case VAR_FLOAT:
3579#ifdef FEAT_FLOAT
3580 return tv->vval.v_float != 0.0;
3581#else
3582 break;
3583#endif
3584 case VAR_PARTIAL:
3585 return tv->vval.v_partial != NULL;
3586 case VAR_FUNC:
3587 case VAR_STRING:
3588 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3589 case VAR_LIST:
3590 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3591 case VAR_DICT:
3592 return tv->vval.v_dict != NULL
3593 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3594 case VAR_BOOL:
3595 case VAR_SPECIAL:
3596 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3597 case VAR_JOB:
3598#ifdef FEAT_JOB_CHANNEL
3599 return tv->vval.v_job != NULL;
3600#else
3601 break;
3602#endif
3603 case VAR_CHANNEL:
3604#ifdef FEAT_JOB_CHANNEL
3605 return tv->vval.v_channel != NULL;
3606#else
3607 break;
3608#endif
3609 case VAR_BLOB:
3610 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3611 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003612 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003613 case VAR_VOID:
3614 break;
3615 }
3616 return FALSE;
3617}
3618
3619/*
3620 * If "tv" is a string give an error and return FAIL.
3621 */
3622 int
3623check_not_string(typval_T *tv)
3624{
3625 if (tv->v_type == VAR_STRING)
3626 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003627 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003628 clear_tv(tv);
3629 return FAIL;
3630 }
3631 return OK;
3632}
3633
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634
3635#endif // FEAT_EVAL