blob: c9ae709d63d0567af908e52edba86d2b4c0a3b2f [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 Moolenaar631e8f92020-11-04 15:07:16 +01001074 {
1075 int save_did_emsg = did_emsg;
1076
1077 SOURCING_LNUM = iptr->isn_lnum;
1078 do_cmdline_cmd(iptr->isn_arg.string);
1079 // do_cmdline_cmd() will reset did_emsg, but we want to
1080 // keep track of the count to compare with did_emsg_before.
1081 did_emsg += save_did_emsg;
1082 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 break;
1084
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001085 // execute Ex command from pieces on the stack
1086 case ISN_EXECCONCAT:
1087 {
1088 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001089 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001090 int pass;
1091 int i;
1092 char_u *cmd = NULL;
1093 char_u *str;
1094
1095 for (pass = 1; pass <= 2; ++pass)
1096 {
1097 for (i = 0; i < count; ++i)
1098 {
1099 tv = STACK_TV_BOT(i - count);
1100 str = tv->vval.v_string;
1101 if (str != NULL && *str != NUL)
1102 {
1103 if (pass == 2)
1104 STRCPY(cmd + len, str);
1105 len += STRLEN(str);
1106 }
1107 if (pass == 2)
1108 clear_tv(tv);
1109 }
1110 if (pass == 1)
1111 {
1112 cmd = alloc(len + 1);
1113 if (cmd == NULL)
1114 goto failed;
1115 len = 0;
1116 }
1117 }
1118
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001119 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001120 do_cmdline_cmd(cmd);
1121 vim_free(cmd);
1122 }
1123 break;
1124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 // execute :echo {string} ...
1126 case ISN_ECHO:
1127 {
1128 int count = iptr->isn_arg.echo.echo_count;
1129 int atstart = TRUE;
1130 int needclr = TRUE;
1131
1132 for (idx = 0; idx < count; ++idx)
1133 {
1134 tv = STACK_TV_BOT(idx - count);
1135 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1136 &atstart, &needclr);
1137 clear_tv(tv);
1138 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001139 if (needclr)
1140 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 ectx.ec_stack.ga_len -= count;
1142 }
1143 break;
1144
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001145 // :execute {string} ...
1146 // :echomsg {string} ...
1147 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001148 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001149 case ISN_ECHOMSG:
1150 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001151 {
1152 int count = iptr->isn_arg.number;
1153 garray_T ga;
1154 char_u buf[NUMBUFLEN];
1155 char_u *p;
1156 int len;
1157 int failed = FALSE;
1158
1159 ga_init2(&ga, 1, 80);
1160 for (idx = 0; idx < count; ++idx)
1161 {
1162 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001163 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001164 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001165 if (tv->v_type == VAR_CHANNEL
1166 || tv->v_type == VAR_JOB)
1167 {
1168 SOURCING_LNUM = iptr->isn_lnum;
1169 emsg(_(e_inval_string));
1170 break;
1171 }
1172 else
1173 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001174 }
1175 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001176 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001177
1178 len = (int)STRLEN(p);
1179 if (ga_grow(&ga, len + 2) == FAIL)
1180 failed = TRUE;
1181 else
1182 {
1183 if (ga.ga_len > 0)
1184 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1185 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1186 ga.ga_len += len;
1187 }
1188 clear_tv(tv);
1189 }
1190 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001191 if (failed)
1192 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001193
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001194 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001195 {
1196 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001197 {
1198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001199 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001200 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001201 else
1202 {
1203 msg_sb_eol();
1204 if (iptr->isn_type == ISN_ECHOMSG)
1205 {
1206 msg_attr(ga.ga_data, echo_attr);
1207 out_flush();
1208 }
1209 else
1210 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001211 SOURCING_LNUM = iptr->isn_lnum;
1212 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001213 }
1214 }
1215 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001216 ga_clear(&ga);
1217 }
1218 break;
1219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 // load local variable or argument
1221 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001222 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 goto failed;
1224 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1225 ++ectx.ec_stack.ga_len;
1226 break;
1227
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001228 // load variable or argument from outer scope
1229 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001230 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001231 goto failed;
1232 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1233 STACK_TV_BOT(0));
1234 ++ectx.ec_stack.ga_len;
1235 break;
1236
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 // load v: variable
1238 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001239 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 goto failed;
1241 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1242 ++ectx.ec_stack.ga_len;
1243 break;
1244
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001245 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 case ISN_LOADSCRIPT:
1247 {
1248 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001249 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 svar_T *sv;
1251
1252 sv = ((svar_T *)si->sn_var_vals.ga_data)
1253 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001254 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 goto failed;
1256 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1257 ++ectx.ec_stack.ga_len;
1258 }
1259 break;
1260
1261 // load s: variable in old script
1262 case ISN_LOADS:
1263 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001264 hashtab_T *ht = &SCRIPT_VARS(
1265 iptr->isn_arg.loadstore.ls_sid);
1266 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (di == NULL)
1270 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001272 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001273 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 }
1275 else
1276 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001277 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 goto failed;
1279 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1280 ++ectx.ec_stack.ga_len;
1281 }
1282 }
1283 break;
1284
Bram Moolenaard3aac292020-04-19 14:32:17 +02001285 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001287 case ISN_LOADB:
1288 case ISN_LOADW:
1289 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001290 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001291 dictitem_T *di = NULL;
1292 hashtab_T *ht = NULL;
1293 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001294
Bram Moolenaard3aac292020-04-19 14:32:17 +02001295 switch (iptr->isn_type)
1296 {
1297 case ISN_LOADG:
1298 ht = get_globvar_ht();
1299 namespace = 'g';
1300 break;
1301 case ISN_LOADB:
1302 ht = &curbuf->b_vars->dv_hashtab;
1303 namespace = 'b';
1304 break;
1305 case ISN_LOADW:
1306 ht = &curwin->w_vars->dv_hashtab;
1307 namespace = 'w';
1308 break;
1309 case ISN_LOADT:
1310 ht = &curtab->tp_vars->dv_hashtab;
1311 namespace = 't';
1312 break;
1313 default: // Cannot reach here
1314 goto failed;
1315 }
1316 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001317
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 if (di == NULL)
1319 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001320 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001321 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001322 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001323 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 }
1325 else
1326 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001327 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 goto failed;
1329 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1330 ++ectx.ec_stack.ga_len;
1331 }
1332 }
1333 break;
1334
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001335 // load g:/b:/w:/t: namespace
1336 case ISN_LOADGDICT:
1337 case ISN_LOADBDICT:
1338 case ISN_LOADWDICT:
1339 case ISN_LOADTDICT:
1340 {
1341 dict_T *d = NULL;
1342
1343 switch (iptr->isn_type)
1344 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001345 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1346 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1347 case ISN_LOADWDICT: d = curwin->w_vars; break;
1348 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001349 default: // Cannot reach here
1350 goto failed;
1351 }
1352 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1353 goto failed;
1354 tv = STACK_TV_BOT(0);
1355 tv->v_type = VAR_DICT;
1356 tv->v_lock = 0;
1357 tv->vval.v_dict = d;
1358 ++ectx.ec_stack.ga_len;
1359 }
1360 break;
1361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 // load &option
1363 case ISN_LOADOPT:
1364 {
1365 typval_T optval;
1366 char_u *name = iptr->isn_arg.string;
1367
Bram Moolenaara8c17702020-04-01 21:17:24 +02001368 // This is not expected to fail, name is checked during
1369 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001370 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001372 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001373 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 *STACK_TV_BOT(0) = optval;
1375 ++ectx.ec_stack.ga_len;
1376 }
1377 break;
1378
1379 // load $ENV
1380 case ISN_LOADENV:
1381 {
1382 typval_T optval;
1383 char_u *name = iptr->isn_arg.string;
1384
Bram Moolenaar270d0382020-05-15 21:42:53 +02001385 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001387 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001388 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389 *STACK_TV_BOT(0) = optval;
1390 ++ectx.ec_stack.ga_len;
1391 }
1392 break;
1393
1394 // load @register
1395 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001396 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 goto failed;
1398 tv = STACK_TV_BOT(0);
1399 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001400 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001401 // This may result in NULL, which should be equivalent to an
1402 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 tv->vval.v_string = get_reg_contents(
1404 iptr->isn_arg.number, GREG_EXPR_SRC);
1405 ++ectx.ec_stack.ga_len;
1406 break;
1407
1408 // store local variable
1409 case ISN_STORE:
1410 --ectx.ec_stack.ga_len;
1411 tv = STACK_TV_VAR(iptr->isn_arg.number);
1412 clear_tv(tv);
1413 *tv = *STACK_TV_BOT(0);
1414 break;
1415
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001416 // store variable or argument in outer scope
1417 case ISN_STOREOUTER:
1418 --ectx.ec_stack.ga_len;
1419 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1420 clear_tv(tv);
1421 *tv = *STACK_TV_BOT(0);
1422 break;
1423
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001424 // store s: variable in old script
1425 case ISN_STORES:
1426 {
1427 hashtab_T *ht = &SCRIPT_VARS(
1428 iptr->isn_arg.loadstore.ls_sid);
1429 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001430 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001431
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001432 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001433 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001434 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001435 else
1436 {
1437 clear_tv(&di->di_tv);
1438 di->di_tv = *STACK_TV_BOT(0);
1439 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001440 }
1441 break;
1442
1443 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 case ISN_STORESCRIPT:
1445 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001446 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001447 iptr->isn_arg.script.script_sid);
1448 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1449 + iptr->isn_arg.script.script_idx;
1450
1451 --ectx.ec_stack.ga_len;
1452 clear_tv(sv->sv_tv);
1453 *sv->sv_tv = *STACK_TV_BOT(0);
1454 }
1455 break;
1456
1457 // store option
1458 case ISN_STOREOPT:
1459 {
1460 long n = 0;
1461 char_u *s = NULL;
1462 char *msg;
1463
1464 --ectx.ec_stack.ga_len;
1465 tv = STACK_TV_BOT(0);
1466 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001467 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001469 if (s == NULL)
1470 s = (char_u *)"";
1471 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001473 // must be VAR_NUMBER, CHECKTYPE makes sure
1474 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1476 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001477 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 if (msg != NULL)
1479 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001480 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 }
1485 break;
1486
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001487 // store $ENV
1488 case ISN_STOREENV:
1489 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001490 tv = STACK_TV_BOT(0);
1491 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1492 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001493 break;
1494
1495 // store @r
1496 case ISN_STOREREG:
1497 {
1498 int reg = iptr->isn_arg.number;
1499
1500 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001501 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001502 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001503 tv_get_string(tv), -1, FALSE);
1504 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001505 }
1506 break;
1507
1508 // store v: variable
1509 case ISN_STOREV:
1510 --ectx.ec_stack.ga_len;
1511 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1512 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001513 // should not happen, type is checked when compiling
1514 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001515 break;
1516
Bram Moolenaard3aac292020-04-19 14:32:17 +02001517 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001519 case ISN_STOREB:
1520 case ISN_STOREW:
1521 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 {
1523 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001524 hashtab_T *ht;
1525 switch (iptr->isn_type)
1526 {
1527 case ISN_STOREG:
1528 ht = get_globvar_ht();
1529 break;
1530 case ISN_STOREB:
1531 ht = &curbuf->b_vars->dv_hashtab;
1532 break;
1533 case ISN_STOREW:
1534 ht = &curwin->w_vars->dv_hashtab;
1535 break;
1536 case ISN_STORET:
1537 ht = &curtab->tp_vars->dv_hashtab;
1538 break;
1539 default: // Cannot reach here
1540 goto failed;
1541 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542
1543 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001544 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001545 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001546 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 else
1548 {
1549 clear_tv(&di->di_tv);
1550 di->di_tv = *STACK_TV_BOT(0);
1551 }
1552 }
1553 break;
1554
1555 // store number in local variable
1556 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001557 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 clear_tv(tv);
1559 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001560 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 break;
1562
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001563 // store value in list variable
1564 case ISN_STORELIST:
1565 {
1566 typval_T *tv_idx = STACK_TV_BOT(-2);
1567 varnumber_T lidx = tv_idx->vval.v_number;
1568 typval_T *tv_list = STACK_TV_BOT(-1);
1569 list_T *list = tv_list->vval.v_list;
1570
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001572 if (lidx < 0 && list->lv_len + lidx >= 0)
1573 // negative index is relative to the end
1574 lidx = list->lv_len + lidx;
1575 if (lidx < 0 || lidx > list->lv_len)
1576 {
1577 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001578 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001579 }
1580 tv = STACK_TV_BOT(-3);
1581 if (lidx < list->lv_len)
1582 {
1583 listitem_T *li = list_find(list, lidx);
1584
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001585 if (error_if_locked(li->li_tv.v_lock,
1586 e_cannot_change_list_item))
1587 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001588 // overwrite existing list item
1589 clear_tv(&li->li_tv);
1590 li->li_tv = *tv;
1591 }
1592 else
1593 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001594 if (error_if_locked(list->lv_lock,
1595 e_cannot_change_list))
1596 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001597 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001598 if (list_append_tv(list, tv) == FAIL)
1599 goto failed;
1600 clear_tv(tv);
1601 }
1602 clear_tv(tv_idx);
1603 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001604 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001605 }
1606 break;
1607
1608 // store value in dict variable
1609 case ISN_STOREDICT:
1610 {
1611 typval_T *tv_key = STACK_TV_BOT(-2);
1612 char_u *key = tv_key->vval.v_string;
1613 typval_T *tv_dict = STACK_TV_BOT(-1);
1614 dict_T *dict = tv_dict->vval.v_dict;
1615 dictitem_T *di;
1616
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001617 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001618 if (dict == NULL)
1619 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001620 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001621 goto on_error;
1622 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001623 if (key == NULL)
1624 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001625 tv = STACK_TV_BOT(-3);
1626 di = dict_find(dict, key, -1);
1627 if (di != NULL)
1628 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001629 if (error_if_locked(di->di_tv.v_lock,
1630 e_cannot_change_dict_item))
1631 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001632 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001633 clear_tv(&di->di_tv);
1634 di->di_tv = *tv;
1635 }
1636 else
1637 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001638 if (error_if_locked(dict->dv_lock,
1639 e_cannot_change_dict))
1640 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001641 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001642 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1643 goto failed;
1644 clear_tv(tv);
1645 }
1646 clear_tv(tv_key);
1647 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001648 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001649 }
1650 break;
1651
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 // push constant
1653 case ISN_PUSHNR:
1654 case ISN_PUSHBOOL:
1655 case ISN_PUSHSPEC:
1656 case ISN_PUSHF:
1657 case ISN_PUSHS:
1658 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001659 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001660 case ISN_PUSHCHANNEL:
1661 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001662 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 goto failed;
1664 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001665 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 ++ectx.ec_stack.ga_len;
1667 switch (iptr->isn_type)
1668 {
1669 case ISN_PUSHNR:
1670 tv->v_type = VAR_NUMBER;
1671 tv->vval.v_number = iptr->isn_arg.number;
1672 break;
1673 case ISN_PUSHBOOL:
1674 tv->v_type = VAR_BOOL;
1675 tv->vval.v_number = iptr->isn_arg.number;
1676 break;
1677 case ISN_PUSHSPEC:
1678 tv->v_type = VAR_SPECIAL;
1679 tv->vval.v_number = iptr->isn_arg.number;
1680 break;
1681#ifdef FEAT_FLOAT
1682 case ISN_PUSHF:
1683 tv->v_type = VAR_FLOAT;
1684 tv->vval.v_float = iptr->isn_arg.fnumber;
1685 break;
1686#endif
1687 case ISN_PUSHBLOB:
1688 blob_copy(iptr->isn_arg.blob, tv);
1689 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001690 case ISN_PUSHFUNC:
1691 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001692 if (iptr->isn_arg.string == NULL)
1693 tv->vval.v_string = NULL;
1694 else
1695 tv->vval.v_string =
1696 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001697 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001698 case ISN_PUSHCHANNEL:
1699#ifdef FEAT_JOB_CHANNEL
1700 tv->v_type = VAR_CHANNEL;
1701 tv->vval.v_channel = iptr->isn_arg.channel;
1702 if (tv->vval.v_channel != NULL)
1703 ++tv->vval.v_channel->ch_refcount;
1704#endif
1705 break;
1706 case ISN_PUSHJOB:
1707#ifdef FEAT_JOB_CHANNEL
1708 tv->v_type = VAR_JOB;
1709 tv->vval.v_job = iptr->isn_arg.job;
1710 if (tv->vval.v_job != NULL)
1711 ++tv->vval.v_job->jv_refcount;
1712#endif
1713 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 default:
1715 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001716 tv->vval.v_string = vim_strsave(
1717 iptr->isn_arg.string == NULL
1718 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 }
1720 break;
1721
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001722 case ISN_UNLET:
1723 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1724 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001725 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001726 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001727 case ISN_UNLETENV:
1728 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1729 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001730
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001731 case ISN_LOCKCONST:
1732 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1733 break;
1734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 // create a list from items on the stack; uses a single allocation
1736 // for the list header and the items
1737 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001738 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1739 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740 break;
1741
1742 // create a dict from items on the stack
1743 case ISN_NEWDICT:
1744 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001745 int count = iptr->isn_arg.number;
1746 dict_T *dict = dict_alloc();
1747 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001748 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749
1750 if (dict == NULL)
1751 goto failed;
1752 for (idx = 0; idx < count; ++idx)
1753 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001754 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001756 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001757 key = tv->vval.v_string == NULL
1758 ? (char_u *)"" : tv->vval.v_string;
1759 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001760 if (item != NULL)
1761 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001762 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001763 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001764 dict_unref(dict);
1765 goto on_error;
1766 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001767 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768 clear_tv(tv);
1769 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001770 {
1771 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1775 item->di_tv.v_lock = 0;
1776 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001777 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001778 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001779 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 }
1783
1784 if (count > 0)
1785 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001786 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 goto failed;
1788 else
1789 ++ectx.ec_stack.ga_len;
1790 tv = STACK_TV_BOT(-1);
1791 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001792 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 tv->vval.v_dict = dict;
1794 ++dict->dv_refcount;
1795 }
1796 break;
1797
1798 // call a :def function
1799 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1802 iptr->isn_arg.dfunc.cdf_argcount,
1803 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001804 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 break;
1806
1807 // call a builtin function
1808 case ISN_BCALL:
1809 SOURCING_LNUM = iptr->isn_lnum;
1810 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1811 iptr->isn_arg.bfunc.cbf_argcount,
1812 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001813 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001814 break;
1815
1816 // call a funcref or partial
1817 case ISN_PCALL:
1818 {
1819 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1820 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001821 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822
1823 SOURCING_LNUM = iptr->isn_lnum;
1824 if (pfunc->cpf_top)
1825 {
1826 // funcref is above the arguments
1827 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1828 }
1829 else
1830 {
1831 // Get the funcref from the stack.
1832 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001833 partial_tv = *STACK_TV_BOT(0);
1834 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 }
1836 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001837 if (tv == &partial_tv)
1838 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001840 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842 break;
1843
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001844 case ISN_PCALL_END:
1845 // PCALL finished, arguments have been consumed and replaced by
1846 // the return value. Now clear the funcref from the stack,
1847 // and move the return value in its place.
1848 --ectx.ec_stack.ga_len;
1849 clear_tv(STACK_TV_BOT(-1));
1850 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1851 break;
1852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 // call a user defined function or funcref/partial
1854 case ISN_UCALL:
1855 {
1856 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1857
1858 SOURCING_LNUM = iptr->isn_lnum;
1859 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001860 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001861 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001862 }
1863 break;
1864
1865 // return from a :def function call
1866 case ISN_RETURN:
1867 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001868 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001869 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001870
1871 if (trystack->ga_len > 0)
1872 trycmd = ((trycmd_T *)trystack->ga_data)
1873 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001874 if (trycmd != NULL
1875 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 && trycmd->tcd_finally_idx != 0)
1877 {
1878 // jump to ":finally"
1879 ectx.ec_iidx = trycmd->tcd_finally_idx;
1880 trycmd->tcd_return = TRUE;
1881 }
1882 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001883 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 }
1885 break;
1886
1887 // push a function reference to a compiled function
1888 case ISN_FUNCREF:
1889 {
1890 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001891 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892
1893 pt = ALLOC_CLEAR_ONE(partial_T);
1894 if (pt == NULL)
1895 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001896 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001897 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001898 vim_free(pt);
1899 goto failed;
1900 }
1901 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1902 + iptr->isn_arg.funcref.fr_func;
1903 pt->pt_func = pt_dfunc->df_ufunc;
1904 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001905
1906 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1907 {
1908 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1909 + ectx.ec_dfunc_idx;
1910
1911 // The closure needs to find arguments and local
1912 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001913 pt->pt_ectx_stack = &ectx.ec_stack;
1914 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001915
1916 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001917 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001918 // (arguments and local variables). Store a reference
1919 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001920 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001921 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001922 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001923 goto failed;
1924 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001925 // Extra variable keeps the count of closures created
1926 // in the current function call.
1927 tv = STACK_TV_VAR(dfunc->df_varcount);
1928 ++tv->vval.v_number;
1929
1930 ((partial_T **)ectx.ec_funcrefs.ga_data)
1931 [ectx.ec_funcrefs.ga_len] = pt;
1932 ++pt->pt_refcount;
1933 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001934 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001935 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 tv = STACK_TV_BOT(0);
1938 ++ectx.ec_stack.ga_len;
1939 tv->vval.v_partial = pt;
1940 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001941 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 }
1943 break;
1944
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001945 // Create a global function from a lambda.
1946 case ISN_NEWFUNC:
1947 {
1948 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1949
1950 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1951 }
1952 break;
1953
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 // jump if a condition is met
1955 case ISN_JUMP:
1956 {
1957 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001958 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 int jump = TRUE;
1960
1961 if (when != JUMP_ALWAYS)
1962 {
1963 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001964 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001965 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001966 || when == JUMP_IF_COND_TRUE)
1967 {
1968 SOURCING_LNUM = iptr->isn_lnum;
1969 jump = tv_get_bool_chk(tv, &error);
1970 if (error)
1971 goto on_error;
1972 }
1973 else
1974 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001976 || when == JUMP_AND_KEEP_IF_FALSE
1977 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001979 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 {
1981 // drop the value from the stack
1982 clear_tv(tv);
1983 --ectx.ec_stack.ga_len;
1984 }
1985 }
1986 if (jump)
1987 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1988 }
1989 break;
1990
1991 // top of a for loop
1992 case ISN_FOR:
1993 {
1994 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1995 typval_T *idxtv =
1996 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1997
1998 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001999 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002001 ++idxtv->vval.v_number;
2002 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 // past the end of the list, jump to "endfor"
2004 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2005 else if (list->lv_first == &range_list_item)
2006 {
2007 // non-materialized range() list
2008 tv = STACK_TV_BOT(0);
2009 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002010 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 tv->vval.v_number = list_find_nr(
2012 list, idxtv->vval.v_number, NULL);
2013 ++ectx.ec_stack.ga_len;
2014 }
2015 else
2016 {
2017 listitem_T *li = list_find(list, idxtv->vval.v_number);
2018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2020 ++ectx.ec_stack.ga_len;
2021 }
2022 }
2023 break;
2024
2025 // start of ":try" block
2026 case ISN_TRY:
2027 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002028 trycmd_T *trycmd = NULL;
2029
Bram Moolenaar270d0382020-05-15 21:42:53 +02002030 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 goto failed;
2032 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2033 + ectx.ec_trystack.ga_len;
2034 ++ectx.ec_trystack.ga_len;
2035 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002036 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2038 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002039 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002040 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 }
2042 break;
2043
2044 case ISN_PUSHEXC:
2045 if (current_exception == NULL)
2046 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002047 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 iemsg("Evaluating catch while current_exception is NULL");
2049 goto failed;
2050 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002051 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 goto failed;
2053 tv = STACK_TV_BOT(0);
2054 ++ectx.ec_stack.ga_len;
2055 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002056 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 tv->vval.v_string = vim_strsave(
2058 (char_u *)current_exception->value);
2059 break;
2060
2061 case ISN_CATCH:
2062 {
2063 garray_T *trystack = &ectx.ec_trystack;
2064
2065 if (trystack->ga_len > 0)
2066 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002067 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 + trystack->ga_len - 1;
2069 trycmd->tcd_caught = TRUE;
2070 }
2071 did_emsg = got_int = did_throw = FALSE;
2072 catch_exception(current_exception);
2073 }
2074 break;
2075
2076 // end of ":try" block
2077 case ISN_ENDTRY:
2078 {
2079 garray_T *trystack = &ectx.ec_trystack;
2080
2081 if (trystack->ga_len > 0)
2082 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002083 trycmd_T *trycmd = NULL;
2084
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 --trystack->ga_len;
2086 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002087 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 trycmd = ((trycmd_T *)trystack->ga_data)
2089 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002090 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002091 {
2092 // discard the exception
2093 if (caught_stack == current_exception)
2094 caught_stack = caught_stack->caught;
2095 discard_current_exception();
2096 }
2097
2098 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002099 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 }
2101 }
2102 break;
2103
2104 case ISN_THROW:
2105 --ectx.ec_stack.ga_len;
2106 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002107 if (tv->vval.v_string == NULL
2108 || *skipwhite(tv->vval.v_string) == NUL)
2109 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002110 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002112 emsg(_(e_throw_with_empty_string));
2113 goto failed;
2114 }
2115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2117 {
2118 vim_free(tv->vval.v_string);
2119 goto failed;
2120 }
2121 did_throw = TRUE;
2122 break;
2123
2124 // compare with special values
2125 case ISN_COMPAREBOOL:
2126 case ISN_COMPARESPECIAL:
2127 {
2128 typval_T *tv1 = STACK_TV_BOT(-2);
2129 typval_T *tv2 = STACK_TV_BOT(-1);
2130 varnumber_T arg1 = tv1->vval.v_number;
2131 varnumber_T arg2 = tv2->vval.v_number;
2132 int res;
2133
2134 switch (iptr->isn_arg.op.op_type)
2135 {
2136 case EXPR_EQUAL: res = arg1 == arg2; break;
2137 case EXPR_NEQUAL: res = arg1 != arg2; break;
2138 default: res = 0; break;
2139 }
2140
2141 --ectx.ec_stack.ga_len;
2142 tv1->v_type = VAR_BOOL;
2143 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2144 }
2145 break;
2146
2147 // Operation with two number arguments
2148 case ISN_OPNR:
2149 case ISN_COMPARENR:
2150 {
2151 typval_T *tv1 = STACK_TV_BOT(-2);
2152 typval_T *tv2 = STACK_TV_BOT(-1);
2153 varnumber_T arg1 = tv1->vval.v_number;
2154 varnumber_T arg2 = tv2->vval.v_number;
2155 varnumber_T res;
2156
2157 switch (iptr->isn_arg.op.op_type)
2158 {
2159 case EXPR_MULT: res = arg1 * arg2; break;
2160 case EXPR_DIV: res = arg1 / arg2; break;
2161 case EXPR_REM: res = arg1 % arg2; break;
2162 case EXPR_SUB: res = arg1 - arg2; break;
2163 case EXPR_ADD: res = arg1 + arg2; break;
2164
2165 case EXPR_EQUAL: res = arg1 == arg2; break;
2166 case EXPR_NEQUAL: res = arg1 != arg2; break;
2167 case EXPR_GREATER: res = arg1 > arg2; break;
2168 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2169 case EXPR_SMALLER: res = arg1 < arg2; break;
2170 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2171 default: res = 0; break;
2172 }
2173
2174 --ectx.ec_stack.ga_len;
2175 if (iptr->isn_type == ISN_COMPARENR)
2176 {
2177 tv1->v_type = VAR_BOOL;
2178 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2179 }
2180 else
2181 tv1->vval.v_number = res;
2182 }
2183 break;
2184
2185 // Computation with two float arguments
2186 case ISN_OPFLOAT:
2187 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002188#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 {
2190 typval_T *tv1 = STACK_TV_BOT(-2);
2191 typval_T *tv2 = STACK_TV_BOT(-1);
2192 float_T arg1 = tv1->vval.v_float;
2193 float_T arg2 = tv2->vval.v_float;
2194 float_T res = 0;
2195 int cmp = FALSE;
2196
2197 switch (iptr->isn_arg.op.op_type)
2198 {
2199 case EXPR_MULT: res = arg1 * arg2; break;
2200 case EXPR_DIV: res = arg1 / arg2; break;
2201 case EXPR_SUB: res = arg1 - arg2; break;
2202 case EXPR_ADD: res = arg1 + arg2; break;
2203
2204 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2205 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2206 case EXPR_GREATER: cmp = arg1 > arg2; break;
2207 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2208 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2209 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2210 default: cmp = 0; break;
2211 }
2212 --ectx.ec_stack.ga_len;
2213 if (iptr->isn_type == ISN_COMPAREFLOAT)
2214 {
2215 tv1->v_type = VAR_BOOL;
2216 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2217 }
2218 else
2219 tv1->vval.v_float = res;
2220 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002221#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 break;
2223
2224 case ISN_COMPARELIST:
2225 {
2226 typval_T *tv1 = STACK_TV_BOT(-2);
2227 typval_T *tv2 = STACK_TV_BOT(-1);
2228 list_T *arg1 = tv1->vval.v_list;
2229 list_T *arg2 = tv2->vval.v_list;
2230 int cmp = FALSE;
2231 int ic = iptr->isn_arg.op.op_ic;
2232
2233 switch (iptr->isn_arg.op.op_type)
2234 {
2235 case EXPR_EQUAL: cmp =
2236 list_equal(arg1, arg2, ic, FALSE); break;
2237 case EXPR_NEQUAL: cmp =
2238 !list_equal(arg1, arg2, ic, FALSE); break;
2239 case EXPR_IS: cmp = arg1 == arg2; break;
2240 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2241 default: cmp = 0; break;
2242 }
2243 --ectx.ec_stack.ga_len;
2244 clear_tv(tv1);
2245 clear_tv(tv2);
2246 tv1->v_type = VAR_BOOL;
2247 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2248 }
2249 break;
2250
2251 case ISN_COMPAREBLOB:
2252 {
2253 typval_T *tv1 = STACK_TV_BOT(-2);
2254 typval_T *tv2 = STACK_TV_BOT(-1);
2255 blob_T *arg1 = tv1->vval.v_blob;
2256 blob_T *arg2 = tv2->vval.v_blob;
2257 int cmp = FALSE;
2258
2259 switch (iptr->isn_arg.op.op_type)
2260 {
2261 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2262 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2263 case EXPR_IS: cmp = arg1 == arg2; break;
2264 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2265 default: cmp = 0; break;
2266 }
2267 --ectx.ec_stack.ga_len;
2268 clear_tv(tv1);
2269 clear_tv(tv2);
2270 tv1->v_type = VAR_BOOL;
2271 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2272 }
2273 break;
2274
2275 // TODO: handle separately
2276 case ISN_COMPARESTRING:
2277 case ISN_COMPAREDICT:
2278 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 case ISN_COMPAREANY:
2280 {
2281 typval_T *tv1 = STACK_TV_BOT(-2);
2282 typval_T *tv2 = STACK_TV_BOT(-1);
2283 exptype_T exptype = iptr->isn_arg.op.op_type;
2284 int ic = iptr->isn_arg.op.op_ic;
2285
Bram Moolenaareb26f432020-09-14 16:50:05 +02002286 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 typval_compare(tv1, tv2, exptype, ic);
2288 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 --ectx.ec_stack.ga_len;
2290 }
2291 break;
2292
2293 case ISN_ADDLIST:
2294 case ISN_ADDBLOB:
2295 {
2296 typval_T *tv1 = STACK_TV_BOT(-2);
2297 typval_T *tv2 = STACK_TV_BOT(-1);
2298
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002299 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 if (iptr->isn_type == ISN_ADDLIST)
2301 eval_addlist(tv1, tv2);
2302 else
2303 eval_addblob(tv1, tv2);
2304 clear_tv(tv2);
2305 --ectx.ec_stack.ga_len;
2306 }
2307 break;
2308
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002309 case ISN_LISTAPPEND:
2310 {
2311 typval_T *tv1 = STACK_TV_BOT(-2);
2312 typval_T *tv2 = STACK_TV_BOT(-1);
2313 list_T *l = tv1->vval.v_list;
2314
2315 // add an item to a list
2316 if (l == NULL)
2317 {
2318 SOURCING_LNUM = iptr->isn_lnum;
2319 emsg(_(e_cannot_add_to_null_list));
2320 goto on_error;
2321 }
2322 if (list_append_tv(l, tv2) == FAIL)
2323 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002324 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002325 --ectx.ec_stack.ga_len;
2326 }
2327 break;
2328
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002329 case ISN_BLOBAPPEND:
2330 {
2331 typval_T *tv1 = STACK_TV_BOT(-2);
2332 typval_T *tv2 = STACK_TV_BOT(-1);
2333 blob_T *b = tv1->vval.v_blob;
2334 int error = FALSE;
2335 varnumber_T n;
2336
2337 // add a number to a blob
2338 if (b == NULL)
2339 {
2340 SOURCING_LNUM = iptr->isn_lnum;
2341 emsg(_(e_cannot_add_to_null_blob));
2342 goto on_error;
2343 }
2344 n = tv_get_number_chk(tv2, &error);
2345 if (error)
2346 goto on_error;
2347 ga_append(&b->bv_ga, (int)n);
2348 --ectx.ec_stack.ga_len;
2349 }
2350 break;
2351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 // Computation with two arguments of unknown type
2353 case ISN_OPANY:
2354 {
2355 typval_T *tv1 = STACK_TV_BOT(-2);
2356 typval_T *tv2 = STACK_TV_BOT(-1);
2357 varnumber_T n1, n2;
2358#ifdef FEAT_FLOAT
2359 float_T f1 = 0, f2 = 0;
2360#endif
2361 int error = FALSE;
2362
2363 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2364 {
2365 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2366 {
2367 eval_addlist(tv1, tv2);
2368 clear_tv(tv2);
2369 --ectx.ec_stack.ga_len;
2370 break;
2371 }
2372 else if (tv1->v_type == VAR_BLOB
2373 && tv2->v_type == VAR_BLOB)
2374 {
2375 eval_addblob(tv1, tv2);
2376 clear_tv(tv2);
2377 --ectx.ec_stack.ga_len;
2378 break;
2379 }
2380 }
2381#ifdef FEAT_FLOAT
2382 if (tv1->v_type == VAR_FLOAT)
2383 {
2384 f1 = tv1->vval.v_float;
2385 n1 = 0;
2386 }
2387 else
2388#endif
2389 {
2390 n1 = tv_get_number_chk(tv1, &error);
2391 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002392 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393#ifdef FEAT_FLOAT
2394 if (tv2->v_type == VAR_FLOAT)
2395 f1 = n1;
2396#endif
2397 }
2398#ifdef FEAT_FLOAT
2399 if (tv2->v_type == VAR_FLOAT)
2400 {
2401 f2 = tv2->vval.v_float;
2402 n2 = 0;
2403 }
2404 else
2405#endif
2406 {
2407 n2 = tv_get_number_chk(tv2, &error);
2408 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002409 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410#ifdef FEAT_FLOAT
2411 if (tv1->v_type == VAR_FLOAT)
2412 f2 = n2;
2413#endif
2414 }
2415#ifdef FEAT_FLOAT
2416 // if there is a float on either side the result is a float
2417 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2418 {
2419 switch (iptr->isn_arg.op.op_type)
2420 {
2421 case EXPR_MULT: f1 = f1 * f2; break;
2422 case EXPR_DIV: f1 = f1 / f2; break;
2423 case EXPR_SUB: f1 = f1 - f2; break;
2424 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002425 default: SOURCING_LNUM = iptr->isn_lnum;
2426 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002427 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429 clear_tv(tv1);
2430 clear_tv(tv2);
2431 tv1->v_type = VAR_FLOAT;
2432 tv1->vval.v_float = f1;
2433 --ectx.ec_stack.ga_len;
2434 }
2435 else
2436#endif
2437 {
2438 switch (iptr->isn_arg.op.op_type)
2439 {
2440 case EXPR_MULT: n1 = n1 * n2; break;
2441 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2442 case EXPR_SUB: n1 = n1 - n2; break;
2443 case EXPR_ADD: n1 = n1 + n2; break;
2444 default: n1 = num_modulus(n1, n2); break;
2445 }
2446 clear_tv(tv1);
2447 clear_tv(tv2);
2448 tv1->v_type = VAR_NUMBER;
2449 tv1->vval.v_number = n1;
2450 --ectx.ec_stack.ga_len;
2451 }
2452 }
2453 break;
2454
2455 case ISN_CONCAT:
2456 {
2457 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2458 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2459 char_u *res;
2460
2461 res = concat_str(str1, str2);
2462 clear_tv(STACK_TV_BOT(-2));
2463 clear_tv(STACK_TV_BOT(-1));
2464 --ectx.ec_stack.ga_len;
2465 STACK_TV_BOT(-1)->vval.v_string = res;
2466 }
2467 break;
2468
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002469 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002470 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002471 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002472 int is_slice = iptr->isn_type == ISN_STRSLICE;
2473 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002474 char_u *res;
2475
2476 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002477 // string slice: string is at stack-3, first index at
2478 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002479 if (is_slice)
2480 {
2481 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002482 n1 = tv->vval.v_number;
2483 }
2484
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002485 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002486 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002487
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002488 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002489 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002490 if (is_slice)
2491 // Slice: Select the characters from the string
2492 res = string_slice(tv->vval.v_string, n1, n2);
2493 else
2494 // Index: The resulting variable is a string of a
2495 // single character. If the index is too big or
2496 // negative the result is empty.
2497 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002498 vim_free(tv->vval.v_string);
2499 tv->vval.v_string = res;
2500 }
2501 break;
2502
2503 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002504 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 {
Bram Moolenaared591872020-08-15 22:14:53 +02002506 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002508 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509
2510 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002511 // list slice: list is at stack-3, indexes at stack-2 and
2512 // stack-1
2513 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 list = tv->vval.v_list;
2515
2516 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002517 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002519
2520 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 {
Bram Moolenaared591872020-08-15 22:14:53 +02002522 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002523 n1 = tv->vval.v_number;
2524 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002525 }
Bram Moolenaared591872020-08-15 22:14:53 +02002526
2527 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002528 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002530 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2531 == FAIL)
2532 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 }
2534 break;
2535
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002536 case ISN_ANYINDEX:
2537 case ISN_ANYSLICE:
2538 {
2539 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2540 typval_T *var1, *var2;
2541 int res;
2542
2543 // index: composite is at stack-2, index at stack-1
2544 // slice: composite is at stack-3, indexes at stack-2 and
2545 // stack-1
2546 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002547 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002548 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2549 goto on_error;
2550 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2551 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2552 res = eval_index_inner(tv, is_slice,
2553 var1, var2, NULL, -1, TRUE);
2554 clear_tv(var1);
2555 if (is_slice)
2556 clear_tv(var2);
2557 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2558 if (res == FAIL)
2559 goto on_error;
2560 }
2561 break;
2562
Bram Moolenaar9af78762020-06-16 11:34:42 +02002563 case ISN_SLICE:
2564 {
2565 list_T *list;
2566 int count = iptr->isn_arg.number;
2567
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002568 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002569 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002570 list = tv->vval.v_list;
2571
2572 // no error for short list, expect it to be checked earlier
2573 if (list != NULL && list->lv_len >= count)
2574 {
2575 list_T *newlist = list_slice(list,
2576 count, list->lv_len - 1);
2577
2578 if (newlist != NULL)
2579 {
2580 list_unref(list);
2581 tv->vval.v_list = newlist;
2582 ++newlist->lv_refcount;
2583 }
2584 }
2585 }
2586 break;
2587
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002588 case ISN_GETITEM:
2589 {
2590 listitem_T *li;
2591 int index = iptr->isn_arg.number;
2592
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002593 // Get list item: list is at stack-1, push item.
2594 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002595 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002596 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002597
2598 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2599 goto failed;
2600 ++ectx.ec_stack.ga_len;
2601 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2602 }
2603 break;
2604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 case ISN_MEMBER:
2606 {
2607 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002608 char_u *key;
2609 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002610 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002611
2612 // dict member: dict is at stack-2, key at stack-1
2613 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002614 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002615 dict = tv->vval.v_dict;
2616
2617 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002618 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002619 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002620 if (key == NULL)
2621 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002622
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002623 if ((di = dict_find(dict, key, -1)) == NULL)
2624 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002625 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002626 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002627 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002628 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002629 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002630 --ectx.ec_stack.ga_len;
2631 // Clear the dict after getting the item, to avoid that it
2632 // make the item invalid.
2633 tv = STACK_TV_BOT(-1);
2634 temp_tv = *tv;
2635 copy_tv(&di->di_tv, tv);
2636 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002637 }
2638 break;
2639
2640 // dict member with string key
2641 case ISN_STRINGMEMBER:
2642 {
2643 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002645 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646
2647 tv = STACK_TV_BOT(-1);
2648 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2649 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002650 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002652 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002653 }
2654 dict = tv->vval.v_dict;
2655
2656 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2657 == NULL)
2658 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002661 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002663 // Clear the dict after getting the item, to avoid that it
2664 // make the item invalid.
2665 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002667 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 }
2669 break;
2670
2671 case ISN_NEGATENR:
2672 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002673 if (tv->v_type != VAR_NUMBER
2674#ifdef FEAT_FLOAT
2675 && tv->v_type != VAR_FLOAT
2676#endif
2677 )
2678 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002679 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002680 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002681 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002682 }
2683#ifdef FEAT_FLOAT
2684 if (tv->v_type == VAR_FLOAT)
2685 tv->vval.v_float = -tv->vval.v_float;
2686 else
2687#endif
2688 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 break;
2690
2691 case ISN_CHECKNR:
2692 {
2693 int error = FALSE;
2694
2695 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002696 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002698 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 (void)tv_get_number_chk(tv, &error);
2700 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002701 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 }
2703 break;
2704
2705 case ISN_CHECKTYPE:
2706 {
2707 checktype_T *ct = &iptr->isn_arg.type;
2708
2709 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002710 SOURCING_LNUM = iptr->isn_lnum;
2711 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2712 goto on_error;
2713
2714 // number 0 is FALSE, number 1 is TRUE
2715 if (tv->v_type == VAR_NUMBER
2716 && ct->ct_type->tt_type == VAR_BOOL
2717 && (tv->vval.v_number == 0
2718 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002719 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002720 tv->v_type = VAR_BOOL;
2721 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002722 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 }
2724 }
2725 break;
2726
Bram Moolenaar9af78762020-06-16 11:34:42 +02002727 case ISN_CHECKLEN:
2728 {
2729 int min_len = iptr->isn_arg.checklen.cl_min_len;
2730 list_T *list = NULL;
2731
2732 tv = STACK_TV_BOT(-1);
2733 if (tv->v_type == VAR_LIST)
2734 list = tv->vval.v_list;
2735 if (list == NULL || list->lv_len < min_len
2736 || (list->lv_len > min_len
2737 && !iptr->isn_arg.checklen.cl_more_OK))
2738 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002739 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002740 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002741 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002742 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002743 }
2744 }
2745 break;
2746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002747 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002748 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 {
2750 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002751 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752
2753 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002754 if (iptr->isn_type == ISN_2BOOL)
2755 {
2756 n = tv2bool(tv);
2757 if (iptr->isn_arg.number) // invert
2758 n = !n;
2759 }
2760 else
2761 {
2762 SOURCING_LNUM = iptr->isn_lnum;
2763 n = tv_get_bool_chk(tv, &error);
2764 if (error)
2765 goto on_error;
2766 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 clear_tv(tv);
2768 tv->v_type = VAR_BOOL;
2769 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2770 }
2771 break;
2772
2773 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002774 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002775 {
2776 char_u *str;
2777
2778 tv = STACK_TV_BOT(iptr->isn_arg.number);
2779 if (tv->v_type != VAR_STRING)
2780 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002781 if (iptr->isn_type == ISN_2STRING_ANY)
2782 {
2783 switch (tv->v_type)
2784 {
2785 case VAR_SPECIAL:
2786 case VAR_BOOL:
2787 case VAR_NUMBER:
2788 case VAR_FLOAT:
2789 case VAR_BLOB: break;
2790 default: to_string_error(tv->v_type);
2791 goto on_error;
2792 }
2793 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 str = typval_tostring(tv);
2795 clear_tv(tv);
2796 tv->v_type = VAR_STRING;
2797 tv->vval.v_string = str;
2798 }
2799 }
2800 break;
2801
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002802 case ISN_PUT:
2803 {
2804 int regname = iptr->isn_arg.put.put_regname;
2805 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2806 char_u *expr = NULL;
2807 int dir = FORWARD;
2808
2809 if (regname == '=')
2810 {
2811 tv = STACK_TV_BOT(-1);
2812 if (tv->v_type == VAR_STRING)
2813 expr = tv->vval.v_string;
2814 else
2815 {
2816 expr = typval_tostring(tv); // allocates value
2817 clear_tv(tv);
2818 }
2819 --ectx.ec_stack.ga_len;
2820 }
2821 if (lnum == -2)
2822 // :put! above cursor
2823 dir = BACKWARD;
2824 else if (lnum >= 0)
2825 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2826 check_cursor();
2827 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2828 vim_free(expr);
2829 }
2830 break;
2831
Bram Moolenaar02194d22020-10-24 23:08:38 +02002832 case ISN_CMDMOD:
2833 save_cmdmod = cmdmod;
2834 restore_cmdmod = TRUE;
2835 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
2836 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002837 break;
2838
Bram Moolenaar02194d22020-10-24 23:08:38 +02002839 case ISN_CMDMOD_REV:
2840 // filter regprog is owned by the instruction, don't free it
2841 cmdmod.cmod_filter_regmatch.regprog = NULL;
2842 undo_cmdmod(&cmdmod);
2843 cmdmod = save_cmdmod;
2844 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002845 break;
2846
Bram Moolenaar389df252020-07-09 21:20:47 +02002847 case ISN_SHUFFLE:
2848 {
2849 typval_T tmp_tv;
2850 int item = iptr->isn_arg.shuffle.shfl_item;
2851 int up = iptr->isn_arg.shuffle.shfl_up;
2852
2853 tmp_tv = *STACK_TV_BOT(-item);
2854 for ( ; up > 0 && item > 1; --up)
2855 {
2856 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2857 --item;
2858 }
2859 *STACK_TV_BOT(-item) = tmp_tv;
2860 }
2861 break;
2862
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 case ISN_DROP:
2864 --ectx.ec_stack.ga_len;
2865 clear_tv(STACK_TV_BOT(0));
2866 break;
2867 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002868 continue;
2869
Bram Moolenaard032f342020-07-18 18:13:02 +02002870func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002871 // Restore previous function. If the frame pointer is where we started
2872 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002873 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002874 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002875
Bram Moolenaard032f342020-07-18 18:13:02 +02002876 if (func_return(&ectx) == FAIL)
2877 // only fails when out of memory
2878 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002879 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002880
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002881on_error:
Bram Moolenaarcd030c42020-10-30 21:49:40 +01002882 // If "emsg_silent" is set then ignore the error.
2883 if (did_emsg == did_emsg_before && emsg_silent)
2884 continue;
2885
Bram Moolenaar171fb922020-10-28 16:54:47 +01002886 // If we are not inside a try-catch started here, abort execution.
2887 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002888 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 }
2890
2891done:
2892 // function finished, get result from the stack.
2893 tv = STACK_TV_BOT(-1);
2894 *rettv = *tv;
2895 tv->v_type = VAR_UNKNOWN;
2896 ret = OK;
2897
2898failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002899 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002900 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002901 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002902
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002903 // Deal with any remaining closures, they may be in use somewhere.
2904 if (ectx.ec_funcrefs.ga_len > 0)
2905 handle_closure_in_use(&ectx, FALSE);
2906
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002907 estack_pop();
2908 current_sctx = save_current_sctx;
2909
Bram Moolenaar352134b2020-10-17 22:04:08 +02002910 if (*msg_list != NULL && saved_msg_list != NULL)
2911 {
2912 msglist_T **plist = saved_msg_list;
2913
2914 // Append entries from the current msg_list (uncaught exceptions) to
2915 // the saved msg_list.
2916 while (*plist != NULL)
2917 plist = &(*plist)->next;
2918
2919 *plist = *msg_list;
2920 }
2921 msg_list = saved_msg_list;
2922
Bram Moolenaar02194d22020-10-24 23:08:38 +02002923 if (restore_cmdmod)
2924 {
2925 cmdmod.cmod_filter_regmatch.regprog = NULL;
2926 undo_cmdmod(&cmdmod);
2927 cmdmod = save_cmdmod;
2928 }
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02002929
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002930failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002931 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2933 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002934
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002936 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002937
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002938 // Not sure if this is necessary.
2939 suppress_errthrow = save_suppress_errthrow;
2940
Bram Moolenaard66960b2020-10-30 20:46:26 +01002941 if (ret != OK && did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002942 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002943 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 return ret;
2945}
2946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947/*
2948 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002949 * We don't really need this at runtime, but we do have tests that require it,
2950 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002951 */
2952 void
2953ex_disassemble(exarg_T *eap)
2954{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002955 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002956 char_u *fname;
2957 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002958 dfunc_T *dfunc;
2959 isn_T *instr;
2960 int current;
2961 int line_idx = 0;
2962 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002963 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002965 if (STRNCMP(arg, "<lambda>", 8) == 0)
2966 {
2967 arg += 8;
2968 (void)getdigits(&arg);
2969 fname = vim_strnsave(eap->arg, arg - eap->arg);
2970 }
2971 else
2972 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002973 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002974 if (fname == NULL)
2975 {
2976 semsg(_(e_invarg2), eap->arg);
2977 return;
2978 }
2979
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002980 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002981 if (ufunc == NULL)
2982 {
2983 char_u *p = untrans_function_name(fname);
2984
2985 if (p != NULL)
2986 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002987 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002988 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002989 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002990 if (ufunc == NULL)
2991 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002992 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 return;
2994 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002995 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002996 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2997 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002998 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003000 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003001 return;
3002 }
3003 if (ufunc->uf_name_exp != NULL)
3004 msg((char *)ufunc->uf_name_exp);
3005 else
3006 msg((char *)ufunc->uf_name);
3007
3008 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3009 instr = dfunc->df_instr;
3010 for (current = 0; current < dfunc->df_instr_count; ++current)
3011 {
3012 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003013 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014
3015 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3016 {
3017 if (current > prev_current)
3018 {
3019 msg_puts("\n\n");
3020 prev_current = current;
3021 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003022 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3023 if (line != NULL)
3024 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003025 }
3026
3027 switch (iptr->isn_type)
3028 {
3029 case ISN_EXEC:
3030 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3031 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003032 case ISN_EXECCONCAT:
3033 smsg("%4d EXECCONCAT %lld", current,
3034 (long long)iptr->isn_arg.number);
3035 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003036 case ISN_ECHO:
3037 {
3038 echo_T *echo = &iptr->isn_arg.echo;
3039
3040 smsg("%4d %s %d", current,
3041 echo->echo_with_white ? "ECHO" : "ECHON",
3042 echo->echo_count);
3043 }
3044 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003045 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003046 smsg("%4d EXECUTE %lld", current,
3047 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003048 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003049 case ISN_ECHOMSG:
3050 smsg("%4d ECHOMSG %lld", current,
3051 (long long)(iptr->isn_arg.number));
3052 break;
3053 case ISN_ECHOERR:
3054 smsg("%4d ECHOERR %lld", current,
3055 (long long)(iptr->isn_arg.number));
3056 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003058 case ISN_LOADOUTER:
3059 {
3060 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3061
3062 if (iptr->isn_arg.number < 0)
3063 smsg("%4d LOAD%s arg[%lld]", current, add,
3064 (long long)(iptr->isn_arg.number
3065 + STACK_FRAME_SIZE));
3066 else
3067 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003068 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003069 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003070 break;
3071 case ISN_LOADV:
3072 smsg("%4d LOADV v:%s", current,
3073 get_vim_var_name(iptr->isn_arg.number));
3074 break;
3075 case ISN_LOADSCRIPT:
3076 {
3077 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003078 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3080 + iptr->isn_arg.script.script_idx;
3081
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003082 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3083 sv->sv_name,
3084 iptr->isn_arg.script.script_idx,
3085 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003086 }
3087 break;
3088 case ISN_LOADS:
3089 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003090 scriptitem_T *si = SCRIPT_ITEM(
3091 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092
3093 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003094 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 }
3096 break;
3097 case ISN_LOADG:
3098 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3099 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003100 case ISN_LOADB:
3101 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3102 break;
3103 case ISN_LOADW:
3104 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3105 break;
3106 case ISN_LOADT:
3107 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3108 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003109 case ISN_LOADGDICT:
3110 smsg("%4d LOAD g:", current);
3111 break;
3112 case ISN_LOADBDICT:
3113 smsg("%4d LOAD b:", current);
3114 break;
3115 case ISN_LOADWDICT:
3116 smsg("%4d LOAD w:", current);
3117 break;
3118 case ISN_LOADTDICT:
3119 smsg("%4d LOAD t:", current);
3120 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003121 case ISN_LOADOPT:
3122 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3123 break;
3124 case ISN_LOADENV:
3125 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3126 break;
3127 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003128 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 break;
3130
3131 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003132 case ISN_STOREOUTER:
3133 {
3134 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3135
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003136 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003137 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003138 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003139 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003140 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003141 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003142 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003144 case ISN_STOREV:
3145 smsg("%4d STOREV v:%s", current,
3146 get_vim_var_name(iptr->isn_arg.number));
3147 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003148 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003149 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3150 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003151 case ISN_STOREB:
3152 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3153 break;
3154 case ISN_STOREW:
3155 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3156 break;
3157 case ISN_STORET:
3158 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3159 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003160 case ISN_STORES:
3161 {
3162 scriptitem_T *si = SCRIPT_ITEM(
3163 iptr->isn_arg.loadstore.ls_sid);
3164
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003165 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003166 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 break;
3169 case ISN_STORESCRIPT:
3170 {
3171 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003172 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003173 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3174 + iptr->isn_arg.script.script_idx;
3175
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003176 smsg("%4d STORESCRIPT %s-%d in %s", current,
3177 sv->sv_name,
3178 iptr->isn_arg.script.script_idx,
3179 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 }
3181 break;
3182 case ISN_STOREOPT:
3183 smsg("%4d STOREOPT &%s", current,
3184 iptr->isn_arg.storeopt.so_name);
3185 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003186 case ISN_STOREENV:
3187 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3188 break;
3189 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003190 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003191 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 case ISN_STORENR:
3193 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003194 iptr->isn_arg.storenr.stnr_val,
3195 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 break;
3197
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003198 case ISN_STORELIST:
3199 smsg("%4d STORELIST", current);
3200 break;
3201
3202 case ISN_STOREDICT:
3203 smsg("%4d STOREDICT", current);
3204 break;
3205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 // constants
3207 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003208 smsg("%4d PUSHNR %lld", current,
3209 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 break;
3211 case ISN_PUSHBOOL:
3212 case ISN_PUSHSPEC:
3213 smsg("%4d PUSH %s", current,
3214 get_var_special_name(iptr->isn_arg.number));
3215 break;
3216 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003217#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003218 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003219#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 break;
3221 case ISN_PUSHS:
3222 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3223 break;
3224 case ISN_PUSHBLOB:
3225 {
3226 char_u *r;
3227 char_u numbuf[NUMBUFLEN];
3228 char_u *tofree;
3229
3230 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003231 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 vim_free(tofree);
3233 }
3234 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003235 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003236 {
3237 char *name = (char *)iptr->isn_arg.string;
3238
3239 smsg("%4d PUSHFUNC \"%s\"", current,
3240 name == NULL ? "[none]" : name);
3241 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003242 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003243 case ISN_PUSHCHANNEL:
3244#ifdef FEAT_JOB_CHANNEL
3245 {
3246 channel_T *channel = iptr->isn_arg.channel;
3247
3248 smsg("%4d PUSHCHANNEL %d", current,
3249 channel == NULL ? 0 : channel->ch_id);
3250 }
3251#endif
3252 break;
3253 case ISN_PUSHJOB:
3254#ifdef FEAT_JOB_CHANNEL
3255 {
3256 typval_T tv;
3257 char_u *name;
3258
3259 tv.v_type = VAR_JOB;
3260 tv.vval.v_job = iptr->isn_arg.job;
3261 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003262 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003263 }
3264#endif
3265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 case ISN_PUSHEXC:
3267 smsg("%4d PUSH v:exception", current);
3268 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003269 case ISN_UNLET:
3270 smsg("%4d UNLET%s %s", current,
3271 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3272 iptr->isn_arg.unlet.ul_name);
3273 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003274 case ISN_UNLETENV:
3275 smsg("%4d UNLETENV%s $%s", current,
3276 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3277 iptr->isn_arg.unlet.ul_name);
3278 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003279 case ISN_LOCKCONST:
3280 smsg("%4d LOCKCONST", current);
3281 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003283 smsg("%4d NEWLIST size %lld", current,
3284 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 break;
3286 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003287 smsg("%4d NEWDICT size %lld", current,
3288 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 break;
3290
3291 // function call
3292 case ISN_BCALL:
3293 {
3294 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3295
3296 smsg("%4d BCALL %s(argc %d)", current,
3297 internal_func_name(cbfunc->cbf_idx),
3298 cbfunc->cbf_argcount);
3299 }
3300 break;
3301 case ISN_DCALL:
3302 {
3303 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3304 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3305 + cdfunc->cdf_idx;
3306
3307 smsg("%4d DCALL %s(argc %d)", current,
3308 df->df_ufunc->uf_name_exp != NULL
3309 ? df->df_ufunc->uf_name_exp
3310 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3311 }
3312 break;
3313 case ISN_UCALL:
3314 {
3315 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3316
3317 smsg("%4d UCALL %s(argc %d)", current,
3318 cufunc->cuf_name, cufunc->cuf_argcount);
3319 }
3320 break;
3321 case ISN_PCALL:
3322 {
3323 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3324
3325 smsg("%4d PCALL%s (argc %d)", current,
3326 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3327 }
3328 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003329 case ISN_PCALL_END:
3330 smsg("%4d PCALL end", current);
3331 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 case ISN_RETURN:
3333 smsg("%4d RETURN", current);
3334 break;
3335 case ISN_FUNCREF:
3336 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003337 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003338 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003339 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003341 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003342 }
3343 break;
3344
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003345 case ISN_NEWFUNC:
3346 {
3347 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3348
3349 smsg("%4d NEWFUNC %s %s", current,
3350 newfunc->nf_lambda, newfunc->nf_global);
3351 }
3352 break;
3353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354 case ISN_JUMP:
3355 {
3356 char *when = "?";
3357
3358 switch (iptr->isn_arg.jump.jump_when)
3359 {
3360 case JUMP_ALWAYS:
3361 when = "JUMP";
3362 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 case JUMP_AND_KEEP_IF_TRUE:
3364 when = "JUMP_AND_KEEP_IF_TRUE";
3365 break;
3366 case JUMP_IF_FALSE:
3367 when = "JUMP_IF_FALSE";
3368 break;
3369 case JUMP_AND_KEEP_IF_FALSE:
3370 when = "JUMP_AND_KEEP_IF_FALSE";
3371 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003372 case JUMP_IF_COND_FALSE:
3373 when = "JUMP_IF_COND_FALSE";
3374 break;
3375 case JUMP_IF_COND_TRUE:
3376 when = "JUMP_IF_COND_TRUE";
3377 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003379 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003380 iptr->isn_arg.jump.jump_where);
3381 }
3382 break;
3383
3384 case ISN_FOR:
3385 {
3386 forloop_T *forloop = &iptr->isn_arg.forloop;
3387
3388 smsg("%4d FOR $%d -> %d", current,
3389 forloop->for_idx, forloop->for_end);
3390 }
3391 break;
3392
3393 case ISN_TRY:
3394 {
3395 try_T *try = &iptr->isn_arg.try;
3396
3397 smsg("%4d TRY catch -> %d, finally -> %d", current,
3398 try->try_catch, try->try_finally);
3399 }
3400 break;
3401 case ISN_CATCH:
3402 // TODO
3403 smsg("%4d CATCH", current);
3404 break;
3405 case ISN_ENDTRY:
3406 smsg("%4d ENDTRY", current);
3407 break;
3408 case ISN_THROW:
3409 smsg("%4d THROW", current);
3410 break;
3411
3412 // expression operations on number
3413 case ISN_OPNR:
3414 case ISN_OPFLOAT:
3415 case ISN_OPANY:
3416 {
3417 char *what;
3418 char *ins;
3419
3420 switch (iptr->isn_arg.op.op_type)
3421 {
3422 case EXPR_MULT: what = "*"; break;
3423 case EXPR_DIV: what = "/"; break;
3424 case EXPR_REM: what = "%"; break;
3425 case EXPR_SUB: what = "-"; break;
3426 case EXPR_ADD: what = "+"; break;
3427 default: what = "???"; break;
3428 }
3429 switch (iptr->isn_type)
3430 {
3431 case ISN_OPNR: ins = "OPNR"; break;
3432 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3433 case ISN_OPANY: ins = "OPANY"; break;
3434 default: ins = "???"; break;
3435 }
3436 smsg("%4d %s %s", current, ins, what);
3437 }
3438 break;
3439
3440 case ISN_COMPAREBOOL:
3441 case ISN_COMPARESPECIAL:
3442 case ISN_COMPARENR:
3443 case ISN_COMPAREFLOAT:
3444 case ISN_COMPARESTRING:
3445 case ISN_COMPAREBLOB:
3446 case ISN_COMPARELIST:
3447 case ISN_COMPAREDICT:
3448 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 case ISN_COMPAREANY:
3450 {
3451 char *p;
3452 char buf[10];
3453 char *type;
3454
3455 switch (iptr->isn_arg.op.op_type)
3456 {
3457 case EXPR_EQUAL: p = "=="; break;
3458 case EXPR_NEQUAL: p = "!="; break;
3459 case EXPR_GREATER: p = ">"; break;
3460 case EXPR_GEQUAL: p = ">="; break;
3461 case EXPR_SMALLER: p = "<"; break;
3462 case EXPR_SEQUAL: p = "<="; break;
3463 case EXPR_MATCH: p = "=~"; break;
3464 case EXPR_IS: p = "is"; break;
3465 case EXPR_ISNOT: p = "isnot"; break;
3466 case EXPR_NOMATCH: p = "!~"; break;
3467 default: p = "???"; break;
3468 }
3469 STRCPY(buf, p);
3470 if (iptr->isn_arg.op.op_ic == TRUE)
3471 strcat(buf, "?");
3472 switch(iptr->isn_type)
3473 {
3474 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3475 case ISN_COMPARESPECIAL:
3476 type = "COMPARESPECIAL"; break;
3477 case ISN_COMPARENR: type = "COMPARENR"; break;
3478 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3479 case ISN_COMPARESTRING:
3480 type = "COMPARESTRING"; break;
3481 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3482 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3483 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3484 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3486 default: type = "???"; break;
3487 }
3488
3489 smsg("%4d %s %s", current, type, buf);
3490 }
3491 break;
3492
3493 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3494 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3495
3496 // expression operations
3497 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003498 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003499 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003500 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003501 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003502 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003503 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003504 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3505 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003506 case ISN_SLICE: smsg("%4d SLICE %lld",
3507 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003508 case ISN_GETITEM: smsg("%4d ITEM %lld",
3509 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003510 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3511 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 iptr->isn_arg.string); break;
3513 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3514
3515 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003516 case ISN_CHECKTYPE:
3517 {
3518 char *tofree;
3519
3520 smsg("%4d CHECKTYPE %s stack[%d]", current,
3521 type_name(iptr->isn_arg.type.ct_type, &tofree),
3522 iptr->isn_arg.type.ct_off);
3523 vim_free(tofree);
3524 break;
3525 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003526 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3527 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3528 iptr->isn_arg.checklen.cl_min_len);
3529 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003530 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003531 case ISN_2BOOL: if (iptr->isn_arg.number)
3532 smsg("%4d INVERT (!val)", current);
3533 else
3534 smsg("%4d 2BOOL (!!val)", current);
3535 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003536 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3537 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003538 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003539 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3540 (long long)(iptr->isn_arg.number));
3541 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003542 case ISN_PUT:
3543 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3544 (long)iptr->isn_arg.put.put_lnum);
3545 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546
Bram Moolenaar02194d22020-10-24 23:08:38 +02003547 // TODO: summarize modifiers
3548 case ISN_CMDMOD:
3549 {
3550 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003551 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003552 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3553
3554 buf = alloc(len + 1);
3555 if (buf != NULL)
3556 {
3557 (void)produce_cmdmods(
3558 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3559 smsg("%4d CMDMOD %s", current, buf);
3560 vim_free(buf);
3561 }
3562 break;
3563 }
3564 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003565
Bram Moolenaar389df252020-07-09 21:20:47 +02003566 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3567 iptr->isn_arg.shuffle.shfl_item,
3568 iptr->isn_arg.shuffle.shfl_up);
3569 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003570 case ISN_DROP: smsg("%4d DROP", current); break;
3571 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003572
3573 out_flush(); // output one line at a time
3574 ui_breakcheck();
3575 if (got_int)
3576 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003578}
3579
3580/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003581 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003582 * list, etc. Mostly like what JavaScript does, except that empty list and
3583 * empty dictionary are FALSE.
3584 */
3585 int
3586tv2bool(typval_T *tv)
3587{
3588 switch (tv->v_type)
3589 {
3590 case VAR_NUMBER:
3591 return tv->vval.v_number != 0;
3592 case VAR_FLOAT:
3593#ifdef FEAT_FLOAT
3594 return tv->vval.v_float != 0.0;
3595#else
3596 break;
3597#endif
3598 case VAR_PARTIAL:
3599 return tv->vval.v_partial != NULL;
3600 case VAR_FUNC:
3601 case VAR_STRING:
3602 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3603 case VAR_LIST:
3604 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3605 case VAR_DICT:
3606 return tv->vval.v_dict != NULL
3607 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3608 case VAR_BOOL:
3609 case VAR_SPECIAL:
3610 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3611 case VAR_JOB:
3612#ifdef FEAT_JOB_CHANNEL
3613 return tv->vval.v_job != NULL;
3614#else
3615 break;
3616#endif
3617 case VAR_CHANNEL:
3618#ifdef FEAT_JOB_CHANNEL
3619 return tv->vval.v_channel != NULL;
3620#else
3621 break;
3622#endif
3623 case VAR_BLOB:
3624 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3625 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003626 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 case VAR_VOID:
3628 break;
3629 }
3630 return FALSE;
3631}
3632
3633/*
3634 * If "tv" is a string give an error and return FAIL.
3635 */
3636 int
3637check_not_string(typval_T *tv)
3638{
3639 if (tv->v_type == VAR_STRING)
3640 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003641 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003642 clear_tv(tv);
3643 return FAIL;
3644 }
3645 return OK;
3646}
3647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003648
3649#endif // FEAT_EVAL