blob: 031761d422de3e792de1a86f5edbae17ccdd3624 [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 Moolenaare13bdec2020-10-16 23:16:47 +0200553 int called_emsg_before = called_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 Moolenaare13bdec2020-10-16 23:16:47 +0200569 if (called_emsg != called_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 Moolenaared677f52020-08-12 16:38:10 +0200585 int called_emsg_before = called_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 Moolenaared677f52020-08-12 16:38:10 +0200623 if (called_emsg > called_emsg_before)
624 // 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 Moolenaar25e0f582020-05-25 22:36:50 +0200831 int called_emsg_before = called_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 Moolenaar8a7d6542020-01-26 15:56:19 +0100835
836// Get pointer to item in the stack.
837#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
838
839// Get pointer to item at the bottom of the stack, -1 is the bottom.
840#undef STACK_TV_BOT
841#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
842
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200843// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200844#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 +0100845
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200846// Like STACK_TV_VAR but use the outer scope
847#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
848
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200849 if (ufunc->uf_def_status == UF_NOT_COMPILED
850 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200851 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200852 {
853 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200854 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200855 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200856 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200857 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200858
Bram Moolenaar09689a02020-05-09 22:50:08 +0200859 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200860 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200861 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
862 + ufunc->uf_dfunc_idx;
863 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200864 {
865 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200866 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200867 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200870 CLEAR_FIELD(ectx);
871 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
872 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
873 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
874 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100875 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200876 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100877
878 // Put arguments on the stack.
879 for (idx = 0; idx < argc; ++idx)
880 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200881 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200882 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
883 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200884 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 copy_tv(&argv[idx], STACK_TV_BOT(0));
886 ++ectx.ec_stack.ga_len;
887 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200888
889 // Turn varargs into a list. Empty list if no args.
890 if (ufunc->uf_va_name != NULL)
891 {
892 int vararg_count = argc - ufunc->uf_args.ga_len;
893
894 if (vararg_count < 0)
895 vararg_count = 0;
896 else
897 argc -= vararg_count;
898 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200899 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200900
901 // Check the type of the list items.
902 tv = STACK_TV_BOT(-1);
903 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200904 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200905 && ufunc->uf_va_type->tt_member != &t_any
906 && tv->vval.v_list != NULL)
907 {
908 type_T *expected = ufunc->uf_va_type->tt_member;
909 listitem_T *li = tv->vval.v_list->lv_first;
910
911 for (idx = 0; idx < vararg_count; ++idx)
912 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200913 if (check_typval_type(expected, &li->li_tv,
914 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200915 goto failed_early;
916 li = li->li_next;
917 }
918 }
919
Bram Moolenaar23e03252020-04-12 22:22:31 +0200920 if (defcount > 0)
921 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200922 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200923 --ectx.ec_stack.ga_len;
924 }
925
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100926 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200927 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100928 if (defcount > 0)
929 for (idx = 0; idx < defcount; ++idx)
930 {
931 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
932 ++ectx.ec_stack.ga_len;
933 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200934 if (ufunc->uf_va_name != NULL)
935 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936
937 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200938 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
939 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200941 if (partial != NULL)
942 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200943 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
944 {
945 // TODO: is this always the right way?
946 ectx.ec_outer_stack = &current_ectx->ec_stack;
947 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
948 }
949 else
950 {
951 ectx.ec_outer_stack = partial->pt_ectx_stack;
952 ectx.ec_outer_frame = partial->pt_ectx_frame;
953 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200954 }
955
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 // dummy frame entries
957 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
958 {
959 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
960 ++ectx.ec_stack.ga_len;
961 }
962
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200963 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200964 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200965 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
966 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200968 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200969 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200970 ectx.ec_stack.ga_len += dfunc->df_varcount;
971 if (dfunc->df_has_closure)
972 {
973 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
974 STACK_TV_VAR(idx)->vval.v_number = 0;
975 ++ectx.ec_stack.ga_len;
976 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200977
978 ectx.ec_instr = dfunc->df_instr;
979 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100980
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200981 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200982 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200983 estack_push_ufunc(ufunc, 1);
984 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200985 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
986
Bram Moolenaar352134b2020-10-17 22:04:08 +0200987 // Use a specific location for storing error messages to be converted to an
988 // exception.
989 saved_msg_list = msg_list;
990 msg_list = &private_msg_list;
991
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200992 // Do turn errors into exceptions.
993 suppress_errthrow = FALSE;
994
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100995 // Decide where to start execution, handles optional arguments.
996 init_instr_idx(ufunc, argc, &ectx);
997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 for (;;)
999 {
1000 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001001
Bram Moolenaar270d0382020-05-15 21:42:53 +02001002 if (++breakcheck_count >= 100)
1003 {
1004 line_breakcheck();
1005 breakcheck_count = 0;
1006 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001007 if (got_int)
1008 {
1009 // Turn CTRL-C into an exception.
1010 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001011 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001012 goto failed;
1013 did_throw = TRUE;
1014 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015
Bram Moolenaara26b9702020-04-18 19:53:28 +02001016 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1017 {
1018 // Turn an error message into an exception.
1019 did_emsg = FALSE;
1020 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1021 goto failed;
1022 did_throw = TRUE;
1023 *msg_list = NULL;
1024 }
1025
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001026 if (did_throw && !ectx.ec_in_catch)
1027 {
1028 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001029 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030
1031 // An exception jumps to the first catch, finally, or returns from
1032 // the current function.
1033 if (trystack->ga_len > 0)
1034 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001035 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 {
1037 // jump to ":catch" or ":finally"
1038 ectx.ec_in_catch = TRUE;
1039 ectx.ec_iidx = trycmd->tcd_catch_idx;
1040 }
1041 else
1042 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001043 // Not inside try or need to return from current functions.
1044 // Push a dummy return value.
1045 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1046 goto failed;
1047 tv = STACK_TV_BOT(0);
1048 tv->v_type = VAR_NUMBER;
1049 tv->vval.v_number = 0;
1050 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001051 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001053 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001054 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001055 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1056 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 goto done;
1058 }
1059
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001060 if (func_return(&ectx) == FAIL)
1061 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001062 }
1063 continue;
1064 }
1065
1066 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1067 switch (iptr->isn_type)
1068 {
1069 // execute Ex command line
1070 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001071 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 do_cmdline_cmd(iptr->isn_arg.string);
1073 break;
1074
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001075 // execute Ex command from pieces on the stack
1076 case ISN_EXECCONCAT:
1077 {
1078 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001079 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001080 int pass;
1081 int i;
1082 char_u *cmd = NULL;
1083 char_u *str;
1084
1085 for (pass = 1; pass <= 2; ++pass)
1086 {
1087 for (i = 0; i < count; ++i)
1088 {
1089 tv = STACK_TV_BOT(i - count);
1090 str = tv->vval.v_string;
1091 if (str != NULL && *str != NUL)
1092 {
1093 if (pass == 2)
1094 STRCPY(cmd + len, str);
1095 len += STRLEN(str);
1096 }
1097 if (pass == 2)
1098 clear_tv(tv);
1099 }
1100 if (pass == 1)
1101 {
1102 cmd = alloc(len + 1);
1103 if (cmd == NULL)
1104 goto failed;
1105 len = 0;
1106 }
1107 }
1108
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001109 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001110 do_cmdline_cmd(cmd);
1111 vim_free(cmd);
1112 }
1113 break;
1114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 // execute :echo {string} ...
1116 case ISN_ECHO:
1117 {
1118 int count = iptr->isn_arg.echo.echo_count;
1119 int atstart = TRUE;
1120 int needclr = TRUE;
1121
1122 for (idx = 0; idx < count; ++idx)
1123 {
1124 tv = STACK_TV_BOT(idx - count);
1125 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1126 &atstart, &needclr);
1127 clear_tv(tv);
1128 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001129 if (needclr)
1130 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 ectx.ec_stack.ga_len -= count;
1132 }
1133 break;
1134
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001135 // :execute {string} ...
1136 // :echomsg {string} ...
1137 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001138 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001139 case ISN_ECHOMSG:
1140 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001141 {
1142 int count = iptr->isn_arg.number;
1143 garray_T ga;
1144 char_u buf[NUMBUFLEN];
1145 char_u *p;
1146 int len;
1147 int failed = FALSE;
1148
1149 ga_init2(&ga, 1, 80);
1150 for (idx = 0; idx < count; ++idx)
1151 {
1152 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001153 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001154 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001155 if (tv->v_type == VAR_CHANNEL
1156 || tv->v_type == VAR_JOB)
1157 {
1158 SOURCING_LNUM = iptr->isn_lnum;
1159 emsg(_(e_inval_string));
1160 break;
1161 }
1162 else
1163 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001164 }
1165 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001166 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001167
1168 len = (int)STRLEN(p);
1169 if (ga_grow(&ga, len + 2) == FAIL)
1170 failed = TRUE;
1171 else
1172 {
1173 if (ga.ga_len > 0)
1174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1176 ga.ga_len += len;
1177 }
1178 clear_tv(tv);
1179 }
1180 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001181 if (failed)
1182 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001183
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001184 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001185 {
1186 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001187 {
1188 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001189 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001190 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001191 else
1192 {
1193 msg_sb_eol();
1194 if (iptr->isn_type == ISN_ECHOMSG)
1195 {
1196 msg_attr(ga.ga_data, echo_attr);
1197 out_flush();
1198 }
1199 else
1200 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001201 SOURCING_LNUM = iptr->isn_lnum;
1202 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001203 }
1204 }
1205 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001206 ga_clear(&ga);
1207 }
1208 break;
1209
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 // load local variable or argument
1211 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001212 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 goto failed;
1214 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1215 ++ectx.ec_stack.ga_len;
1216 break;
1217
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001218 // load variable or argument from outer scope
1219 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001220 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001221 goto failed;
1222 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1223 STACK_TV_BOT(0));
1224 ++ectx.ec_stack.ga_len;
1225 break;
1226
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 // load v: variable
1228 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001229 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 goto failed;
1231 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1232 ++ectx.ec_stack.ga_len;
1233 break;
1234
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001235 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 case ISN_LOADSCRIPT:
1237 {
1238 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001239 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 svar_T *sv;
1241
1242 sv = ((svar_T *)si->sn_var_vals.ga_data)
1243 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001244 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 goto failed;
1246 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1247 ++ectx.ec_stack.ga_len;
1248 }
1249 break;
1250
1251 // load s: variable in old script
1252 case ISN_LOADS:
1253 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001254 hashtab_T *ht = &SCRIPT_VARS(
1255 iptr->isn_arg.loadstore.ls_sid);
1256 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001258
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 if (di == NULL)
1260 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001261 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001262 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001263 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 }
1265 else
1266 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001267 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268 goto failed;
1269 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1270 ++ectx.ec_stack.ga_len;
1271 }
1272 }
1273 break;
1274
Bram Moolenaard3aac292020-04-19 14:32:17 +02001275 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001277 case ISN_LOADB:
1278 case ISN_LOADW:
1279 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001280 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001281 dictitem_T *di = NULL;
1282 hashtab_T *ht = NULL;
1283 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001284
Bram Moolenaard3aac292020-04-19 14:32:17 +02001285 switch (iptr->isn_type)
1286 {
1287 case ISN_LOADG:
1288 ht = get_globvar_ht();
1289 namespace = 'g';
1290 break;
1291 case ISN_LOADB:
1292 ht = &curbuf->b_vars->dv_hashtab;
1293 namespace = 'b';
1294 break;
1295 case ISN_LOADW:
1296 ht = &curwin->w_vars->dv_hashtab;
1297 namespace = 'w';
1298 break;
1299 case ISN_LOADT:
1300 ht = &curtab->tp_vars->dv_hashtab;
1301 namespace = 't';
1302 break;
1303 default: // Cannot reach here
1304 goto failed;
1305 }
1306 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001308 if (di == NULL)
1309 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001311 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001312 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001313 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 }
1315 else
1316 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001317 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001318 goto failed;
1319 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1320 ++ectx.ec_stack.ga_len;
1321 }
1322 }
1323 break;
1324
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001325 // load g:/b:/w:/t: namespace
1326 case ISN_LOADGDICT:
1327 case ISN_LOADBDICT:
1328 case ISN_LOADWDICT:
1329 case ISN_LOADTDICT:
1330 {
1331 dict_T *d = NULL;
1332
1333 switch (iptr->isn_type)
1334 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001335 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1336 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1337 case ISN_LOADWDICT: d = curwin->w_vars; break;
1338 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001339 default: // Cannot reach here
1340 goto failed;
1341 }
1342 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1343 goto failed;
1344 tv = STACK_TV_BOT(0);
1345 tv->v_type = VAR_DICT;
1346 tv->v_lock = 0;
1347 tv->vval.v_dict = d;
1348 ++ectx.ec_stack.ga_len;
1349 }
1350 break;
1351
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 // load &option
1353 case ISN_LOADOPT:
1354 {
1355 typval_T optval;
1356 char_u *name = iptr->isn_arg.string;
1357
Bram Moolenaara8c17702020-04-01 21:17:24 +02001358 // This is not expected to fail, name is checked during
1359 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001360 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001362 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001363 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 *STACK_TV_BOT(0) = optval;
1365 ++ectx.ec_stack.ga_len;
1366 }
1367 break;
1368
1369 // load $ENV
1370 case ISN_LOADENV:
1371 {
1372 typval_T optval;
1373 char_u *name = iptr->isn_arg.string;
1374
Bram Moolenaar270d0382020-05-15 21:42:53 +02001375 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001377 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001378 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001379 *STACK_TV_BOT(0) = optval;
1380 ++ectx.ec_stack.ga_len;
1381 }
1382 break;
1383
1384 // load @register
1385 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001386 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 goto failed;
1388 tv = STACK_TV_BOT(0);
1389 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001390 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001391 // This may result in NULL, which should be equivalent to an
1392 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 tv->vval.v_string = get_reg_contents(
1394 iptr->isn_arg.number, GREG_EXPR_SRC);
1395 ++ectx.ec_stack.ga_len;
1396 break;
1397
1398 // store local variable
1399 case ISN_STORE:
1400 --ectx.ec_stack.ga_len;
1401 tv = STACK_TV_VAR(iptr->isn_arg.number);
1402 clear_tv(tv);
1403 *tv = *STACK_TV_BOT(0);
1404 break;
1405
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001406 // store variable or argument in outer scope
1407 case ISN_STOREOUTER:
1408 --ectx.ec_stack.ga_len;
1409 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1410 clear_tv(tv);
1411 *tv = *STACK_TV_BOT(0);
1412 break;
1413
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001414 // store s: variable in old script
1415 case ISN_STORES:
1416 {
1417 hashtab_T *ht = &SCRIPT_VARS(
1418 iptr->isn_arg.loadstore.ls_sid);
1419 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001420 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001421
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001422 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001423 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001424 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001425 else
1426 {
1427 clear_tv(&di->di_tv);
1428 di->di_tv = *STACK_TV_BOT(0);
1429 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001430 }
1431 break;
1432
1433 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 case ISN_STORESCRIPT:
1435 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001436 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 iptr->isn_arg.script.script_sid);
1438 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1439 + iptr->isn_arg.script.script_idx;
1440
1441 --ectx.ec_stack.ga_len;
1442 clear_tv(sv->sv_tv);
1443 *sv->sv_tv = *STACK_TV_BOT(0);
1444 }
1445 break;
1446
1447 // store option
1448 case ISN_STOREOPT:
1449 {
1450 long n = 0;
1451 char_u *s = NULL;
1452 char *msg;
1453
1454 --ectx.ec_stack.ga_len;
1455 tv = STACK_TV_BOT(0);
1456 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001457 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001459 if (s == NULL)
1460 s = (char_u *)"";
1461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001463 // must be VAR_NUMBER, CHECKTYPE makes sure
1464 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1466 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001467 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001468 if (msg != NULL)
1469 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 }
1475 break;
1476
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001477 // store $ENV
1478 case ISN_STOREENV:
1479 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001480 tv = STACK_TV_BOT(0);
1481 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1482 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001483 break;
1484
1485 // store @r
1486 case ISN_STOREREG:
1487 {
1488 int reg = iptr->isn_arg.number;
1489
1490 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001491 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001492 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001493 tv_get_string(tv), -1, FALSE);
1494 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001495 }
1496 break;
1497
1498 // store v: variable
1499 case ISN_STOREV:
1500 --ectx.ec_stack.ga_len;
1501 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1502 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001503 // should not happen, type is checked when compiling
1504 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001505 break;
1506
Bram Moolenaard3aac292020-04-19 14:32:17 +02001507 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001509 case ISN_STOREB:
1510 case ISN_STOREW:
1511 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 {
1513 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001514 hashtab_T *ht;
1515 switch (iptr->isn_type)
1516 {
1517 case ISN_STOREG:
1518 ht = get_globvar_ht();
1519 break;
1520 case ISN_STOREB:
1521 ht = &curbuf->b_vars->dv_hashtab;
1522 break;
1523 case ISN_STOREW:
1524 ht = &curwin->w_vars->dv_hashtab;
1525 break;
1526 case ISN_STORET:
1527 ht = &curtab->tp_vars->dv_hashtab;
1528 break;
1529 default: // Cannot reach here
1530 goto failed;
1531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532
1533 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001534 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001536 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 else
1538 {
1539 clear_tv(&di->di_tv);
1540 di->di_tv = *STACK_TV_BOT(0);
1541 }
1542 }
1543 break;
1544
1545 // store number in local variable
1546 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001547 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 clear_tv(tv);
1549 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001550 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 break;
1552
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001553 // store value in list variable
1554 case ISN_STORELIST:
1555 {
1556 typval_T *tv_idx = STACK_TV_BOT(-2);
1557 varnumber_T lidx = tv_idx->vval.v_number;
1558 typval_T *tv_list = STACK_TV_BOT(-1);
1559 list_T *list = tv_list->vval.v_list;
1560
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001561 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001562 if (lidx < 0 && list->lv_len + lidx >= 0)
1563 // negative index is relative to the end
1564 lidx = list->lv_len + lidx;
1565 if (lidx < 0 || lidx > list->lv_len)
1566 {
1567 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001568 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001569 }
1570 tv = STACK_TV_BOT(-3);
1571 if (lidx < list->lv_len)
1572 {
1573 listitem_T *li = list_find(list, lidx);
1574
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001575 if (error_if_locked(li->li_tv.v_lock,
1576 e_cannot_change_list_item))
1577 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001578 // overwrite existing list item
1579 clear_tv(&li->li_tv);
1580 li->li_tv = *tv;
1581 }
1582 else
1583 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001584 if (error_if_locked(list->lv_lock,
1585 e_cannot_change_list))
1586 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001587 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001588 if (list_append_tv(list, tv) == FAIL)
1589 goto failed;
1590 clear_tv(tv);
1591 }
1592 clear_tv(tv_idx);
1593 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001594 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001595 }
1596 break;
1597
1598 // store value in dict variable
1599 case ISN_STOREDICT:
1600 {
1601 typval_T *tv_key = STACK_TV_BOT(-2);
1602 char_u *key = tv_key->vval.v_string;
1603 typval_T *tv_dict = STACK_TV_BOT(-1);
1604 dict_T *dict = tv_dict->vval.v_dict;
1605 dictitem_T *di;
1606
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001607 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001608 if (dict == NULL)
1609 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001610 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001611 goto on_error;
1612 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001613 if (key == NULL)
1614 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001615 tv = STACK_TV_BOT(-3);
1616 di = dict_find(dict, key, -1);
1617 if (di != NULL)
1618 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001619 if (error_if_locked(di->di_tv.v_lock,
1620 e_cannot_change_dict_item))
1621 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001622 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001623 clear_tv(&di->di_tv);
1624 di->di_tv = *tv;
1625 }
1626 else
1627 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001628 if (error_if_locked(dict->dv_lock,
1629 e_cannot_change_dict))
1630 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001631 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001632 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1633 goto failed;
1634 clear_tv(tv);
1635 }
1636 clear_tv(tv_key);
1637 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001638 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001639 }
1640 break;
1641
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 // push constant
1643 case ISN_PUSHNR:
1644 case ISN_PUSHBOOL:
1645 case ISN_PUSHSPEC:
1646 case ISN_PUSHF:
1647 case ISN_PUSHS:
1648 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001649 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001650 case ISN_PUSHCHANNEL:
1651 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001652 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 goto failed;
1654 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001655 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 ++ectx.ec_stack.ga_len;
1657 switch (iptr->isn_type)
1658 {
1659 case ISN_PUSHNR:
1660 tv->v_type = VAR_NUMBER;
1661 tv->vval.v_number = iptr->isn_arg.number;
1662 break;
1663 case ISN_PUSHBOOL:
1664 tv->v_type = VAR_BOOL;
1665 tv->vval.v_number = iptr->isn_arg.number;
1666 break;
1667 case ISN_PUSHSPEC:
1668 tv->v_type = VAR_SPECIAL;
1669 tv->vval.v_number = iptr->isn_arg.number;
1670 break;
1671#ifdef FEAT_FLOAT
1672 case ISN_PUSHF:
1673 tv->v_type = VAR_FLOAT;
1674 tv->vval.v_float = iptr->isn_arg.fnumber;
1675 break;
1676#endif
1677 case ISN_PUSHBLOB:
1678 blob_copy(iptr->isn_arg.blob, tv);
1679 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001680 case ISN_PUSHFUNC:
1681 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001682 if (iptr->isn_arg.string == NULL)
1683 tv->vval.v_string = NULL;
1684 else
1685 tv->vval.v_string =
1686 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001687 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001688 case ISN_PUSHCHANNEL:
1689#ifdef FEAT_JOB_CHANNEL
1690 tv->v_type = VAR_CHANNEL;
1691 tv->vval.v_channel = iptr->isn_arg.channel;
1692 if (tv->vval.v_channel != NULL)
1693 ++tv->vval.v_channel->ch_refcount;
1694#endif
1695 break;
1696 case ISN_PUSHJOB:
1697#ifdef FEAT_JOB_CHANNEL
1698 tv->v_type = VAR_JOB;
1699 tv->vval.v_job = iptr->isn_arg.job;
1700 if (tv->vval.v_job != NULL)
1701 ++tv->vval.v_job->jv_refcount;
1702#endif
1703 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 default:
1705 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001706 tv->vval.v_string = vim_strsave(
1707 iptr->isn_arg.string == NULL
1708 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
1710 break;
1711
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001712 case ISN_UNLET:
1713 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1714 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001715 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001716 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001717 case ISN_UNLETENV:
1718 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1719 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001720
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001721 case ISN_LOCKCONST:
1722 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1723 break;
1724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // create a list from items on the stack; uses a single allocation
1726 // for the list header and the items
1727 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001728 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1729 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 break;
1731
1732 // create a dict from items on the stack
1733 case ISN_NEWDICT:
1734 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001735 int count = iptr->isn_arg.number;
1736 dict_T *dict = dict_alloc();
1737 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738
1739 if (dict == NULL)
1740 goto failed;
1741 for (idx = 0; idx < count; ++idx)
1742 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001743 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001745 // check key is unique
1746 item = dict_find(dict, tv->vval.v_string, -1);
1747 if (item != NULL)
1748 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001749 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001750 semsg(_(e_duplicate_key), tv->vval.v_string);
1751 dict_unref(dict);
1752 goto on_error;
1753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 item = dictitem_alloc(tv->vval.v_string);
1755 clear_tv(tv);
1756 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001757 {
1758 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1762 item->di_tv.v_lock = 0;
1763 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001764 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001765 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001766 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001768 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 }
1770
1771 if (count > 0)
1772 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001773 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774 goto failed;
1775 else
1776 ++ectx.ec_stack.ga_len;
1777 tv = STACK_TV_BOT(-1);
1778 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001779 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001780 tv->vval.v_dict = dict;
1781 ++dict->dv_refcount;
1782 }
1783 break;
1784
1785 // call a :def function
1786 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001787 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001788 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1789 iptr->isn_arg.dfunc.cdf_argcount,
1790 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001791 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 break;
1793
1794 // call a builtin function
1795 case ISN_BCALL:
1796 SOURCING_LNUM = iptr->isn_lnum;
1797 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1798 iptr->isn_arg.bfunc.cbf_argcount,
1799 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001800 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 break;
1802
1803 // call a funcref or partial
1804 case ISN_PCALL:
1805 {
1806 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1807 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001808 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809
1810 SOURCING_LNUM = iptr->isn_lnum;
1811 if (pfunc->cpf_top)
1812 {
1813 // funcref is above the arguments
1814 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1815 }
1816 else
1817 {
1818 // Get the funcref from the stack.
1819 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001820 partial_tv = *STACK_TV_BOT(0);
1821 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 }
1823 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001824 if (tv == &partial_tv)
1825 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001827 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 }
1829 break;
1830
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001831 case ISN_PCALL_END:
1832 // PCALL finished, arguments have been consumed and replaced by
1833 // the return value. Now clear the funcref from the stack,
1834 // and move the return value in its place.
1835 --ectx.ec_stack.ga_len;
1836 clear_tv(STACK_TV_BOT(-1));
1837 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1838 break;
1839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 // call a user defined function or funcref/partial
1841 case ISN_UCALL:
1842 {
1843 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1844
1845 SOURCING_LNUM = iptr->isn_lnum;
1846 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001847 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001848 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 }
1850 break;
1851
1852 // return from a :def function call
1853 case ISN_RETURN:
1854 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001855 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001856 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001857
1858 if (trystack->ga_len > 0)
1859 trycmd = ((trycmd_T *)trystack->ga_data)
1860 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001861 if (trycmd != NULL
1862 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 && trycmd->tcd_finally_idx != 0)
1864 {
1865 // jump to ":finally"
1866 ectx.ec_iidx = trycmd->tcd_finally_idx;
1867 trycmd->tcd_return = TRUE;
1868 }
1869 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001870 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 }
1872 break;
1873
1874 // push a function reference to a compiled function
1875 case ISN_FUNCREF:
1876 {
1877 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001878 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879
1880 pt = ALLOC_CLEAR_ONE(partial_T);
1881 if (pt == NULL)
1882 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001883 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001884 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001885 vim_free(pt);
1886 goto failed;
1887 }
1888 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1889 + iptr->isn_arg.funcref.fr_func;
1890 pt->pt_func = pt_dfunc->df_ufunc;
1891 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001892
1893 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1894 {
1895 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1896 + ectx.ec_dfunc_idx;
1897
1898 // The closure needs to find arguments and local
1899 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001900 pt->pt_ectx_stack = &ectx.ec_stack;
1901 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001902
1903 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001904 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001905 // (arguments and local variables). Store a reference
1906 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001907 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001908 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001909 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001910 goto failed;
1911 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001912 // Extra variable keeps the count of closures created
1913 // in the current function call.
1914 tv = STACK_TV_VAR(dfunc->df_varcount);
1915 ++tv->vval.v_number;
1916
1917 ((partial_T **)ectx.ec_funcrefs.ga_data)
1918 [ectx.ec_funcrefs.ga_len] = pt;
1919 ++pt->pt_refcount;
1920 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001921 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001922 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 tv = STACK_TV_BOT(0);
1925 ++ectx.ec_stack.ga_len;
1926 tv->vval.v_partial = pt;
1927 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001928 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929 }
1930 break;
1931
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001932 // Create a global function from a lambda.
1933 case ISN_NEWFUNC:
1934 {
1935 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1936
1937 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1938 }
1939 break;
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 // jump if a condition is met
1942 case ISN_JUMP:
1943 {
1944 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001945 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 int jump = TRUE;
1947
1948 if (when != JUMP_ALWAYS)
1949 {
1950 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001951 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001952 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001953 || when == JUMP_IF_COND_TRUE)
1954 {
1955 SOURCING_LNUM = iptr->isn_lnum;
1956 jump = tv_get_bool_chk(tv, &error);
1957 if (error)
1958 goto on_error;
1959 }
1960 else
1961 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001963 || when == JUMP_AND_KEEP_IF_FALSE
1964 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001966 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 {
1968 // drop the value from the stack
1969 clear_tv(tv);
1970 --ectx.ec_stack.ga_len;
1971 }
1972 }
1973 if (jump)
1974 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1975 }
1976 break;
1977
1978 // top of a for loop
1979 case ISN_FOR:
1980 {
1981 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1982 typval_T *idxtv =
1983 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1984
1985 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001986 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001988 ++idxtv->vval.v_number;
1989 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 // past the end of the list, jump to "endfor"
1991 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1992 else if (list->lv_first == &range_list_item)
1993 {
1994 // non-materialized range() list
1995 tv = STACK_TV_BOT(0);
1996 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001997 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 tv->vval.v_number = list_find_nr(
1999 list, idxtv->vval.v_number, NULL);
2000 ++ectx.ec_stack.ga_len;
2001 }
2002 else
2003 {
2004 listitem_T *li = list_find(list, idxtv->vval.v_number);
2005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2007 ++ectx.ec_stack.ga_len;
2008 }
2009 }
2010 break;
2011
2012 // start of ":try" block
2013 case ISN_TRY:
2014 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002015 trycmd_T *trycmd = NULL;
2016
Bram Moolenaar270d0382020-05-15 21:42:53 +02002017 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002018 goto failed;
2019 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2020 + ectx.ec_trystack.ga_len;
2021 ++ectx.ec_trystack.ga_len;
2022 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002023 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2025 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002026 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002027 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 }
2029 break;
2030
2031 case ISN_PUSHEXC:
2032 if (current_exception == NULL)
2033 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002034 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 iemsg("Evaluating catch while current_exception is NULL");
2036 goto failed;
2037 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002038 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 goto failed;
2040 tv = STACK_TV_BOT(0);
2041 ++ectx.ec_stack.ga_len;
2042 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002043 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 tv->vval.v_string = vim_strsave(
2045 (char_u *)current_exception->value);
2046 break;
2047
2048 case ISN_CATCH:
2049 {
2050 garray_T *trystack = &ectx.ec_trystack;
2051
2052 if (trystack->ga_len > 0)
2053 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002054 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 + trystack->ga_len - 1;
2056 trycmd->tcd_caught = TRUE;
2057 }
2058 did_emsg = got_int = did_throw = FALSE;
2059 catch_exception(current_exception);
2060 }
2061 break;
2062
2063 // end of ":try" block
2064 case ISN_ENDTRY:
2065 {
2066 garray_T *trystack = &ectx.ec_trystack;
2067
2068 if (trystack->ga_len > 0)
2069 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002070 trycmd_T *trycmd = NULL;
2071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 --trystack->ga_len;
2073 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002074 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002075 trycmd = ((trycmd_T *)trystack->ga_data)
2076 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002077 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 {
2079 // discard the exception
2080 if (caught_stack == current_exception)
2081 caught_stack = caught_stack->caught;
2082 discard_current_exception();
2083 }
2084
2085 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002086 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 }
2088 }
2089 break;
2090
2091 case ISN_THROW:
2092 --ectx.ec_stack.ga_len;
2093 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002094 if (tv->vval.v_string == NULL
2095 || *skipwhite(tv->vval.v_string) == NUL)
2096 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002097 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002098 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002099 emsg(_(e_throw_with_empty_string));
2100 goto failed;
2101 }
2102
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2104 {
2105 vim_free(tv->vval.v_string);
2106 goto failed;
2107 }
2108 did_throw = TRUE;
2109 break;
2110
2111 // compare with special values
2112 case ISN_COMPAREBOOL:
2113 case ISN_COMPARESPECIAL:
2114 {
2115 typval_T *tv1 = STACK_TV_BOT(-2);
2116 typval_T *tv2 = STACK_TV_BOT(-1);
2117 varnumber_T arg1 = tv1->vval.v_number;
2118 varnumber_T arg2 = tv2->vval.v_number;
2119 int res;
2120
2121 switch (iptr->isn_arg.op.op_type)
2122 {
2123 case EXPR_EQUAL: res = arg1 == arg2; break;
2124 case EXPR_NEQUAL: res = arg1 != arg2; break;
2125 default: res = 0; break;
2126 }
2127
2128 --ectx.ec_stack.ga_len;
2129 tv1->v_type = VAR_BOOL;
2130 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2131 }
2132 break;
2133
2134 // Operation with two number arguments
2135 case ISN_OPNR:
2136 case ISN_COMPARENR:
2137 {
2138 typval_T *tv1 = STACK_TV_BOT(-2);
2139 typval_T *tv2 = STACK_TV_BOT(-1);
2140 varnumber_T arg1 = tv1->vval.v_number;
2141 varnumber_T arg2 = tv2->vval.v_number;
2142 varnumber_T res;
2143
2144 switch (iptr->isn_arg.op.op_type)
2145 {
2146 case EXPR_MULT: res = arg1 * arg2; break;
2147 case EXPR_DIV: res = arg1 / arg2; break;
2148 case EXPR_REM: res = arg1 % arg2; break;
2149 case EXPR_SUB: res = arg1 - arg2; break;
2150 case EXPR_ADD: res = arg1 + arg2; break;
2151
2152 case EXPR_EQUAL: res = arg1 == arg2; break;
2153 case EXPR_NEQUAL: res = arg1 != arg2; break;
2154 case EXPR_GREATER: res = arg1 > arg2; break;
2155 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2156 case EXPR_SMALLER: res = arg1 < arg2; break;
2157 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2158 default: res = 0; break;
2159 }
2160
2161 --ectx.ec_stack.ga_len;
2162 if (iptr->isn_type == ISN_COMPARENR)
2163 {
2164 tv1->v_type = VAR_BOOL;
2165 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2166 }
2167 else
2168 tv1->vval.v_number = res;
2169 }
2170 break;
2171
2172 // Computation with two float arguments
2173 case ISN_OPFLOAT:
2174 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002175#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 {
2177 typval_T *tv1 = STACK_TV_BOT(-2);
2178 typval_T *tv2 = STACK_TV_BOT(-1);
2179 float_T arg1 = tv1->vval.v_float;
2180 float_T arg2 = tv2->vval.v_float;
2181 float_T res = 0;
2182 int cmp = FALSE;
2183
2184 switch (iptr->isn_arg.op.op_type)
2185 {
2186 case EXPR_MULT: res = arg1 * arg2; break;
2187 case EXPR_DIV: res = arg1 / arg2; break;
2188 case EXPR_SUB: res = arg1 - arg2; break;
2189 case EXPR_ADD: res = arg1 + arg2; break;
2190
2191 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2192 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2193 case EXPR_GREATER: cmp = arg1 > arg2; break;
2194 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2195 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2196 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2197 default: cmp = 0; break;
2198 }
2199 --ectx.ec_stack.ga_len;
2200 if (iptr->isn_type == ISN_COMPAREFLOAT)
2201 {
2202 tv1->v_type = VAR_BOOL;
2203 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2204 }
2205 else
2206 tv1->vval.v_float = res;
2207 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002208#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 break;
2210
2211 case ISN_COMPARELIST:
2212 {
2213 typval_T *tv1 = STACK_TV_BOT(-2);
2214 typval_T *tv2 = STACK_TV_BOT(-1);
2215 list_T *arg1 = tv1->vval.v_list;
2216 list_T *arg2 = tv2->vval.v_list;
2217 int cmp = FALSE;
2218 int ic = iptr->isn_arg.op.op_ic;
2219
2220 switch (iptr->isn_arg.op.op_type)
2221 {
2222 case EXPR_EQUAL: cmp =
2223 list_equal(arg1, arg2, ic, FALSE); break;
2224 case EXPR_NEQUAL: cmp =
2225 !list_equal(arg1, arg2, ic, FALSE); break;
2226 case EXPR_IS: cmp = arg1 == arg2; break;
2227 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2228 default: cmp = 0; break;
2229 }
2230 --ectx.ec_stack.ga_len;
2231 clear_tv(tv1);
2232 clear_tv(tv2);
2233 tv1->v_type = VAR_BOOL;
2234 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2235 }
2236 break;
2237
2238 case ISN_COMPAREBLOB:
2239 {
2240 typval_T *tv1 = STACK_TV_BOT(-2);
2241 typval_T *tv2 = STACK_TV_BOT(-1);
2242 blob_T *arg1 = tv1->vval.v_blob;
2243 blob_T *arg2 = tv2->vval.v_blob;
2244 int cmp = FALSE;
2245
2246 switch (iptr->isn_arg.op.op_type)
2247 {
2248 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2249 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2250 case EXPR_IS: cmp = arg1 == arg2; break;
2251 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2252 default: cmp = 0; break;
2253 }
2254 --ectx.ec_stack.ga_len;
2255 clear_tv(tv1);
2256 clear_tv(tv2);
2257 tv1->v_type = VAR_BOOL;
2258 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2259 }
2260 break;
2261
2262 // TODO: handle separately
2263 case ISN_COMPARESTRING:
2264 case ISN_COMPAREDICT:
2265 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002266 case ISN_COMPAREANY:
2267 {
2268 typval_T *tv1 = STACK_TV_BOT(-2);
2269 typval_T *tv2 = STACK_TV_BOT(-1);
2270 exptype_T exptype = iptr->isn_arg.op.op_type;
2271 int ic = iptr->isn_arg.op.op_ic;
2272
Bram Moolenaareb26f432020-09-14 16:50:05 +02002273 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 typval_compare(tv1, tv2, exptype, ic);
2275 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 --ectx.ec_stack.ga_len;
2277 }
2278 break;
2279
2280 case ISN_ADDLIST:
2281 case ISN_ADDBLOB:
2282 {
2283 typval_T *tv1 = STACK_TV_BOT(-2);
2284 typval_T *tv2 = STACK_TV_BOT(-1);
2285
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002286 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 if (iptr->isn_type == ISN_ADDLIST)
2288 eval_addlist(tv1, tv2);
2289 else
2290 eval_addblob(tv1, tv2);
2291 clear_tv(tv2);
2292 --ectx.ec_stack.ga_len;
2293 }
2294 break;
2295
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002296 case ISN_LISTAPPEND:
2297 {
2298 typval_T *tv1 = STACK_TV_BOT(-2);
2299 typval_T *tv2 = STACK_TV_BOT(-1);
2300 list_T *l = tv1->vval.v_list;
2301
2302 // add an item to a list
2303 if (l == NULL)
2304 {
2305 SOURCING_LNUM = iptr->isn_lnum;
2306 emsg(_(e_cannot_add_to_null_list));
2307 goto on_error;
2308 }
2309 if (list_append_tv(l, tv2) == FAIL)
2310 goto failed;
2311 --ectx.ec_stack.ga_len;
2312 }
2313 break;
2314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 // Computation with two arguments of unknown type
2316 case ISN_OPANY:
2317 {
2318 typval_T *tv1 = STACK_TV_BOT(-2);
2319 typval_T *tv2 = STACK_TV_BOT(-1);
2320 varnumber_T n1, n2;
2321#ifdef FEAT_FLOAT
2322 float_T f1 = 0, f2 = 0;
2323#endif
2324 int error = FALSE;
2325
2326 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2327 {
2328 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2329 {
2330 eval_addlist(tv1, tv2);
2331 clear_tv(tv2);
2332 --ectx.ec_stack.ga_len;
2333 break;
2334 }
2335 else if (tv1->v_type == VAR_BLOB
2336 && tv2->v_type == VAR_BLOB)
2337 {
2338 eval_addblob(tv1, tv2);
2339 clear_tv(tv2);
2340 --ectx.ec_stack.ga_len;
2341 break;
2342 }
2343 }
2344#ifdef FEAT_FLOAT
2345 if (tv1->v_type == VAR_FLOAT)
2346 {
2347 f1 = tv1->vval.v_float;
2348 n1 = 0;
2349 }
2350 else
2351#endif
2352 {
2353 n1 = tv_get_number_chk(tv1, &error);
2354 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002355 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356#ifdef FEAT_FLOAT
2357 if (tv2->v_type == VAR_FLOAT)
2358 f1 = n1;
2359#endif
2360 }
2361#ifdef FEAT_FLOAT
2362 if (tv2->v_type == VAR_FLOAT)
2363 {
2364 f2 = tv2->vval.v_float;
2365 n2 = 0;
2366 }
2367 else
2368#endif
2369 {
2370 n2 = tv_get_number_chk(tv2, &error);
2371 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002372 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373#ifdef FEAT_FLOAT
2374 if (tv1->v_type == VAR_FLOAT)
2375 f2 = n2;
2376#endif
2377 }
2378#ifdef FEAT_FLOAT
2379 // if there is a float on either side the result is a float
2380 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2381 {
2382 switch (iptr->isn_arg.op.op_type)
2383 {
2384 case EXPR_MULT: f1 = f1 * f2; break;
2385 case EXPR_DIV: f1 = f1 / f2; break;
2386 case EXPR_SUB: f1 = f1 - f2; break;
2387 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002388 default: SOURCING_LNUM = iptr->isn_lnum;
2389 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 }
2392 clear_tv(tv1);
2393 clear_tv(tv2);
2394 tv1->v_type = VAR_FLOAT;
2395 tv1->vval.v_float = f1;
2396 --ectx.ec_stack.ga_len;
2397 }
2398 else
2399#endif
2400 {
2401 switch (iptr->isn_arg.op.op_type)
2402 {
2403 case EXPR_MULT: n1 = n1 * n2; break;
2404 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2405 case EXPR_SUB: n1 = n1 - n2; break;
2406 case EXPR_ADD: n1 = n1 + n2; break;
2407 default: n1 = num_modulus(n1, n2); break;
2408 }
2409 clear_tv(tv1);
2410 clear_tv(tv2);
2411 tv1->v_type = VAR_NUMBER;
2412 tv1->vval.v_number = n1;
2413 --ectx.ec_stack.ga_len;
2414 }
2415 }
2416 break;
2417
2418 case ISN_CONCAT:
2419 {
2420 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2421 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2422 char_u *res;
2423
2424 res = concat_str(str1, str2);
2425 clear_tv(STACK_TV_BOT(-2));
2426 clear_tv(STACK_TV_BOT(-1));
2427 --ectx.ec_stack.ga_len;
2428 STACK_TV_BOT(-1)->vval.v_string = res;
2429 }
2430 break;
2431
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002432 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002433 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002434 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002435 int is_slice = iptr->isn_type == ISN_STRSLICE;
2436 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002437 char_u *res;
2438
2439 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002440 // string slice: string is at stack-3, first index at
2441 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002442 if (is_slice)
2443 {
2444 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002445 n1 = tv->vval.v_number;
2446 }
2447
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002448 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002449 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002450
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002451 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002452 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002453 if (is_slice)
2454 // Slice: Select the characters from the string
2455 res = string_slice(tv->vval.v_string, n1, n2);
2456 else
2457 // Index: The resulting variable is a string of a
2458 // single character. If the index is too big or
2459 // negative the result is empty.
2460 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002461 vim_free(tv->vval.v_string);
2462 tv->vval.v_string = res;
2463 }
2464 break;
2465
2466 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002467 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 {
Bram Moolenaared591872020-08-15 22:14:53 +02002469 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002471 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472
2473 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002474 // list slice: list is at stack-3, indexes at stack-2 and
2475 // stack-1
2476 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 list = tv->vval.v_list;
2478
2479 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002480 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002482
2483 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 {
Bram Moolenaared591872020-08-15 22:14:53 +02002485 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002486 n1 = tv->vval.v_number;
2487 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 }
Bram Moolenaared591872020-08-15 22:14:53 +02002489
2490 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002491 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002493 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2494 == FAIL)
2495 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 break;
2498
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002499 case ISN_ANYINDEX:
2500 case ISN_ANYSLICE:
2501 {
2502 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2503 typval_T *var1, *var2;
2504 int res;
2505
2506 // index: composite is at stack-2, index at stack-1
2507 // slice: composite is at stack-3, indexes at stack-2 and
2508 // stack-1
2509 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002510 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002511 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2512 goto on_error;
2513 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2514 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2515 res = eval_index_inner(tv, is_slice,
2516 var1, var2, NULL, -1, TRUE);
2517 clear_tv(var1);
2518 if (is_slice)
2519 clear_tv(var2);
2520 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2521 if (res == FAIL)
2522 goto on_error;
2523 }
2524 break;
2525
Bram Moolenaar9af78762020-06-16 11:34:42 +02002526 case ISN_SLICE:
2527 {
2528 list_T *list;
2529 int count = iptr->isn_arg.number;
2530
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002531 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002532 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002533 list = tv->vval.v_list;
2534
2535 // no error for short list, expect it to be checked earlier
2536 if (list != NULL && list->lv_len >= count)
2537 {
2538 list_T *newlist = list_slice(list,
2539 count, list->lv_len - 1);
2540
2541 if (newlist != NULL)
2542 {
2543 list_unref(list);
2544 tv->vval.v_list = newlist;
2545 ++newlist->lv_refcount;
2546 }
2547 }
2548 }
2549 break;
2550
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002551 case ISN_GETITEM:
2552 {
2553 listitem_T *li;
2554 int index = iptr->isn_arg.number;
2555
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002556 // Get list item: list is at stack-1, push item.
2557 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002558 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002559 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002560
2561 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2562 goto failed;
2563 ++ectx.ec_stack.ga_len;
2564 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2565 }
2566 break;
2567
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 case ISN_MEMBER:
2569 {
2570 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002571 char_u *key;
2572 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002573 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002574
2575 // dict member: dict is at stack-2, key at stack-1
2576 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002577 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002578 dict = tv->vval.v_dict;
2579
2580 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002581 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002582 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002583
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002584 if ((di = dict_find(dict, key, -1)) == NULL)
2585 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002586 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002587 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002588 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002589 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002590 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002591 --ectx.ec_stack.ga_len;
2592 // Clear the dict after getting the item, to avoid that it
2593 // make the item invalid.
2594 tv = STACK_TV_BOT(-1);
2595 temp_tv = *tv;
2596 copy_tv(&di->di_tv, tv);
2597 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002598 }
2599 break;
2600
2601 // dict member with string key
2602 case ISN_STRINGMEMBER:
2603 {
2604 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002606 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607
2608 tv = STACK_TV_BOT(-1);
2609 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2610 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002611 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002613 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 }
2615 dict = tv->vval.v_dict;
2616
2617 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2618 == NULL)
2619 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002624 // Clear the dict after getting the item, to avoid that it
2625 // make the item invalid.
2626 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002628 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 }
2630 break;
2631
2632 case ISN_NEGATENR:
2633 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002634 if (tv->v_type != VAR_NUMBER
2635#ifdef FEAT_FLOAT
2636 && tv->v_type != VAR_FLOAT
2637#endif
2638 )
2639 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002640 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002641 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002642 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002643 }
2644#ifdef FEAT_FLOAT
2645 if (tv->v_type == VAR_FLOAT)
2646 tv->vval.v_float = -tv->vval.v_float;
2647 else
2648#endif
2649 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650 break;
2651
2652 case ISN_CHECKNR:
2653 {
2654 int error = FALSE;
2655
2656 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002657 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002659 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 (void)tv_get_number_chk(tv, &error);
2661 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 }
2664 break;
2665
2666 case ISN_CHECKTYPE:
2667 {
2668 checktype_T *ct = &iptr->isn_arg.type;
2669
2670 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002671 SOURCING_LNUM = iptr->isn_lnum;
2672 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2673 goto on_error;
2674
2675 // number 0 is FALSE, number 1 is TRUE
2676 if (tv->v_type == VAR_NUMBER
2677 && ct->ct_type->tt_type == VAR_BOOL
2678 && (tv->vval.v_number == 0
2679 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002681 tv->v_type = VAR_BOOL;
2682 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002683 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 }
2685 }
2686 break;
2687
Bram Moolenaar9af78762020-06-16 11:34:42 +02002688 case ISN_CHECKLEN:
2689 {
2690 int min_len = iptr->isn_arg.checklen.cl_min_len;
2691 list_T *list = NULL;
2692
2693 tv = STACK_TV_BOT(-1);
2694 if (tv->v_type == VAR_LIST)
2695 list = tv->vval.v_list;
2696 if (list == NULL || list->lv_len < min_len
2697 || (list->lv_len > min_len
2698 && !iptr->isn_arg.checklen.cl_more_OK))
2699 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002700 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002701 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002702 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002703 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002704 }
2705 }
2706 break;
2707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002708 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002709 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 {
2711 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002712 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713
2714 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002715 if (iptr->isn_type == ISN_2BOOL)
2716 {
2717 n = tv2bool(tv);
2718 if (iptr->isn_arg.number) // invert
2719 n = !n;
2720 }
2721 else
2722 {
2723 SOURCING_LNUM = iptr->isn_lnum;
2724 n = tv_get_bool_chk(tv, &error);
2725 if (error)
2726 goto on_error;
2727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 clear_tv(tv);
2729 tv->v_type = VAR_BOOL;
2730 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2731 }
2732 break;
2733
2734 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002735 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 {
2737 char_u *str;
2738
2739 tv = STACK_TV_BOT(iptr->isn_arg.number);
2740 if (tv->v_type != VAR_STRING)
2741 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002742 if (iptr->isn_type == ISN_2STRING_ANY)
2743 {
2744 switch (tv->v_type)
2745 {
2746 case VAR_SPECIAL:
2747 case VAR_BOOL:
2748 case VAR_NUMBER:
2749 case VAR_FLOAT:
2750 case VAR_BLOB: break;
2751 default: to_string_error(tv->v_type);
2752 goto on_error;
2753 }
2754 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 str = typval_tostring(tv);
2756 clear_tv(tv);
2757 tv->v_type = VAR_STRING;
2758 tv->vval.v_string = str;
2759 }
2760 }
2761 break;
2762
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002763 case ISN_PUT:
2764 {
2765 int regname = iptr->isn_arg.put.put_regname;
2766 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2767 char_u *expr = NULL;
2768 int dir = FORWARD;
2769
2770 if (regname == '=')
2771 {
2772 tv = STACK_TV_BOT(-1);
2773 if (tv->v_type == VAR_STRING)
2774 expr = tv->vval.v_string;
2775 else
2776 {
2777 expr = typval_tostring(tv); // allocates value
2778 clear_tv(tv);
2779 }
2780 --ectx.ec_stack.ga_len;
2781 }
2782 if (lnum == -2)
2783 // :put! above cursor
2784 dir = BACKWARD;
2785 else if (lnum >= 0)
2786 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2787 check_cursor();
2788 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2789 vim_free(expr);
2790 }
2791 break;
2792
Bram Moolenaar389df252020-07-09 21:20:47 +02002793 case ISN_SHUFFLE:
2794 {
2795 typval_T tmp_tv;
2796 int item = iptr->isn_arg.shuffle.shfl_item;
2797 int up = iptr->isn_arg.shuffle.shfl_up;
2798
2799 tmp_tv = *STACK_TV_BOT(-item);
2800 for ( ; up > 0 && item > 1; --up)
2801 {
2802 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2803 --item;
2804 }
2805 *STACK_TV_BOT(-item) = tmp_tv;
2806 }
2807 break;
2808
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 case ISN_DROP:
2810 --ectx.ec_stack.ga_len;
2811 clear_tv(STACK_TV_BOT(0));
2812 break;
2813 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002814 continue;
2815
Bram Moolenaard032f342020-07-18 18:13:02 +02002816func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002817 // Restore previous function. If the frame pointer is where we started
2818 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002819 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002820 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002821
Bram Moolenaard032f342020-07-18 18:13:02 +02002822 if (func_return(&ectx) == FAIL)
2823 // only fails when out of memory
2824 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002825 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002826
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002827on_error:
2828 if (trylevel == 0)
2829 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 }
2831
2832done:
2833 // function finished, get result from the stack.
2834 tv = STACK_TV_BOT(-1);
2835 *rettv = *tv;
2836 tv->v_type = VAR_UNKNOWN;
2837 ret = OK;
2838
2839failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002840 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002841 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002842 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002843
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002844 // Deal with any remaining closures, they may be in use somewhere.
2845 if (ectx.ec_funcrefs.ga_len > 0)
2846 handle_closure_in_use(&ectx, FALSE);
2847
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002848 estack_pop();
2849 current_sctx = save_current_sctx;
2850
Bram Moolenaar352134b2020-10-17 22:04:08 +02002851 if (*msg_list != NULL && saved_msg_list != NULL)
2852 {
2853 msglist_T **plist = saved_msg_list;
2854
2855 // Append entries from the current msg_list (uncaught exceptions) to
2856 // the saved msg_list.
2857 while (*plist != NULL)
2858 plist = &(*plist)->next;
2859
2860 *plist = *msg_list;
2861 }
2862 msg_list = saved_msg_list;
2863
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002864failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002865 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2867 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002870 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002871
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002872 // Not sure if this is necessary.
2873 suppress_errthrow = save_suppress_errthrow;
2874
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002875 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002876 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002877 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 return ret;
2879}
2880
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881/*
2882 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002883 * We don't really need this at runtime, but we do have tests that require it,
2884 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 */
2886 void
2887ex_disassemble(exarg_T *eap)
2888{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002889 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002890 char_u *fname;
2891 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 dfunc_T *dfunc;
2893 isn_T *instr;
2894 int current;
2895 int line_idx = 0;
2896 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002897 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002899 if (STRNCMP(arg, "<lambda>", 8) == 0)
2900 {
2901 arg += 8;
2902 (void)getdigits(&arg);
2903 fname = vim_strnsave(eap->arg, arg - eap->arg);
2904 }
2905 else
2906 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002907 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002908 if (fname == NULL)
2909 {
2910 semsg(_(e_invarg2), eap->arg);
2911 return;
2912 }
2913
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002914 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002915 if (ufunc == NULL)
2916 {
2917 char_u *p = untrans_function_name(fname);
2918
2919 if (p != NULL)
2920 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002921 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002922 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002923 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 if (ufunc == NULL)
2925 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002926 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927 return;
2928 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002929 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002930 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2931 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002932 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002934 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002935 return;
2936 }
2937 if (ufunc->uf_name_exp != NULL)
2938 msg((char *)ufunc->uf_name_exp);
2939 else
2940 msg((char *)ufunc->uf_name);
2941
2942 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2943 instr = dfunc->df_instr;
2944 for (current = 0; current < dfunc->df_instr_count; ++current)
2945 {
2946 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002947 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002948
2949 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2950 {
2951 if (current > prev_current)
2952 {
2953 msg_puts("\n\n");
2954 prev_current = current;
2955 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002956 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2957 if (line != NULL)
2958 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 }
2960
2961 switch (iptr->isn_type)
2962 {
2963 case ISN_EXEC:
2964 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2965 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002966 case ISN_EXECCONCAT:
2967 smsg("%4d EXECCONCAT %lld", current,
2968 (long long)iptr->isn_arg.number);
2969 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case ISN_ECHO:
2971 {
2972 echo_T *echo = &iptr->isn_arg.echo;
2973
2974 smsg("%4d %s %d", current,
2975 echo->echo_with_white ? "ECHO" : "ECHON",
2976 echo->echo_count);
2977 }
2978 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002979 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002980 smsg("%4d EXECUTE %lld", current,
2981 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002982 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002983 case ISN_ECHOMSG:
2984 smsg("%4d ECHOMSG %lld", current,
2985 (long long)(iptr->isn_arg.number));
2986 break;
2987 case ISN_ECHOERR:
2988 smsg("%4d ECHOERR %lld", current,
2989 (long long)(iptr->isn_arg.number));
2990 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002992 case ISN_LOADOUTER:
2993 {
2994 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2995
2996 if (iptr->isn_arg.number < 0)
2997 smsg("%4d LOAD%s arg[%lld]", current, add,
2998 (long long)(iptr->isn_arg.number
2999 + STACK_FRAME_SIZE));
3000 else
3001 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003002 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003003 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 break;
3005 case ISN_LOADV:
3006 smsg("%4d LOADV v:%s", current,
3007 get_vim_var_name(iptr->isn_arg.number));
3008 break;
3009 case ISN_LOADSCRIPT:
3010 {
3011 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003012 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003013 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3014 + iptr->isn_arg.script.script_idx;
3015
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003016 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3017 sv->sv_name,
3018 iptr->isn_arg.script.script_idx,
3019 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 }
3021 break;
3022 case ISN_LOADS:
3023 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003024 scriptitem_T *si = SCRIPT_ITEM(
3025 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026
3027 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003028 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 }
3030 break;
3031 case ISN_LOADG:
3032 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3033 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003034 case ISN_LOADB:
3035 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3036 break;
3037 case ISN_LOADW:
3038 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3039 break;
3040 case ISN_LOADT:
3041 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3042 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003043 case ISN_LOADGDICT:
3044 smsg("%4d LOAD g:", current);
3045 break;
3046 case ISN_LOADBDICT:
3047 smsg("%4d LOAD b:", current);
3048 break;
3049 case ISN_LOADWDICT:
3050 smsg("%4d LOAD w:", current);
3051 break;
3052 case ISN_LOADTDICT:
3053 smsg("%4d LOAD t:", current);
3054 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 case ISN_LOADOPT:
3056 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3057 break;
3058 case ISN_LOADENV:
3059 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3060 break;
3061 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003062 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003063 break;
3064
3065 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003066 case ISN_STOREOUTER:
3067 {
3068 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3069
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003070 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003071 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003072 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003073 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003074 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003075 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003076 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003077 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003078 case ISN_STOREV:
3079 smsg("%4d STOREV v:%s", current,
3080 get_vim_var_name(iptr->isn_arg.number));
3081 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003083 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3084 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003085 case ISN_STOREB:
3086 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3087 break;
3088 case ISN_STOREW:
3089 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3090 break;
3091 case ISN_STORET:
3092 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3093 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003094 case ISN_STORES:
3095 {
3096 scriptitem_T *si = SCRIPT_ITEM(
3097 iptr->isn_arg.loadstore.ls_sid);
3098
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003099 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003100 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003101 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 break;
3103 case ISN_STORESCRIPT:
3104 {
3105 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003106 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3108 + iptr->isn_arg.script.script_idx;
3109
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003110 smsg("%4d STORESCRIPT %s-%d in %s", current,
3111 sv->sv_name,
3112 iptr->isn_arg.script.script_idx,
3113 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 }
3115 break;
3116 case ISN_STOREOPT:
3117 smsg("%4d STOREOPT &%s", current,
3118 iptr->isn_arg.storeopt.so_name);
3119 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003120 case ISN_STOREENV:
3121 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3122 break;
3123 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003124 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003125 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003126 case ISN_STORENR:
3127 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003128 iptr->isn_arg.storenr.stnr_val,
3129 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 break;
3131
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003132 case ISN_STORELIST:
3133 smsg("%4d STORELIST", current);
3134 break;
3135
3136 case ISN_STOREDICT:
3137 smsg("%4d STOREDICT", current);
3138 break;
3139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003140 // constants
3141 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003142 smsg("%4d PUSHNR %lld", current,
3143 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 break;
3145 case ISN_PUSHBOOL:
3146 case ISN_PUSHSPEC:
3147 smsg("%4d PUSH %s", current,
3148 get_var_special_name(iptr->isn_arg.number));
3149 break;
3150 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003151#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003153#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 break;
3155 case ISN_PUSHS:
3156 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3157 break;
3158 case ISN_PUSHBLOB:
3159 {
3160 char_u *r;
3161 char_u numbuf[NUMBUFLEN];
3162 char_u *tofree;
3163
3164 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003165 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003166 vim_free(tofree);
3167 }
3168 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003169 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003170 {
3171 char *name = (char *)iptr->isn_arg.string;
3172
3173 smsg("%4d PUSHFUNC \"%s\"", current,
3174 name == NULL ? "[none]" : name);
3175 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003176 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003177 case ISN_PUSHCHANNEL:
3178#ifdef FEAT_JOB_CHANNEL
3179 {
3180 channel_T *channel = iptr->isn_arg.channel;
3181
3182 smsg("%4d PUSHCHANNEL %d", current,
3183 channel == NULL ? 0 : channel->ch_id);
3184 }
3185#endif
3186 break;
3187 case ISN_PUSHJOB:
3188#ifdef FEAT_JOB_CHANNEL
3189 {
3190 typval_T tv;
3191 char_u *name;
3192
3193 tv.v_type = VAR_JOB;
3194 tv.vval.v_job = iptr->isn_arg.job;
3195 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003196 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003197 }
3198#endif
3199 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 case ISN_PUSHEXC:
3201 smsg("%4d PUSH v:exception", current);
3202 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003203 case ISN_UNLET:
3204 smsg("%4d UNLET%s %s", current,
3205 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3206 iptr->isn_arg.unlet.ul_name);
3207 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003208 case ISN_UNLETENV:
3209 smsg("%4d UNLETENV%s $%s", current,
3210 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3211 iptr->isn_arg.unlet.ul_name);
3212 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003213 case ISN_LOCKCONST:
3214 smsg("%4d LOCKCONST", current);
3215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003216 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003217 smsg("%4d NEWLIST size %lld", current,
3218 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 break;
3220 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003221 smsg("%4d NEWDICT size %lld", current,
3222 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 break;
3224
3225 // function call
3226 case ISN_BCALL:
3227 {
3228 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3229
3230 smsg("%4d BCALL %s(argc %d)", current,
3231 internal_func_name(cbfunc->cbf_idx),
3232 cbfunc->cbf_argcount);
3233 }
3234 break;
3235 case ISN_DCALL:
3236 {
3237 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3238 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3239 + cdfunc->cdf_idx;
3240
3241 smsg("%4d DCALL %s(argc %d)", current,
3242 df->df_ufunc->uf_name_exp != NULL
3243 ? df->df_ufunc->uf_name_exp
3244 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3245 }
3246 break;
3247 case ISN_UCALL:
3248 {
3249 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3250
3251 smsg("%4d UCALL %s(argc %d)", current,
3252 cufunc->cuf_name, cufunc->cuf_argcount);
3253 }
3254 break;
3255 case ISN_PCALL:
3256 {
3257 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3258
3259 smsg("%4d PCALL%s (argc %d)", current,
3260 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3261 }
3262 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003263 case ISN_PCALL_END:
3264 smsg("%4d PCALL end", current);
3265 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 case ISN_RETURN:
3267 smsg("%4d RETURN", current);
3268 break;
3269 case ISN_FUNCREF:
3270 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003271 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003272 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003273 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003275 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 }
3277 break;
3278
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003279 case ISN_NEWFUNC:
3280 {
3281 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3282
3283 smsg("%4d NEWFUNC %s %s", current,
3284 newfunc->nf_lambda, newfunc->nf_global);
3285 }
3286 break;
3287
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 case ISN_JUMP:
3289 {
3290 char *when = "?";
3291
3292 switch (iptr->isn_arg.jump.jump_when)
3293 {
3294 case JUMP_ALWAYS:
3295 when = "JUMP";
3296 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 case JUMP_AND_KEEP_IF_TRUE:
3298 when = "JUMP_AND_KEEP_IF_TRUE";
3299 break;
3300 case JUMP_IF_FALSE:
3301 when = "JUMP_IF_FALSE";
3302 break;
3303 case JUMP_AND_KEEP_IF_FALSE:
3304 when = "JUMP_AND_KEEP_IF_FALSE";
3305 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003306 case JUMP_IF_COND_FALSE:
3307 when = "JUMP_IF_COND_FALSE";
3308 break;
3309 case JUMP_IF_COND_TRUE:
3310 when = "JUMP_IF_COND_TRUE";
3311 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003313 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003314 iptr->isn_arg.jump.jump_where);
3315 }
3316 break;
3317
3318 case ISN_FOR:
3319 {
3320 forloop_T *forloop = &iptr->isn_arg.forloop;
3321
3322 smsg("%4d FOR $%d -> %d", current,
3323 forloop->for_idx, forloop->for_end);
3324 }
3325 break;
3326
3327 case ISN_TRY:
3328 {
3329 try_T *try = &iptr->isn_arg.try;
3330
3331 smsg("%4d TRY catch -> %d, finally -> %d", current,
3332 try->try_catch, try->try_finally);
3333 }
3334 break;
3335 case ISN_CATCH:
3336 // TODO
3337 smsg("%4d CATCH", current);
3338 break;
3339 case ISN_ENDTRY:
3340 smsg("%4d ENDTRY", current);
3341 break;
3342 case ISN_THROW:
3343 smsg("%4d THROW", current);
3344 break;
3345
3346 // expression operations on number
3347 case ISN_OPNR:
3348 case ISN_OPFLOAT:
3349 case ISN_OPANY:
3350 {
3351 char *what;
3352 char *ins;
3353
3354 switch (iptr->isn_arg.op.op_type)
3355 {
3356 case EXPR_MULT: what = "*"; break;
3357 case EXPR_DIV: what = "/"; break;
3358 case EXPR_REM: what = "%"; break;
3359 case EXPR_SUB: what = "-"; break;
3360 case EXPR_ADD: what = "+"; break;
3361 default: what = "???"; break;
3362 }
3363 switch (iptr->isn_type)
3364 {
3365 case ISN_OPNR: ins = "OPNR"; break;
3366 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3367 case ISN_OPANY: ins = "OPANY"; break;
3368 default: ins = "???"; break;
3369 }
3370 smsg("%4d %s %s", current, ins, what);
3371 }
3372 break;
3373
3374 case ISN_COMPAREBOOL:
3375 case ISN_COMPARESPECIAL:
3376 case ISN_COMPARENR:
3377 case ISN_COMPAREFLOAT:
3378 case ISN_COMPARESTRING:
3379 case ISN_COMPAREBLOB:
3380 case ISN_COMPARELIST:
3381 case ISN_COMPAREDICT:
3382 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 case ISN_COMPAREANY:
3384 {
3385 char *p;
3386 char buf[10];
3387 char *type;
3388
3389 switch (iptr->isn_arg.op.op_type)
3390 {
3391 case EXPR_EQUAL: p = "=="; break;
3392 case EXPR_NEQUAL: p = "!="; break;
3393 case EXPR_GREATER: p = ">"; break;
3394 case EXPR_GEQUAL: p = ">="; break;
3395 case EXPR_SMALLER: p = "<"; break;
3396 case EXPR_SEQUAL: p = "<="; break;
3397 case EXPR_MATCH: p = "=~"; break;
3398 case EXPR_IS: p = "is"; break;
3399 case EXPR_ISNOT: p = "isnot"; break;
3400 case EXPR_NOMATCH: p = "!~"; break;
3401 default: p = "???"; break;
3402 }
3403 STRCPY(buf, p);
3404 if (iptr->isn_arg.op.op_ic == TRUE)
3405 strcat(buf, "?");
3406 switch(iptr->isn_type)
3407 {
3408 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3409 case ISN_COMPARESPECIAL:
3410 type = "COMPARESPECIAL"; break;
3411 case ISN_COMPARENR: type = "COMPARENR"; break;
3412 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3413 case ISN_COMPARESTRING:
3414 type = "COMPARESTRING"; break;
3415 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3416 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3417 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3418 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3420 default: type = "???"; break;
3421 }
3422
3423 smsg("%4d %s %s", current, type, buf);
3424 }
3425 break;
3426
3427 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3428 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3429
3430 // expression operations
3431 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003432 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003433 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003434 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003435 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003436 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003437 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3438 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003439 case ISN_SLICE: smsg("%4d SLICE %lld",
3440 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003441 case ISN_GETITEM: smsg("%4d ITEM %lld",
3442 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003443 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3444 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 iptr->isn_arg.string); break;
3446 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3447
3448 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003449 case ISN_CHECKTYPE:
3450 {
3451 char *tofree;
3452
3453 smsg("%4d CHECKTYPE %s stack[%d]", current,
3454 type_name(iptr->isn_arg.type.ct_type, &tofree),
3455 iptr->isn_arg.type.ct_off);
3456 vim_free(tofree);
3457 break;
3458 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003459 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3460 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3461 iptr->isn_arg.checklen.cl_min_len);
3462 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003463 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 case ISN_2BOOL: if (iptr->isn_arg.number)
3465 smsg("%4d INVERT (!val)", current);
3466 else
3467 smsg("%4d 2BOOL (!!val)", current);
3468 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003469 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3470 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003471 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003472 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3473 (long long)(iptr->isn_arg.number));
3474 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003475 case ISN_PUT:
3476 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3477 (long)iptr->isn_arg.put.put_lnum);
3478 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479
Bram Moolenaar389df252020-07-09 21:20:47 +02003480 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3481 iptr->isn_arg.shuffle.shfl_item,
3482 iptr->isn_arg.shuffle.shfl_up);
3483 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 case ISN_DROP: smsg("%4d DROP", current); break;
3485 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003486
3487 out_flush(); // output one line at a time
3488 ui_breakcheck();
3489 if (got_int)
3490 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003492}
3493
3494/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003495 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003496 * list, etc. Mostly like what JavaScript does, except that empty list and
3497 * empty dictionary are FALSE.
3498 */
3499 int
3500tv2bool(typval_T *tv)
3501{
3502 switch (tv->v_type)
3503 {
3504 case VAR_NUMBER:
3505 return tv->vval.v_number != 0;
3506 case VAR_FLOAT:
3507#ifdef FEAT_FLOAT
3508 return tv->vval.v_float != 0.0;
3509#else
3510 break;
3511#endif
3512 case VAR_PARTIAL:
3513 return tv->vval.v_partial != NULL;
3514 case VAR_FUNC:
3515 case VAR_STRING:
3516 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3517 case VAR_LIST:
3518 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3519 case VAR_DICT:
3520 return tv->vval.v_dict != NULL
3521 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3522 case VAR_BOOL:
3523 case VAR_SPECIAL:
3524 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3525 case VAR_JOB:
3526#ifdef FEAT_JOB_CHANNEL
3527 return tv->vval.v_job != NULL;
3528#else
3529 break;
3530#endif
3531 case VAR_CHANNEL:
3532#ifdef FEAT_JOB_CHANNEL
3533 return tv->vval.v_channel != NULL;
3534#else
3535 break;
3536#endif
3537 case VAR_BLOB:
3538 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3539 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003540 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003541 case VAR_VOID:
3542 break;
3543 }
3544 return FALSE;
3545}
3546
3547/*
3548 * If "tv" is a string give an error and return FAIL.
3549 */
3550 int
3551check_not_string(typval_T *tv)
3552{
3553 if (tv->v_type == VAR_STRING)
3554 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003555 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 clear_tv(tv);
3557 return FAIL;
3558 }
3559 return OK;
3560}
3561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003562
3563#endif // FEAT_EVAL