blob: 7afc5c27cdec53d5cd73e69374601191f68ed026 [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.
380 // TODO: this won't work if the closure actually uses it. But when
381 // keeping it it gets complicated: it will create a reference cycle
382 // inside the partial, thus needs special handling for garbage
383 // collection.
384 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
385 {
386 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200387
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 for (i = 0; i < closure_count; ++i)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200389 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
391 - closure_count + i];
392 if (tv->vval.v_partial == pt)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200393 break;
394 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200395 if (i < closure_count)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 continue;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200397 }
398
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
400 tv->v_type = VAR_UNKNOWN;
401 }
402
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200403 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200405 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
406 - closure_count + idx];
407 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200409 ++funcstack->fs_refcount;
410 pt->pt_funcstack = funcstack;
411 pt->pt_ectx_stack = &funcstack->fs_ga;
412 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 }
414 }
415 }
416
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200417 for (idx = 0; idx < closure_count; ++idx)
418 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
419 - closure_count + idx]);
420 gap->ga_len -= closure_count;
421 if (gap->ga_len == 0)
422 ga_clear(gap);
423
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200424 return OK;
425}
426
427/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 * Return from the current function.
429 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200430 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100431func_return(ectx_T *ectx)
432{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
435 + ectx->ec_dfunc_idx;
436 int argcount = ufunc_argcount(dfunc->df_ufunc);
437 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200438 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439
440 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200441 entry = estack_pop();
442 if (entry != NULL)
443 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100444
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 if (handle_closure_in_use(ectx, TRUE) == FAIL)
446 return FAIL;
447
448 // Clear the arguments.
449 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
450 clear_tv(STACK_TV(idx));
451
452 // Clear local variables and temp values, but not the return value.
453 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100454 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100456
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100457 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200458 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
459 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200460 ectx->ec_outer_stack =
461 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
462 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
463 // restoring ec_frame_idx must be last
464 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100465 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
466 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100467
468 // Reset the stack to the position before the call, move the return value
469 // to the top of the stack.
470 idx = ectx->ec_stack.ga_len - 1;
471 ectx->ec_stack.ga_len = top + 1;
472 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200473
474 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475}
476
477#undef STACK_TV
478
479/*
480 * Prepare arguments and rettv for calling a builtin or user function.
481 */
482 static int
483call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
484{
485 int idx;
486 typval_T *tv;
487
488 // Move arguments from bottom of the stack to argvars[] and add terminator.
489 for (idx = 0; idx < argcount; ++idx)
490 argvars[idx] = *STACK_TV_BOT(idx - argcount);
491 argvars[argcount].v_type = VAR_UNKNOWN;
492
493 // Result replaces the arguments on the stack.
494 if (argcount > 0)
495 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200496 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497 return FAIL;
498 else
499 ++ectx->ec_stack.ga_len;
500
501 // Default return value is zero.
502 tv = STACK_TV_BOT(-1);
503 tv->v_type = VAR_NUMBER;
504 tv->vval.v_number = 0;
505
506 return OK;
507}
508
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200509// Ugly global to avoid passing the execution context around through many
510// layers.
511static ectx_T *current_ectx = NULL;
512
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100513/*
514 * Call a builtin function by index.
515 */
516 static int
517call_bfunc(int func_idx, int argcount, ectx_T *ectx)
518{
519 typval_T argvars[MAX_FUNC_ARGS];
520 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200521 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200522 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
524 if (call_prepare(argcount, argvars, ectx) == FAIL)
525 return FAIL;
526
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200527 // Call the builtin function. Set "current_ectx" so that when it
528 // recursively invokes call_def_function() a closure context can be set.
529 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200531 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 // Clear the arguments.
534 for (idx = 0; idx < argcount; ++idx)
535 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200536
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200537 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200538 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 return OK;
540}
541
542/*
543 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100544 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545 */
546 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100547call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548{
549 typval_T argvars[MAX_FUNC_ARGS];
550 funcexe_T funcexe;
551 int error;
552 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200553 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200555 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200556 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
557 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200558 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100559 {
560 // The function has been compiled, can call it quickly. For a function
561 // that was defined later: we can call it directly next time.
562 if (iptr != NULL)
563 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100564 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100565 iptr->isn_type = ISN_DCALL;
566 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
567 iptr->isn_arg.dfunc.cdf_argcount = argcount;
568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571
572 if (call_prepare(argcount, argvars, ectx) == FAIL)
573 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200574 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 funcexe.evaluate = TRUE;
576
577 // Call the user function. Result goes in last position on the stack.
578 // TODO: add selfdict if there is one
579 error = call_user_func_check(ufunc, argcount, argvars,
580 STACK_TV_BOT(-1), &funcexe, NULL);
581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
585
586 if (error != FCERR_NONE)
587 {
588 user_func_error(error, ufunc->uf_name);
589 return FAIL;
590 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200591 if (called_emsg > called_emsg_before)
592 // Error other than from calling the function itself.
593 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 return OK;
595}
596
597/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200598 * Return TRUE if an error was given or CTRL-C was pressed.
599 */
600 static int
601vim9_aborting(int prev_called_emsg)
602{
603 return called_emsg > prev_called_emsg || got_int || did_throw;
604}
605
606/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 * Execute a function by "name".
608 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100609 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610 * Returns FAIL if not found without an error message.
611 */
612 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100613call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614{
615 ufunc_T *ufunc;
616
617 if (builtin_function(name, -1))
618 {
619 int func_idx = find_internal_func(name);
620
621 if (func_idx < 0)
622 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200623 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 return call_bfunc(func_idx, argcount, ectx);
626 }
627
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200628 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200629
630 if (ufunc == NULL)
631 {
632 int called_emsg_before = called_emsg;
633
634 if (script_autoload(name, TRUE))
635 // loaded a package, search for the function again
636 ufunc = find_func(name, FALSE, NULL);
637 if (vim9_aborting(called_emsg_before))
638 return FAIL; // bail out if loading the script caused an error
639 }
640
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100642 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643
644 return FAIL;
645}
646
647 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200648call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200650 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200651 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200653 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654
655 if (tv->v_type == VAR_PARTIAL)
656 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200657 partial_T *pt = tv->vval.v_partial;
658 int i;
659
660 if (pt->pt_argc > 0)
661 {
662 // Make space for arguments from the partial, shift the "argcount"
663 // arguments up.
664 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
665 return FAIL;
666 for (i = 1; i <= argcount; ++i)
667 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
668 ectx->ec_stack.ga_len += pt->pt_argc;
669 argcount += pt->pt_argc;
670
671 // copy the arguments from the partial onto the stack
672 for (i = 0; i < pt->pt_argc; ++i)
673 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200677 {
678 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
679
680 // closure may need the function context where it was defined
681 ectx->ec_outer_stack = pt->pt_ectx_stack;
682 ectx->ec_outer_frame = pt->pt_ectx_frame;
683
684 return ret;
685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100686 name = pt->pt_name;
687 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200688 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200690 if (name != NULL)
691 {
692 char_u fname_buf[FLEN_FIXED + 1];
693 char_u *tofree = NULL;
694 int error = FCERR_NONE;
695 char_u *fname;
696
697 // May need to translate <SNR>123_ to K_SNR.
698 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
699 if (error != FCERR_NONE)
700 res = FAIL;
701 else
702 res = call_by_name(fname, argcount, ectx, NULL);
703 vim_free(tofree);
704 }
705
706 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100707 {
708 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200709 semsg(_(e_unknownfunc),
710 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 return FAIL;
712 }
713 return OK;
714}
715
716/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200717 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
718 * TRUE.
719 */
720 static int
721error_if_locked(int lock, char *error)
722{
723 if (lock & (VAR_LOCKED | VAR_FIXED))
724 {
725 emsg(_(error));
726 return TRUE;
727 }
728 return FALSE;
729}
730
731/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100732 * Store "tv" in variable "name".
733 * This is for s: and g: variables.
734 */
735 static void
736store_var(char_u *name, typval_T *tv)
737{
738 funccal_entry_T entry;
739
740 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200741 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100742 restore_funccal();
743}
744
Bram Moolenaard3aac292020-04-19 14:32:17 +0200745
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100746/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 * Execute a function by "name".
748 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100749 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 */
751 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100752call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753{
Bram Moolenaared677f52020-08-12 16:38:10 +0200754 int called_emsg_before = called_emsg;
755 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756
Bram Moolenaared677f52020-08-12 16:38:10 +0200757 res = call_by_name(name, argcount, ectx, iptr);
758 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100759 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200760 dictitem_T *v;
761
762 v = find_var(name, NULL, FALSE);
763 if (v == NULL)
764 {
765 semsg(_(e_unknownfunc), name);
766 return FAIL;
767 }
768 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
769 {
770 semsg(_(e_unknownfunc), name);
771 return FAIL;
772 }
773 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200775 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776}
777
778/*
779 * Call a "def" function from old Vim script.
780 * Return OK or FAIL.
781 */
782 int
783call_def_function(
784 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200785 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200787 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788 typval_T *rettv) // return value
789{
790 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200791 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200792 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 typval_T *tv;
794 int idx;
795 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100796 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200797 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200798 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200799 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200800 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801
802// Get pointer to item in the stack.
803#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
804
805// Get pointer to item at the bottom of the stack, -1 is the bottom.
806#undef STACK_TV_BOT
807#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
808
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200809// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810#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 +0100811
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200812// Like STACK_TV_VAR but use the outer scope
813#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
814
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200815 if (ufunc->uf_def_status == UF_NOT_COMPILED
816 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200817 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200818 {
819 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200820 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200821 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200822 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200823 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200824
Bram Moolenaar09689a02020-05-09 22:50:08 +0200825 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200826 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200827 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
828 + ufunc->uf_dfunc_idx;
829 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200830 {
831 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200832 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200833 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200834 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200836 CLEAR_FIELD(ectx);
837 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
838 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
839 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
840 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200842 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843
844 // Put arguments on the stack.
845 for (idx = 0; idx < argc; ++idx)
846 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200847 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200848 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
849 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200850 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 copy_tv(&argv[idx], STACK_TV_BOT(0));
852 ++ectx.ec_stack.ga_len;
853 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200854
855 // Turn varargs into a list. Empty list if no args.
856 if (ufunc->uf_va_name != NULL)
857 {
858 int vararg_count = argc - ufunc->uf_args.ga_len;
859
860 if (vararg_count < 0)
861 vararg_count = 0;
862 else
863 argc -= vararg_count;
864 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200865 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200866
867 // Check the type of the list items.
868 tv = STACK_TV_BOT(-1);
869 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200870 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200871 && ufunc->uf_va_type->tt_member != &t_any
872 && tv->vval.v_list != NULL)
873 {
874 type_T *expected = ufunc->uf_va_type->tt_member;
875 listitem_T *li = tv->vval.v_list->lv_first;
876
877 for (idx = 0; idx < vararg_count; ++idx)
878 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200879 if (check_typval_type(expected, &li->li_tv,
880 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200881 goto failed_early;
882 li = li->li_next;
883 }
884 }
885
Bram Moolenaar23e03252020-04-12 22:22:31 +0200886 if (defcount > 0)
887 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200888 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200889 --ectx.ec_stack.ga_len;
890 }
891
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100892 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200893 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100894 if (defcount > 0)
895 for (idx = 0; idx < defcount; ++idx)
896 {
897 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
898 ++ectx.ec_stack.ga_len;
899 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200900 if (ufunc->uf_va_name != NULL)
901 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902
903 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200904 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
905 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100906
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200907 if (partial != NULL)
908 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200909 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
910 {
911 // TODO: is this always the right way?
912 ectx.ec_outer_stack = &current_ectx->ec_stack;
913 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
914 }
915 else
916 {
917 ectx.ec_outer_stack = partial->pt_ectx_stack;
918 ectx.ec_outer_frame = partial->pt_ectx_frame;
919 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200920 }
921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100922 // dummy frame entries
923 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
924 {
925 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
926 ++ectx.ec_stack.ga_len;
927 }
928
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200929 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200930 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200931 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
932 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200934 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200935 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200936 ectx.ec_stack.ga_len += dfunc->df_varcount;
937 if (dfunc->df_has_closure)
938 {
939 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
940 STACK_TV_VAR(idx)->vval.v_number = 0;
941 ++ectx.ec_stack.ga_len;
942 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200943
944 ectx.ec_instr = dfunc->df_instr;
945 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100946
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200947 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200948 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200949 estack_push_ufunc(ufunc, 1);
950 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200951 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
952
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200953 // Do turn errors into exceptions.
954 suppress_errthrow = FALSE;
955
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100956 // Decide where to start execution, handles optional arguments.
957 init_instr_idx(ufunc, argc, &ectx);
958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 for (;;)
960 {
961 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100962
Bram Moolenaar270d0382020-05-15 21:42:53 +0200963 if (++breakcheck_count >= 100)
964 {
965 line_breakcheck();
966 breakcheck_count = 0;
967 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100968 if (got_int)
969 {
970 // Turn CTRL-C into an exception.
971 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100972 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100973 goto failed;
974 did_throw = TRUE;
975 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976
Bram Moolenaara26b9702020-04-18 19:53:28 +0200977 if (did_emsg && msg_list != NULL && *msg_list != NULL)
978 {
979 // Turn an error message into an exception.
980 did_emsg = FALSE;
981 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
982 goto failed;
983 did_throw = TRUE;
984 *msg_list = NULL;
985 }
986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 if (did_throw && !ectx.ec_in_catch)
988 {
989 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100990 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100991
992 // An exception jumps to the first catch, finally, or returns from
993 // the current function.
994 if (trystack->ga_len > 0)
995 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200996 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 {
998 // jump to ":catch" or ":finally"
999 ectx.ec_in_catch = TRUE;
1000 ectx.ec_iidx = trycmd->tcd_catch_idx;
1001 }
1002 else
1003 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001004 // Not inside try or need to return from current functions.
1005 // Push a dummy return value.
1006 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1007 goto failed;
1008 tv = STACK_TV_BOT(0);
1009 tv->v_type = VAR_NUMBER;
1010 tv->vval.v_number = 0;
1011 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001012 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001014 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001015 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001016 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1017 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 goto done;
1019 }
1020
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001021 if (func_return(&ectx) == FAIL)
1022 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001023 }
1024 continue;
1025 }
1026
1027 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1028 switch (iptr->isn_type)
1029 {
1030 // execute Ex command line
1031 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001032 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 do_cmdline_cmd(iptr->isn_arg.string);
1034 break;
1035
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001036 // execute Ex command from pieces on the stack
1037 case ISN_EXECCONCAT:
1038 {
1039 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001040 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001041 int pass;
1042 int i;
1043 char_u *cmd = NULL;
1044 char_u *str;
1045
1046 for (pass = 1; pass <= 2; ++pass)
1047 {
1048 for (i = 0; i < count; ++i)
1049 {
1050 tv = STACK_TV_BOT(i - count);
1051 str = tv->vval.v_string;
1052 if (str != NULL && *str != NUL)
1053 {
1054 if (pass == 2)
1055 STRCPY(cmd + len, str);
1056 len += STRLEN(str);
1057 }
1058 if (pass == 2)
1059 clear_tv(tv);
1060 }
1061 if (pass == 1)
1062 {
1063 cmd = alloc(len + 1);
1064 if (cmd == NULL)
1065 goto failed;
1066 len = 0;
1067 }
1068 }
1069
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001070 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001071 do_cmdline_cmd(cmd);
1072 vim_free(cmd);
1073 }
1074 break;
1075
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 // execute :echo {string} ...
1077 case ISN_ECHO:
1078 {
1079 int count = iptr->isn_arg.echo.echo_count;
1080 int atstart = TRUE;
1081 int needclr = TRUE;
1082
1083 for (idx = 0; idx < count; ++idx)
1084 {
1085 tv = STACK_TV_BOT(idx - count);
1086 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1087 &atstart, &needclr);
1088 clear_tv(tv);
1089 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001090 if (needclr)
1091 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 ectx.ec_stack.ga_len -= count;
1093 }
1094 break;
1095
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001096 // :execute {string} ...
1097 // :echomsg {string} ...
1098 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001099 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001100 case ISN_ECHOMSG:
1101 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001102 {
1103 int count = iptr->isn_arg.number;
1104 garray_T ga;
1105 char_u buf[NUMBUFLEN];
1106 char_u *p;
1107 int len;
1108 int failed = FALSE;
1109
1110 ga_init2(&ga, 1, 80);
1111 for (idx = 0; idx < count; ++idx)
1112 {
1113 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001114 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001115 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001116 if (tv->v_type == VAR_CHANNEL
1117 || tv->v_type == VAR_JOB)
1118 {
1119 SOURCING_LNUM = iptr->isn_lnum;
1120 emsg(_(e_inval_string));
1121 break;
1122 }
1123 else
1124 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001125 }
1126 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001127 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001128
1129 len = (int)STRLEN(p);
1130 if (ga_grow(&ga, len + 2) == FAIL)
1131 failed = TRUE;
1132 else
1133 {
1134 if (ga.ga_len > 0)
1135 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1136 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1137 ga.ga_len += len;
1138 }
1139 clear_tv(tv);
1140 }
1141 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001142 if (failed)
1143 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001144
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001145 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001146 {
1147 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001148 {
1149 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001150 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001151 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001152 else
1153 {
1154 msg_sb_eol();
1155 if (iptr->isn_type == ISN_ECHOMSG)
1156 {
1157 msg_attr(ga.ga_data, echo_attr);
1158 out_flush();
1159 }
1160 else
1161 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001162 SOURCING_LNUM = iptr->isn_lnum;
1163 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001164 }
1165 }
1166 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001167 ga_clear(&ga);
1168 }
1169 break;
1170
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 // load local variable or argument
1172 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001173 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 goto failed;
1175 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1176 ++ectx.ec_stack.ga_len;
1177 break;
1178
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001179 // load variable or argument from outer scope
1180 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001182 goto failed;
1183 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1184 STACK_TV_BOT(0));
1185 ++ectx.ec_stack.ga_len;
1186 break;
1187
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 // load v: variable
1189 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001190 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 goto failed;
1192 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1193 ++ectx.ec_stack.ga_len;
1194 break;
1195
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001196 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 case ISN_LOADSCRIPT:
1198 {
1199 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001200 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 svar_T *sv;
1202
1203 sv = ((svar_T *)si->sn_var_vals.ga_data)
1204 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001205 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 goto failed;
1207 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1208 ++ectx.ec_stack.ga_len;
1209 }
1210 break;
1211
1212 // load s: variable in old script
1213 case ISN_LOADS:
1214 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001215 hashtab_T *ht = &SCRIPT_VARS(
1216 iptr->isn_arg.loadstore.ls_sid);
1217 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001219
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 if (di == NULL)
1221 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001222 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001223 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001224 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 }
1226 else
1227 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001228 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 goto failed;
1230 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1231 ++ectx.ec_stack.ga_len;
1232 }
1233 }
1234 break;
1235
Bram Moolenaard3aac292020-04-19 14:32:17 +02001236 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001238 case ISN_LOADB:
1239 case ISN_LOADW:
1240 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001242 dictitem_T *di = NULL;
1243 hashtab_T *ht = NULL;
1244 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001245
Bram Moolenaard3aac292020-04-19 14:32:17 +02001246 switch (iptr->isn_type)
1247 {
1248 case ISN_LOADG:
1249 ht = get_globvar_ht();
1250 namespace = 'g';
1251 break;
1252 case ISN_LOADB:
1253 ht = &curbuf->b_vars->dv_hashtab;
1254 namespace = 'b';
1255 break;
1256 case ISN_LOADW:
1257 ht = &curwin->w_vars->dv_hashtab;
1258 namespace = 'w';
1259 break;
1260 case ISN_LOADT:
1261 ht = &curtab->tp_vars->dv_hashtab;
1262 namespace = 't';
1263 break;
1264 default: // Cannot reach here
1265 goto failed;
1266 }
1267 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 if (di == NULL)
1270 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001272 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001273 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001274 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001275 }
1276 else
1277 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001278 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 goto failed;
1280 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1281 ++ectx.ec_stack.ga_len;
1282 }
1283 }
1284 break;
1285
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001286 // load g:/b:/w:/t: namespace
1287 case ISN_LOADGDICT:
1288 case ISN_LOADBDICT:
1289 case ISN_LOADWDICT:
1290 case ISN_LOADTDICT:
1291 {
1292 dict_T *d = NULL;
1293
1294 switch (iptr->isn_type)
1295 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001296 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1297 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1298 case ISN_LOADWDICT: d = curwin->w_vars; break;
1299 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001300 default: // Cannot reach here
1301 goto failed;
1302 }
1303 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1304 goto failed;
1305 tv = STACK_TV_BOT(0);
1306 tv->v_type = VAR_DICT;
1307 tv->v_lock = 0;
1308 tv->vval.v_dict = d;
1309 ++ectx.ec_stack.ga_len;
1310 }
1311 break;
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 // load &option
1314 case ISN_LOADOPT:
1315 {
1316 typval_T optval;
1317 char_u *name = iptr->isn_arg.string;
1318
Bram Moolenaara8c17702020-04-01 21:17:24 +02001319 // This is not expected to fail, name is checked during
1320 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001321 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001322 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001323 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001324 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 *STACK_TV_BOT(0) = optval;
1326 ++ectx.ec_stack.ga_len;
1327 }
1328 break;
1329
1330 // load $ENV
1331 case ISN_LOADENV:
1332 {
1333 typval_T optval;
1334 char_u *name = iptr->isn_arg.string;
1335
Bram Moolenaar270d0382020-05-15 21:42:53 +02001336 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001338 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001339 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 *STACK_TV_BOT(0) = optval;
1341 ++ectx.ec_stack.ga_len;
1342 }
1343 break;
1344
1345 // load @register
1346 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001347 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 goto failed;
1349 tv = STACK_TV_BOT(0);
1350 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001351 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 tv->vval.v_string = get_reg_contents(
1353 iptr->isn_arg.number, GREG_EXPR_SRC);
1354 ++ectx.ec_stack.ga_len;
1355 break;
1356
1357 // store local variable
1358 case ISN_STORE:
1359 --ectx.ec_stack.ga_len;
1360 tv = STACK_TV_VAR(iptr->isn_arg.number);
1361 clear_tv(tv);
1362 *tv = *STACK_TV_BOT(0);
1363 break;
1364
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001365 // store variable or argument in outer scope
1366 case ISN_STOREOUTER:
1367 --ectx.ec_stack.ga_len;
1368 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1369 clear_tv(tv);
1370 *tv = *STACK_TV_BOT(0);
1371 break;
1372
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001373 // store s: variable in old script
1374 case ISN_STORES:
1375 {
1376 hashtab_T *ht = &SCRIPT_VARS(
1377 iptr->isn_arg.loadstore.ls_sid);
1378 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001379 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001380
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001381 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001382 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001383 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001384 else
1385 {
1386 clear_tv(&di->di_tv);
1387 di->di_tv = *STACK_TV_BOT(0);
1388 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001389 }
1390 break;
1391
1392 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 case ISN_STORESCRIPT:
1394 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001395 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 iptr->isn_arg.script.script_sid);
1397 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1398 + iptr->isn_arg.script.script_idx;
1399
1400 --ectx.ec_stack.ga_len;
1401 clear_tv(sv->sv_tv);
1402 *sv->sv_tv = *STACK_TV_BOT(0);
1403 }
1404 break;
1405
1406 // store option
1407 case ISN_STOREOPT:
1408 {
1409 long n = 0;
1410 char_u *s = NULL;
1411 char *msg;
1412
1413 --ectx.ec_stack.ga_len;
1414 tv = STACK_TV_BOT(0);
1415 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001416 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001418 if (s == NULL)
1419 s = (char_u *)"";
1420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001422 // must be VAR_NUMBER, CHECKTYPE makes sure
1423 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1425 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001426 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 if (msg != NULL)
1428 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001429 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001431 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 }
1434 break;
1435
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001436 // store $ENV
1437 case ISN_STOREENV:
1438 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001439 tv = STACK_TV_BOT(0);
1440 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1441 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442 break;
1443
1444 // store @r
1445 case ISN_STOREREG:
1446 {
1447 int reg = iptr->isn_arg.number;
1448
1449 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001450 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001451 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001452 tv_get_string(tv), -1, FALSE);
1453 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001454 }
1455 break;
1456
1457 // store v: variable
1458 case ISN_STOREV:
1459 --ectx.ec_stack.ga_len;
1460 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1461 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001462 // should not happen, type is checked when compiling
1463 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001464 break;
1465
Bram Moolenaard3aac292020-04-19 14:32:17 +02001466 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001468 case ISN_STOREB:
1469 case ISN_STOREW:
1470 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001471 {
1472 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001473 hashtab_T *ht;
1474 switch (iptr->isn_type)
1475 {
1476 case ISN_STOREG:
1477 ht = get_globvar_ht();
1478 break;
1479 case ISN_STOREB:
1480 ht = &curbuf->b_vars->dv_hashtab;
1481 break;
1482 case ISN_STOREW:
1483 ht = &curwin->w_vars->dv_hashtab;
1484 break;
1485 case ISN_STORET:
1486 ht = &curtab->tp_vars->dv_hashtab;
1487 break;
1488 default: // Cannot reach here
1489 goto failed;
1490 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001491
1492 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001493 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001495 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 else
1497 {
1498 clear_tv(&di->di_tv);
1499 di->di_tv = *STACK_TV_BOT(0);
1500 }
1501 }
1502 break;
1503
1504 // store number in local variable
1505 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001506 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 clear_tv(tv);
1508 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001509 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 break;
1511
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001512 // store value in list variable
1513 case ISN_STORELIST:
1514 {
1515 typval_T *tv_idx = STACK_TV_BOT(-2);
1516 varnumber_T lidx = tv_idx->vval.v_number;
1517 typval_T *tv_list = STACK_TV_BOT(-1);
1518 list_T *list = tv_list->vval.v_list;
1519
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001521 if (lidx < 0 && list->lv_len + lidx >= 0)
1522 // negative index is relative to the end
1523 lidx = list->lv_len + lidx;
1524 if (lidx < 0 || lidx > list->lv_len)
1525 {
1526 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001527 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001528 }
1529 tv = STACK_TV_BOT(-3);
1530 if (lidx < list->lv_len)
1531 {
1532 listitem_T *li = list_find(list, lidx);
1533
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001534 if (error_if_locked(li->li_tv.v_lock,
1535 e_cannot_change_list_item))
1536 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001537 // overwrite existing list item
1538 clear_tv(&li->li_tv);
1539 li->li_tv = *tv;
1540 }
1541 else
1542 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001543 if (error_if_locked(list->lv_lock,
1544 e_cannot_change_list))
1545 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001546 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001547 if (list_append_tv(list, tv) == FAIL)
1548 goto failed;
1549 clear_tv(tv);
1550 }
1551 clear_tv(tv_idx);
1552 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001553 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001554 }
1555 break;
1556
1557 // store value in dict variable
1558 case ISN_STOREDICT:
1559 {
1560 typval_T *tv_key = STACK_TV_BOT(-2);
1561 char_u *key = tv_key->vval.v_string;
1562 typval_T *tv_dict = STACK_TV_BOT(-1);
1563 dict_T *dict = tv_dict->vval.v_dict;
1564 dictitem_T *di;
1565
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001567 if (dict == NULL)
1568 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001569 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001570 goto on_error;
1571 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001572 if (key == NULL)
1573 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001574 tv = STACK_TV_BOT(-3);
1575 di = dict_find(dict, key, -1);
1576 if (di != NULL)
1577 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 if (error_if_locked(di->di_tv.v_lock,
1579 e_cannot_change_dict_item))
1580 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001581 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001582 clear_tv(&di->di_tv);
1583 di->di_tv = *tv;
1584 }
1585 else
1586 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001587 if (error_if_locked(dict->dv_lock,
1588 e_cannot_change_dict))
1589 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001590 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001591 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1592 goto failed;
1593 clear_tv(tv);
1594 }
1595 clear_tv(tv_key);
1596 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001597 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001598 }
1599 break;
1600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 // push constant
1602 case ISN_PUSHNR:
1603 case ISN_PUSHBOOL:
1604 case ISN_PUSHSPEC:
1605 case ISN_PUSHF:
1606 case ISN_PUSHS:
1607 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001608 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001609 case ISN_PUSHCHANNEL:
1610 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001611 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 goto failed;
1613 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001614 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 ++ectx.ec_stack.ga_len;
1616 switch (iptr->isn_type)
1617 {
1618 case ISN_PUSHNR:
1619 tv->v_type = VAR_NUMBER;
1620 tv->vval.v_number = iptr->isn_arg.number;
1621 break;
1622 case ISN_PUSHBOOL:
1623 tv->v_type = VAR_BOOL;
1624 tv->vval.v_number = iptr->isn_arg.number;
1625 break;
1626 case ISN_PUSHSPEC:
1627 tv->v_type = VAR_SPECIAL;
1628 tv->vval.v_number = iptr->isn_arg.number;
1629 break;
1630#ifdef FEAT_FLOAT
1631 case ISN_PUSHF:
1632 tv->v_type = VAR_FLOAT;
1633 tv->vval.v_float = iptr->isn_arg.fnumber;
1634 break;
1635#endif
1636 case ISN_PUSHBLOB:
1637 blob_copy(iptr->isn_arg.blob, tv);
1638 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001639 case ISN_PUSHFUNC:
1640 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001641 if (iptr->isn_arg.string == NULL)
1642 tv->vval.v_string = NULL;
1643 else
1644 tv->vval.v_string =
1645 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001646 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001647 case ISN_PUSHCHANNEL:
1648#ifdef FEAT_JOB_CHANNEL
1649 tv->v_type = VAR_CHANNEL;
1650 tv->vval.v_channel = iptr->isn_arg.channel;
1651 if (tv->vval.v_channel != NULL)
1652 ++tv->vval.v_channel->ch_refcount;
1653#endif
1654 break;
1655 case ISN_PUSHJOB:
1656#ifdef FEAT_JOB_CHANNEL
1657 tv->v_type = VAR_JOB;
1658 tv->vval.v_job = iptr->isn_arg.job;
1659 if (tv->vval.v_job != NULL)
1660 ++tv->vval.v_job->jv_refcount;
1661#endif
1662 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 default:
1664 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001665 tv->vval.v_string = vim_strsave(
1666 iptr->isn_arg.string == NULL
1667 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 }
1669 break;
1670
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001671 case ISN_UNLET:
1672 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1673 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001674 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001675 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001676 case ISN_UNLETENV:
1677 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1678 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001679
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001680 case ISN_LOCKCONST:
1681 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1682 break;
1683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001684 // create a list from items on the stack; uses a single allocation
1685 // for the list header and the items
1686 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001687 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1688 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 break;
1690
1691 // create a dict from items on the stack
1692 case ISN_NEWDICT:
1693 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001694 int count = iptr->isn_arg.number;
1695 dict_T *dict = dict_alloc();
1696 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697
1698 if (dict == NULL)
1699 goto failed;
1700 for (idx = 0; idx < count; ++idx)
1701 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001702 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001704 // check key is unique
1705 item = dict_find(dict, tv->vval.v_string, -1);
1706 if (item != NULL)
1707 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001708 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001709 semsg(_(e_duplicate_key), tv->vval.v_string);
1710 dict_unref(dict);
1711 goto on_error;
1712 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001713 item = dictitem_alloc(tv->vval.v_string);
1714 clear_tv(tv);
1715 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001716 {
1717 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001719 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1721 item->di_tv.v_lock = 0;
1722 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001723 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001724 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001725 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 }
1729
1730 if (count > 0)
1731 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001732 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 goto failed;
1734 else
1735 ++ectx.ec_stack.ga_len;
1736 tv = STACK_TV_BOT(-1);
1737 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001738 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 tv->vval.v_dict = dict;
1740 ++dict->dv_refcount;
1741 }
1742 break;
1743
1744 // call a :def function
1745 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001746 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1748 iptr->isn_arg.dfunc.cdf_argcount,
1749 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 break;
1752
1753 // call a builtin function
1754 case ISN_BCALL:
1755 SOURCING_LNUM = iptr->isn_lnum;
1756 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1757 iptr->isn_arg.bfunc.cbf_argcount,
1758 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001759 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 break;
1761
1762 // call a funcref or partial
1763 case ISN_PCALL:
1764 {
1765 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1766 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001767 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001768
1769 SOURCING_LNUM = iptr->isn_lnum;
1770 if (pfunc->cpf_top)
1771 {
1772 // funcref is above the arguments
1773 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1774 }
1775 else
1776 {
1777 // Get the funcref from the stack.
1778 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001779 partial_tv = *STACK_TV_BOT(0);
1780 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 }
1782 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001783 if (tv == &partial_tv)
1784 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001786 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 }
1788 break;
1789
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001790 case ISN_PCALL_END:
1791 // PCALL finished, arguments have been consumed and replaced by
1792 // the return value. Now clear the funcref from the stack,
1793 // and move the return value in its place.
1794 --ectx.ec_stack.ga_len;
1795 clear_tv(STACK_TV_BOT(-1));
1796 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1797 break;
1798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 // call a user defined function or funcref/partial
1800 case ISN_UCALL:
1801 {
1802 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1803
1804 SOURCING_LNUM = iptr->isn_lnum;
1805 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001806 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001807 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 }
1809 break;
1810
1811 // return from a :def function call
1812 case ISN_RETURN:
1813 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001814 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001815 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001816
1817 if (trystack->ga_len > 0)
1818 trycmd = ((trycmd_T *)trystack->ga_data)
1819 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001820 if (trycmd != NULL
1821 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001822 && trycmd->tcd_finally_idx != 0)
1823 {
1824 // jump to ":finally"
1825 ectx.ec_iidx = trycmd->tcd_finally_idx;
1826 trycmd->tcd_return = TRUE;
1827 }
1828 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001829 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 }
1831 break;
1832
1833 // push a function reference to a compiled function
1834 case ISN_FUNCREF:
1835 {
1836 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001837 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838
1839 pt = ALLOC_CLEAR_ONE(partial_T);
1840 if (pt == NULL)
1841 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001842 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001843 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001844 vim_free(pt);
1845 goto failed;
1846 }
1847 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1848 + iptr->isn_arg.funcref.fr_func;
1849 pt->pt_func = pt_dfunc->df_ufunc;
1850 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001851
1852 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1853 {
1854 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1855 + ectx.ec_dfunc_idx;
1856
1857 // The closure needs to find arguments and local
1858 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001859 pt->pt_ectx_stack = &ectx.ec_stack;
1860 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001861
1862 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001863 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001864 // (arguments and local variables). Store a reference
1865 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001866 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001867 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001868 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001869 goto failed;
1870 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001871 // Extra variable keeps the count of closures created
1872 // in the current function call.
1873 tv = STACK_TV_VAR(dfunc->df_varcount);
1874 ++tv->vval.v_number;
1875
1876 ((partial_T **)ectx.ec_funcrefs.ga_data)
1877 [ectx.ec_funcrefs.ga_len] = pt;
1878 ++pt->pt_refcount;
1879 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001880 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001881 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001882
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 tv = STACK_TV_BOT(0);
1884 ++ectx.ec_stack.ga_len;
1885 tv->vval.v_partial = pt;
1886 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001887 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 }
1889 break;
1890
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001891 // Create a global function from a lambda.
1892 case ISN_NEWFUNC:
1893 {
1894 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1895
1896 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1897 }
1898 break;
1899
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 // jump if a condition is met
1901 case ISN_JUMP:
1902 {
1903 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001904 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 int jump = TRUE;
1906
1907 if (when != JUMP_ALWAYS)
1908 {
1909 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001910 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02001911 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001912 || when == JUMP_IF_COND_TRUE)
1913 {
1914 SOURCING_LNUM = iptr->isn_lnum;
1915 jump = tv_get_bool_chk(tv, &error);
1916 if (error)
1917 goto on_error;
1918 }
1919 else
1920 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02001922 || when == JUMP_AND_KEEP_IF_FALSE
1923 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001925 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 {
1927 // drop the value from the stack
1928 clear_tv(tv);
1929 --ectx.ec_stack.ga_len;
1930 }
1931 }
1932 if (jump)
1933 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1934 }
1935 break;
1936
1937 // top of a for loop
1938 case ISN_FOR:
1939 {
1940 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1941 typval_T *idxtv =
1942 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1943
1944 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001945 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001947 ++idxtv->vval.v_number;
1948 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 // past the end of the list, jump to "endfor"
1950 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1951 else if (list->lv_first == &range_list_item)
1952 {
1953 // non-materialized range() list
1954 tv = STACK_TV_BOT(0);
1955 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001956 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 tv->vval.v_number = list_find_nr(
1958 list, idxtv->vval.v_number, NULL);
1959 ++ectx.ec_stack.ga_len;
1960 }
1961 else
1962 {
1963 listitem_T *li = list_find(list, idxtv->vval.v_number);
1964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1966 ++ectx.ec_stack.ga_len;
1967 }
1968 }
1969 break;
1970
1971 // start of ":try" block
1972 case ISN_TRY:
1973 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001974 trycmd_T *trycmd = NULL;
1975
Bram Moolenaar270d0382020-05-15 21:42:53 +02001976 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 goto failed;
1978 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1979 + ectx.ec_trystack.ga_len;
1980 ++ectx.ec_trystack.ga_len;
1981 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001982 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1984 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001985 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001986 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 }
1988 break;
1989
1990 case ISN_PUSHEXC:
1991 if (current_exception == NULL)
1992 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001993 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 iemsg("Evaluating catch while current_exception is NULL");
1995 goto failed;
1996 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001997 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 goto failed;
1999 tv = STACK_TV_BOT(0);
2000 ++ectx.ec_stack.ga_len;
2001 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002002 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 tv->vval.v_string = vim_strsave(
2004 (char_u *)current_exception->value);
2005 break;
2006
2007 case ISN_CATCH:
2008 {
2009 garray_T *trystack = &ectx.ec_trystack;
2010
2011 if (trystack->ga_len > 0)
2012 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002013 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 + trystack->ga_len - 1;
2015 trycmd->tcd_caught = TRUE;
2016 }
2017 did_emsg = got_int = did_throw = FALSE;
2018 catch_exception(current_exception);
2019 }
2020 break;
2021
2022 // end of ":try" block
2023 case ISN_ENDTRY:
2024 {
2025 garray_T *trystack = &ectx.ec_trystack;
2026
2027 if (trystack->ga_len > 0)
2028 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002029 trycmd_T *trycmd = NULL;
2030
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 --trystack->ga_len;
2032 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002033 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 trycmd = ((trycmd_T *)trystack->ga_data)
2035 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002036 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 {
2038 // discard the exception
2039 if (caught_stack == current_exception)
2040 caught_stack = caught_stack->caught;
2041 discard_current_exception();
2042 }
2043
2044 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002045 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 }
2047 }
2048 break;
2049
2050 case ISN_THROW:
2051 --ectx.ec_stack.ga_len;
2052 tv = STACK_TV_BOT(0);
2053 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2054 {
2055 vim_free(tv->vval.v_string);
2056 goto failed;
2057 }
2058 did_throw = TRUE;
2059 break;
2060
2061 // compare with special values
2062 case ISN_COMPAREBOOL:
2063 case ISN_COMPARESPECIAL:
2064 {
2065 typval_T *tv1 = STACK_TV_BOT(-2);
2066 typval_T *tv2 = STACK_TV_BOT(-1);
2067 varnumber_T arg1 = tv1->vval.v_number;
2068 varnumber_T arg2 = tv2->vval.v_number;
2069 int res;
2070
2071 switch (iptr->isn_arg.op.op_type)
2072 {
2073 case EXPR_EQUAL: res = arg1 == arg2; break;
2074 case EXPR_NEQUAL: res = arg1 != arg2; break;
2075 default: res = 0; break;
2076 }
2077
2078 --ectx.ec_stack.ga_len;
2079 tv1->v_type = VAR_BOOL;
2080 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2081 }
2082 break;
2083
2084 // Operation with two number arguments
2085 case ISN_OPNR:
2086 case ISN_COMPARENR:
2087 {
2088 typval_T *tv1 = STACK_TV_BOT(-2);
2089 typval_T *tv2 = STACK_TV_BOT(-1);
2090 varnumber_T arg1 = tv1->vval.v_number;
2091 varnumber_T arg2 = tv2->vval.v_number;
2092 varnumber_T res;
2093
2094 switch (iptr->isn_arg.op.op_type)
2095 {
2096 case EXPR_MULT: res = arg1 * arg2; break;
2097 case EXPR_DIV: res = arg1 / arg2; break;
2098 case EXPR_REM: res = arg1 % arg2; break;
2099 case EXPR_SUB: res = arg1 - arg2; break;
2100 case EXPR_ADD: res = arg1 + arg2; break;
2101
2102 case EXPR_EQUAL: res = arg1 == arg2; break;
2103 case EXPR_NEQUAL: res = arg1 != arg2; break;
2104 case EXPR_GREATER: res = arg1 > arg2; break;
2105 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2106 case EXPR_SMALLER: res = arg1 < arg2; break;
2107 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2108 default: res = 0; break;
2109 }
2110
2111 --ectx.ec_stack.ga_len;
2112 if (iptr->isn_type == ISN_COMPARENR)
2113 {
2114 tv1->v_type = VAR_BOOL;
2115 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2116 }
2117 else
2118 tv1->vval.v_number = res;
2119 }
2120 break;
2121
2122 // Computation with two float arguments
2123 case ISN_OPFLOAT:
2124 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002125#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002126 {
2127 typval_T *tv1 = STACK_TV_BOT(-2);
2128 typval_T *tv2 = STACK_TV_BOT(-1);
2129 float_T arg1 = tv1->vval.v_float;
2130 float_T arg2 = tv2->vval.v_float;
2131 float_T res = 0;
2132 int cmp = FALSE;
2133
2134 switch (iptr->isn_arg.op.op_type)
2135 {
2136 case EXPR_MULT: res = arg1 * arg2; break;
2137 case EXPR_DIV: res = arg1 / arg2; break;
2138 case EXPR_SUB: res = arg1 - arg2; break;
2139 case EXPR_ADD: res = arg1 + arg2; break;
2140
2141 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2142 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2143 case EXPR_GREATER: cmp = arg1 > arg2; break;
2144 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2145 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2146 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2147 default: cmp = 0; break;
2148 }
2149 --ectx.ec_stack.ga_len;
2150 if (iptr->isn_type == ISN_COMPAREFLOAT)
2151 {
2152 tv1->v_type = VAR_BOOL;
2153 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2154 }
2155 else
2156 tv1->vval.v_float = res;
2157 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002158#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 break;
2160
2161 case ISN_COMPARELIST:
2162 {
2163 typval_T *tv1 = STACK_TV_BOT(-2);
2164 typval_T *tv2 = STACK_TV_BOT(-1);
2165 list_T *arg1 = tv1->vval.v_list;
2166 list_T *arg2 = tv2->vval.v_list;
2167 int cmp = FALSE;
2168 int ic = iptr->isn_arg.op.op_ic;
2169
2170 switch (iptr->isn_arg.op.op_type)
2171 {
2172 case EXPR_EQUAL: cmp =
2173 list_equal(arg1, arg2, ic, FALSE); break;
2174 case EXPR_NEQUAL: cmp =
2175 !list_equal(arg1, arg2, ic, FALSE); break;
2176 case EXPR_IS: cmp = arg1 == arg2; break;
2177 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2178 default: cmp = 0; break;
2179 }
2180 --ectx.ec_stack.ga_len;
2181 clear_tv(tv1);
2182 clear_tv(tv2);
2183 tv1->v_type = VAR_BOOL;
2184 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2185 }
2186 break;
2187
2188 case ISN_COMPAREBLOB:
2189 {
2190 typval_T *tv1 = STACK_TV_BOT(-2);
2191 typval_T *tv2 = STACK_TV_BOT(-1);
2192 blob_T *arg1 = tv1->vval.v_blob;
2193 blob_T *arg2 = tv2->vval.v_blob;
2194 int cmp = FALSE;
2195
2196 switch (iptr->isn_arg.op.op_type)
2197 {
2198 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2199 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2200 case EXPR_IS: cmp = arg1 == arg2; break;
2201 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2202 default: cmp = 0; break;
2203 }
2204 --ectx.ec_stack.ga_len;
2205 clear_tv(tv1);
2206 clear_tv(tv2);
2207 tv1->v_type = VAR_BOOL;
2208 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2209 }
2210 break;
2211
2212 // TODO: handle separately
2213 case ISN_COMPARESTRING:
2214 case ISN_COMPAREDICT:
2215 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 case ISN_COMPAREANY:
2217 {
2218 typval_T *tv1 = STACK_TV_BOT(-2);
2219 typval_T *tv2 = STACK_TV_BOT(-1);
2220 exptype_T exptype = iptr->isn_arg.op.op_type;
2221 int ic = iptr->isn_arg.op.op_ic;
2222
Bram Moolenaareb26f432020-09-14 16:50:05 +02002223 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 typval_compare(tv1, tv2, exptype, ic);
2225 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 --ectx.ec_stack.ga_len;
2227 }
2228 break;
2229
2230 case ISN_ADDLIST:
2231 case ISN_ADDBLOB:
2232 {
2233 typval_T *tv1 = STACK_TV_BOT(-2);
2234 typval_T *tv2 = STACK_TV_BOT(-1);
2235
2236 if (iptr->isn_type == ISN_ADDLIST)
2237 eval_addlist(tv1, tv2);
2238 else
2239 eval_addblob(tv1, tv2);
2240 clear_tv(tv2);
2241 --ectx.ec_stack.ga_len;
2242 }
2243 break;
2244
2245 // Computation with two arguments of unknown type
2246 case ISN_OPANY:
2247 {
2248 typval_T *tv1 = STACK_TV_BOT(-2);
2249 typval_T *tv2 = STACK_TV_BOT(-1);
2250 varnumber_T n1, n2;
2251#ifdef FEAT_FLOAT
2252 float_T f1 = 0, f2 = 0;
2253#endif
2254 int error = FALSE;
2255
2256 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2257 {
2258 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2259 {
2260 eval_addlist(tv1, tv2);
2261 clear_tv(tv2);
2262 --ectx.ec_stack.ga_len;
2263 break;
2264 }
2265 else if (tv1->v_type == VAR_BLOB
2266 && tv2->v_type == VAR_BLOB)
2267 {
2268 eval_addblob(tv1, tv2);
2269 clear_tv(tv2);
2270 --ectx.ec_stack.ga_len;
2271 break;
2272 }
2273 }
2274#ifdef FEAT_FLOAT
2275 if (tv1->v_type == VAR_FLOAT)
2276 {
2277 f1 = tv1->vval.v_float;
2278 n1 = 0;
2279 }
2280 else
2281#endif
2282 {
2283 n1 = tv_get_number_chk(tv1, &error);
2284 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002285 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286#ifdef FEAT_FLOAT
2287 if (tv2->v_type == VAR_FLOAT)
2288 f1 = n1;
2289#endif
2290 }
2291#ifdef FEAT_FLOAT
2292 if (tv2->v_type == VAR_FLOAT)
2293 {
2294 f2 = tv2->vval.v_float;
2295 n2 = 0;
2296 }
2297 else
2298#endif
2299 {
2300 n2 = tv_get_number_chk(tv2, &error);
2301 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002302 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303#ifdef FEAT_FLOAT
2304 if (tv1->v_type == VAR_FLOAT)
2305 f2 = n2;
2306#endif
2307 }
2308#ifdef FEAT_FLOAT
2309 // if there is a float on either side the result is a float
2310 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2311 {
2312 switch (iptr->isn_arg.op.op_type)
2313 {
2314 case EXPR_MULT: f1 = f1 * f2; break;
2315 case EXPR_DIV: f1 = f1 / f2; break;
2316 case EXPR_SUB: f1 = f1 - f2; break;
2317 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002318 default: SOURCING_LNUM = iptr->isn_lnum;
2319 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
2322 clear_tv(tv1);
2323 clear_tv(tv2);
2324 tv1->v_type = VAR_FLOAT;
2325 tv1->vval.v_float = f1;
2326 --ectx.ec_stack.ga_len;
2327 }
2328 else
2329#endif
2330 {
2331 switch (iptr->isn_arg.op.op_type)
2332 {
2333 case EXPR_MULT: n1 = n1 * n2; break;
2334 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2335 case EXPR_SUB: n1 = n1 - n2; break;
2336 case EXPR_ADD: n1 = n1 + n2; break;
2337 default: n1 = num_modulus(n1, n2); break;
2338 }
2339 clear_tv(tv1);
2340 clear_tv(tv2);
2341 tv1->v_type = VAR_NUMBER;
2342 tv1->vval.v_number = n1;
2343 --ectx.ec_stack.ga_len;
2344 }
2345 }
2346 break;
2347
2348 case ISN_CONCAT:
2349 {
2350 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2351 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2352 char_u *res;
2353
2354 res = concat_str(str1, str2);
2355 clear_tv(STACK_TV_BOT(-2));
2356 clear_tv(STACK_TV_BOT(-1));
2357 --ectx.ec_stack.ga_len;
2358 STACK_TV_BOT(-1)->vval.v_string = res;
2359 }
2360 break;
2361
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002362 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002363 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002364 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002365 int is_slice = iptr->isn_type == ISN_STRSLICE;
2366 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002367 char_u *res;
2368
2369 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002370 // string slice: string is at stack-3, first index at
2371 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002372 if (is_slice)
2373 {
2374 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002375 n1 = tv->vval.v_number;
2376 }
2377
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002378 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002379 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002380
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002381 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002382 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002383 if (is_slice)
2384 // Slice: Select the characters from the string
2385 res = string_slice(tv->vval.v_string, n1, n2);
2386 else
2387 // Index: The resulting variable is a string of a
2388 // single character. If the index is too big or
2389 // negative the result is empty.
2390 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002391 vim_free(tv->vval.v_string);
2392 tv->vval.v_string = res;
2393 }
2394 break;
2395
2396 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002397 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 {
Bram Moolenaared591872020-08-15 22:14:53 +02002399 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002401 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402
2403 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002404 // list slice: list is at stack-3, indexes at stack-2 and
2405 // stack-1
2406 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 list = tv->vval.v_list;
2408
2409 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002410 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002412
2413 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 {
Bram Moolenaared591872020-08-15 22:14:53 +02002415 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002416 n1 = tv->vval.v_number;
2417 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 }
Bram Moolenaared591872020-08-15 22:14:53 +02002419
2420 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002421 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002423 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2424 == FAIL)
2425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 }
2427 break;
2428
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002429 case ISN_ANYINDEX:
2430 case ISN_ANYSLICE:
2431 {
2432 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2433 typval_T *var1, *var2;
2434 int res;
2435
2436 // index: composite is at stack-2, index at stack-1
2437 // slice: composite is at stack-3, indexes at stack-2 and
2438 // stack-1
2439 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002440 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002441 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2442 goto on_error;
2443 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2444 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2445 res = eval_index_inner(tv, is_slice,
2446 var1, var2, NULL, -1, TRUE);
2447 clear_tv(var1);
2448 if (is_slice)
2449 clear_tv(var2);
2450 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2451 if (res == FAIL)
2452 goto on_error;
2453 }
2454 break;
2455
Bram Moolenaar9af78762020-06-16 11:34:42 +02002456 case ISN_SLICE:
2457 {
2458 list_T *list;
2459 int count = iptr->isn_arg.number;
2460
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002461 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002462 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002463 list = tv->vval.v_list;
2464
2465 // no error for short list, expect it to be checked earlier
2466 if (list != NULL && list->lv_len >= count)
2467 {
2468 list_T *newlist = list_slice(list,
2469 count, list->lv_len - 1);
2470
2471 if (newlist != NULL)
2472 {
2473 list_unref(list);
2474 tv->vval.v_list = newlist;
2475 ++newlist->lv_refcount;
2476 }
2477 }
2478 }
2479 break;
2480
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002481 case ISN_GETITEM:
2482 {
2483 listitem_T *li;
2484 int index = iptr->isn_arg.number;
2485
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002486 // Get list item: list is at stack-1, push item.
2487 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002488 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002489 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002490
2491 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2492 goto failed;
2493 ++ectx.ec_stack.ga_len;
2494 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2495 }
2496 break;
2497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 case ISN_MEMBER:
2499 {
2500 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002501 char_u *key;
2502 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002503 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002504
2505 // dict member: dict is at stack-2, key at stack-1
2506 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002507 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002508 dict = tv->vval.v_dict;
2509
2510 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002511 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002512 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002513
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002514 if ((di = dict_find(dict, key, -1)) == NULL)
2515 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002516 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002517 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002518 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002519 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002520 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002521 --ectx.ec_stack.ga_len;
2522 // Clear the dict after getting the item, to avoid that it
2523 // make the item invalid.
2524 tv = STACK_TV_BOT(-1);
2525 temp_tv = *tv;
2526 copy_tv(&di->di_tv, tv);
2527 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002528 }
2529 break;
2530
2531 // dict member with string key
2532 case ISN_STRINGMEMBER:
2533 {
2534 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002536 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537
2538 tv = STACK_TV_BOT(-1);
2539 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2540 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002541 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002544 }
2545 dict = tv->vval.v_dict;
2546
2547 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2548 == NULL)
2549 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002552 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002554 // Clear the dict after getting the item, to avoid that it
2555 // make the item invalid.
2556 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002558 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 }
2560 break;
2561
2562 case ISN_NEGATENR:
2563 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002564 if (tv->v_type != VAR_NUMBER
2565#ifdef FEAT_FLOAT
2566 && tv->v_type != VAR_FLOAT
2567#endif
2568 )
2569 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002570 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002571 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002572 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002573 }
2574#ifdef FEAT_FLOAT
2575 if (tv->v_type == VAR_FLOAT)
2576 tv->vval.v_float = -tv->vval.v_float;
2577 else
2578#endif
2579 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 break;
2581
2582 case ISN_CHECKNR:
2583 {
2584 int error = FALSE;
2585
2586 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002587 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002589 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 (void)tv_get_number_chk(tv, &error);
2591 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002592 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 break;
2595
2596 case ISN_CHECKTYPE:
2597 {
2598 checktype_T *ct = &iptr->isn_arg.type;
2599
2600 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002601 SOURCING_LNUM = iptr->isn_lnum;
2602 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2603 goto on_error;
2604
2605 // number 0 is FALSE, number 1 is TRUE
2606 if (tv->v_type == VAR_NUMBER
2607 && ct->ct_type->tt_type == VAR_BOOL
2608 && (tv->vval.v_number == 0
2609 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002611 tv->v_type = VAR_BOOL;
2612 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002613 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 }
2615 }
2616 break;
2617
Bram Moolenaar9af78762020-06-16 11:34:42 +02002618 case ISN_CHECKLEN:
2619 {
2620 int min_len = iptr->isn_arg.checklen.cl_min_len;
2621 list_T *list = NULL;
2622
2623 tv = STACK_TV_BOT(-1);
2624 if (tv->v_type == VAR_LIST)
2625 list = tv->vval.v_list;
2626 if (list == NULL || list->lv_len < min_len
2627 || (list->lv_len > min_len
2628 && !iptr->isn_arg.checklen.cl_more_OK))
2629 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002630 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002631 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002632 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002633 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002634 }
2635 }
2636 break;
2637
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002639 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 {
2641 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002642 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643
2644 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002645 if (iptr->isn_type == ISN_2BOOL)
2646 {
2647 n = tv2bool(tv);
2648 if (iptr->isn_arg.number) // invert
2649 n = !n;
2650 }
2651 else
2652 {
2653 SOURCING_LNUM = iptr->isn_lnum;
2654 n = tv_get_bool_chk(tv, &error);
2655 if (error)
2656 goto on_error;
2657 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 clear_tv(tv);
2659 tv->v_type = VAR_BOOL;
2660 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2661 }
2662 break;
2663
2664 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002665 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 {
2667 char_u *str;
2668
2669 tv = STACK_TV_BOT(iptr->isn_arg.number);
2670 if (tv->v_type != VAR_STRING)
2671 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002672 if (iptr->isn_type == ISN_2STRING_ANY)
2673 {
2674 switch (tv->v_type)
2675 {
2676 case VAR_SPECIAL:
2677 case VAR_BOOL:
2678 case VAR_NUMBER:
2679 case VAR_FLOAT:
2680 case VAR_BLOB: break;
2681 default: to_string_error(tv->v_type);
2682 goto on_error;
2683 }
2684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002685 str = typval_tostring(tv);
2686 clear_tv(tv);
2687 tv->v_type = VAR_STRING;
2688 tv->vval.v_string = str;
2689 }
2690 }
2691 break;
2692
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002693 case ISN_PUT:
2694 {
2695 int regname = iptr->isn_arg.put.put_regname;
2696 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2697 char_u *expr = NULL;
2698 int dir = FORWARD;
2699
2700 if (regname == '=')
2701 {
2702 tv = STACK_TV_BOT(-1);
2703 if (tv->v_type == VAR_STRING)
2704 expr = tv->vval.v_string;
2705 else
2706 {
2707 expr = typval_tostring(tv); // allocates value
2708 clear_tv(tv);
2709 }
2710 --ectx.ec_stack.ga_len;
2711 }
2712 if (lnum == -2)
2713 // :put! above cursor
2714 dir = BACKWARD;
2715 else if (lnum >= 0)
2716 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2717 check_cursor();
2718 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2719 vim_free(expr);
2720 }
2721 break;
2722
Bram Moolenaar389df252020-07-09 21:20:47 +02002723 case ISN_SHUFFLE:
2724 {
2725 typval_T tmp_tv;
2726 int item = iptr->isn_arg.shuffle.shfl_item;
2727 int up = iptr->isn_arg.shuffle.shfl_up;
2728
2729 tmp_tv = *STACK_TV_BOT(-item);
2730 for ( ; up > 0 && item > 1; --up)
2731 {
2732 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2733 --item;
2734 }
2735 *STACK_TV_BOT(-item) = tmp_tv;
2736 }
2737 break;
2738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 case ISN_DROP:
2740 --ectx.ec_stack.ga_len;
2741 clear_tv(STACK_TV_BOT(0));
2742 break;
2743 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002744 continue;
2745
Bram Moolenaard032f342020-07-18 18:13:02 +02002746func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002747 // Restore previous function. If the frame pointer is where we started
2748 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002749 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002750 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002751
Bram Moolenaard032f342020-07-18 18:13:02 +02002752 if (func_return(&ectx) == FAIL)
2753 // only fails when out of memory
2754 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002755 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002756
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002757on_error:
2758 if (trylevel == 0)
2759 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 }
2761
2762done:
2763 // function finished, get result from the stack.
2764 tv = STACK_TV_BOT(-1);
2765 *rettv = *tv;
2766 tv->v_type = VAR_UNKNOWN;
2767 ret = OK;
2768
2769failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002770 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002771 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002772 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002773
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02002774 // Deal with any remaining closures, they may be in use somewhere.
2775 if (ectx.ec_funcrefs.ga_len > 0)
2776 handle_closure_in_use(&ectx, FALSE);
2777
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002778 estack_pop();
2779 current_sctx = save_current_sctx;
2780
2781failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002782 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2784 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002785
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002786 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002787 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002788
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002789 // Not sure if this is necessary.
2790 suppress_errthrow = save_suppress_errthrow;
2791
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002792 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002793 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002794 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 return ret;
2796}
2797
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002798/*
2799 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002800 * We don't really need this at runtime, but we do have tests that require it,
2801 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 */
2803 void
2804ex_disassemble(exarg_T *eap)
2805{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002806 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002807 char_u *fname;
2808 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 dfunc_T *dfunc;
2810 isn_T *instr;
2811 int current;
2812 int line_idx = 0;
2813 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002814 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002816 if (STRNCMP(arg, "<lambda>", 8) == 0)
2817 {
2818 arg += 8;
2819 (void)getdigits(&arg);
2820 fname = vim_strnsave(eap->arg, arg - eap->arg);
2821 }
2822 else
2823 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002824 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002825 if (fname == NULL)
2826 {
2827 semsg(_(e_invarg2), eap->arg);
2828 return;
2829 }
2830
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002831 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002832 if (ufunc == NULL)
2833 {
2834 char_u *p = untrans_function_name(fname);
2835
2836 if (p != NULL)
2837 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002838 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002839 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002840 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 if (ufunc == NULL)
2842 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002843 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 return;
2845 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002846 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002847 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2848 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002849 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002851 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 return;
2853 }
2854 if (ufunc->uf_name_exp != NULL)
2855 msg((char *)ufunc->uf_name_exp);
2856 else
2857 msg((char *)ufunc->uf_name);
2858
2859 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2860 instr = dfunc->df_instr;
2861 for (current = 0; current < dfunc->df_instr_count; ++current)
2862 {
2863 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002864 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865
2866 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2867 {
2868 if (current > prev_current)
2869 {
2870 msg_puts("\n\n");
2871 prev_current = current;
2872 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002873 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2874 if (line != NULL)
2875 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002876 }
2877
2878 switch (iptr->isn_type)
2879 {
2880 case ISN_EXEC:
2881 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2882 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002883 case ISN_EXECCONCAT:
2884 smsg("%4d EXECCONCAT %lld", current,
2885 (long long)iptr->isn_arg.number);
2886 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 case ISN_ECHO:
2888 {
2889 echo_T *echo = &iptr->isn_arg.echo;
2890
2891 smsg("%4d %s %d", current,
2892 echo->echo_with_white ? "ECHO" : "ECHON",
2893 echo->echo_count);
2894 }
2895 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002896 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002897 smsg("%4d EXECUTE %lld", current,
2898 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002899 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002900 case ISN_ECHOMSG:
2901 smsg("%4d ECHOMSG %lld", current,
2902 (long long)(iptr->isn_arg.number));
2903 break;
2904 case ISN_ECHOERR:
2905 smsg("%4d ECHOERR %lld", current,
2906 (long long)(iptr->isn_arg.number));
2907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002909 case ISN_LOADOUTER:
2910 {
2911 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2912
2913 if (iptr->isn_arg.number < 0)
2914 smsg("%4d LOAD%s arg[%lld]", current, add,
2915 (long long)(iptr->isn_arg.number
2916 + STACK_FRAME_SIZE));
2917 else
2918 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002919 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002920 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 break;
2922 case ISN_LOADV:
2923 smsg("%4d LOADV v:%s", current,
2924 get_vim_var_name(iptr->isn_arg.number));
2925 break;
2926 case ISN_LOADSCRIPT:
2927 {
2928 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002929 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002930 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2931 + iptr->isn_arg.script.script_idx;
2932
2933 smsg("%4d LOADSCRIPT %s from %s", current,
2934 sv->sv_name, si->sn_name);
2935 }
2936 break;
2937 case ISN_LOADS:
2938 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002939 scriptitem_T *si = SCRIPT_ITEM(
2940 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941
2942 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002943 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 }
2945 break;
2946 case ISN_LOADG:
2947 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2948 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002949 case ISN_LOADB:
2950 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2951 break;
2952 case ISN_LOADW:
2953 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2954 break;
2955 case ISN_LOADT:
2956 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2957 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002958 case ISN_LOADGDICT:
2959 smsg("%4d LOAD g:", current);
2960 break;
2961 case ISN_LOADBDICT:
2962 smsg("%4d LOAD b:", current);
2963 break;
2964 case ISN_LOADWDICT:
2965 smsg("%4d LOAD w:", current);
2966 break;
2967 case ISN_LOADTDICT:
2968 smsg("%4d LOAD t:", current);
2969 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 case ISN_LOADOPT:
2971 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2972 break;
2973 case ISN_LOADENV:
2974 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2975 break;
2976 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002977 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978 break;
2979
2980 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002981 case ISN_STOREOUTER:
2982 {
2983 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2984
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002985 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002986 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002987 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002988 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002989 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002990 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002991 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002992 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002993 case ISN_STOREV:
2994 smsg("%4d STOREV v:%s", current,
2995 get_vim_var_name(iptr->isn_arg.number));
2996 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002997 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002998 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2999 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003000 case ISN_STOREB:
3001 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3002 break;
3003 case ISN_STOREW:
3004 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3005 break;
3006 case ISN_STORET:
3007 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3008 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003009 case ISN_STORES:
3010 {
3011 scriptitem_T *si = SCRIPT_ITEM(
3012 iptr->isn_arg.loadstore.ls_sid);
3013
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003014 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003015 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003017 break;
3018 case ISN_STORESCRIPT:
3019 {
3020 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003021 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3023 + iptr->isn_arg.script.script_idx;
3024
3025 smsg("%4d STORESCRIPT %s in %s", current,
3026 sv->sv_name, si->sn_name);
3027 }
3028 break;
3029 case ISN_STOREOPT:
3030 smsg("%4d STOREOPT &%s", current,
3031 iptr->isn_arg.storeopt.so_name);
3032 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003033 case ISN_STOREENV:
3034 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3035 break;
3036 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003037 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003038 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 case ISN_STORENR:
3040 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003041 iptr->isn_arg.storenr.stnr_val,
3042 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 break;
3044
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003045 case ISN_STORELIST:
3046 smsg("%4d STORELIST", current);
3047 break;
3048
3049 case ISN_STOREDICT:
3050 smsg("%4d STOREDICT", current);
3051 break;
3052
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 // constants
3054 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003055 smsg("%4d PUSHNR %lld", current,
3056 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003057 break;
3058 case ISN_PUSHBOOL:
3059 case ISN_PUSHSPEC:
3060 smsg("%4d PUSH %s", current,
3061 get_var_special_name(iptr->isn_arg.number));
3062 break;
3063 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003064#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003065 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003066#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003067 break;
3068 case ISN_PUSHS:
3069 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3070 break;
3071 case ISN_PUSHBLOB:
3072 {
3073 char_u *r;
3074 char_u numbuf[NUMBUFLEN];
3075 char_u *tofree;
3076
3077 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003078 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 vim_free(tofree);
3080 }
3081 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003082 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003083 {
3084 char *name = (char *)iptr->isn_arg.string;
3085
3086 smsg("%4d PUSHFUNC \"%s\"", current,
3087 name == NULL ? "[none]" : name);
3088 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003089 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003090 case ISN_PUSHCHANNEL:
3091#ifdef FEAT_JOB_CHANNEL
3092 {
3093 channel_T *channel = iptr->isn_arg.channel;
3094
3095 smsg("%4d PUSHCHANNEL %d", current,
3096 channel == NULL ? 0 : channel->ch_id);
3097 }
3098#endif
3099 break;
3100 case ISN_PUSHJOB:
3101#ifdef FEAT_JOB_CHANNEL
3102 {
3103 typval_T tv;
3104 char_u *name;
3105
3106 tv.v_type = VAR_JOB;
3107 tv.vval.v_job = iptr->isn_arg.job;
3108 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003109 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003110 }
3111#endif
3112 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003113 case ISN_PUSHEXC:
3114 smsg("%4d PUSH v:exception", current);
3115 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003116 case ISN_UNLET:
3117 smsg("%4d UNLET%s %s", current,
3118 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3119 iptr->isn_arg.unlet.ul_name);
3120 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003121 case ISN_UNLETENV:
3122 smsg("%4d UNLETENV%s $%s", current,
3123 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3124 iptr->isn_arg.unlet.ul_name);
3125 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003126 case ISN_LOCKCONST:
3127 smsg("%4d LOCKCONST", current);
3128 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003129 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003130 smsg("%4d NEWLIST size %lld", current,
3131 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003132 break;
3133 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003134 smsg("%4d NEWDICT size %lld", current,
3135 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003136 break;
3137
3138 // function call
3139 case ISN_BCALL:
3140 {
3141 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3142
3143 smsg("%4d BCALL %s(argc %d)", current,
3144 internal_func_name(cbfunc->cbf_idx),
3145 cbfunc->cbf_argcount);
3146 }
3147 break;
3148 case ISN_DCALL:
3149 {
3150 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3151 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3152 + cdfunc->cdf_idx;
3153
3154 smsg("%4d DCALL %s(argc %d)", current,
3155 df->df_ufunc->uf_name_exp != NULL
3156 ? df->df_ufunc->uf_name_exp
3157 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3158 }
3159 break;
3160 case ISN_UCALL:
3161 {
3162 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3163
3164 smsg("%4d UCALL %s(argc %d)", current,
3165 cufunc->cuf_name, cufunc->cuf_argcount);
3166 }
3167 break;
3168 case ISN_PCALL:
3169 {
3170 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3171
3172 smsg("%4d PCALL%s (argc %d)", current,
3173 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3174 }
3175 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003176 case ISN_PCALL_END:
3177 smsg("%4d PCALL end", current);
3178 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 case ISN_RETURN:
3180 smsg("%4d RETURN", current);
3181 break;
3182 case ISN_FUNCREF:
3183 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003184 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003186 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003188 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003189 }
3190 break;
3191
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003192 case ISN_NEWFUNC:
3193 {
3194 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3195
3196 smsg("%4d NEWFUNC %s %s", current,
3197 newfunc->nf_lambda, newfunc->nf_global);
3198 }
3199 break;
3200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003201 case ISN_JUMP:
3202 {
3203 char *when = "?";
3204
3205 switch (iptr->isn_arg.jump.jump_when)
3206 {
3207 case JUMP_ALWAYS:
3208 when = "JUMP";
3209 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 case JUMP_AND_KEEP_IF_TRUE:
3211 when = "JUMP_AND_KEEP_IF_TRUE";
3212 break;
3213 case JUMP_IF_FALSE:
3214 when = "JUMP_IF_FALSE";
3215 break;
3216 case JUMP_AND_KEEP_IF_FALSE:
3217 when = "JUMP_AND_KEEP_IF_FALSE";
3218 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003219 case JUMP_IF_COND_FALSE:
3220 when = "JUMP_IF_COND_FALSE";
3221 break;
3222 case JUMP_IF_COND_TRUE:
3223 when = "JUMP_IF_COND_TRUE";
3224 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003226 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 iptr->isn_arg.jump.jump_where);
3228 }
3229 break;
3230
3231 case ISN_FOR:
3232 {
3233 forloop_T *forloop = &iptr->isn_arg.forloop;
3234
3235 smsg("%4d FOR $%d -> %d", current,
3236 forloop->for_idx, forloop->for_end);
3237 }
3238 break;
3239
3240 case ISN_TRY:
3241 {
3242 try_T *try = &iptr->isn_arg.try;
3243
3244 smsg("%4d TRY catch -> %d, finally -> %d", current,
3245 try->try_catch, try->try_finally);
3246 }
3247 break;
3248 case ISN_CATCH:
3249 // TODO
3250 smsg("%4d CATCH", current);
3251 break;
3252 case ISN_ENDTRY:
3253 smsg("%4d ENDTRY", current);
3254 break;
3255 case ISN_THROW:
3256 smsg("%4d THROW", current);
3257 break;
3258
3259 // expression operations on number
3260 case ISN_OPNR:
3261 case ISN_OPFLOAT:
3262 case ISN_OPANY:
3263 {
3264 char *what;
3265 char *ins;
3266
3267 switch (iptr->isn_arg.op.op_type)
3268 {
3269 case EXPR_MULT: what = "*"; break;
3270 case EXPR_DIV: what = "/"; break;
3271 case EXPR_REM: what = "%"; break;
3272 case EXPR_SUB: what = "-"; break;
3273 case EXPR_ADD: what = "+"; break;
3274 default: what = "???"; break;
3275 }
3276 switch (iptr->isn_type)
3277 {
3278 case ISN_OPNR: ins = "OPNR"; break;
3279 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3280 case ISN_OPANY: ins = "OPANY"; break;
3281 default: ins = "???"; break;
3282 }
3283 smsg("%4d %s %s", current, ins, what);
3284 }
3285 break;
3286
3287 case ISN_COMPAREBOOL:
3288 case ISN_COMPARESPECIAL:
3289 case ISN_COMPARENR:
3290 case ISN_COMPAREFLOAT:
3291 case ISN_COMPARESTRING:
3292 case ISN_COMPAREBLOB:
3293 case ISN_COMPARELIST:
3294 case ISN_COMPAREDICT:
3295 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 case ISN_COMPAREANY:
3297 {
3298 char *p;
3299 char buf[10];
3300 char *type;
3301
3302 switch (iptr->isn_arg.op.op_type)
3303 {
3304 case EXPR_EQUAL: p = "=="; break;
3305 case EXPR_NEQUAL: p = "!="; break;
3306 case EXPR_GREATER: p = ">"; break;
3307 case EXPR_GEQUAL: p = ">="; break;
3308 case EXPR_SMALLER: p = "<"; break;
3309 case EXPR_SEQUAL: p = "<="; break;
3310 case EXPR_MATCH: p = "=~"; break;
3311 case EXPR_IS: p = "is"; break;
3312 case EXPR_ISNOT: p = "isnot"; break;
3313 case EXPR_NOMATCH: p = "!~"; break;
3314 default: p = "???"; break;
3315 }
3316 STRCPY(buf, p);
3317 if (iptr->isn_arg.op.op_ic == TRUE)
3318 strcat(buf, "?");
3319 switch(iptr->isn_type)
3320 {
3321 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3322 case ISN_COMPARESPECIAL:
3323 type = "COMPARESPECIAL"; break;
3324 case ISN_COMPARENR: type = "COMPARENR"; break;
3325 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3326 case ISN_COMPARESTRING:
3327 type = "COMPARESTRING"; break;
3328 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3329 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3330 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3331 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3333 default: type = "???"; break;
3334 }
3335
3336 smsg("%4d %s %s", current, type, buf);
3337 }
3338 break;
3339
3340 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3341 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3342
3343 // expression operations
3344 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003345 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003346 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003347 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003348 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003349 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3350 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003351 case ISN_SLICE: smsg("%4d SLICE %lld",
3352 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003353 case ISN_GETITEM: smsg("%4d ITEM %lld",
3354 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003355 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3356 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 iptr->isn_arg.string); break;
3358 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3359
3360 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003361 case ISN_CHECKTYPE:
3362 {
3363 char *tofree;
3364
3365 smsg("%4d CHECKTYPE %s stack[%d]", current,
3366 type_name(iptr->isn_arg.type.ct_type, &tofree),
3367 iptr->isn_arg.type.ct_off);
3368 vim_free(tofree);
3369 break;
3370 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003371 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3372 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3373 iptr->isn_arg.checklen.cl_min_len);
3374 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003375 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003376 case ISN_2BOOL: if (iptr->isn_arg.number)
3377 smsg("%4d INVERT (!val)", current);
3378 else
3379 smsg("%4d 2BOOL (!!val)", current);
3380 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003381 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3382 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003383 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003384 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3385 (long long)(iptr->isn_arg.number));
3386 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003387 case ISN_PUT:
3388 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3389 (long)iptr->isn_arg.put.put_lnum);
3390 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391
Bram Moolenaar389df252020-07-09 21:20:47 +02003392 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3393 iptr->isn_arg.shuffle.shfl_item,
3394 iptr->isn_arg.shuffle.shfl_up);
3395 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 case ISN_DROP: smsg("%4d DROP", current); break;
3397 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003398
3399 out_flush(); // output one line at a time
3400 ui_breakcheck();
3401 if (got_int)
3402 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404}
3405
3406/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003407 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 * list, etc. Mostly like what JavaScript does, except that empty list and
3409 * empty dictionary are FALSE.
3410 */
3411 int
3412tv2bool(typval_T *tv)
3413{
3414 switch (tv->v_type)
3415 {
3416 case VAR_NUMBER:
3417 return tv->vval.v_number != 0;
3418 case VAR_FLOAT:
3419#ifdef FEAT_FLOAT
3420 return tv->vval.v_float != 0.0;
3421#else
3422 break;
3423#endif
3424 case VAR_PARTIAL:
3425 return tv->vval.v_partial != NULL;
3426 case VAR_FUNC:
3427 case VAR_STRING:
3428 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3429 case VAR_LIST:
3430 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3431 case VAR_DICT:
3432 return tv->vval.v_dict != NULL
3433 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3434 case VAR_BOOL:
3435 case VAR_SPECIAL:
3436 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3437 case VAR_JOB:
3438#ifdef FEAT_JOB_CHANNEL
3439 return tv->vval.v_job != NULL;
3440#else
3441 break;
3442#endif
3443 case VAR_CHANNEL:
3444#ifdef FEAT_JOB_CHANNEL
3445 return tv->vval.v_channel != NULL;
3446#else
3447 break;
3448#endif
3449 case VAR_BLOB:
3450 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3451 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003452 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 case VAR_VOID:
3454 break;
3455 }
3456 return FALSE;
3457}
3458
3459/*
3460 * If "tv" is a string give an error and return FAIL.
3461 */
3462 int
3463check_not_string(typval_T *tv)
3464{
3465 if (tv->v_type == VAR_STRING)
3466 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003467 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 clear_tv(tv);
3469 return FAIL;
3470 }
3471 return OK;
3472}
3473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474
3475#endif // FEAT_EVAL