blob: 2387ac9061e744863f05832139f98163b96fb96d [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;
352 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
353 + dfunc->df_varcount;
354 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
379 // Do not copy a partial created for a local function.
Bram Moolenaar0876c782020-10-07 19:08:04 +0200380 // TODO: This won't work if the closure actually uses it. But when
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200381 // keeping it it gets complicated: it will create a reference cycle
382 // inside the partial, thus needs special handling for garbage
383 // collection.
Bram Moolenaar0876c782020-10-07 19:08:04 +0200384 // For now, decide on the reference count.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200385 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
386 {
387 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200389 for (i = 0; i < closure_count; ++i)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200390 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200391 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
392 - closure_count + i];
Bram Moolenaar0876c782020-10-07 19:08:04 +0200393
394 if (tv->vval.v_partial == pt && pt->pt_refcount < 2)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200395 break;
396 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200397 if (i < closure_count)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200398 continue;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200399 }
400
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200401 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
402 tv->v_type = VAR_UNKNOWN;
403 }
404
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200405 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200407 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
408 - closure_count + idx];
409 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200410 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200411 ++funcstack->fs_refcount;
412 pt->pt_funcstack = funcstack;
413 pt->pt_ectx_stack = &funcstack->fs_ga;
414 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200415 }
416 }
417 }
418
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200419 for (idx = 0; idx < closure_count; ++idx)
420 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
421 - closure_count + idx]);
422 gap->ga_len -= closure_count;
423 if (gap->ga_len == 0)
424 ga_clear(gap);
425
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200426 return OK;
427}
428
429/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430 * Return from the current function.
431 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200432 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433func_return(ectx_T *ectx)
434{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200436 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
437 + ectx->ec_dfunc_idx;
438 int argcount = ufunc_argcount(dfunc->df_ufunc);
439 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200440 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441
442 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200443 entry = estack_pop();
444 if (entry != NULL)
445 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100446
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200447 if (handle_closure_in_use(ectx, TRUE) == FAIL)
448 return FAIL;
449
450 // Clear the arguments.
451 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
452 clear_tv(STACK_TV(idx));
453
454 // Clear local variables and temp values, but not the return value.
455 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100456 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100458
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100459 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200460 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
461 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200462 ectx->ec_outer_stack =
463 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
464 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
465 // restoring ec_frame_idx must be last
466 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
468 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100469
470 // Reset the stack to the position before the call, move the return value
471 // to the top of the stack.
472 idx = ectx->ec_stack.ga_len - 1;
473 ectx->ec_stack.ga_len = top + 1;
474 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200475
476 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477}
478
479#undef STACK_TV
480
481/*
482 * Prepare arguments and rettv for calling a builtin or user function.
483 */
484 static int
485call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
486{
487 int idx;
488 typval_T *tv;
489
490 // Move arguments from bottom of the stack to argvars[] and add terminator.
491 for (idx = 0; idx < argcount; ++idx)
492 argvars[idx] = *STACK_TV_BOT(idx - argcount);
493 argvars[argcount].v_type = VAR_UNKNOWN;
494
495 // Result replaces the arguments on the stack.
496 if (argcount > 0)
497 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200498 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100499 return FAIL;
500 else
501 ++ectx->ec_stack.ga_len;
502
503 // Default return value is zero.
504 tv = STACK_TV_BOT(-1);
505 tv->v_type = VAR_NUMBER;
506 tv->vval.v_number = 0;
507
508 return OK;
509}
510
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200511// Ugly global to avoid passing the execution context around through many
512// layers.
513static ectx_T *current_ectx = NULL;
514
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515/*
516 * Call a builtin function by index.
517 */
518 static int
519call_bfunc(int func_idx, int argcount, ectx_T *ectx)
520{
521 typval_T argvars[MAX_FUNC_ARGS];
522 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200523 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200524 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (call_prepare(argcount, argvars, ectx) == FAIL)
527 return FAIL;
528
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200529 // Call the builtin function. Set "current_ectx" so that when it
530 // recursively invokes call_def_function() a closure context can be set.
531 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200533 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534
535 // Clear the arguments.
536 for (idx = 0; idx < argcount; ++idx)
537 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200538
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200539 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200540 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100541 return OK;
542}
543
544/*
545 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100546 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100547 */
548 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100549call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550{
551 typval_T argvars[MAX_FUNC_ARGS];
552 funcexe_T funcexe;
553 int error;
554 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200555 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200557 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200558 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
559 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200560 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100561 {
562 // The function has been compiled, can call it quickly. For a function
563 // that was defined later: we can call it directly next time.
564 if (iptr != NULL)
565 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100566 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100567 iptr->isn_type = ISN_DCALL;
568 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
569 iptr->isn_arg.dfunc.cdf_argcount = argcount;
570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100572 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
574 if (call_prepare(argcount, argvars, ectx) == FAIL)
575 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200576 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 funcexe.evaluate = TRUE;
578
579 // Call the user function. Result goes in last position on the stack.
580 // TODO: add selfdict if there is one
581 error = call_user_func_check(ufunc, argcount, argvars,
582 STACK_TV_BOT(-1), &funcexe, NULL);
583
584 // Clear the arguments.
585 for (idx = 0; idx < argcount; ++idx)
586 clear_tv(&argvars[idx]);
587
588 if (error != FCERR_NONE)
589 {
590 user_func_error(error, ufunc->uf_name);
591 return FAIL;
592 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200593 if (called_emsg > called_emsg_before)
594 // Error other than from calling the function itself.
595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 return OK;
597}
598
599/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200600 * Return TRUE if an error was given or CTRL-C was pressed.
601 */
602 static int
603vim9_aborting(int prev_called_emsg)
604{
605 return called_emsg > prev_called_emsg || got_int || did_throw;
606}
607
608/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609 * Execute a function by "name".
610 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100611 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 * Returns FAIL if not found without an error message.
613 */
614 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100615call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616{
617 ufunc_T *ufunc;
618
619 if (builtin_function(name, -1))
620 {
621 int func_idx = find_internal_func(name);
622
623 if (func_idx < 0)
624 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200625 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 return FAIL;
627 return call_bfunc(func_idx, argcount, ectx);
628 }
629
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200630 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200631
632 if (ufunc == NULL)
633 {
634 int called_emsg_before = called_emsg;
635
636 if (script_autoload(name, TRUE))
637 // loaded a package, search for the function again
638 ufunc = find_func(name, FALSE, NULL);
639 if (vim9_aborting(called_emsg_before))
640 return FAIL; // bail out if loading the script caused an error
641 }
642
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100644 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645
646 return FAIL;
647}
648
649 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200650call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200652 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200653 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200655 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 if (tv->v_type == VAR_PARTIAL)
658 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200659 partial_T *pt = tv->vval.v_partial;
660 int i;
661
662 if (pt->pt_argc > 0)
663 {
664 // Make space for arguments from the partial, shift the "argcount"
665 // arguments up.
666 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
667 return FAIL;
668 for (i = 1; i <= argcount; ++i)
669 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
670 ectx->ec_stack.ga_len += pt->pt_argc;
671 argcount += pt->pt_argc;
672
673 // copy the arguments from the partial onto the stack
674 for (i = 0; i < pt->pt_argc; ++i)
675 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677
678 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200679 {
680 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
681
682 // closure may need the function context where it was defined
683 ectx->ec_outer_stack = pt->pt_ectx_stack;
684 ectx->ec_outer_frame = pt->pt_ectx_frame;
685
686 return ret;
687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 name = pt->pt_name;
689 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200690 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200692 if (name != NULL)
693 {
694 char_u fname_buf[FLEN_FIXED + 1];
695 char_u *tofree = NULL;
696 int error = FCERR_NONE;
697 char_u *fname;
698
699 // May need to translate <SNR>123_ to K_SNR.
700 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
701 if (error != FCERR_NONE)
702 res = FAIL;
703 else
704 res = call_by_name(fname, argcount, ectx, NULL);
705 vim_free(tofree);
706 }
707
708 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 {
710 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200711 semsg(_(e_unknownfunc),
712 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713 return FAIL;
714 }
715 return OK;
716}
717
718/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200719 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
720 * TRUE.
721 */
722 static int
723error_if_locked(int lock, char *error)
724{
725 if (lock & (VAR_LOCKED | VAR_FIXED))
726 {
727 emsg(_(error));
728 return TRUE;
729 }
730 return FALSE;
731}
732
733/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100734 * Store "tv" in variable "name".
735 * This is for s: and g: variables.
736 */
737 static void
738store_var(char_u *name, typval_T *tv)
739{
740 funccal_entry_T entry;
741
742 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200743 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100744 restore_funccal();
745}
746
Bram Moolenaard3aac292020-04-19 14:32:17 +0200747
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100748/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 * Execute a function by "name".
750 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100751 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 */
753 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100754call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755{
Bram Moolenaared677f52020-08-12 16:38:10 +0200756 int called_emsg_before = called_emsg;
757 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
Bram Moolenaared677f52020-08-12 16:38:10 +0200759 res = call_by_name(name, argcount, ectx, iptr);
760 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200762 dictitem_T *v;
763
764 v = find_var(name, NULL, FALSE);
765 if (v == NULL)
766 {
767 semsg(_(e_unknownfunc), name);
768 return FAIL;
769 }
770 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
771 {
772 semsg(_(e_unknownfunc), name);
773 return FAIL;
774 }
775 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200777 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778}
779
780/*
781 * Call a "def" function from old Vim script.
782 * Return OK or FAIL.
783 */
784 int
785call_def_function(
786 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200787 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200789 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100790 typval_T *rettv) // return value
791{
792 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200793 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200794 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 typval_T *tv;
796 int idx;
797 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100798 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200799 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200800 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200801 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200802 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803
804// Get pointer to item in the stack.
805#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
806
807// Get pointer to item at the bottom of the stack, -1 is the bottom.
808#undef STACK_TV_BOT
809#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
810
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200811// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200812#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 +0100813
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200814// Like STACK_TV_VAR but use the outer scope
815#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
816
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200817 if (ufunc->uf_def_status == UF_NOT_COMPILED
818 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200819 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200820 {
821 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200822 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200823 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200824 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200825 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200826
Bram Moolenaar09689a02020-05-09 22:50:08 +0200827 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200828 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200829 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
830 + ufunc->uf_dfunc_idx;
831 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200832 {
833 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200834 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200835 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200838 CLEAR_FIELD(ectx);
839 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
840 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
841 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
842 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200844 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845
846 // Put arguments on the stack.
847 for (idx = 0; idx < argc; ++idx)
848 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200849 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200850 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
851 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200852 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 copy_tv(&argv[idx], STACK_TV_BOT(0));
854 ++ectx.ec_stack.ga_len;
855 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200856
857 // Turn varargs into a list. Empty list if no args.
858 if (ufunc->uf_va_name != NULL)
859 {
860 int vararg_count = argc - ufunc->uf_args.ga_len;
861
862 if (vararg_count < 0)
863 vararg_count = 0;
864 else
865 argc -= vararg_count;
866 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200867 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200868
869 // Check the type of the list items.
870 tv = STACK_TV_BOT(-1);
871 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200872 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200873 && ufunc->uf_va_type->tt_member != &t_any
874 && tv->vval.v_list != NULL)
875 {
876 type_T *expected = ufunc->uf_va_type->tt_member;
877 listitem_T *li = tv->vval.v_list->lv_first;
878
879 for (idx = 0; idx < vararg_count; ++idx)
880 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200881 if (check_typval_type(expected, &li->li_tv,
882 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200883 goto failed_early;
884 li = li->li_next;
885 }
886 }
887
Bram Moolenaar23e03252020-04-12 22:22:31 +0200888 if (defcount > 0)
889 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200890 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200891 --ectx.ec_stack.ga_len;
892 }
893
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100894 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200895 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100896 if (defcount > 0)
897 for (idx = 0; idx < defcount; ++idx)
898 {
899 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
900 ++ectx.ec_stack.ga_len;
901 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200902 if (ufunc->uf_va_name != NULL)
903 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904
905 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200906 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
907 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200909 if (partial != NULL)
910 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200911 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
912 {
913 // TODO: is this always the right way?
914 ectx.ec_outer_stack = &current_ectx->ec_stack;
915 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
916 }
917 else
918 {
919 ectx.ec_outer_stack = partial->pt_ectx_stack;
920 ectx.ec_outer_frame = partial->pt_ectx_frame;
921 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200922 }
923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 // dummy frame entries
925 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
926 {
927 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
928 ++ectx.ec_stack.ga_len;
929 }
930
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200932 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200933 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
934 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200936 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200937 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200938 ectx.ec_stack.ga_len += dfunc->df_varcount;
939 if (dfunc->df_has_closure)
940 {
941 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
942 STACK_TV_VAR(idx)->vval.v_number = 0;
943 ++ectx.ec_stack.ga_len;
944 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200945
946 ectx.ec_instr = dfunc->df_instr;
947 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100948
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200949 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200950 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200951 estack_push_ufunc(ufunc, 1);
952 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200953 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
954
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200955 // Do turn errors into exceptions.
956 suppress_errthrow = FALSE;
957
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100958 // Decide where to start execution, handles optional arguments.
959 init_instr_idx(ufunc, argc, &ectx);
960
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100961 for (;;)
962 {
963 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100964
Bram Moolenaar270d0382020-05-15 21:42:53 +0200965 if (++breakcheck_count >= 100)
966 {
967 line_breakcheck();
968 breakcheck_count = 0;
969 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100970 if (got_int)
971 {
972 // Turn CTRL-C into an exception.
973 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100974 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100975 goto failed;
976 did_throw = TRUE;
977 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978
Bram Moolenaara26b9702020-04-18 19:53:28 +0200979 if (did_emsg && msg_list != NULL && *msg_list != NULL)
980 {
981 // Turn an error message into an exception.
982 did_emsg = FALSE;
983 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
984 goto failed;
985 did_throw = TRUE;
986 *msg_list = NULL;
987 }
988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 if (did_throw && !ectx.ec_in_catch)
990 {
991 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100992 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993
994 // An exception jumps to the first catch, finally, or returns from
995 // the current function.
996 if (trystack->ga_len > 0)
997 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200998 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 {
1000 // jump to ":catch" or ":finally"
1001 ectx.ec_in_catch = TRUE;
1002 ectx.ec_iidx = trycmd->tcd_catch_idx;
1003 }
1004 else
1005 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001006 // Not inside try or need to return from current functions.
1007 // Push a dummy return value.
1008 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1009 goto failed;
1010 tv = STACK_TV_BOT(0);
1011 tv->v_type = VAR_NUMBER;
1012 tv->vval.v_number = 0;
1013 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001014 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001016 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001017 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001018 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1019 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 goto done;
1021 }
1022
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001023 if (func_return(&ectx) == FAIL)
1024 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001025 }
1026 continue;
1027 }
1028
1029 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1030 switch (iptr->isn_type)
1031 {
1032 // execute Ex command line
1033 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001034 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 do_cmdline_cmd(iptr->isn_arg.string);
1036 break;
1037
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001038 // execute Ex command from pieces on the stack
1039 case ISN_EXECCONCAT:
1040 {
1041 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001042 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001043 int pass;
1044 int i;
1045 char_u *cmd = NULL;
1046 char_u *str;
1047
1048 for (pass = 1; pass <= 2; ++pass)
1049 {
1050 for (i = 0; i < count; ++i)
1051 {
1052 tv = STACK_TV_BOT(i - count);
1053 str = tv->vval.v_string;
1054 if (str != NULL && *str != NUL)
1055 {
1056 if (pass == 2)
1057 STRCPY(cmd + len, str);
1058 len += STRLEN(str);
1059 }
1060 if (pass == 2)
1061 clear_tv(tv);
1062 }
1063 if (pass == 1)
1064 {
1065 cmd = alloc(len + 1);
1066 if (cmd == NULL)
1067 goto failed;
1068 len = 0;
1069 }
1070 }
1071
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001072 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001073 do_cmdline_cmd(cmd);
1074 vim_free(cmd);
1075 }
1076 break;
1077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 // execute :echo {string} ...
1079 case ISN_ECHO:
1080 {
1081 int count = iptr->isn_arg.echo.echo_count;
1082 int atstart = TRUE;
1083 int needclr = TRUE;
1084
1085 for (idx = 0; idx < count; ++idx)
1086 {
1087 tv = STACK_TV_BOT(idx - count);
1088 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1089 &atstart, &needclr);
1090 clear_tv(tv);
1091 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001092 if (needclr)
1093 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 ectx.ec_stack.ga_len -= count;
1095 }
1096 break;
1097
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001098 // :execute {string} ...
1099 // :echomsg {string} ...
1100 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001101 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001102 case ISN_ECHOMSG:
1103 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001104 {
1105 int count = iptr->isn_arg.number;
1106 garray_T ga;
1107 char_u buf[NUMBUFLEN];
1108 char_u *p;
1109 int len;
1110 int failed = FALSE;
1111
1112 ga_init2(&ga, 1, 80);
1113 for (idx = 0; idx < count; ++idx)
1114 {
1115 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001116 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001117 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001118 if (tv->v_type == VAR_CHANNEL
1119 || tv->v_type == VAR_JOB)
1120 {
1121 SOURCING_LNUM = iptr->isn_lnum;
1122 emsg(_(e_inval_string));
1123 break;
1124 }
1125 else
1126 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001127 }
1128 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001129 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001130
1131 len = (int)STRLEN(p);
1132 if (ga_grow(&ga, len + 2) == FAIL)
1133 failed = TRUE;
1134 else
1135 {
1136 if (ga.ga_len > 0)
1137 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1138 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1139 ga.ga_len += len;
1140 }
1141 clear_tv(tv);
1142 }
1143 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001144 if (failed)
1145 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001146
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001147 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001148 {
1149 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001150 {
1151 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001152 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001153 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001154 else
1155 {
1156 msg_sb_eol();
1157 if (iptr->isn_type == ISN_ECHOMSG)
1158 {
1159 msg_attr(ga.ga_data, echo_attr);
1160 out_flush();
1161 }
1162 else
1163 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001164 SOURCING_LNUM = iptr->isn_lnum;
1165 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001166 }
1167 }
1168 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001169 ga_clear(&ga);
1170 }
1171 break;
1172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 // load local variable or argument
1174 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001175 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 goto failed;
1177 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1178 ++ectx.ec_stack.ga_len;
1179 break;
1180
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001181 // load variable or argument from outer scope
1182 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001183 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001184 goto failed;
1185 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1186 STACK_TV_BOT(0));
1187 ++ectx.ec_stack.ga_len;
1188 break;
1189
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 // load v: variable
1191 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001192 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 goto failed;
1194 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1195 ++ectx.ec_stack.ga_len;
1196 break;
1197
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 case ISN_LOADSCRIPT:
1200 {
1201 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001202 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 svar_T *sv;
1204
1205 sv = ((svar_T *)si->sn_var_vals.ga_data)
1206 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001207 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 goto failed;
1209 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1210 ++ectx.ec_stack.ga_len;
1211 }
1212 break;
1213
1214 // load s: variable in old script
1215 case ISN_LOADS:
1216 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001217 hashtab_T *ht = &SCRIPT_VARS(
1218 iptr->isn_arg.loadstore.ls_sid);
1219 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001221
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 if (di == NULL)
1223 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001224 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001225 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001226 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227 }
1228 else
1229 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001230 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 goto failed;
1232 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1233 ++ectx.ec_stack.ga_len;
1234 }
1235 }
1236 break;
1237
Bram Moolenaard3aac292020-04-19 14:32:17 +02001238 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001240 case ISN_LOADB:
1241 case ISN_LOADW:
1242 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001244 dictitem_T *di = NULL;
1245 hashtab_T *ht = NULL;
1246 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001247
Bram Moolenaard3aac292020-04-19 14:32:17 +02001248 switch (iptr->isn_type)
1249 {
1250 case ISN_LOADG:
1251 ht = get_globvar_ht();
1252 namespace = 'g';
1253 break;
1254 case ISN_LOADB:
1255 ht = &curbuf->b_vars->dv_hashtab;
1256 namespace = 'b';
1257 break;
1258 case ISN_LOADW:
1259 ht = &curwin->w_vars->dv_hashtab;
1260 namespace = 'w';
1261 break;
1262 case ISN_LOADT:
1263 ht = &curtab->tp_vars->dv_hashtab;
1264 namespace = 't';
1265 break;
1266 default: // Cannot reach here
1267 goto failed;
1268 }
1269 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 if (di == NULL)
1272 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001273 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001274 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001275 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001276 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 }
1278 else
1279 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001280 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 goto failed;
1282 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1283 ++ectx.ec_stack.ga_len;
1284 }
1285 }
1286 break;
1287
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001288 // load g:/b:/w:/t: namespace
1289 case ISN_LOADGDICT:
1290 case ISN_LOADBDICT:
1291 case ISN_LOADWDICT:
1292 case ISN_LOADTDICT:
1293 {
1294 dict_T *d = NULL;
1295
1296 switch (iptr->isn_type)
1297 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001298 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1299 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1300 case ISN_LOADWDICT: d = curwin->w_vars; break;
1301 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001302 default: // Cannot reach here
1303 goto failed;
1304 }
1305 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1306 goto failed;
1307 tv = STACK_TV_BOT(0);
1308 tv->v_type = VAR_DICT;
1309 tv->v_lock = 0;
1310 tv->vval.v_dict = d;
1311 ++ectx.ec_stack.ga_len;
1312 }
1313 break;
1314
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 // load &option
1316 case ISN_LOADOPT:
1317 {
1318 typval_T optval;
1319 char_u *name = iptr->isn_arg.string;
1320
Bram Moolenaara8c17702020-04-01 21:17:24 +02001321 // This is not expected to fail, name is checked during
1322 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001323 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001325 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001326 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 *STACK_TV_BOT(0) = optval;
1328 ++ectx.ec_stack.ga_len;
1329 }
1330 break;
1331
1332 // load $ENV
1333 case ISN_LOADENV:
1334 {
1335 typval_T optval;
1336 char_u *name = iptr->isn_arg.string;
1337
Bram Moolenaar270d0382020-05-15 21:42:53 +02001338 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001340 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001341 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342 *STACK_TV_BOT(0) = optval;
1343 ++ectx.ec_stack.ga_len;
1344 }
1345 break;
1346
1347 // load @register
1348 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001349 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 goto failed;
1351 tv = STACK_TV_BOT(0);
1352 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001353 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 tv->vval.v_string = get_reg_contents(
1355 iptr->isn_arg.number, GREG_EXPR_SRC);
1356 ++ectx.ec_stack.ga_len;
1357 break;
1358
1359 // store local variable
1360 case ISN_STORE:
1361 --ectx.ec_stack.ga_len;
1362 tv = STACK_TV_VAR(iptr->isn_arg.number);
1363 clear_tv(tv);
1364 *tv = *STACK_TV_BOT(0);
1365 break;
1366
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001367 // store variable or argument in outer scope
1368 case ISN_STOREOUTER:
1369 --ectx.ec_stack.ga_len;
1370 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1371 clear_tv(tv);
1372 *tv = *STACK_TV_BOT(0);
1373 break;
1374
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001375 // store s: variable in old script
1376 case ISN_STORES:
1377 {
1378 hashtab_T *ht = &SCRIPT_VARS(
1379 iptr->isn_arg.loadstore.ls_sid);
1380 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001381 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001382
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001383 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001384 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001385 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001386 else
1387 {
1388 clear_tv(&di->di_tv);
1389 di->di_tv = *STACK_TV_BOT(0);
1390 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001391 }
1392 break;
1393
1394 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 case ISN_STORESCRIPT:
1396 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001397 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 iptr->isn_arg.script.script_sid);
1399 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1400 + iptr->isn_arg.script.script_idx;
1401
1402 --ectx.ec_stack.ga_len;
1403 clear_tv(sv->sv_tv);
1404 *sv->sv_tv = *STACK_TV_BOT(0);
1405 }
1406 break;
1407
1408 // store option
1409 case ISN_STOREOPT:
1410 {
1411 long n = 0;
1412 char_u *s = NULL;
1413 char *msg;
1414
1415 --ectx.ec_stack.ga_len;
1416 tv = STACK_TV_BOT(0);
1417 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001418 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001419 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001420 if (s == NULL)
1421 s = (char_u *)"";
1422 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001424 // must be VAR_NUMBER, CHECKTYPE makes sure
1425 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1427 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001428 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 if (msg != NULL)
1430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001433 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 }
1436 break;
1437
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001438 // store $ENV
1439 case ISN_STOREENV:
1440 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001441 tv = STACK_TV_BOT(0);
1442 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1443 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001444 break;
1445
1446 // store @r
1447 case ISN_STOREREG:
1448 {
1449 int reg = iptr->isn_arg.number;
1450
1451 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001452 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001453 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001454 tv_get_string(tv), -1, FALSE);
1455 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001456 }
1457 break;
1458
1459 // store v: variable
1460 case ISN_STOREV:
1461 --ectx.ec_stack.ga_len;
1462 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1463 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001464 // should not happen, type is checked when compiling
1465 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001466 break;
1467
Bram Moolenaard3aac292020-04-19 14:32:17 +02001468 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001470 case ISN_STOREB:
1471 case ISN_STOREW:
1472 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 {
1474 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001475 hashtab_T *ht;
1476 switch (iptr->isn_type)
1477 {
1478 case ISN_STOREG:
1479 ht = get_globvar_ht();
1480 break;
1481 case ISN_STOREB:
1482 ht = &curbuf->b_vars->dv_hashtab;
1483 break;
1484 case ISN_STOREW:
1485 ht = &curwin->w_vars->dv_hashtab;
1486 break;
1487 case ISN_STORET:
1488 ht = &curtab->tp_vars->dv_hashtab;
1489 break;
1490 default: // Cannot reach here
1491 goto failed;
1492 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493
1494 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001495 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001497 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 else
1499 {
1500 clear_tv(&di->di_tv);
1501 di->di_tv = *STACK_TV_BOT(0);
1502 }
1503 }
1504 break;
1505
1506 // store number in local variable
1507 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001508 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 clear_tv(tv);
1510 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001511 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001512 break;
1513
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001514 // store value in list variable
1515 case ISN_STORELIST:
1516 {
1517 typval_T *tv_idx = STACK_TV_BOT(-2);
1518 varnumber_T lidx = tv_idx->vval.v_number;
1519 typval_T *tv_list = STACK_TV_BOT(-1);
1520 list_T *list = tv_list->vval.v_list;
1521
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001522 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001523 if (lidx < 0 && list->lv_len + lidx >= 0)
1524 // negative index is relative to the end
1525 lidx = list->lv_len + lidx;
1526 if (lidx < 0 || lidx > list->lv_len)
1527 {
1528 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001529 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001530 }
1531 tv = STACK_TV_BOT(-3);
1532 if (lidx < list->lv_len)
1533 {
1534 listitem_T *li = list_find(list, lidx);
1535
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001536 if (error_if_locked(li->li_tv.v_lock,
1537 e_cannot_change_list_item))
1538 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001539 // overwrite existing list item
1540 clear_tv(&li->li_tv);
1541 li->li_tv = *tv;
1542 }
1543 else
1544 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001545 if (error_if_locked(list->lv_lock,
1546 e_cannot_change_list))
1547 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001548 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001549 if (list_append_tv(list, tv) == FAIL)
1550 goto failed;
1551 clear_tv(tv);
1552 }
1553 clear_tv(tv_idx);
1554 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001555 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001556 }
1557 break;
1558
1559 // store value in dict variable
1560 case ISN_STOREDICT:
1561 {
1562 typval_T *tv_key = STACK_TV_BOT(-2);
1563 char_u *key = tv_key->vval.v_string;
1564 typval_T *tv_dict = STACK_TV_BOT(-1);
1565 dict_T *dict = tv_dict->vval.v_dict;
1566 dictitem_T *di;
1567
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001568 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001569 if (dict == NULL)
1570 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001571 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001572 goto on_error;
1573 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001574 if (key == NULL)
1575 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001576 tv = STACK_TV_BOT(-3);
1577 di = dict_find(dict, key, -1);
1578 if (di != NULL)
1579 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001580 if (error_if_locked(di->di_tv.v_lock,
1581 e_cannot_change_dict_item))
1582 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001583 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001584 clear_tv(&di->di_tv);
1585 di->di_tv = *tv;
1586 }
1587 else
1588 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001589 if (error_if_locked(dict->dv_lock,
1590 e_cannot_change_dict))
1591 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001592 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001593 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1594 goto failed;
1595 clear_tv(tv);
1596 }
1597 clear_tv(tv_key);
1598 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001599 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001600 }
1601 break;
1602
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 // push constant
1604 case ISN_PUSHNR:
1605 case ISN_PUSHBOOL:
1606 case ISN_PUSHSPEC:
1607 case ISN_PUSHF:
1608 case ISN_PUSHS:
1609 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001610 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001611 case ISN_PUSHCHANNEL:
1612 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001613 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 goto failed;
1615 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001616 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 ++ectx.ec_stack.ga_len;
1618 switch (iptr->isn_type)
1619 {
1620 case ISN_PUSHNR:
1621 tv->v_type = VAR_NUMBER;
1622 tv->vval.v_number = iptr->isn_arg.number;
1623 break;
1624 case ISN_PUSHBOOL:
1625 tv->v_type = VAR_BOOL;
1626 tv->vval.v_number = iptr->isn_arg.number;
1627 break;
1628 case ISN_PUSHSPEC:
1629 tv->v_type = VAR_SPECIAL;
1630 tv->vval.v_number = iptr->isn_arg.number;
1631 break;
1632#ifdef FEAT_FLOAT
1633 case ISN_PUSHF:
1634 tv->v_type = VAR_FLOAT;
1635 tv->vval.v_float = iptr->isn_arg.fnumber;
1636 break;
1637#endif
1638 case ISN_PUSHBLOB:
1639 blob_copy(iptr->isn_arg.blob, tv);
1640 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001641 case ISN_PUSHFUNC:
1642 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001643 if (iptr->isn_arg.string == NULL)
1644 tv->vval.v_string = NULL;
1645 else
1646 tv->vval.v_string =
1647 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001648 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001649 case ISN_PUSHCHANNEL:
1650#ifdef FEAT_JOB_CHANNEL
1651 tv->v_type = VAR_CHANNEL;
1652 tv->vval.v_channel = iptr->isn_arg.channel;
1653 if (tv->vval.v_channel != NULL)
1654 ++tv->vval.v_channel->ch_refcount;
1655#endif
1656 break;
1657 case ISN_PUSHJOB:
1658#ifdef FEAT_JOB_CHANNEL
1659 tv->v_type = VAR_JOB;
1660 tv->vval.v_job = iptr->isn_arg.job;
1661 if (tv->vval.v_job != NULL)
1662 ++tv->vval.v_job->jv_refcount;
1663#endif
1664 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 default:
1666 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001667 tv->vval.v_string = vim_strsave(
1668 iptr->isn_arg.string == NULL
1669 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 }
1671 break;
1672
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001673 case ISN_UNLET:
1674 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1675 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001676 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001677 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001678 case ISN_UNLETENV:
1679 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1680 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001681
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001682 case ISN_LOCKCONST:
1683 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1684 break;
1685
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 // create a list from items on the stack; uses a single allocation
1687 // for the list header and the items
1688 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001689 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1690 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 break;
1692
1693 // create a dict from items on the stack
1694 case ISN_NEWDICT:
1695 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001696 int count = iptr->isn_arg.number;
1697 dict_T *dict = dict_alloc();
1698 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699
1700 if (dict == NULL)
1701 goto failed;
1702 for (idx = 0; idx < count; ++idx)
1703 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001704 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001706 // check key is unique
1707 item = dict_find(dict, tv->vval.v_string, -1);
1708 if (item != NULL)
1709 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001710 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001711 semsg(_(e_duplicate_key), tv->vval.v_string);
1712 dict_unref(dict);
1713 goto on_error;
1714 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001715 item = dictitem_alloc(tv->vval.v_string);
1716 clear_tv(tv);
1717 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001718 {
1719 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1723 item->di_tv.v_lock = 0;
1724 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001725 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001726 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001727 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 }
1731
1732 if (count > 0)
1733 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001734 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 goto failed;
1736 else
1737 ++ectx.ec_stack.ga_len;
1738 tv = STACK_TV_BOT(-1);
1739 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001740 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 tv->vval.v_dict = dict;
1742 ++dict->dv_refcount;
1743 }
1744 break;
1745
1746 // call a :def function
1747 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001748 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1750 iptr->isn_arg.dfunc.cdf_argcount,
1751 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001752 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 break;
1754
1755 // call a builtin function
1756 case ISN_BCALL:
1757 SOURCING_LNUM = iptr->isn_lnum;
1758 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1759 iptr->isn_arg.bfunc.cbf_argcount,
1760 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001761 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 break;
1763
1764 // call a funcref or partial
1765 case ISN_PCALL:
1766 {
1767 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1768 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001769 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770
1771 SOURCING_LNUM = iptr->isn_lnum;
1772 if (pfunc->cpf_top)
1773 {
1774 // funcref is above the arguments
1775 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1776 }
1777 else
1778 {
1779 // Get the funcref from the stack.
1780 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001781 partial_tv = *STACK_TV_BOT(0);
1782 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 }
1784 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001785 if (tv == &partial_tv)
1786 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001788 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 }
1790 break;
1791
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001792 case ISN_PCALL_END:
1793 // PCALL finished, arguments have been consumed and replaced by
1794 // the return value. Now clear the funcref from the stack,
1795 // and move the return value in its place.
1796 --ectx.ec_stack.ga_len;
1797 clear_tv(STACK_TV_BOT(-1));
1798 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1799 break;
1800
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 // call a user defined function or funcref/partial
1802 case ISN_UCALL:
1803 {
1804 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1805
1806 SOURCING_LNUM = iptr->isn_lnum;
1807 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001808 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001809 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 }
1811 break;
1812
1813 // return from a :def function call
1814 case ISN_RETURN:
1815 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001816 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001817 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001818
1819 if (trystack->ga_len > 0)
1820 trycmd = ((trycmd_T *)trystack->ga_data)
1821 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001822 if (trycmd != NULL
1823 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 && trycmd->tcd_finally_idx != 0)
1825 {
1826 // jump to ":finally"
1827 ectx.ec_iidx = trycmd->tcd_finally_idx;
1828 trycmd->tcd_return = TRUE;
1829 }
1830 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001831 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 }
1833 break;
1834
1835 // push a function reference to a compiled function
1836 case ISN_FUNCREF:
1837 {
1838 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001839 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840
1841 pt = ALLOC_CLEAR_ONE(partial_T);
1842 if (pt == NULL)
1843 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001844 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001845 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001846 vim_free(pt);
1847 goto failed;
1848 }
1849 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1850 + iptr->isn_arg.funcref.fr_func;
1851 pt->pt_func = pt_dfunc->df_ufunc;
1852 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001853
1854 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1855 {
1856 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1857 + ectx.ec_dfunc_idx;
1858
1859 // The closure needs to find arguments and local
1860 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001861 pt->pt_ectx_stack = &ectx.ec_stack;
1862 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001863
1864 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001865 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001866 // (arguments and local variables). Store a reference
1867 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001868 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001869 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001870 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001871 goto failed;
1872 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001873 // Extra variable keeps the count of closures created
1874 // in the current function call.
1875 tv = STACK_TV_VAR(dfunc->df_varcount);
1876 ++tv->vval.v_number;
1877
1878 ((partial_T **)ectx.ec_funcrefs.ga_data)
1879 [ectx.ec_funcrefs.ga_len] = pt;
1880 ++pt->pt_refcount;
1881 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001882 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001883 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 tv = STACK_TV_BOT(0);
1886 ++ectx.ec_stack.ga_len;
1887 tv->vval.v_partial = pt;
1888 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001889 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 }
1891 break;
1892
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001893 // Create a global function from a lambda.
1894 case ISN_NEWFUNC:
1895 {
1896 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1897
1898 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1899 }
1900 break;
1901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 // jump if a condition is met
1903 case ISN_JUMP:
1904 {
1905 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001906 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 int jump = TRUE;
1908
1909 if (when != JUMP_ALWAYS)
1910 {
1911 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001912 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001913 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001914 || when == JUMP_IF_COND_TRUE)
1915 {
1916 SOURCING_LNUM = iptr->isn_lnum;
1917 jump = tv_get_bool_chk(tv, &error);
1918 if (error)
1919 goto on_error;
1920 }
1921 else
1922 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001924 || when == JUMP_AND_KEEP_IF_FALSE
1925 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001927 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 {
1929 // drop the value from the stack
1930 clear_tv(tv);
1931 --ectx.ec_stack.ga_len;
1932 }
1933 }
1934 if (jump)
1935 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1936 }
1937 break;
1938
1939 // top of a for loop
1940 case ISN_FOR:
1941 {
1942 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1943 typval_T *idxtv =
1944 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1945
1946 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001947 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001949 ++idxtv->vval.v_number;
1950 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001951 // past the end of the list, jump to "endfor"
1952 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1953 else if (list->lv_first == &range_list_item)
1954 {
1955 // non-materialized range() list
1956 tv = STACK_TV_BOT(0);
1957 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001958 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001959 tv->vval.v_number = list_find_nr(
1960 list, idxtv->vval.v_number, NULL);
1961 ++ectx.ec_stack.ga_len;
1962 }
1963 else
1964 {
1965 listitem_T *li = list_find(list, idxtv->vval.v_number);
1966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1968 ++ectx.ec_stack.ga_len;
1969 }
1970 }
1971 break;
1972
1973 // start of ":try" block
1974 case ISN_TRY:
1975 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001976 trycmd_T *trycmd = NULL;
1977
Bram Moolenaar270d0382020-05-15 21:42:53 +02001978 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 goto failed;
1980 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1981 + ectx.ec_trystack.ga_len;
1982 ++ectx.ec_trystack.ga_len;
1983 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001984 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1986 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001987 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001988 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 }
1990 break;
1991
1992 case ISN_PUSHEXC:
1993 if (current_exception == NULL)
1994 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001995 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 iemsg("Evaluating catch while current_exception is NULL");
1997 goto failed;
1998 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001999 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 goto failed;
2001 tv = STACK_TV_BOT(0);
2002 ++ectx.ec_stack.ga_len;
2003 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002004 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002005 tv->vval.v_string = vim_strsave(
2006 (char_u *)current_exception->value);
2007 break;
2008
2009 case ISN_CATCH:
2010 {
2011 garray_T *trystack = &ectx.ec_trystack;
2012
2013 if (trystack->ga_len > 0)
2014 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002015 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 + trystack->ga_len - 1;
2017 trycmd->tcd_caught = TRUE;
2018 }
2019 did_emsg = got_int = did_throw = FALSE;
2020 catch_exception(current_exception);
2021 }
2022 break;
2023
2024 // end of ":try" block
2025 case ISN_ENDTRY:
2026 {
2027 garray_T *trystack = &ectx.ec_trystack;
2028
2029 if (trystack->ga_len > 0)
2030 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002031 trycmd_T *trycmd = NULL;
2032
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 --trystack->ga_len;
2034 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002035 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 trycmd = ((trycmd_T *)trystack->ga_data)
2037 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002038 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 {
2040 // discard the exception
2041 if (caught_stack == current_exception)
2042 caught_stack = caught_stack->caught;
2043 discard_current_exception();
2044 }
2045
2046 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002047 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 }
2049 }
2050 break;
2051
2052 case ISN_THROW:
2053 --ectx.ec_stack.ga_len;
2054 tv = STACK_TV_BOT(0);
2055 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2056 {
2057 vim_free(tv->vval.v_string);
2058 goto failed;
2059 }
2060 did_throw = TRUE;
2061 break;
2062
2063 // compare with special values
2064 case ISN_COMPAREBOOL:
2065 case ISN_COMPARESPECIAL:
2066 {
2067 typval_T *tv1 = STACK_TV_BOT(-2);
2068 typval_T *tv2 = STACK_TV_BOT(-1);
2069 varnumber_T arg1 = tv1->vval.v_number;
2070 varnumber_T arg2 = tv2->vval.v_number;
2071 int res;
2072
2073 switch (iptr->isn_arg.op.op_type)
2074 {
2075 case EXPR_EQUAL: res = arg1 == arg2; break;
2076 case EXPR_NEQUAL: res = arg1 != arg2; break;
2077 default: res = 0; break;
2078 }
2079
2080 --ectx.ec_stack.ga_len;
2081 tv1->v_type = VAR_BOOL;
2082 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2083 }
2084 break;
2085
2086 // Operation with two number arguments
2087 case ISN_OPNR:
2088 case ISN_COMPARENR:
2089 {
2090 typval_T *tv1 = STACK_TV_BOT(-2);
2091 typval_T *tv2 = STACK_TV_BOT(-1);
2092 varnumber_T arg1 = tv1->vval.v_number;
2093 varnumber_T arg2 = tv2->vval.v_number;
2094 varnumber_T res;
2095
2096 switch (iptr->isn_arg.op.op_type)
2097 {
2098 case EXPR_MULT: res = arg1 * arg2; break;
2099 case EXPR_DIV: res = arg1 / arg2; break;
2100 case EXPR_REM: res = arg1 % arg2; break;
2101 case EXPR_SUB: res = arg1 - arg2; break;
2102 case EXPR_ADD: res = arg1 + arg2; break;
2103
2104 case EXPR_EQUAL: res = arg1 == arg2; break;
2105 case EXPR_NEQUAL: res = arg1 != arg2; break;
2106 case EXPR_GREATER: res = arg1 > arg2; break;
2107 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2108 case EXPR_SMALLER: res = arg1 < arg2; break;
2109 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2110 default: res = 0; break;
2111 }
2112
2113 --ectx.ec_stack.ga_len;
2114 if (iptr->isn_type == ISN_COMPARENR)
2115 {
2116 tv1->v_type = VAR_BOOL;
2117 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2118 }
2119 else
2120 tv1->vval.v_number = res;
2121 }
2122 break;
2123
2124 // Computation with two float arguments
2125 case ISN_OPFLOAT:
2126 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002127#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 {
2129 typval_T *tv1 = STACK_TV_BOT(-2);
2130 typval_T *tv2 = STACK_TV_BOT(-1);
2131 float_T arg1 = tv1->vval.v_float;
2132 float_T arg2 = tv2->vval.v_float;
2133 float_T res = 0;
2134 int cmp = FALSE;
2135
2136 switch (iptr->isn_arg.op.op_type)
2137 {
2138 case EXPR_MULT: res = arg1 * arg2; break;
2139 case EXPR_DIV: res = arg1 / arg2; break;
2140 case EXPR_SUB: res = arg1 - arg2; break;
2141 case EXPR_ADD: res = arg1 + arg2; break;
2142
2143 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2144 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2145 case EXPR_GREATER: cmp = arg1 > arg2; break;
2146 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2147 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2148 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2149 default: cmp = 0; break;
2150 }
2151 --ectx.ec_stack.ga_len;
2152 if (iptr->isn_type == ISN_COMPAREFLOAT)
2153 {
2154 tv1->v_type = VAR_BOOL;
2155 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2156 }
2157 else
2158 tv1->vval.v_float = res;
2159 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002160#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 break;
2162
2163 case ISN_COMPARELIST:
2164 {
2165 typval_T *tv1 = STACK_TV_BOT(-2);
2166 typval_T *tv2 = STACK_TV_BOT(-1);
2167 list_T *arg1 = tv1->vval.v_list;
2168 list_T *arg2 = tv2->vval.v_list;
2169 int cmp = FALSE;
2170 int ic = iptr->isn_arg.op.op_ic;
2171
2172 switch (iptr->isn_arg.op.op_type)
2173 {
2174 case EXPR_EQUAL: cmp =
2175 list_equal(arg1, arg2, ic, FALSE); break;
2176 case EXPR_NEQUAL: cmp =
2177 !list_equal(arg1, arg2, ic, FALSE); break;
2178 case EXPR_IS: cmp = arg1 == arg2; break;
2179 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2180 default: cmp = 0; break;
2181 }
2182 --ectx.ec_stack.ga_len;
2183 clear_tv(tv1);
2184 clear_tv(tv2);
2185 tv1->v_type = VAR_BOOL;
2186 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2187 }
2188 break;
2189
2190 case ISN_COMPAREBLOB:
2191 {
2192 typval_T *tv1 = STACK_TV_BOT(-2);
2193 typval_T *tv2 = STACK_TV_BOT(-1);
2194 blob_T *arg1 = tv1->vval.v_blob;
2195 blob_T *arg2 = tv2->vval.v_blob;
2196 int cmp = FALSE;
2197
2198 switch (iptr->isn_arg.op.op_type)
2199 {
2200 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2201 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2202 case EXPR_IS: cmp = arg1 == arg2; break;
2203 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2204 default: cmp = 0; break;
2205 }
2206 --ectx.ec_stack.ga_len;
2207 clear_tv(tv1);
2208 clear_tv(tv2);
2209 tv1->v_type = VAR_BOOL;
2210 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2211 }
2212 break;
2213
2214 // TODO: handle separately
2215 case ISN_COMPARESTRING:
2216 case ISN_COMPAREDICT:
2217 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 case ISN_COMPAREANY:
2219 {
2220 typval_T *tv1 = STACK_TV_BOT(-2);
2221 typval_T *tv2 = STACK_TV_BOT(-1);
2222 exptype_T exptype = iptr->isn_arg.op.op_type;
2223 int ic = iptr->isn_arg.op.op_ic;
2224
Bram Moolenaareb26f432020-09-14 16:50:05 +02002225 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 typval_compare(tv1, tv2, exptype, ic);
2227 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 --ectx.ec_stack.ga_len;
2229 }
2230 break;
2231
2232 case ISN_ADDLIST:
2233 case ISN_ADDBLOB:
2234 {
2235 typval_T *tv1 = STACK_TV_BOT(-2);
2236 typval_T *tv2 = STACK_TV_BOT(-1);
2237
2238 if (iptr->isn_type == ISN_ADDLIST)
2239 eval_addlist(tv1, tv2);
2240 else
2241 eval_addblob(tv1, tv2);
2242 clear_tv(tv2);
2243 --ectx.ec_stack.ga_len;
2244 }
2245 break;
2246
2247 // Computation with two arguments of unknown type
2248 case ISN_OPANY:
2249 {
2250 typval_T *tv1 = STACK_TV_BOT(-2);
2251 typval_T *tv2 = STACK_TV_BOT(-1);
2252 varnumber_T n1, n2;
2253#ifdef FEAT_FLOAT
2254 float_T f1 = 0, f2 = 0;
2255#endif
2256 int error = FALSE;
2257
2258 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2259 {
2260 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2261 {
2262 eval_addlist(tv1, tv2);
2263 clear_tv(tv2);
2264 --ectx.ec_stack.ga_len;
2265 break;
2266 }
2267 else if (tv1->v_type == VAR_BLOB
2268 && tv2->v_type == VAR_BLOB)
2269 {
2270 eval_addblob(tv1, tv2);
2271 clear_tv(tv2);
2272 --ectx.ec_stack.ga_len;
2273 break;
2274 }
2275 }
2276#ifdef FEAT_FLOAT
2277 if (tv1->v_type == VAR_FLOAT)
2278 {
2279 f1 = tv1->vval.v_float;
2280 n1 = 0;
2281 }
2282 else
2283#endif
2284 {
2285 n1 = tv_get_number_chk(tv1, &error);
2286 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002287 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288#ifdef FEAT_FLOAT
2289 if (tv2->v_type == VAR_FLOAT)
2290 f1 = n1;
2291#endif
2292 }
2293#ifdef FEAT_FLOAT
2294 if (tv2->v_type == VAR_FLOAT)
2295 {
2296 f2 = tv2->vval.v_float;
2297 n2 = 0;
2298 }
2299 else
2300#endif
2301 {
2302 n2 = tv_get_number_chk(tv2, &error);
2303 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002304 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305#ifdef FEAT_FLOAT
2306 if (tv1->v_type == VAR_FLOAT)
2307 f2 = n2;
2308#endif
2309 }
2310#ifdef FEAT_FLOAT
2311 // if there is a float on either side the result is a float
2312 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2313 {
2314 switch (iptr->isn_arg.op.op_type)
2315 {
2316 case EXPR_MULT: f1 = f1 * f2; break;
2317 case EXPR_DIV: f1 = f1 / f2; break;
2318 case EXPR_SUB: f1 = f1 - f2; break;
2319 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002320 default: SOURCING_LNUM = iptr->isn_lnum;
2321 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002322 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 }
2324 clear_tv(tv1);
2325 clear_tv(tv2);
2326 tv1->v_type = VAR_FLOAT;
2327 tv1->vval.v_float = f1;
2328 --ectx.ec_stack.ga_len;
2329 }
2330 else
2331#endif
2332 {
2333 switch (iptr->isn_arg.op.op_type)
2334 {
2335 case EXPR_MULT: n1 = n1 * n2; break;
2336 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2337 case EXPR_SUB: n1 = n1 - n2; break;
2338 case EXPR_ADD: n1 = n1 + n2; break;
2339 default: n1 = num_modulus(n1, n2); break;
2340 }
2341 clear_tv(tv1);
2342 clear_tv(tv2);
2343 tv1->v_type = VAR_NUMBER;
2344 tv1->vval.v_number = n1;
2345 --ectx.ec_stack.ga_len;
2346 }
2347 }
2348 break;
2349
2350 case ISN_CONCAT:
2351 {
2352 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2353 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2354 char_u *res;
2355
2356 res = concat_str(str1, str2);
2357 clear_tv(STACK_TV_BOT(-2));
2358 clear_tv(STACK_TV_BOT(-1));
2359 --ectx.ec_stack.ga_len;
2360 STACK_TV_BOT(-1)->vval.v_string = res;
2361 }
2362 break;
2363
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002364 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002365 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002366 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002367 int is_slice = iptr->isn_type == ISN_STRSLICE;
2368 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002369 char_u *res;
2370
2371 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002372 // string slice: string is at stack-3, first index at
2373 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002374 if (is_slice)
2375 {
2376 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002377 n1 = tv->vval.v_number;
2378 }
2379
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002380 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002381 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002382
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002383 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002384 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002385 if (is_slice)
2386 // Slice: Select the characters from the string
2387 res = string_slice(tv->vval.v_string, n1, n2);
2388 else
2389 // Index: The resulting variable is a string of a
2390 // single character. If the index is too big or
2391 // negative the result is empty.
2392 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002393 vim_free(tv->vval.v_string);
2394 tv->vval.v_string = res;
2395 }
2396 break;
2397
2398 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002399 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 {
Bram Moolenaared591872020-08-15 22:14:53 +02002401 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002403 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404
2405 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002406 // list slice: list is at stack-3, indexes at stack-2 and
2407 // stack-1
2408 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 list = tv->vval.v_list;
2410
2411 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002412 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002414
2415 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 {
Bram Moolenaared591872020-08-15 22:14:53 +02002417 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002418 n1 = tv->vval.v_number;
2419 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 }
Bram Moolenaared591872020-08-15 22:14:53 +02002421
2422 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002423 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002424 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002425 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2426 == FAIL)
2427 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429 break;
2430
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002431 case ISN_ANYINDEX:
2432 case ISN_ANYSLICE:
2433 {
2434 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2435 typval_T *var1, *var2;
2436 int res;
2437
2438 // index: composite is at stack-2, index at stack-1
2439 // slice: composite is at stack-3, indexes at stack-2 and
2440 // stack-1
2441 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002442 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002443 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2444 goto on_error;
2445 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2446 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2447 res = eval_index_inner(tv, is_slice,
2448 var1, var2, NULL, -1, TRUE);
2449 clear_tv(var1);
2450 if (is_slice)
2451 clear_tv(var2);
2452 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2453 if (res == FAIL)
2454 goto on_error;
2455 }
2456 break;
2457
Bram Moolenaar9af78762020-06-16 11:34:42 +02002458 case ISN_SLICE:
2459 {
2460 list_T *list;
2461 int count = iptr->isn_arg.number;
2462
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002463 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002464 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002465 list = tv->vval.v_list;
2466
2467 // no error for short list, expect it to be checked earlier
2468 if (list != NULL && list->lv_len >= count)
2469 {
2470 list_T *newlist = list_slice(list,
2471 count, list->lv_len - 1);
2472
2473 if (newlist != NULL)
2474 {
2475 list_unref(list);
2476 tv->vval.v_list = newlist;
2477 ++newlist->lv_refcount;
2478 }
2479 }
2480 }
2481 break;
2482
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002483 case ISN_GETITEM:
2484 {
2485 listitem_T *li;
2486 int index = iptr->isn_arg.number;
2487
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002488 // Get list item: list is at stack-1, push item.
2489 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002490 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002491 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002492
2493 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2494 goto failed;
2495 ++ectx.ec_stack.ga_len;
2496 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2497 }
2498 break;
2499
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 case ISN_MEMBER:
2501 {
2502 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002503 char_u *key;
2504 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002505 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002506
2507 // dict member: dict is at stack-2, key at stack-1
2508 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002509 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002510 dict = tv->vval.v_dict;
2511
2512 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002513 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002514 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002515
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002516 if ((di = dict_find(dict, key, -1)) == NULL)
2517 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002518 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002519 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002520 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002521 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002522 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002523 --ectx.ec_stack.ga_len;
2524 // Clear the dict after getting the item, to avoid that it
2525 // make the item invalid.
2526 tv = STACK_TV_BOT(-1);
2527 temp_tv = *tv;
2528 copy_tv(&di->di_tv, tv);
2529 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002530 }
2531 break;
2532
2533 // dict member with string key
2534 case ISN_STRINGMEMBER:
2535 {
2536 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002538 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539
2540 tv = STACK_TV_BOT(-1);
2541 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2542 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002543 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002545 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 }
2547 dict = tv->vval.v_dict;
2548
2549 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2550 == NULL)
2551 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002552 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002554 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002556 // Clear the dict after getting the item, to avoid that it
2557 // make the item invalid.
2558 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002560 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 }
2562 break;
2563
2564 case ISN_NEGATENR:
2565 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002566 if (tv->v_type != VAR_NUMBER
2567#ifdef FEAT_FLOAT
2568 && tv->v_type != VAR_FLOAT
2569#endif
2570 )
2571 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002572 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002573 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002574 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002575 }
2576#ifdef FEAT_FLOAT
2577 if (tv->v_type == VAR_FLOAT)
2578 tv->vval.v_float = -tv->vval.v_float;
2579 else
2580#endif
2581 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 break;
2583
2584 case ISN_CHECKNR:
2585 {
2586 int error = FALSE;
2587
2588 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 (void)tv_get_number_chk(tv, &error);
2593 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002594 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 }
2596 break;
2597
2598 case ISN_CHECKTYPE:
2599 {
2600 checktype_T *ct = &iptr->isn_arg.type;
2601
2602 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002603 SOURCING_LNUM = iptr->isn_lnum;
2604 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2605 goto on_error;
2606
2607 // number 0 is FALSE, number 1 is TRUE
2608 if (tv->v_type == VAR_NUMBER
2609 && ct->ct_type->tt_type == VAR_BOOL
2610 && (tv->vval.v_number == 0
2611 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002613 tv->v_type = VAR_BOOL;
2614 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002615 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 }
2617 }
2618 break;
2619
Bram Moolenaar9af78762020-06-16 11:34:42 +02002620 case ISN_CHECKLEN:
2621 {
2622 int min_len = iptr->isn_arg.checklen.cl_min_len;
2623 list_T *list = NULL;
2624
2625 tv = STACK_TV_BOT(-1);
2626 if (tv->v_type == VAR_LIST)
2627 list = tv->vval.v_list;
2628 if (list == NULL || list->lv_len < min_len
2629 || (list->lv_len > min_len
2630 && !iptr->isn_arg.checklen.cl_more_OK))
2631 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002632 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002633 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002634 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002635 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002636 }
2637 }
2638 break;
2639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002641 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 {
2643 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002644 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645
2646 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002647 if (iptr->isn_type == ISN_2BOOL)
2648 {
2649 n = tv2bool(tv);
2650 if (iptr->isn_arg.number) // invert
2651 n = !n;
2652 }
2653 else
2654 {
2655 SOURCING_LNUM = iptr->isn_lnum;
2656 n = tv_get_bool_chk(tv, &error);
2657 if (error)
2658 goto on_error;
2659 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 clear_tv(tv);
2661 tv->v_type = VAR_BOOL;
2662 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2663 }
2664 break;
2665
2666 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002667 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 {
2669 char_u *str;
2670
2671 tv = STACK_TV_BOT(iptr->isn_arg.number);
2672 if (tv->v_type != VAR_STRING)
2673 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002674 if (iptr->isn_type == ISN_2STRING_ANY)
2675 {
2676 switch (tv->v_type)
2677 {
2678 case VAR_SPECIAL:
2679 case VAR_BOOL:
2680 case VAR_NUMBER:
2681 case VAR_FLOAT:
2682 case VAR_BLOB: break;
2683 default: to_string_error(tv->v_type);
2684 goto on_error;
2685 }
2686 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 str = typval_tostring(tv);
2688 clear_tv(tv);
2689 tv->v_type = VAR_STRING;
2690 tv->vval.v_string = str;
2691 }
2692 }
2693 break;
2694
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002695 case ISN_PUT:
2696 {
2697 int regname = iptr->isn_arg.put.put_regname;
2698 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2699 char_u *expr = NULL;
2700 int dir = FORWARD;
2701
2702 if (regname == '=')
2703 {
2704 tv = STACK_TV_BOT(-1);
2705 if (tv->v_type == VAR_STRING)
2706 expr = tv->vval.v_string;
2707 else
2708 {
2709 expr = typval_tostring(tv); // allocates value
2710 clear_tv(tv);
2711 }
2712 --ectx.ec_stack.ga_len;
2713 }
2714 if (lnum == -2)
2715 // :put! above cursor
2716 dir = BACKWARD;
2717 else if (lnum >= 0)
2718 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2719 check_cursor();
2720 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2721 vim_free(expr);
2722 }
2723 break;
2724
Bram Moolenaar389df252020-07-09 21:20:47 +02002725 case ISN_SHUFFLE:
2726 {
2727 typval_T tmp_tv;
2728 int item = iptr->isn_arg.shuffle.shfl_item;
2729 int up = iptr->isn_arg.shuffle.shfl_up;
2730
2731 tmp_tv = *STACK_TV_BOT(-item);
2732 for ( ; up > 0 && item > 1; --up)
2733 {
2734 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2735 --item;
2736 }
2737 *STACK_TV_BOT(-item) = tmp_tv;
2738 }
2739 break;
2740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 case ISN_DROP:
2742 --ectx.ec_stack.ga_len;
2743 clear_tv(STACK_TV_BOT(0));
2744 break;
2745 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002746 continue;
2747
Bram Moolenaard032f342020-07-18 18:13:02 +02002748func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002749 // Restore previous function. If the frame pointer is where we started
2750 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002751 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002752 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002753
Bram Moolenaard032f342020-07-18 18:13:02 +02002754 if (func_return(&ectx) == FAIL)
2755 // only fails when out of memory
2756 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002757 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002758
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002759on_error:
2760 if (trylevel == 0)
2761 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 }
2763
2764done:
2765 // function finished, get result from the stack.
2766 tv = STACK_TV_BOT(-1);
2767 *rettv = *tv;
2768 tv->v_type = VAR_UNKNOWN;
2769 ret = OK;
2770
2771failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002772 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002773 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002774 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002775
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002776 // Deal with any remaining closures, they may be in use somewhere.
2777 if (ectx.ec_funcrefs.ga_len > 0)
2778 handle_closure_in_use(&ectx, FALSE);
2779
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002780 estack_pop();
2781 current_sctx = save_current_sctx;
2782
2783failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002784 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2786 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002787
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002789 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002790
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002791 // Not sure if this is necessary.
2792 suppress_errthrow = save_suppress_errthrow;
2793
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002794 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002795 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002796 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 return ret;
2798}
2799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800/*
2801 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002802 * We don't really need this at runtime, but we do have tests that require it,
2803 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 */
2805 void
2806ex_disassemble(exarg_T *eap)
2807{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002808 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002809 char_u *fname;
2810 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 dfunc_T *dfunc;
2812 isn_T *instr;
2813 int current;
2814 int line_idx = 0;
2815 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002816 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002818 if (STRNCMP(arg, "<lambda>", 8) == 0)
2819 {
2820 arg += 8;
2821 (void)getdigits(&arg);
2822 fname = vim_strnsave(eap->arg, arg - eap->arg);
2823 }
2824 else
2825 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002826 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002827 if (fname == NULL)
2828 {
2829 semsg(_(e_invarg2), eap->arg);
2830 return;
2831 }
2832
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002833 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002834 if (ufunc == NULL)
2835 {
2836 char_u *p = untrans_function_name(fname);
2837
2838 if (p != NULL)
2839 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002840 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002841 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002842 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 if (ufunc == NULL)
2844 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002845 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 return;
2847 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002848 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002849 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2850 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002851 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002853 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 return;
2855 }
2856 if (ufunc->uf_name_exp != NULL)
2857 msg((char *)ufunc->uf_name_exp);
2858 else
2859 msg((char *)ufunc->uf_name);
2860
2861 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2862 instr = dfunc->df_instr;
2863 for (current = 0; current < dfunc->df_instr_count; ++current)
2864 {
2865 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002866 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867
2868 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2869 {
2870 if (current > prev_current)
2871 {
2872 msg_puts("\n\n");
2873 prev_current = current;
2874 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002875 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2876 if (line != NULL)
2877 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 }
2879
2880 switch (iptr->isn_type)
2881 {
2882 case ISN_EXEC:
2883 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2884 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002885 case ISN_EXECCONCAT:
2886 smsg("%4d EXECCONCAT %lld", current,
2887 (long long)iptr->isn_arg.number);
2888 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 case ISN_ECHO:
2890 {
2891 echo_T *echo = &iptr->isn_arg.echo;
2892
2893 smsg("%4d %s %d", current,
2894 echo->echo_with_white ? "ECHO" : "ECHON",
2895 echo->echo_count);
2896 }
2897 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002898 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002899 smsg("%4d EXECUTE %lld", current,
2900 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002901 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002902 case ISN_ECHOMSG:
2903 smsg("%4d ECHOMSG %lld", current,
2904 (long long)(iptr->isn_arg.number));
2905 break;
2906 case ISN_ECHOERR:
2907 smsg("%4d ECHOERR %lld", current,
2908 (long long)(iptr->isn_arg.number));
2909 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002911 case ISN_LOADOUTER:
2912 {
2913 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2914
2915 if (iptr->isn_arg.number < 0)
2916 smsg("%4d LOAD%s arg[%lld]", current, add,
2917 (long long)(iptr->isn_arg.number
2918 + STACK_FRAME_SIZE));
2919 else
2920 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002921 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002923 break;
2924 case ISN_LOADV:
2925 smsg("%4d LOADV v:%s", current,
2926 get_vim_var_name(iptr->isn_arg.number));
2927 break;
2928 case ISN_LOADSCRIPT:
2929 {
2930 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002931 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2933 + iptr->isn_arg.script.script_idx;
2934
2935 smsg("%4d LOADSCRIPT %s from %s", current,
2936 sv->sv_name, si->sn_name);
2937 }
2938 break;
2939 case ISN_LOADS:
2940 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002941 scriptitem_T *si = SCRIPT_ITEM(
2942 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943
2944 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002945 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 break;
2948 case ISN_LOADG:
2949 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2950 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002951 case ISN_LOADB:
2952 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2953 break;
2954 case ISN_LOADW:
2955 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2956 break;
2957 case ISN_LOADT:
2958 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2959 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002960 case ISN_LOADGDICT:
2961 smsg("%4d LOAD g:", current);
2962 break;
2963 case ISN_LOADBDICT:
2964 smsg("%4d LOAD b:", current);
2965 break;
2966 case ISN_LOADWDICT:
2967 smsg("%4d LOAD w:", current);
2968 break;
2969 case ISN_LOADTDICT:
2970 smsg("%4d LOAD t:", current);
2971 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 case ISN_LOADOPT:
2973 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2974 break;
2975 case ISN_LOADENV:
2976 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2977 break;
2978 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002979 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 break;
2981
2982 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002983 case ISN_STOREOUTER:
2984 {
2985 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2986
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002987 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002988 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002989 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002990 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002991 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002992 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002993 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002995 case ISN_STOREV:
2996 smsg("%4d STOREV v:%s", current,
2997 get_vim_var_name(iptr->isn_arg.number));
2998 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003000 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3001 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003002 case ISN_STOREB:
3003 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3004 break;
3005 case ISN_STOREW:
3006 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3007 break;
3008 case ISN_STORET:
3009 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3010 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003011 case ISN_STORES:
3012 {
3013 scriptitem_T *si = SCRIPT_ITEM(
3014 iptr->isn_arg.loadstore.ls_sid);
3015
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003016 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003017 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003018 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003019 break;
3020 case ISN_STORESCRIPT:
3021 {
3022 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003023 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3025 + iptr->isn_arg.script.script_idx;
3026
3027 smsg("%4d STORESCRIPT %s in %s", current,
3028 sv->sv_name, si->sn_name);
3029 }
3030 break;
3031 case ISN_STOREOPT:
3032 smsg("%4d STOREOPT &%s", current,
3033 iptr->isn_arg.storeopt.so_name);
3034 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003035 case ISN_STOREENV:
3036 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3037 break;
3038 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003039 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003040 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 case ISN_STORENR:
3042 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003043 iptr->isn_arg.storenr.stnr_val,
3044 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003045 break;
3046
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003047 case ISN_STORELIST:
3048 smsg("%4d STORELIST", current);
3049 break;
3050
3051 case ISN_STOREDICT:
3052 smsg("%4d STOREDICT", current);
3053 break;
3054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003055 // constants
3056 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003057 smsg("%4d PUSHNR %lld", current,
3058 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003059 break;
3060 case ISN_PUSHBOOL:
3061 case ISN_PUSHSPEC:
3062 smsg("%4d PUSH %s", current,
3063 get_var_special_name(iptr->isn_arg.number));
3064 break;
3065 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003066#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003068#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069 break;
3070 case ISN_PUSHS:
3071 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3072 break;
3073 case ISN_PUSHBLOB:
3074 {
3075 char_u *r;
3076 char_u numbuf[NUMBUFLEN];
3077 char_u *tofree;
3078
3079 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003080 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003081 vim_free(tofree);
3082 }
3083 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003084 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003085 {
3086 char *name = (char *)iptr->isn_arg.string;
3087
3088 smsg("%4d PUSHFUNC \"%s\"", current,
3089 name == NULL ? "[none]" : name);
3090 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003091 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003092 case ISN_PUSHCHANNEL:
3093#ifdef FEAT_JOB_CHANNEL
3094 {
3095 channel_T *channel = iptr->isn_arg.channel;
3096
3097 smsg("%4d PUSHCHANNEL %d", current,
3098 channel == NULL ? 0 : channel->ch_id);
3099 }
3100#endif
3101 break;
3102 case ISN_PUSHJOB:
3103#ifdef FEAT_JOB_CHANNEL
3104 {
3105 typval_T tv;
3106 char_u *name;
3107
3108 tv.v_type = VAR_JOB;
3109 tv.vval.v_job = iptr->isn_arg.job;
3110 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003111 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003112 }
3113#endif
3114 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 case ISN_PUSHEXC:
3116 smsg("%4d PUSH v:exception", current);
3117 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003118 case ISN_UNLET:
3119 smsg("%4d UNLET%s %s", current,
3120 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3121 iptr->isn_arg.unlet.ul_name);
3122 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003123 case ISN_UNLETENV:
3124 smsg("%4d UNLETENV%s $%s", current,
3125 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3126 iptr->isn_arg.unlet.ul_name);
3127 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003128 case ISN_LOCKCONST:
3129 smsg("%4d LOCKCONST", current);
3130 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003132 smsg("%4d NEWLIST size %lld", current,
3133 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 break;
3135 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003136 smsg("%4d NEWDICT size %lld", current,
3137 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138 break;
3139
3140 // function call
3141 case ISN_BCALL:
3142 {
3143 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3144
3145 smsg("%4d BCALL %s(argc %d)", current,
3146 internal_func_name(cbfunc->cbf_idx),
3147 cbfunc->cbf_argcount);
3148 }
3149 break;
3150 case ISN_DCALL:
3151 {
3152 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3153 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3154 + cdfunc->cdf_idx;
3155
3156 smsg("%4d DCALL %s(argc %d)", current,
3157 df->df_ufunc->uf_name_exp != NULL
3158 ? df->df_ufunc->uf_name_exp
3159 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3160 }
3161 break;
3162 case ISN_UCALL:
3163 {
3164 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3165
3166 smsg("%4d UCALL %s(argc %d)", current,
3167 cufunc->cuf_name, cufunc->cuf_argcount);
3168 }
3169 break;
3170 case ISN_PCALL:
3171 {
3172 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3173
3174 smsg("%4d PCALL%s (argc %d)", current,
3175 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3176 }
3177 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003178 case ISN_PCALL_END:
3179 smsg("%4d PCALL end", current);
3180 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003181 case ISN_RETURN:
3182 smsg("%4d RETURN", current);
3183 break;
3184 case ISN_FUNCREF:
3185 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003186 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003188 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003190 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 }
3192 break;
3193
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003194 case ISN_NEWFUNC:
3195 {
3196 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3197
3198 smsg("%4d NEWFUNC %s %s", current,
3199 newfunc->nf_lambda, newfunc->nf_global);
3200 }
3201 break;
3202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203 case ISN_JUMP:
3204 {
3205 char *when = "?";
3206
3207 switch (iptr->isn_arg.jump.jump_when)
3208 {
3209 case JUMP_ALWAYS:
3210 when = "JUMP";
3211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 case JUMP_AND_KEEP_IF_TRUE:
3213 when = "JUMP_AND_KEEP_IF_TRUE";
3214 break;
3215 case JUMP_IF_FALSE:
3216 when = "JUMP_IF_FALSE";
3217 break;
3218 case JUMP_AND_KEEP_IF_FALSE:
3219 when = "JUMP_AND_KEEP_IF_FALSE";
3220 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003221 case JUMP_IF_COND_FALSE:
3222 when = "JUMP_IF_COND_FALSE";
3223 break;
3224 case JUMP_IF_COND_TRUE:
3225 when = "JUMP_IF_COND_TRUE";
3226 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003228 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003229 iptr->isn_arg.jump.jump_where);
3230 }
3231 break;
3232
3233 case ISN_FOR:
3234 {
3235 forloop_T *forloop = &iptr->isn_arg.forloop;
3236
3237 smsg("%4d FOR $%d -> %d", current,
3238 forloop->for_idx, forloop->for_end);
3239 }
3240 break;
3241
3242 case ISN_TRY:
3243 {
3244 try_T *try = &iptr->isn_arg.try;
3245
3246 smsg("%4d TRY catch -> %d, finally -> %d", current,
3247 try->try_catch, try->try_finally);
3248 }
3249 break;
3250 case ISN_CATCH:
3251 // TODO
3252 smsg("%4d CATCH", current);
3253 break;
3254 case ISN_ENDTRY:
3255 smsg("%4d ENDTRY", current);
3256 break;
3257 case ISN_THROW:
3258 smsg("%4d THROW", current);
3259 break;
3260
3261 // expression operations on number
3262 case ISN_OPNR:
3263 case ISN_OPFLOAT:
3264 case ISN_OPANY:
3265 {
3266 char *what;
3267 char *ins;
3268
3269 switch (iptr->isn_arg.op.op_type)
3270 {
3271 case EXPR_MULT: what = "*"; break;
3272 case EXPR_DIV: what = "/"; break;
3273 case EXPR_REM: what = "%"; break;
3274 case EXPR_SUB: what = "-"; break;
3275 case EXPR_ADD: what = "+"; break;
3276 default: what = "???"; break;
3277 }
3278 switch (iptr->isn_type)
3279 {
3280 case ISN_OPNR: ins = "OPNR"; break;
3281 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3282 case ISN_OPANY: ins = "OPANY"; break;
3283 default: ins = "???"; break;
3284 }
3285 smsg("%4d %s %s", current, ins, what);
3286 }
3287 break;
3288
3289 case ISN_COMPAREBOOL:
3290 case ISN_COMPARESPECIAL:
3291 case ISN_COMPARENR:
3292 case ISN_COMPAREFLOAT:
3293 case ISN_COMPARESTRING:
3294 case ISN_COMPAREBLOB:
3295 case ISN_COMPARELIST:
3296 case ISN_COMPAREDICT:
3297 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 case ISN_COMPAREANY:
3299 {
3300 char *p;
3301 char buf[10];
3302 char *type;
3303
3304 switch (iptr->isn_arg.op.op_type)
3305 {
3306 case EXPR_EQUAL: p = "=="; break;
3307 case EXPR_NEQUAL: p = "!="; break;
3308 case EXPR_GREATER: p = ">"; break;
3309 case EXPR_GEQUAL: p = ">="; break;
3310 case EXPR_SMALLER: p = "<"; break;
3311 case EXPR_SEQUAL: p = "<="; break;
3312 case EXPR_MATCH: p = "=~"; break;
3313 case EXPR_IS: p = "is"; break;
3314 case EXPR_ISNOT: p = "isnot"; break;
3315 case EXPR_NOMATCH: p = "!~"; break;
3316 default: p = "???"; break;
3317 }
3318 STRCPY(buf, p);
3319 if (iptr->isn_arg.op.op_ic == TRUE)
3320 strcat(buf, "?");
3321 switch(iptr->isn_type)
3322 {
3323 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3324 case ISN_COMPARESPECIAL:
3325 type = "COMPARESPECIAL"; break;
3326 case ISN_COMPARENR: type = "COMPARENR"; break;
3327 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3328 case ISN_COMPARESTRING:
3329 type = "COMPARESTRING"; break;
3330 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3331 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3332 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3333 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003334 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3335 default: type = "???"; break;
3336 }
3337
3338 smsg("%4d %s %s", current, type, buf);
3339 }
3340 break;
3341
3342 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3343 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3344
3345 // expression operations
3346 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003347 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003348 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003349 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003350 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003351 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3352 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003353 case ISN_SLICE: smsg("%4d SLICE %lld",
3354 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003355 case ISN_GETITEM: smsg("%4d ITEM %lld",
3356 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003357 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3358 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 iptr->isn_arg.string); break;
3360 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3361
3362 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003363 case ISN_CHECKTYPE:
3364 {
3365 char *tofree;
3366
3367 smsg("%4d CHECKTYPE %s stack[%d]", current,
3368 type_name(iptr->isn_arg.type.ct_type, &tofree),
3369 iptr->isn_arg.type.ct_off);
3370 vim_free(tofree);
3371 break;
3372 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003373 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3374 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3375 iptr->isn_arg.checklen.cl_min_len);
3376 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003377 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 case ISN_2BOOL: if (iptr->isn_arg.number)
3379 smsg("%4d INVERT (!val)", current);
3380 else
3381 smsg("%4d 2BOOL (!!val)", current);
3382 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003383 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3384 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003385 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003386 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3387 (long long)(iptr->isn_arg.number));
3388 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003389 case ISN_PUT:
3390 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3391 (long)iptr->isn_arg.put.put_lnum);
3392 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393
Bram Moolenaar389df252020-07-09 21:20:47 +02003394 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3395 iptr->isn_arg.shuffle.shfl_item,
3396 iptr->isn_arg.shuffle.shfl_up);
3397 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 case ISN_DROP: smsg("%4d DROP", current); break;
3399 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003400
3401 out_flush(); // output one line at a time
3402 ui_breakcheck();
3403 if (got_int)
3404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003406}
3407
3408/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003409 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 * list, etc. Mostly like what JavaScript does, except that empty list and
3411 * empty dictionary are FALSE.
3412 */
3413 int
3414tv2bool(typval_T *tv)
3415{
3416 switch (tv->v_type)
3417 {
3418 case VAR_NUMBER:
3419 return tv->vval.v_number != 0;
3420 case VAR_FLOAT:
3421#ifdef FEAT_FLOAT
3422 return tv->vval.v_float != 0.0;
3423#else
3424 break;
3425#endif
3426 case VAR_PARTIAL:
3427 return tv->vval.v_partial != NULL;
3428 case VAR_FUNC:
3429 case VAR_STRING:
3430 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3431 case VAR_LIST:
3432 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3433 case VAR_DICT:
3434 return tv->vval.v_dict != NULL
3435 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3436 case VAR_BOOL:
3437 case VAR_SPECIAL:
3438 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3439 case VAR_JOB:
3440#ifdef FEAT_JOB_CHANNEL
3441 return tv->vval.v_job != NULL;
3442#else
3443 break;
3444#endif
3445 case VAR_CHANNEL:
3446#ifdef FEAT_JOB_CHANNEL
3447 return tv->vval.v_channel != NULL;
3448#else
3449 break;
3450#endif
3451 case VAR_BLOB:
3452 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3453 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003454 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 case VAR_VOID:
3456 break;
3457 }
3458 return FALSE;
3459}
3460
3461/*
3462 * If "tv" is a string give an error and return FAIL.
3463 */
3464 int
3465check_not_string(typval_T *tv)
3466{
3467 if (tv->v_type == VAR_STRING)
3468 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003469 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 clear_tv(tv);
3471 return FAIL;
3472 }
3473 return OK;
3474}
3475
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476
3477#endif // FEAT_EVAL