blob: 955c3aa4de948fe3b5fae4dfc2790b1516eea0ae [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 Moolenaarbf67ea12020-05-02 17:52:42 +0200242 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
243 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
245 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200246 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200248 if (dfunc->df_has_closure)
249 {
250 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
251
252 tv->v_type = VAR_NUMBER;
253 tv->vval.v_number = 0;
254 }
255 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256
257 // Set execution state to the start of the called function.
258 ectx->ec_dfunc_idx = cdf_idx;
259 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200260 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
261 if (entry != NULL)
262 {
263 // Set the script context to the script where the function was defined.
264 // TODO: save more than the SID?
265 entry->es_save_sid = current_sctx.sc_sid;
266 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
267 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100268
269 // Decide where to start execution, handles optional arguments.
270 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271
272 return OK;
273}
274
275// Get pointer to item in the stack.
276#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
277
278/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200279 * Used when returning from a function: Check if any closure is still
280 * referenced. If so then move the arguments and variables to a separate piece
281 * of stack to be used when the closure is called.
282 * When "free_arguments" is TRUE the arguments are to be freed.
283 * Returns FAIL when out of memory.
284 */
285 static int
286handle_closure_in_use(ectx_T *ectx, int free_arguments)
287{
288 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
289 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200290 int argcount;
291 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200292 int idx;
293 typval_T *tv;
294 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200295 garray_T *gap = &ectx->ec_funcrefs;
296 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200298 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200299 return OK; // function was freed
300 if (dfunc->df_has_closure == 0)
301 return OK; // no closures
302 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
303 closure_count = tv->vval.v_number;
304 if (closure_count == 0)
305 return OK; // no funcrefs created
306
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200307 argcount = ufunc_argcount(dfunc->df_ufunc);
308 top = ectx->ec_frame_idx - argcount;
309
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200310 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200311 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200312 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
314 - closure_count + idx];
315
316 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200317 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200318 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200319 int i;
320
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200321 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200322 // unreferenced on return.
323 for (i = 0; i < dfunc->df_varcount; ++i)
324 {
325 typval_T *stv = STACK_TV(ectx->ec_frame_idx
326 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200328 --refcount;
329 }
330 if (refcount > 1)
331 {
332 closure_in_use = TRUE;
333 break;
334 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200335 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200336 }
337
338 if (closure_in_use)
339 {
340 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
341 typval_T *stack;
342
343 // A closure is using the arguments and/or local variables.
344 // Move them to the called function.
345 if (funcstack == NULL)
346 return FAIL;
347 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
348 + dfunc->df_varcount;
349 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
350 funcstack->fs_ga.ga_data = stack;
351 if (stack == NULL)
352 {
353 vim_free(funcstack);
354 return FAIL;
355 }
356
357 // Move or copy the arguments.
358 for (idx = 0; idx < argcount; ++idx)
359 {
360 tv = STACK_TV(top + idx);
361 if (free_arguments)
362 {
363 *(stack + idx) = *tv;
364 tv->v_type = VAR_UNKNOWN;
365 }
366 else
367 copy_tv(tv, stack + idx);
368 }
369 // Move the local variables.
370 for (idx = 0; idx < dfunc->df_varcount; ++idx)
371 {
372 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200373
374 // Do not copy a partial created for a local function.
375 // TODO: this won't work if the closure actually uses it. But when
376 // keeping it it gets complicated: it will create a reference cycle
377 // inside the partial, thus needs special handling for garbage
378 // collection.
379 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
380 {
381 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200382
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200383 for (i = 0; i < closure_count; ++i)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200384 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200385 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
386 - closure_count + i];
387 if (tv->vval.v_partial == pt)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388 break;
389 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 if (i < closure_count)
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391 continue;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200392 }
393
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200394 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
395 tv->v_type = VAR_UNKNOWN;
396 }
397
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200398 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200400 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
401 - closure_count + idx];
402 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 ++funcstack->fs_refcount;
405 pt->pt_funcstack = funcstack;
406 pt->pt_ectx_stack = &funcstack->fs_ga;
407 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409 }
410 }
411
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 for (idx = 0; idx < closure_count; ++idx)
413 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
414 - closure_count + idx]);
415 gap->ga_len -= closure_count;
416 if (gap->ga_len == 0)
417 ga_clear(gap);
418
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200419 return OK;
420}
421
422/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 * Return from the current function.
424 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200425 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426func_return(ectx_T *ectx)
427{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100428 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200429 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
430 + ectx->ec_dfunc_idx;
431 int argcount = ufunc_argcount(dfunc->df_ufunc);
432 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200433 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434
435 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200436 entry = estack_pop();
437 if (entry != NULL)
438 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200440 if (handle_closure_in_use(ectx, TRUE) == FAIL)
441 return FAIL;
442
443 // Clear the arguments.
444 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
445 clear_tv(STACK_TV(idx));
446
447 // Clear local variables and temp values, but not the return value.
448 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100449 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100450 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100451
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100452 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200453 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
454 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
455 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
457 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100458
459 // Reset the stack to the position before the call, move the return value
460 // to the top of the stack.
461 idx = ectx->ec_stack.ga_len - 1;
462 ectx->ec_stack.ga_len = top + 1;
463 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200464
465 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100466}
467
468#undef STACK_TV
469
470/*
471 * Prepare arguments and rettv for calling a builtin or user function.
472 */
473 static int
474call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
475{
476 int idx;
477 typval_T *tv;
478
479 // Move arguments from bottom of the stack to argvars[] and add terminator.
480 for (idx = 0; idx < argcount; ++idx)
481 argvars[idx] = *STACK_TV_BOT(idx - argcount);
482 argvars[argcount].v_type = VAR_UNKNOWN;
483
484 // Result replaces the arguments on the stack.
485 if (argcount > 0)
486 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200487 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100488 return FAIL;
489 else
490 ++ectx->ec_stack.ga_len;
491
492 // Default return value is zero.
493 tv = STACK_TV_BOT(-1);
494 tv->v_type = VAR_NUMBER;
495 tv->vval.v_number = 0;
496
497 return OK;
498}
499
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200500// Ugly global to avoid passing the execution context around through many
501// layers.
502static ectx_T *current_ectx = NULL;
503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504/*
505 * Call a builtin function by index.
506 */
507 static int
508call_bfunc(int func_idx, int argcount, ectx_T *ectx)
509{
510 typval_T argvars[MAX_FUNC_ARGS];
511 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200512 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200513 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100514
515 if (call_prepare(argcount, argvars, ectx) == FAIL)
516 return FAIL;
517
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200518 // Call the builtin function. Set "current_ectx" so that when it
519 // recursively invokes call_def_function() a closure context can be set.
520 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200522 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
524 // Clear the arguments.
525 for (idx = 0; idx < argcount; ++idx)
526 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200527
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200528 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200529 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 return OK;
531}
532
533/*
534 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100535 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 */
537 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100538call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539{
540 typval_T argvars[MAX_FUNC_ARGS];
541 funcexe_T funcexe;
542 int error;
543 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200544 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200546 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200547 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
548 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200549 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100550 {
551 // The function has been compiled, can call it quickly. For a function
552 // that was defined later: we can call it directly next time.
553 if (iptr != NULL)
554 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100555 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100556 iptr->isn_type = ISN_DCALL;
557 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
558 iptr->isn_arg.dfunc.cdf_argcount = argcount;
559 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562
563 if (call_prepare(argcount, argvars, ectx) == FAIL)
564 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200565 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 funcexe.evaluate = TRUE;
567
568 // Call the user function. Result goes in last position on the stack.
569 // TODO: add selfdict if there is one
570 error = call_user_func_check(ufunc, argcount, argvars,
571 STACK_TV_BOT(-1), &funcexe, NULL);
572
573 // Clear the arguments.
574 for (idx = 0; idx < argcount; ++idx)
575 clear_tv(&argvars[idx]);
576
577 if (error != FCERR_NONE)
578 {
579 user_func_error(error, ufunc->uf_name);
580 return FAIL;
581 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200582 if (called_emsg > called_emsg_before)
583 // Error other than from calling the function itself.
584 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 return OK;
586}
587
588/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200589 * Return TRUE if an error was given or CTRL-C was pressed.
590 */
591 static int
592vim9_aborting(int prev_called_emsg)
593{
594 return called_emsg > prev_called_emsg || got_int || did_throw;
595}
596
597/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 * Execute a function by "name".
599 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100600 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 * Returns FAIL if not found without an error message.
602 */
603 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100604call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605{
606 ufunc_T *ufunc;
607
608 if (builtin_function(name, -1))
609 {
610 int func_idx = find_internal_func(name);
611
612 if (func_idx < 0)
613 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200614 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 return FAIL;
616 return call_bfunc(func_idx, argcount, ectx);
617 }
618
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200619 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200620
621 if (ufunc == NULL)
622 {
623 int called_emsg_before = called_emsg;
624
625 if (script_autoload(name, TRUE))
626 // loaded a package, search for the function again
627 ufunc = find_func(name, FALSE, NULL);
628 if (vim9_aborting(called_emsg_before))
629 return FAIL; // bail out if loading the script caused an error
630 }
631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100632 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100633 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634
635 return FAIL;
636}
637
638 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200639call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200641 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200642 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200644 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645
646 if (tv->v_type == VAR_PARTIAL)
647 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200648 partial_T *pt = tv->vval.v_partial;
649 int i;
650
651 if (pt->pt_argc > 0)
652 {
653 // Make space for arguments from the partial, shift the "argcount"
654 // arguments up.
655 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
656 return FAIL;
657 for (i = 1; i <= argcount; ++i)
658 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
659 ectx->ec_stack.ga_len += pt->pt_argc;
660 argcount += pt->pt_argc;
661
662 // copy the arguments from the partial onto the stack
663 for (i = 0; i < pt->pt_argc; ++i)
664 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
665 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666
667 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200668 {
669 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
670
671 // closure may need the function context where it was defined
672 ectx->ec_outer_stack = pt->pt_ectx_stack;
673 ectx->ec_outer_frame = pt->pt_ectx_frame;
674
675 return ret;
676 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 name = pt->pt_name;
678 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200679 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100680 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200681 if (name != NULL)
682 {
683 char_u fname_buf[FLEN_FIXED + 1];
684 char_u *tofree = NULL;
685 int error = FCERR_NONE;
686 char_u *fname;
687
688 // May need to translate <SNR>123_ to K_SNR.
689 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
690 if (error != FCERR_NONE)
691 res = FAIL;
692 else
693 res = call_by_name(fname, argcount, ectx, NULL);
694 vim_free(tofree);
695 }
696
697 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 {
699 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200700 semsg(_(e_unknownfunc),
701 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 return FAIL;
703 }
704 return OK;
705}
706
707/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200708 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
709 * TRUE.
710 */
711 static int
712error_if_locked(int lock, char *error)
713{
714 if (lock & (VAR_LOCKED | VAR_FIXED))
715 {
716 emsg(_(error));
717 return TRUE;
718 }
719 return FALSE;
720}
721
722/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100723 * Store "tv" in variable "name".
724 * This is for s: and g: variables.
725 */
726 static void
727store_var(char_u *name, typval_T *tv)
728{
729 funccal_entry_T entry;
730
731 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200732 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100733 restore_funccal();
734}
735
Bram Moolenaard3aac292020-04-19 14:32:17 +0200736
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100737/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 * Execute a function by "name".
739 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100740 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 */
742 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100743call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744{
Bram Moolenaared677f52020-08-12 16:38:10 +0200745 int called_emsg_before = called_emsg;
746 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
Bram Moolenaared677f52020-08-12 16:38:10 +0200748 res = call_by_name(name, argcount, ectx, iptr);
749 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200751 dictitem_T *v;
752
753 v = find_var(name, NULL, FALSE);
754 if (v == NULL)
755 {
756 semsg(_(e_unknownfunc), name);
757 return FAIL;
758 }
759 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
760 {
761 semsg(_(e_unknownfunc), name);
762 return FAIL;
763 }
764 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200766 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767}
768
769/*
770 * Call a "def" function from old Vim script.
771 * Return OK or FAIL.
772 */
773 int
774call_def_function(
775 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200776 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200778 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 typval_T *rettv) // return value
780{
781 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200782 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200783 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 typval_T *tv;
785 int idx;
786 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100787 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200788 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200789 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200790 int called_emsg_before = called_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200791 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792
793// Get pointer to item in the stack.
794#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
795
796// Get pointer to item at the bottom of the stack, -1 is the bottom.
797#undef STACK_TV_BOT
798#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
799
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200800// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801#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 +0100802
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200803// Like STACK_TV_VAR but use the outer scope
804#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
805
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200806 if (ufunc->uf_def_status == UF_NOT_COMPILED
807 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200808 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200809 {
810 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200811 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200812 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200813 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200814 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200815
Bram Moolenaar09689a02020-05-09 22:50:08 +0200816 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200817 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200818 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
819 + ufunc->uf_dfunc_idx;
820 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200821 {
822 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200823 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200824 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200825 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200827 CLEAR_FIELD(ectx);
828 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
829 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
830 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
831 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200833 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
835 // Put arguments on the stack.
836 for (idx = 0; idx < argc; ++idx)
837 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200838 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200839 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
840 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200841 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100842 copy_tv(&argv[idx], STACK_TV_BOT(0));
843 ++ectx.ec_stack.ga_len;
844 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200845
846 // Turn varargs into a list. Empty list if no args.
847 if (ufunc->uf_va_name != NULL)
848 {
849 int vararg_count = argc - ufunc->uf_args.ga_len;
850
851 if (vararg_count < 0)
852 vararg_count = 0;
853 else
854 argc -= vararg_count;
855 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200856 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200857
858 // Check the type of the list items.
859 tv = STACK_TV_BOT(-1);
860 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200861 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200862 && ufunc->uf_va_type->tt_member != &t_any
863 && tv->vval.v_list != NULL)
864 {
865 type_T *expected = ufunc->uf_va_type->tt_member;
866 listitem_T *li = tv->vval.v_list->lv_first;
867
868 for (idx = 0; idx < vararg_count; ++idx)
869 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200870 if (check_typval_type(expected, &li->li_tv,
871 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200872 goto failed_early;
873 li = li->li_next;
874 }
875 }
876
Bram Moolenaar23e03252020-04-12 22:22:31 +0200877 if (defcount > 0)
878 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200879 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200880 --ectx.ec_stack.ga_len;
881 }
882
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100883 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200884 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100885 if (defcount > 0)
886 for (idx = 0; idx < defcount; ++idx)
887 {
888 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
889 ++ectx.ec_stack.ga_len;
890 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200891 if (ufunc->uf_va_name != NULL)
892 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893
894 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200895 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
896 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100897
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200898 if (partial != NULL)
899 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200900 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
901 {
902 // TODO: is this always the right way?
903 ectx.ec_outer_stack = &current_ectx->ec_stack;
904 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
905 }
906 else
907 {
908 ectx.ec_outer_stack = partial->pt_ectx_stack;
909 ectx.ec_outer_frame = partial->pt_ectx_frame;
910 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200911 }
912
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100913 // dummy frame entries
914 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
915 {
916 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
917 ++ectx.ec_stack.ga_len;
918 }
919
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200920 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200921 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200922 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
923 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200925 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200926 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200927 ectx.ec_stack.ga_len += dfunc->df_varcount;
928 if (dfunc->df_has_closure)
929 {
930 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
931 STACK_TV_VAR(idx)->vval.v_number = 0;
932 ++ectx.ec_stack.ga_len;
933 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200934
935 ectx.ec_instr = dfunc->df_instr;
936 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100937
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200938 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +0200939 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200940 estack_push_ufunc(ufunc, 1);
941 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200942 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
943
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200944 // Do turn errors into exceptions.
945 suppress_errthrow = FALSE;
946
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100947 // Decide where to start execution, handles optional arguments.
948 init_instr_idx(ufunc, argc, &ectx);
949
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 for (;;)
951 {
952 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100953
Bram Moolenaar270d0382020-05-15 21:42:53 +0200954 if (++breakcheck_count >= 100)
955 {
956 line_breakcheck();
957 breakcheck_count = 0;
958 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100959 if (got_int)
960 {
961 // Turn CTRL-C into an exception.
962 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100963 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100964 goto failed;
965 did_throw = TRUE;
966 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100967
Bram Moolenaara26b9702020-04-18 19:53:28 +0200968 if (did_emsg && msg_list != NULL && *msg_list != NULL)
969 {
970 // Turn an error message into an exception.
971 did_emsg = FALSE;
972 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
973 goto failed;
974 did_throw = TRUE;
975 *msg_list = NULL;
976 }
977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 if (did_throw && !ectx.ec_in_catch)
979 {
980 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100981 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982
983 // An exception jumps to the first catch, finally, or returns from
984 // the current function.
985 if (trystack->ga_len > 0)
986 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200987 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 {
989 // jump to ":catch" or ":finally"
990 ectx.ec_in_catch = TRUE;
991 ectx.ec_iidx = trycmd->tcd_catch_idx;
992 }
993 else
994 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200995 // Not inside try or need to return from current functions.
996 // Push a dummy return value.
997 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
998 goto failed;
999 tv = STACK_TV_BOT(0);
1000 tv->v_type = VAR_NUMBER;
1001 tv->vval.v_number = 0;
1002 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001003 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001004 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001005 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001006 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001007 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1008 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 goto done;
1010 }
1011
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001012 if (func_return(&ectx) == FAIL)
1013 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 }
1015 continue;
1016 }
1017
1018 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1019 switch (iptr->isn_type)
1020 {
1021 // execute Ex command line
1022 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001023 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 do_cmdline_cmd(iptr->isn_arg.string);
1025 break;
1026
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001027 // execute Ex command from pieces on the stack
1028 case ISN_EXECCONCAT:
1029 {
1030 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001031 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001032 int pass;
1033 int i;
1034 char_u *cmd = NULL;
1035 char_u *str;
1036
1037 for (pass = 1; pass <= 2; ++pass)
1038 {
1039 for (i = 0; i < count; ++i)
1040 {
1041 tv = STACK_TV_BOT(i - count);
1042 str = tv->vval.v_string;
1043 if (str != NULL && *str != NUL)
1044 {
1045 if (pass == 2)
1046 STRCPY(cmd + len, str);
1047 len += STRLEN(str);
1048 }
1049 if (pass == 2)
1050 clear_tv(tv);
1051 }
1052 if (pass == 1)
1053 {
1054 cmd = alloc(len + 1);
1055 if (cmd == NULL)
1056 goto failed;
1057 len = 0;
1058 }
1059 }
1060
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001061 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001062 do_cmdline_cmd(cmd);
1063 vim_free(cmd);
1064 }
1065 break;
1066
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 // execute :echo {string} ...
1068 case ISN_ECHO:
1069 {
1070 int count = iptr->isn_arg.echo.echo_count;
1071 int atstart = TRUE;
1072 int needclr = TRUE;
1073
1074 for (idx = 0; idx < count; ++idx)
1075 {
1076 tv = STACK_TV_BOT(idx - count);
1077 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1078 &atstart, &needclr);
1079 clear_tv(tv);
1080 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001081 if (needclr)
1082 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 ectx.ec_stack.ga_len -= count;
1084 }
1085 break;
1086
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001087 // :execute {string} ...
1088 // :echomsg {string} ...
1089 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001090 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001091 case ISN_ECHOMSG:
1092 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001093 {
1094 int count = iptr->isn_arg.number;
1095 garray_T ga;
1096 char_u buf[NUMBUFLEN];
1097 char_u *p;
1098 int len;
1099 int failed = FALSE;
1100
1101 ga_init2(&ga, 1, 80);
1102 for (idx = 0; idx < count; ++idx)
1103 {
1104 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001105 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001106 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001107 if (tv->v_type == VAR_CHANNEL
1108 || tv->v_type == VAR_JOB)
1109 {
1110 SOURCING_LNUM = iptr->isn_lnum;
1111 emsg(_(e_inval_string));
1112 break;
1113 }
1114 else
1115 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001116 }
1117 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001118 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001119
1120 len = (int)STRLEN(p);
1121 if (ga_grow(&ga, len + 2) == FAIL)
1122 failed = TRUE;
1123 else
1124 {
1125 if (ga.ga_len > 0)
1126 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1127 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1128 ga.ga_len += len;
1129 }
1130 clear_tv(tv);
1131 }
1132 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001133 if (failed)
1134 goto on_error;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001135
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001136 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001137 {
1138 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001139 {
1140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001141 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaar430deb12020-08-23 16:29:11 +02001142 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001143 else
1144 {
1145 msg_sb_eol();
1146 if (iptr->isn_type == ISN_ECHOMSG)
1147 {
1148 msg_attr(ga.ga_data, echo_attr);
1149 out_flush();
1150 }
1151 else
1152 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001153 SOURCING_LNUM = iptr->isn_lnum;
1154 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001155 }
1156 }
1157 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001158 ga_clear(&ga);
1159 }
1160 break;
1161
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 // load local variable or argument
1163 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001164 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001165 goto failed;
1166 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1167 ++ectx.ec_stack.ga_len;
1168 break;
1169
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001170 // load variable or argument from outer scope
1171 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001172 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001173 goto failed;
1174 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1175 STACK_TV_BOT(0));
1176 ++ectx.ec_stack.ga_len;
1177 break;
1178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 // load v: variable
1180 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 goto failed;
1183 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1184 ++ectx.ec_stack.ga_len;
1185 break;
1186
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001187 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 case ISN_LOADSCRIPT:
1189 {
1190 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001191 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 svar_T *sv;
1193
1194 sv = ((svar_T *)si->sn_var_vals.ga_data)
1195 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001196 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 goto failed;
1198 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1199 ++ectx.ec_stack.ga_len;
1200 }
1201 break;
1202
1203 // load s: variable in old script
1204 case ISN_LOADS:
1205 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001206 hashtab_T *ht = &SCRIPT_VARS(
1207 iptr->isn_arg.loadstore.ls_sid);
1208 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001210
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 if (di == NULL)
1212 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001213 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001214 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001215 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 }
1217 else
1218 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001219 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 goto failed;
1221 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1222 ++ectx.ec_stack.ga_len;
1223 }
1224 }
1225 break;
1226
Bram Moolenaard3aac292020-04-19 14:32:17 +02001227 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001229 case ISN_LOADB:
1230 case ISN_LOADW:
1231 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001233 dictitem_T *di = NULL;
1234 hashtab_T *ht = NULL;
1235 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001236
Bram Moolenaard3aac292020-04-19 14:32:17 +02001237 switch (iptr->isn_type)
1238 {
1239 case ISN_LOADG:
1240 ht = get_globvar_ht();
1241 namespace = 'g';
1242 break;
1243 case ISN_LOADB:
1244 ht = &curbuf->b_vars->dv_hashtab;
1245 namespace = 'b';
1246 break;
1247 case ISN_LOADW:
1248 ht = &curwin->w_vars->dv_hashtab;
1249 namespace = 'w';
1250 break;
1251 case ISN_LOADT:
1252 ht = &curtab->tp_vars->dv_hashtab;
1253 namespace = 't';
1254 break;
1255 default: // Cannot reach here
1256 goto failed;
1257 }
1258 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001259
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if (di == NULL)
1261 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001262 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001263 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001264 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001265 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 }
1267 else
1268 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001269 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 goto failed;
1271 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1272 ++ectx.ec_stack.ga_len;
1273 }
1274 }
1275 break;
1276
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001277 // load g:/b:/w:/t: namespace
1278 case ISN_LOADGDICT:
1279 case ISN_LOADBDICT:
1280 case ISN_LOADWDICT:
1281 case ISN_LOADTDICT:
1282 {
1283 dict_T *d = NULL;
1284
1285 switch (iptr->isn_type)
1286 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001287 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1288 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1289 case ISN_LOADWDICT: d = curwin->w_vars; break;
1290 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001291 default: // Cannot reach here
1292 goto failed;
1293 }
1294 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1295 goto failed;
1296 tv = STACK_TV_BOT(0);
1297 tv->v_type = VAR_DICT;
1298 tv->v_lock = 0;
1299 tv->vval.v_dict = d;
1300 ++ectx.ec_stack.ga_len;
1301 }
1302 break;
1303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304 // load &option
1305 case ISN_LOADOPT:
1306 {
1307 typval_T optval;
1308 char_u *name = iptr->isn_arg.string;
1309
Bram Moolenaara8c17702020-04-01 21:17:24 +02001310 // This is not expected to fail, name is checked during
1311 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001312 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001314 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001315 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 *STACK_TV_BOT(0) = optval;
1317 ++ectx.ec_stack.ga_len;
1318 }
1319 break;
1320
1321 // load $ENV
1322 case ISN_LOADENV:
1323 {
1324 typval_T optval;
1325 char_u *name = iptr->isn_arg.string;
1326
Bram Moolenaar270d0382020-05-15 21:42:53 +02001327 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001329 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001330 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 *STACK_TV_BOT(0) = optval;
1332 ++ectx.ec_stack.ga_len;
1333 }
1334 break;
1335
1336 // load @register
1337 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001338 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 goto failed;
1340 tv = STACK_TV_BOT(0);
1341 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001342 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 tv->vval.v_string = get_reg_contents(
1344 iptr->isn_arg.number, GREG_EXPR_SRC);
1345 ++ectx.ec_stack.ga_len;
1346 break;
1347
1348 // store local variable
1349 case ISN_STORE:
1350 --ectx.ec_stack.ga_len;
1351 tv = STACK_TV_VAR(iptr->isn_arg.number);
1352 clear_tv(tv);
1353 *tv = *STACK_TV_BOT(0);
1354 break;
1355
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001356 // store variable or argument in outer scope
1357 case ISN_STOREOUTER:
1358 --ectx.ec_stack.ga_len;
1359 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1360 clear_tv(tv);
1361 *tv = *STACK_TV_BOT(0);
1362 break;
1363
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001364 // store s: variable in old script
1365 case ISN_STORES:
1366 {
1367 hashtab_T *ht = &SCRIPT_VARS(
1368 iptr->isn_arg.loadstore.ls_sid);
1369 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001370 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001371
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001372 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001373 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001374 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001375 else
1376 {
1377 clear_tv(&di->di_tv);
1378 di->di_tv = *STACK_TV_BOT(0);
1379 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001380 }
1381 break;
1382
1383 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 case ISN_STORESCRIPT:
1385 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001386 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 iptr->isn_arg.script.script_sid);
1388 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1389 + iptr->isn_arg.script.script_idx;
1390
1391 --ectx.ec_stack.ga_len;
1392 clear_tv(sv->sv_tv);
1393 *sv->sv_tv = *STACK_TV_BOT(0);
1394 }
1395 break;
1396
1397 // store option
1398 case ISN_STOREOPT:
1399 {
1400 long n = 0;
1401 char_u *s = NULL;
1402 char *msg;
1403
1404 --ectx.ec_stack.ga_len;
1405 tv = STACK_TV_BOT(0);
1406 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001407 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001409 if (s == NULL)
1410 s = (char_u *)"";
1411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001413 // must be VAR_NUMBER, CHECKTYPE makes sure
1414 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1416 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001417 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 if (msg != NULL)
1419 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001420 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001422 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 }
1425 break;
1426
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001427 // store $ENV
1428 case ISN_STOREENV:
1429 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001430 tv = STACK_TV_BOT(0);
1431 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1432 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001433 break;
1434
1435 // store @r
1436 case ISN_STOREREG:
1437 {
1438 int reg = iptr->isn_arg.number;
1439
1440 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001441 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001442 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001443 tv_get_string(tv), -1, FALSE);
1444 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001445 }
1446 break;
1447
1448 // store v: variable
1449 case ISN_STOREV:
1450 --ectx.ec_stack.ga_len;
1451 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1452 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001453 // should not happen, type is checked when compiling
1454 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001455 break;
1456
Bram Moolenaard3aac292020-04-19 14:32:17 +02001457 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001458 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001459 case ISN_STOREB:
1460 case ISN_STOREW:
1461 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 {
1463 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001464 hashtab_T *ht;
1465 switch (iptr->isn_type)
1466 {
1467 case ISN_STOREG:
1468 ht = get_globvar_ht();
1469 break;
1470 case ISN_STOREB:
1471 ht = &curbuf->b_vars->dv_hashtab;
1472 break;
1473 case ISN_STOREW:
1474 ht = &curwin->w_vars->dv_hashtab;
1475 break;
1476 case ISN_STORET:
1477 ht = &curtab->tp_vars->dv_hashtab;
1478 break;
1479 default: // Cannot reach here
1480 goto failed;
1481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482
1483 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001484 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001486 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 else
1488 {
1489 clear_tv(&di->di_tv);
1490 di->di_tv = *STACK_TV_BOT(0);
1491 }
1492 }
1493 break;
1494
1495 // store number in local variable
1496 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001497 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 clear_tv(tv);
1499 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001500 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001501 break;
1502
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001503 // store value in list variable
1504 case ISN_STORELIST:
1505 {
1506 typval_T *tv_idx = STACK_TV_BOT(-2);
1507 varnumber_T lidx = tv_idx->vval.v_number;
1508 typval_T *tv_list = STACK_TV_BOT(-1);
1509 list_T *list = tv_list->vval.v_list;
1510
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001511 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001512 if (lidx < 0 && list->lv_len + lidx >= 0)
1513 // negative index is relative to the end
1514 lidx = list->lv_len + lidx;
1515 if (lidx < 0 || lidx > list->lv_len)
1516 {
1517 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001518 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001519 }
1520 tv = STACK_TV_BOT(-3);
1521 if (lidx < list->lv_len)
1522 {
1523 listitem_T *li = list_find(list, lidx);
1524
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001525 if (error_if_locked(li->li_tv.v_lock,
1526 e_cannot_change_list_item))
1527 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001528 // overwrite existing list item
1529 clear_tv(&li->li_tv);
1530 li->li_tv = *tv;
1531 }
1532 else
1533 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001534 if (error_if_locked(list->lv_lock,
1535 e_cannot_change_list))
1536 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001537 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001538 if (list_append_tv(list, tv) == FAIL)
1539 goto failed;
1540 clear_tv(tv);
1541 }
1542 clear_tv(tv_idx);
1543 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001544 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001545 }
1546 break;
1547
1548 // store value in dict variable
1549 case ISN_STOREDICT:
1550 {
1551 typval_T *tv_key = STACK_TV_BOT(-2);
1552 char_u *key = tv_key->vval.v_string;
1553 typval_T *tv_dict = STACK_TV_BOT(-1);
1554 dict_T *dict = tv_dict->vval.v_dict;
1555 dictitem_T *di;
1556
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001557 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001558 if (dict == NULL)
1559 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001560 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001561 goto on_error;
1562 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001563 if (key == NULL)
1564 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001565 tv = STACK_TV_BOT(-3);
1566 di = dict_find(dict, key, -1);
1567 if (di != NULL)
1568 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001569 if (error_if_locked(di->di_tv.v_lock,
1570 e_cannot_change_dict_item))
1571 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001572 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001573 clear_tv(&di->di_tv);
1574 di->di_tv = *tv;
1575 }
1576 else
1577 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001578 if (error_if_locked(dict->dv_lock,
1579 e_cannot_change_dict))
1580 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001581 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001582 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1583 goto failed;
1584 clear_tv(tv);
1585 }
1586 clear_tv(tv_key);
1587 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001588 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001589 }
1590 break;
1591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 // push constant
1593 case ISN_PUSHNR:
1594 case ISN_PUSHBOOL:
1595 case ISN_PUSHSPEC:
1596 case ISN_PUSHF:
1597 case ISN_PUSHS:
1598 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001599 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001600 case ISN_PUSHCHANNEL:
1601 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001602 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 goto failed;
1604 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001605 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 ++ectx.ec_stack.ga_len;
1607 switch (iptr->isn_type)
1608 {
1609 case ISN_PUSHNR:
1610 tv->v_type = VAR_NUMBER;
1611 tv->vval.v_number = iptr->isn_arg.number;
1612 break;
1613 case ISN_PUSHBOOL:
1614 tv->v_type = VAR_BOOL;
1615 tv->vval.v_number = iptr->isn_arg.number;
1616 break;
1617 case ISN_PUSHSPEC:
1618 tv->v_type = VAR_SPECIAL;
1619 tv->vval.v_number = iptr->isn_arg.number;
1620 break;
1621#ifdef FEAT_FLOAT
1622 case ISN_PUSHF:
1623 tv->v_type = VAR_FLOAT;
1624 tv->vval.v_float = iptr->isn_arg.fnumber;
1625 break;
1626#endif
1627 case ISN_PUSHBLOB:
1628 blob_copy(iptr->isn_arg.blob, tv);
1629 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001630 case ISN_PUSHFUNC:
1631 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001632 if (iptr->isn_arg.string == NULL)
1633 tv->vval.v_string = NULL;
1634 else
1635 tv->vval.v_string =
1636 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001637 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001638 case ISN_PUSHCHANNEL:
1639#ifdef FEAT_JOB_CHANNEL
1640 tv->v_type = VAR_CHANNEL;
1641 tv->vval.v_channel = iptr->isn_arg.channel;
1642 if (tv->vval.v_channel != NULL)
1643 ++tv->vval.v_channel->ch_refcount;
1644#endif
1645 break;
1646 case ISN_PUSHJOB:
1647#ifdef FEAT_JOB_CHANNEL
1648 tv->v_type = VAR_JOB;
1649 tv->vval.v_job = iptr->isn_arg.job;
1650 if (tv->vval.v_job != NULL)
1651 ++tv->vval.v_job->jv_refcount;
1652#endif
1653 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 default:
1655 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001656 tv->vval.v_string = vim_strsave(
1657 iptr->isn_arg.string == NULL
1658 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 }
1660 break;
1661
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001662 case ISN_UNLET:
1663 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1664 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001665 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001666 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001667 case ISN_UNLETENV:
1668 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1669 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001670
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001671 case ISN_LOCKCONST:
1672 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1673 break;
1674
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 // create a list from items on the stack; uses a single allocation
1676 // for the list header and the items
1677 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001678 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1679 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 break;
1681
1682 // create a dict from items on the stack
1683 case ISN_NEWDICT:
1684 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001685 int count = iptr->isn_arg.number;
1686 dict_T *dict = dict_alloc();
1687 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688
1689 if (dict == NULL)
1690 goto failed;
1691 for (idx = 0; idx < count; ++idx)
1692 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001693 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001695 // check key is unique
1696 item = dict_find(dict, tv->vval.v_string, -1);
1697 if (item != NULL)
1698 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001699 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001700 semsg(_(e_duplicate_key), tv->vval.v_string);
1701 dict_unref(dict);
1702 goto on_error;
1703 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 item = dictitem_alloc(tv->vval.v_string);
1705 clear_tv(tv);
1706 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001707 {
1708 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001710 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1712 item->di_tv.v_lock = 0;
1713 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001714 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001715 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001716 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001718 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 }
1720
1721 if (count > 0)
1722 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001723 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 goto failed;
1725 else
1726 ++ectx.ec_stack.ga_len;
1727 tv = STACK_TV_BOT(-1);
1728 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001729 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 tv->vval.v_dict = dict;
1731 ++dict->dv_refcount;
1732 }
1733 break;
1734
1735 // call a :def function
1736 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001737 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1739 iptr->isn_arg.dfunc.cdf_argcount,
1740 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001741 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001742 break;
1743
1744 // call a builtin function
1745 case ISN_BCALL:
1746 SOURCING_LNUM = iptr->isn_lnum;
1747 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1748 iptr->isn_arg.bfunc.cbf_argcount,
1749 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001750 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001751 break;
1752
1753 // call a funcref or partial
1754 case ISN_PCALL:
1755 {
1756 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1757 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001758 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759
1760 SOURCING_LNUM = iptr->isn_lnum;
1761 if (pfunc->cpf_top)
1762 {
1763 // funcref is above the arguments
1764 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1765 }
1766 else
1767 {
1768 // Get the funcref from the stack.
1769 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001770 partial_tv = *STACK_TV_BOT(0);
1771 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 }
1773 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001774 if (tv == &partial_tv)
1775 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001777 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 }
1779 break;
1780
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001781 case ISN_PCALL_END:
1782 // PCALL finished, arguments have been consumed and replaced by
1783 // the return value. Now clear the funcref from the stack,
1784 // and move the return value in its place.
1785 --ectx.ec_stack.ga_len;
1786 clear_tv(STACK_TV_BOT(-1));
1787 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1788 break;
1789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 // call a user defined function or funcref/partial
1791 case ISN_UCALL:
1792 {
1793 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1794
1795 SOURCING_LNUM = iptr->isn_lnum;
1796 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001797 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001798 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 }
1800 break;
1801
1802 // return from a :def function call
1803 case ISN_RETURN:
1804 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001805 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001806 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001807
1808 if (trystack->ga_len > 0)
1809 trycmd = ((trycmd_T *)trystack->ga_data)
1810 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001811 if (trycmd != NULL
1812 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 && trycmd->tcd_finally_idx != 0)
1814 {
1815 // jump to ":finally"
1816 ectx.ec_iidx = trycmd->tcd_finally_idx;
1817 trycmd->tcd_return = TRUE;
1818 }
1819 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001820 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 }
1822 break;
1823
1824 // push a function reference to a compiled function
1825 case ISN_FUNCREF:
1826 {
1827 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001828 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
1830 pt = ALLOC_CLEAR_ONE(partial_T);
1831 if (pt == NULL)
1832 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001833 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001834 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001835 vim_free(pt);
1836 goto failed;
1837 }
1838 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1839 + iptr->isn_arg.funcref.fr_func;
1840 pt->pt_func = pt_dfunc->df_ufunc;
1841 pt->pt_refcount = 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001842
1843 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1844 {
1845 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1846 + ectx.ec_dfunc_idx;
1847
1848 // The closure needs to find arguments and local
1849 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001850 pt->pt_ectx_stack = &ectx.ec_stack;
1851 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001852
1853 // If this function returns and the closure is still
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001854 // being used, we need to make a copy of the context
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001855 // (arguments and local variables). Store a reference
1856 // to the partial so we can handle that.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001857 if (ga_grow(&ectx.ec_funcrefs, 1) == FAIL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001858 {
Bram Moolenaardec07512020-09-18 23:11:10 +02001859 vim_free(pt);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001860 goto failed;
1861 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001862 // Extra variable keeps the count of closures created
1863 // in the current function call.
1864 tv = STACK_TV_VAR(dfunc->df_varcount);
1865 ++tv->vval.v_number;
1866
1867 ((partial_T **)ectx.ec_funcrefs.ga_data)
1868 [ectx.ec_funcrefs.ga_len] = pt;
1869 ++pt->pt_refcount;
1870 ++ectx.ec_funcrefs.ga_len;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001871 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001872 ++pt_dfunc->df_ufunc->uf_refcount;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001873
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 tv = STACK_TV_BOT(0);
1875 ++ectx.ec_stack.ga_len;
1876 tv->vval.v_partial = pt;
1877 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001878 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 }
1880 break;
1881
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001882 // Create a global function from a lambda.
1883 case ISN_NEWFUNC:
1884 {
1885 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1886
1887 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1888 }
1889 break;
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 // jump if a condition is met
1892 case ISN_JUMP:
1893 {
1894 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1895 int jump = TRUE;
1896
1897 if (when != JUMP_ALWAYS)
1898 {
1899 tv = STACK_TV_BOT(-1);
1900 jump = tv2bool(tv);
1901 if (when == JUMP_IF_FALSE
1902 || when == JUMP_AND_KEEP_IF_FALSE)
1903 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001904 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 {
1906 // drop the value from the stack
1907 clear_tv(tv);
1908 --ectx.ec_stack.ga_len;
1909 }
1910 }
1911 if (jump)
1912 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1913 }
1914 break;
1915
1916 // top of a for loop
1917 case ISN_FOR:
1918 {
1919 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1920 typval_T *idxtv =
1921 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1922
1923 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001924 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001926 ++idxtv->vval.v_number;
1927 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 // past the end of the list, jump to "endfor"
1929 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1930 else if (list->lv_first == &range_list_item)
1931 {
1932 // non-materialized range() list
1933 tv = STACK_TV_BOT(0);
1934 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001935 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001936 tv->vval.v_number = list_find_nr(
1937 list, idxtv->vval.v_number, NULL);
1938 ++ectx.ec_stack.ga_len;
1939 }
1940 else
1941 {
1942 listitem_T *li = list_find(list, idxtv->vval.v_number);
1943
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1945 ++ectx.ec_stack.ga_len;
1946 }
1947 }
1948 break;
1949
1950 // start of ":try" block
1951 case ISN_TRY:
1952 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001953 trycmd_T *trycmd = NULL;
1954
Bram Moolenaar270d0382020-05-15 21:42:53 +02001955 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 goto failed;
1957 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1958 + ectx.ec_trystack.ga_len;
1959 ++ectx.ec_trystack.ga_len;
1960 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001961 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1963 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001964 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02001965 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 }
1967 break;
1968
1969 case ISN_PUSHEXC:
1970 if (current_exception == NULL)
1971 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001973 iemsg("Evaluating catch while current_exception is NULL");
1974 goto failed;
1975 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001976 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 goto failed;
1978 tv = STACK_TV_BOT(0);
1979 ++ectx.ec_stack.ga_len;
1980 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001981 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 tv->vval.v_string = vim_strsave(
1983 (char_u *)current_exception->value);
1984 break;
1985
1986 case ISN_CATCH:
1987 {
1988 garray_T *trystack = &ectx.ec_trystack;
1989
1990 if (trystack->ga_len > 0)
1991 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001992 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 + trystack->ga_len - 1;
1994 trycmd->tcd_caught = TRUE;
1995 }
1996 did_emsg = got_int = did_throw = FALSE;
1997 catch_exception(current_exception);
1998 }
1999 break;
2000
2001 // end of ":try" block
2002 case ISN_ENDTRY:
2003 {
2004 garray_T *trystack = &ectx.ec_trystack;
2005
2006 if (trystack->ga_len > 0)
2007 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002008 trycmd_T *trycmd = NULL;
2009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002010 --trystack->ga_len;
2011 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002012 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 trycmd = ((trycmd_T *)trystack->ga_data)
2014 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002015 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 {
2017 // discard the exception
2018 if (caught_stack == current_exception)
2019 caught_stack = caught_stack->caught;
2020 discard_current_exception();
2021 }
2022
2023 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002024 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 }
2026 }
2027 break;
2028
2029 case ISN_THROW:
2030 --ectx.ec_stack.ga_len;
2031 tv = STACK_TV_BOT(0);
2032 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2033 {
2034 vim_free(tv->vval.v_string);
2035 goto failed;
2036 }
2037 did_throw = TRUE;
2038 break;
2039
2040 // compare with special values
2041 case ISN_COMPAREBOOL:
2042 case ISN_COMPARESPECIAL:
2043 {
2044 typval_T *tv1 = STACK_TV_BOT(-2);
2045 typval_T *tv2 = STACK_TV_BOT(-1);
2046 varnumber_T arg1 = tv1->vval.v_number;
2047 varnumber_T arg2 = tv2->vval.v_number;
2048 int res;
2049
2050 switch (iptr->isn_arg.op.op_type)
2051 {
2052 case EXPR_EQUAL: res = arg1 == arg2; break;
2053 case EXPR_NEQUAL: res = arg1 != arg2; break;
2054 default: res = 0; break;
2055 }
2056
2057 --ectx.ec_stack.ga_len;
2058 tv1->v_type = VAR_BOOL;
2059 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2060 }
2061 break;
2062
2063 // Operation with two number arguments
2064 case ISN_OPNR:
2065 case ISN_COMPARENR:
2066 {
2067 typval_T *tv1 = STACK_TV_BOT(-2);
2068 typval_T *tv2 = STACK_TV_BOT(-1);
2069 varnumber_T arg1 = tv1->vval.v_number;
2070 varnumber_T arg2 = tv2->vval.v_number;
2071 varnumber_T res;
2072
2073 switch (iptr->isn_arg.op.op_type)
2074 {
2075 case EXPR_MULT: res = arg1 * arg2; break;
2076 case EXPR_DIV: res = arg1 / arg2; break;
2077 case EXPR_REM: res = arg1 % arg2; break;
2078 case EXPR_SUB: res = arg1 - arg2; break;
2079 case EXPR_ADD: res = arg1 + arg2; break;
2080
2081 case EXPR_EQUAL: res = arg1 == arg2; break;
2082 case EXPR_NEQUAL: res = arg1 != arg2; break;
2083 case EXPR_GREATER: res = arg1 > arg2; break;
2084 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2085 case EXPR_SMALLER: res = arg1 < arg2; break;
2086 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2087 default: res = 0; break;
2088 }
2089
2090 --ectx.ec_stack.ga_len;
2091 if (iptr->isn_type == ISN_COMPARENR)
2092 {
2093 tv1->v_type = VAR_BOOL;
2094 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2095 }
2096 else
2097 tv1->vval.v_number = res;
2098 }
2099 break;
2100
2101 // Computation with two float arguments
2102 case ISN_OPFLOAT:
2103 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002104#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 {
2106 typval_T *tv1 = STACK_TV_BOT(-2);
2107 typval_T *tv2 = STACK_TV_BOT(-1);
2108 float_T arg1 = tv1->vval.v_float;
2109 float_T arg2 = tv2->vval.v_float;
2110 float_T res = 0;
2111 int cmp = FALSE;
2112
2113 switch (iptr->isn_arg.op.op_type)
2114 {
2115 case EXPR_MULT: res = arg1 * arg2; break;
2116 case EXPR_DIV: res = arg1 / arg2; break;
2117 case EXPR_SUB: res = arg1 - arg2; break;
2118 case EXPR_ADD: res = arg1 + arg2; break;
2119
2120 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2121 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2122 case EXPR_GREATER: cmp = arg1 > arg2; break;
2123 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2124 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2125 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2126 default: cmp = 0; break;
2127 }
2128 --ectx.ec_stack.ga_len;
2129 if (iptr->isn_type == ISN_COMPAREFLOAT)
2130 {
2131 tv1->v_type = VAR_BOOL;
2132 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2133 }
2134 else
2135 tv1->vval.v_float = res;
2136 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002137#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002138 break;
2139
2140 case ISN_COMPARELIST:
2141 {
2142 typval_T *tv1 = STACK_TV_BOT(-2);
2143 typval_T *tv2 = STACK_TV_BOT(-1);
2144 list_T *arg1 = tv1->vval.v_list;
2145 list_T *arg2 = tv2->vval.v_list;
2146 int cmp = FALSE;
2147 int ic = iptr->isn_arg.op.op_ic;
2148
2149 switch (iptr->isn_arg.op.op_type)
2150 {
2151 case EXPR_EQUAL: cmp =
2152 list_equal(arg1, arg2, ic, FALSE); break;
2153 case EXPR_NEQUAL: cmp =
2154 !list_equal(arg1, arg2, ic, FALSE); break;
2155 case EXPR_IS: cmp = arg1 == arg2; break;
2156 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2157 default: cmp = 0; break;
2158 }
2159 --ectx.ec_stack.ga_len;
2160 clear_tv(tv1);
2161 clear_tv(tv2);
2162 tv1->v_type = VAR_BOOL;
2163 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2164 }
2165 break;
2166
2167 case ISN_COMPAREBLOB:
2168 {
2169 typval_T *tv1 = STACK_TV_BOT(-2);
2170 typval_T *tv2 = STACK_TV_BOT(-1);
2171 blob_T *arg1 = tv1->vval.v_blob;
2172 blob_T *arg2 = tv2->vval.v_blob;
2173 int cmp = FALSE;
2174
2175 switch (iptr->isn_arg.op.op_type)
2176 {
2177 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2178 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2179 case EXPR_IS: cmp = arg1 == arg2; break;
2180 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2181 default: cmp = 0; break;
2182 }
2183 --ectx.ec_stack.ga_len;
2184 clear_tv(tv1);
2185 clear_tv(tv2);
2186 tv1->v_type = VAR_BOOL;
2187 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2188 }
2189 break;
2190
2191 // TODO: handle separately
2192 case ISN_COMPARESTRING:
2193 case ISN_COMPAREDICT:
2194 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 case ISN_COMPAREANY:
2196 {
2197 typval_T *tv1 = STACK_TV_BOT(-2);
2198 typval_T *tv2 = STACK_TV_BOT(-1);
2199 exptype_T exptype = iptr->isn_arg.op.op_type;
2200 int ic = iptr->isn_arg.op.op_ic;
2201
Bram Moolenaareb26f432020-09-14 16:50:05 +02002202 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002203 typval_compare(tv1, tv2, exptype, ic);
2204 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 --ectx.ec_stack.ga_len;
2206 }
2207 break;
2208
2209 case ISN_ADDLIST:
2210 case ISN_ADDBLOB:
2211 {
2212 typval_T *tv1 = STACK_TV_BOT(-2);
2213 typval_T *tv2 = STACK_TV_BOT(-1);
2214
2215 if (iptr->isn_type == ISN_ADDLIST)
2216 eval_addlist(tv1, tv2);
2217 else
2218 eval_addblob(tv1, tv2);
2219 clear_tv(tv2);
2220 --ectx.ec_stack.ga_len;
2221 }
2222 break;
2223
2224 // Computation with two arguments of unknown type
2225 case ISN_OPANY:
2226 {
2227 typval_T *tv1 = STACK_TV_BOT(-2);
2228 typval_T *tv2 = STACK_TV_BOT(-1);
2229 varnumber_T n1, n2;
2230#ifdef FEAT_FLOAT
2231 float_T f1 = 0, f2 = 0;
2232#endif
2233 int error = FALSE;
2234
2235 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2236 {
2237 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2238 {
2239 eval_addlist(tv1, tv2);
2240 clear_tv(tv2);
2241 --ectx.ec_stack.ga_len;
2242 break;
2243 }
2244 else if (tv1->v_type == VAR_BLOB
2245 && tv2->v_type == VAR_BLOB)
2246 {
2247 eval_addblob(tv1, tv2);
2248 clear_tv(tv2);
2249 --ectx.ec_stack.ga_len;
2250 break;
2251 }
2252 }
2253#ifdef FEAT_FLOAT
2254 if (tv1->v_type == VAR_FLOAT)
2255 {
2256 f1 = tv1->vval.v_float;
2257 n1 = 0;
2258 }
2259 else
2260#endif
2261 {
2262 n1 = tv_get_number_chk(tv1, &error);
2263 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002264 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265#ifdef FEAT_FLOAT
2266 if (tv2->v_type == VAR_FLOAT)
2267 f1 = n1;
2268#endif
2269 }
2270#ifdef FEAT_FLOAT
2271 if (tv2->v_type == VAR_FLOAT)
2272 {
2273 f2 = tv2->vval.v_float;
2274 n2 = 0;
2275 }
2276 else
2277#endif
2278 {
2279 n2 = tv_get_number_chk(tv2, &error);
2280 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002281 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282#ifdef FEAT_FLOAT
2283 if (tv1->v_type == VAR_FLOAT)
2284 f2 = n2;
2285#endif
2286 }
2287#ifdef FEAT_FLOAT
2288 // if there is a float on either side the result is a float
2289 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2290 {
2291 switch (iptr->isn_arg.op.op_type)
2292 {
2293 case EXPR_MULT: f1 = f1 * f2; break;
2294 case EXPR_DIV: f1 = f1 / f2; break;
2295 case EXPR_SUB: f1 = f1 - f2; break;
2296 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002297 default: SOURCING_LNUM = iptr->isn_lnum;
2298 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002299 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 }
2301 clear_tv(tv1);
2302 clear_tv(tv2);
2303 tv1->v_type = VAR_FLOAT;
2304 tv1->vval.v_float = f1;
2305 --ectx.ec_stack.ga_len;
2306 }
2307 else
2308#endif
2309 {
2310 switch (iptr->isn_arg.op.op_type)
2311 {
2312 case EXPR_MULT: n1 = n1 * n2; break;
2313 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2314 case EXPR_SUB: n1 = n1 - n2; break;
2315 case EXPR_ADD: n1 = n1 + n2; break;
2316 default: n1 = num_modulus(n1, n2); break;
2317 }
2318 clear_tv(tv1);
2319 clear_tv(tv2);
2320 tv1->v_type = VAR_NUMBER;
2321 tv1->vval.v_number = n1;
2322 --ectx.ec_stack.ga_len;
2323 }
2324 }
2325 break;
2326
2327 case ISN_CONCAT:
2328 {
2329 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2330 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2331 char_u *res;
2332
2333 res = concat_str(str1, str2);
2334 clear_tv(STACK_TV_BOT(-2));
2335 clear_tv(STACK_TV_BOT(-1));
2336 --ectx.ec_stack.ga_len;
2337 STACK_TV_BOT(-1)->vval.v_string = res;
2338 }
2339 break;
2340
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002341 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002342 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002343 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002344 int is_slice = iptr->isn_type == ISN_STRSLICE;
2345 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002346 char_u *res;
2347
2348 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002349 // string slice: string is at stack-3, first index at
2350 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002351 if (is_slice)
2352 {
2353 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002354 n1 = tv->vval.v_number;
2355 }
2356
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002357 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002358 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002359
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002360 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002361 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002362 if (is_slice)
2363 // Slice: Select the characters from the string
2364 res = string_slice(tv->vval.v_string, n1, n2);
2365 else
2366 // Index: The resulting variable is a string of a
2367 // single character. If the index is too big or
2368 // negative the result is empty.
2369 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002370 vim_free(tv->vval.v_string);
2371 tv->vval.v_string = res;
2372 }
2373 break;
2374
2375 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002376 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 {
Bram Moolenaared591872020-08-15 22:14:53 +02002378 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002380 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381
2382 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002383 // list slice: list is at stack-3, indexes at stack-2 and
2384 // stack-1
2385 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 list = tv->vval.v_list;
2387
2388 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002389 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002391
2392 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393 {
Bram Moolenaared591872020-08-15 22:14:53 +02002394 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002395 n1 = tv->vval.v_number;
2396 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
Bram Moolenaared591872020-08-15 22:14:53 +02002398
2399 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002400 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002401 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002402 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2403 == FAIL)
2404 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 }
2406 break;
2407
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002408 case ISN_ANYINDEX:
2409 case ISN_ANYSLICE:
2410 {
2411 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2412 typval_T *var1, *var2;
2413 int res;
2414
2415 // index: composite is at stack-2, index at stack-1
2416 // slice: composite is at stack-3, indexes at stack-2 and
2417 // stack-1
2418 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002420 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2421 goto on_error;
2422 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2423 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2424 res = eval_index_inner(tv, is_slice,
2425 var1, var2, NULL, -1, TRUE);
2426 clear_tv(var1);
2427 if (is_slice)
2428 clear_tv(var2);
2429 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2430 if (res == FAIL)
2431 goto on_error;
2432 }
2433 break;
2434
Bram Moolenaar9af78762020-06-16 11:34:42 +02002435 case ISN_SLICE:
2436 {
2437 list_T *list;
2438 int count = iptr->isn_arg.number;
2439
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002440 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002441 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002442 list = tv->vval.v_list;
2443
2444 // no error for short list, expect it to be checked earlier
2445 if (list != NULL && list->lv_len >= count)
2446 {
2447 list_T *newlist = list_slice(list,
2448 count, list->lv_len - 1);
2449
2450 if (newlist != NULL)
2451 {
2452 list_unref(list);
2453 tv->vval.v_list = newlist;
2454 ++newlist->lv_refcount;
2455 }
2456 }
2457 }
2458 break;
2459
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002460 case ISN_GETITEM:
2461 {
2462 listitem_T *li;
2463 int index = iptr->isn_arg.number;
2464
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002465 // Get list item: list is at stack-1, push item.
2466 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002467 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002468 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002469
2470 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2471 goto failed;
2472 ++ectx.ec_stack.ga_len;
2473 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2474 }
2475 break;
2476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 case ISN_MEMBER:
2478 {
2479 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002480 char_u *key;
2481 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002482 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002483
2484 // dict member: dict is at stack-2, key at stack-1
2485 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002486 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002487 dict = tv->vval.v_dict;
2488
2489 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002490 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002491 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002492
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002493 if ((di = dict_find(dict, key, -1)) == NULL)
2494 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002495 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002496 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002497 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002498 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002499 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002500 --ectx.ec_stack.ga_len;
2501 // Clear the dict after getting the item, to avoid that it
2502 // make the item invalid.
2503 tv = STACK_TV_BOT(-1);
2504 temp_tv = *tv;
2505 copy_tv(&di->di_tv, tv);
2506 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002507 }
2508 break;
2509
2510 // dict member with string key
2511 case ISN_STRINGMEMBER:
2512 {
2513 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002515 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516
2517 tv = STACK_TV_BOT(-1);
2518 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2519 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 }
2524 dict = tv->vval.v_dict;
2525
2526 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2527 == NULL)
2528 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002531 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002533 // Clear the dict after getting the item, to avoid that it
2534 // make the item invalid.
2535 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002537 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 }
2539 break;
2540
2541 case ISN_NEGATENR:
2542 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002543 if (tv->v_type != VAR_NUMBER
2544#ifdef FEAT_FLOAT
2545 && tv->v_type != VAR_FLOAT
2546#endif
2547 )
2548 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002550 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002551 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002552 }
2553#ifdef FEAT_FLOAT
2554 if (tv->v_type == VAR_FLOAT)
2555 tv->vval.v_float = -tv->vval.v_float;
2556 else
2557#endif
2558 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 break;
2560
2561 case ISN_CHECKNR:
2562 {
2563 int error = FALSE;
2564
2565 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002566 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002568 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 (void)tv_get_number_chk(tv, &error);
2570 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002571 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 }
2573 break;
2574
2575 case ISN_CHECKTYPE:
2576 {
2577 checktype_T *ct = &iptr->isn_arg.type;
2578
2579 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002580 SOURCING_LNUM = iptr->isn_lnum;
2581 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2582 goto on_error;
2583
2584 // number 0 is FALSE, number 1 is TRUE
2585 if (tv->v_type == VAR_NUMBER
2586 && ct->ct_type->tt_type == VAR_BOOL
2587 && (tv->vval.v_number == 0
2588 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002590 tv->v_type = VAR_BOOL;
2591 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002592 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 }
2594 }
2595 break;
2596
Bram Moolenaar9af78762020-06-16 11:34:42 +02002597 case ISN_CHECKLEN:
2598 {
2599 int min_len = iptr->isn_arg.checklen.cl_min_len;
2600 list_T *list = NULL;
2601
2602 tv = STACK_TV_BOT(-1);
2603 if (tv->v_type == VAR_LIST)
2604 list = tv->vval.v_list;
2605 if (list == NULL || list->lv_len < min_len
2606 || (list->lv_len > min_len
2607 && !iptr->isn_arg.checklen.cl_more_OK))
2608 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002609 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002610 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002611 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002612 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002613 }
2614 }
2615 break;
2616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 case ISN_2BOOL:
2618 {
2619 int n;
2620
2621 tv = STACK_TV_BOT(-1);
2622 n = tv2bool(tv);
2623 if (iptr->isn_arg.number) // invert
2624 n = !n;
2625 clear_tv(tv);
2626 tv->v_type = VAR_BOOL;
2627 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2628 }
2629 break;
2630
2631 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002632 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002633 {
2634 char_u *str;
2635
2636 tv = STACK_TV_BOT(iptr->isn_arg.number);
2637 if (tv->v_type != VAR_STRING)
2638 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002639 if (iptr->isn_type == ISN_2STRING_ANY)
2640 {
2641 switch (tv->v_type)
2642 {
2643 case VAR_SPECIAL:
2644 case VAR_BOOL:
2645 case VAR_NUMBER:
2646 case VAR_FLOAT:
2647 case VAR_BLOB: break;
2648 default: to_string_error(tv->v_type);
2649 goto on_error;
2650 }
2651 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 str = typval_tostring(tv);
2653 clear_tv(tv);
2654 tv->v_type = VAR_STRING;
2655 tv->vval.v_string = str;
2656 }
2657 }
2658 break;
2659
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002660 case ISN_PUT:
2661 {
2662 int regname = iptr->isn_arg.put.put_regname;
2663 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2664 char_u *expr = NULL;
2665 int dir = FORWARD;
2666
2667 if (regname == '=')
2668 {
2669 tv = STACK_TV_BOT(-1);
2670 if (tv->v_type == VAR_STRING)
2671 expr = tv->vval.v_string;
2672 else
2673 {
2674 expr = typval_tostring(tv); // allocates value
2675 clear_tv(tv);
2676 }
2677 --ectx.ec_stack.ga_len;
2678 }
2679 if (lnum == -2)
2680 // :put! above cursor
2681 dir = BACKWARD;
2682 else if (lnum >= 0)
2683 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2684 check_cursor();
2685 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2686 vim_free(expr);
2687 }
2688 break;
2689
Bram Moolenaar389df252020-07-09 21:20:47 +02002690 case ISN_SHUFFLE:
2691 {
2692 typval_T tmp_tv;
2693 int item = iptr->isn_arg.shuffle.shfl_item;
2694 int up = iptr->isn_arg.shuffle.shfl_up;
2695
2696 tmp_tv = *STACK_TV_BOT(-item);
2697 for ( ; up > 0 && item > 1; --up)
2698 {
2699 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2700 --item;
2701 }
2702 *STACK_TV_BOT(-item) = tmp_tv;
2703 }
2704 break;
2705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 case ISN_DROP:
2707 --ectx.ec_stack.ga_len;
2708 clear_tv(STACK_TV_BOT(0));
2709 break;
2710 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002711 continue;
2712
Bram Moolenaard032f342020-07-18 18:13:02 +02002713func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002714 // Restore previous function. If the frame pointer is where we started
2715 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02002716 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02002717 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002718
Bram Moolenaard032f342020-07-18 18:13:02 +02002719 if (func_return(&ectx) == FAIL)
2720 // only fails when out of memory
2721 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002722 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002723
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002724on_error:
2725 if (trylevel == 0)
2726 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 }
2728
2729done:
2730 // function finished, get result from the stack.
2731 tv = STACK_TV_BOT(-1);
2732 *rettv = *tv;
2733 tv->v_type = VAR_UNKNOWN;
2734 ret = OK;
2735
2736failed:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02002737 // Also deal with closures when failed, they may already be in use
2738 // somewhere.
2739 handle_closure_in_use(&ectx, FALSE);
2740
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002741 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002742 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002743 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002744
Bram Moolenaaree8580e2020-08-28 17:19:07 +02002745 estack_pop();
2746 current_sctx = save_current_sctx;
2747
2748failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002749 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2751 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002752
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002754 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002755
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002756 // Not sure if this is necessary.
2757 suppress_errthrow = save_suppress_errthrow;
2758
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002759 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002760 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002761 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 return ret;
2763}
2764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765/*
2766 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002767 * We don't really need this at runtime, but we do have tests that require it,
2768 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002769 */
2770 void
2771ex_disassemble(exarg_T *eap)
2772{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002773 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002774 char_u *fname;
2775 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002776 dfunc_T *dfunc;
2777 isn_T *instr;
2778 int current;
2779 int line_idx = 0;
2780 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002781 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002783 if (STRNCMP(arg, "<lambda>", 8) == 0)
2784 {
2785 arg += 8;
2786 (void)getdigits(&arg);
2787 fname = vim_strnsave(eap->arg, arg - eap->arg);
2788 }
2789 else
2790 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002791 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002792 if (fname == NULL)
2793 {
2794 semsg(_(e_invarg2), eap->arg);
2795 return;
2796 }
2797
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002798 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002799 if (ufunc == NULL)
2800 {
2801 char_u *p = untrans_function_name(fname);
2802
2803 if (p != NULL)
2804 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002805 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002806 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002807 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 if (ufunc == NULL)
2809 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002810 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 return;
2812 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002813 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002814 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2815 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002816 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002818 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 return;
2820 }
2821 if (ufunc->uf_name_exp != NULL)
2822 msg((char *)ufunc->uf_name_exp);
2823 else
2824 msg((char *)ufunc->uf_name);
2825
2826 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2827 instr = dfunc->df_instr;
2828 for (current = 0; current < dfunc->df_instr_count; ++current)
2829 {
2830 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002831 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832
2833 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2834 {
2835 if (current > prev_current)
2836 {
2837 msg_puts("\n\n");
2838 prev_current = current;
2839 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002840 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2841 if (line != NULL)
2842 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 }
2844
2845 switch (iptr->isn_type)
2846 {
2847 case ISN_EXEC:
2848 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2849 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002850 case ISN_EXECCONCAT:
2851 smsg("%4d EXECCONCAT %lld", current,
2852 (long long)iptr->isn_arg.number);
2853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 case ISN_ECHO:
2855 {
2856 echo_T *echo = &iptr->isn_arg.echo;
2857
2858 smsg("%4d %s %d", current,
2859 echo->echo_with_white ? "ECHO" : "ECHON",
2860 echo->echo_count);
2861 }
2862 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002863 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002864 smsg("%4d EXECUTE %lld", current,
2865 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002866 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002867 case ISN_ECHOMSG:
2868 smsg("%4d ECHOMSG %lld", current,
2869 (long long)(iptr->isn_arg.number));
2870 break;
2871 case ISN_ECHOERR:
2872 smsg("%4d ECHOERR %lld", current,
2873 (long long)(iptr->isn_arg.number));
2874 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002876 case ISN_LOADOUTER:
2877 {
2878 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2879
2880 if (iptr->isn_arg.number < 0)
2881 smsg("%4d LOAD%s arg[%lld]", current, add,
2882 (long long)(iptr->isn_arg.number
2883 + STACK_FRAME_SIZE));
2884 else
2885 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002886 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002887 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 break;
2889 case ISN_LOADV:
2890 smsg("%4d LOADV v:%s", current,
2891 get_vim_var_name(iptr->isn_arg.number));
2892 break;
2893 case ISN_LOADSCRIPT:
2894 {
2895 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002896 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2898 + iptr->isn_arg.script.script_idx;
2899
2900 smsg("%4d LOADSCRIPT %s from %s", current,
2901 sv->sv_name, si->sn_name);
2902 }
2903 break;
2904 case ISN_LOADS:
2905 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002906 scriptitem_T *si = SCRIPT_ITEM(
2907 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908
2909 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002910 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 }
2912 break;
2913 case ISN_LOADG:
2914 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2915 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002916 case ISN_LOADB:
2917 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2918 break;
2919 case ISN_LOADW:
2920 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2921 break;
2922 case ISN_LOADT:
2923 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2924 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002925 case ISN_LOADGDICT:
2926 smsg("%4d LOAD g:", current);
2927 break;
2928 case ISN_LOADBDICT:
2929 smsg("%4d LOAD b:", current);
2930 break;
2931 case ISN_LOADWDICT:
2932 smsg("%4d LOAD w:", current);
2933 break;
2934 case ISN_LOADTDICT:
2935 smsg("%4d LOAD t:", current);
2936 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002937 case ISN_LOADOPT:
2938 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2939 break;
2940 case ISN_LOADENV:
2941 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2942 break;
2943 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002944 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 break;
2946
2947 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002948 case ISN_STOREOUTER:
2949 {
2950 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2951
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002952 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002953 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002954 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002955 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002956 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002957 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002958 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002960 case ISN_STOREV:
2961 smsg("%4d STOREV v:%s", current,
2962 get_vim_var_name(iptr->isn_arg.number));
2963 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002965 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2966 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002967 case ISN_STOREB:
2968 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2969 break;
2970 case ISN_STOREW:
2971 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2972 break;
2973 case ISN_STORET:
2974 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2975 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002976 case ISN_STORES:
2977 {
2978 scriptitem_T *si = SCRIPT_ITEM(
2979 iptr->isn_arg.loadstore.ls_sid);
2980
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002981 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002982 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002983 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 break;
2985 case ISN_STORESCRIPT:
2986 {
2987 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002988 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2990 + iptr->isn_arg.script.script_idx;
2991
2992 smsg("%4d STORESCRIPT %s in %s", current,
2993 sv->sv_name, si->sn_name);
2994 }
2995 break;
2996 case ISN_STOREOPT:
2997 smsg("%4d STOREOPT &%s", current,
2998 iptr->isn_arg.storeopt.so_name);
2999 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003000 case ISN_STOREENV:
3001 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3002 break;
3003 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003004 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003005 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 case ISN_STORENR:
3007 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003008 iptr->isn_arg.storenr.stnr_val,
3009 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 break;
3011
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003012 case ISN_STORELIST:
3013 smsg("%4d STORELIST", current);
3014 break;
3015
3016 case ISN_STOREDICT:
3017 smsg("%4d STOREDICT", current);
3018 break;
3019
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 // constants
3021 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003022 smsg("%4d PUSHNR %lld", current,
3023 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003024 break;
3025 case ISN_PUSHBOOL:
3026 case ISN_PUSHSPEC:
3027 smsg("%4d PUSH %s", current,
3028 get_var_special_name(iptr->isn_arg.number));
3029 break;
3030 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003031#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003033#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 break;
3035 case ISN_PUSHS:
3036 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3037 break;
3038 case ISN_PUSHBLOB:
3039 {
3040 char_u *r;
3041 char_u numbuf[NUMBUFLEN];
3042 char_u *tofree;
3043
3044 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003045 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 vim_free(tofree);
3047 }
3048 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003049 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003050 {
3051 char *name = (char *)iptr->isn_arg.string;
3052
3053 smsg("%4d PUSHFUNC \"%s\"", current,
3054 name == NULL ? "[none]" : name);
3055 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003056 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003057 case ISN_PUSHCHANNEL:
3058#ifdef FEAT_JOB_CHANNEL
3059 {
3060 channel_T *channel = iptr->isn_arg.channel;
3061
3062 smsg("%4d PUSHCHANNEL %d", current,
3063 channel == NULL ? 0 : channel->ch_id);
3064 }
3065#endif
3066 break;
3067 case ISN_PUSHJOB:
3068#ifdef FEAT_JOB_CHANNEL
3069 {
3070 typval_T tv;
3071 char_u *name;
3072
3073 tv.v_type = VAR_JOB;
3074 tv.vval.v_job = iptr->isn_arg.job;
3075 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003076 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003077 }
3078#endif
3079 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 case ISN_PUSHEXC:
3081 smsg("%4d PUSH v:exception", current);
3082 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003083 case ISN_UNLET:
3084 smsg("%4d UNLET%s %s", current,
3085 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3086 iptr->isn_arg.unlet.ul_name);
3087 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003088 case ISN_UNLETENV:
3089 smsg("%4d UNLETENV%s $%s", current,
3090 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3091 iptr->isn_arg.unlet.ul_name);
3092 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003093 case ISN_LOCKCONST:
3094 smsg("%4d LOCKCONST", current);
3095 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003097 smsg("%4d NEWLIST size %lld", current,
3098 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099 break;
3100 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003101 smsg("%4d NEWDICT size %lld", current,
3102 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003103 break;
3104
3105 // function call
3106 case ISN_BCALL:
3107 {
3108 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3109
3110 smsg("%4d BCALL %s(argc %d)", current,
3111 internal_func_name(cbfunc->cbf_idx),
3112 cbfunc->cbf_argcount);
3113 }
3114 break;
3115 case ISN_DCALL:
3116 {
3117 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3118 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3119 + cdfunc->cdf_idx;
3120
3121 smsg("%4d DCALL %s(argc %d)", current,
3122 df->df_ufunc->uf_name_exp != NULL
3123 ? df->df_ufunc->uf_name_exp
3124 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3125 }
3126 break;
3127 case ISN_UCALL:
3128 {
3129 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3130
3131 smsg("%4d UCALL %s(argc %d)", current,
3132 cufunc->cuf_name, cufunc->cuf_argcount);
3133 }
3134 break;
3135 case ISN_PCALL:
3136 {
3137 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3138
3139 smsg("%4d PCALL%s (argc %d)", current,
3140 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3141 }
3142 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003143 case ISN_PCALL_END:
3144 smsg("%4d PCALL end", current);
3145 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003146 case ISN_RETURN:
3147 smsg("%4d RETURN", current);
3148 break;
3149 case ISN_FUNCREF:
3150 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003151 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003153 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003155 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 }
3157 break;
3158
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003159 case ISN_NEWFUNC:
3160 {
3161 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3162
3163 smsg("%4d NEWFUNC %s %s", current,
3164 newfunc->nf_lambda, newfunc->nf_global);
3165 }
3166 break;
3167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 case ISN_JUMP:
3169 {
3170 char *when = "?";
3171
3172 switch (iptr->isn_arg.jump.jump_when)
3173 {
3174 case JUMP_ALWAYS:
3175 when = "JUMP";
3176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 case JUMP_AND_KEEP_IF_TRUE:
3178 when = "JUMP_AND_KEEP_IF_TRUE";
3179 break;
3180 case JUMP_IF_FALSE:
3181 when = "JUMP_IF_FALSE";
3182 break;
3183 case JUMP_AND_KEEP_IF_FALSE:
3184 when = "JUMP_AND_KEEP_IF_FALSE";
3185 break;
3186 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003187 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 iptr->isn_arg.jump.jump_where);
3189 }
3190 break;
3191
3192 case ISN_FOR:
3193 {
3194 forloop_T *forloop = &iptr->isn_arg.forloop;
3195
3196 smsg("%4d FOR $%d -> %d", current,
3197 forloop->for_idx, forloop->for_end);
3198 }
3199 break;
3200
3201 case ISN_TRY:
3202 {
3203 try_T *try = &iptr->isn_arg.try;
3204
3205 smsg("%4d TRY catch -> %d, finally -> %d", current,
3206 try->try_catch, try->try_finally);
3207 }
3208 break;
3209 case ISN_CATCH:
3210 // TODO
3211 smsg("%4d CATCH", current);
3212 break;
3213 case ISN_ENDTRY:
3214 smsg("%4d ENDTRY", current);
3215 break;
3216 case ISN_THROW:
3217 smsg("%4d THROW", current);
3218 break;
3219
3220 // expression operations on number
3221 case ISN_OPNR:
3222 case ISN_OPFLOAT:
3223 case ISN_OPANY:
3224 {
3225 char *what;
3226 char *ins;
3227
3228 switch (iptr->isn_arg.op.op_type)
3229 {
3230 case EXPR_MULT: what = "*"; break;
3231 case EXPR_DIV: what = "/"; break;
3232 case EXPR_REM: what = "%"; break;
3233 case EXPR_SUB: what = "-"; break;
3234 case EXPR_ADD: what = "+"; break;
3235 default: what = "???"; break;
3236 }
3237 switch (iptr->isn_type)
3238 {
3239 case ISN_OPNR: ins = "OPNR"; break;
3240 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3241 case ISN_OPANY: ins = "OPANY"; break;
3242 default: ins = "???"; break;
3243 }
3244 smsg("%4d %s %s", current, ins, what);
3245 }
3246 break;
3247
3248 case ISN_COMPAREBOOL:
3249 case ISN_COMPARESPECIAL:
3250 case ISN_COMPARENR:
3251 case ISN_COMPAREFLOAT:
3252 case ISN_COMPARESTRING:
3253 case ISN_COMPAREBLOB:
3254 case ISN_COMPARELIST:
3255 case ISN_COMPAREDICT:
3256 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 case ISN_COMPAREANY:
3258 {
3259 char *p;
3260 char buf[10];
3261 char *type;
3262
3263 switch (iptr->isn_arg.op.op_type)
3264 {
3265 case EXPR_EQUAL: p = "=="; break;
3266 case EXPR_NEQUAL: p = "!="; break;
3267 case EXPR_GREATER: p = ">"; break;
3268 case EXPR_GEQUAL: p = ">="; break;
3269 case EXPR_SMALLER: p = "<"; break;
3270 case EXPR_SEQUAL: p = "<="; break;
3271 case EXPR_MATCH: p = "=~"; break;
3272 case EXPR_IS: p = "is"; break;
3273 case EXPR_ISNOT: p = "isnot"; break;
3274 case EXPR_NOMATCH: p = "!~"; break;
3275 default: p = "???"; break;
3276 }
3277 STRCPY(buf, p);
3278 if (iptr->isn_arg.op.op_ic == TRUE)
3279 strcat(buf, "?");
3280 switch(iptr->isn_type)
3281 {
3282 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3283 case ISN_COMPARESPECIAL:
3284 type = "COMPARESPECIAL"; break;
3285 case ISN_COMPARENR: type = "COMPARENR"; break;
3286 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3287 case ISN_COMPARESTRING:
3288 type = "COMPARESTRING"; break;
3289 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3290 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3291 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3292 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3294 default: type = "???"; break;
3295 }
3296
3297 smsg("%4d %s %s", current, type, buf);
3298 }
3299 break;
3300
3301 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3302 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3303
3304 // expression operations
3305 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003306 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003307 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003308 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003309 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003310 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3311 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003312 case ISN_SLICE: smsg("%4d SLICE %lld",
3313 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003314 case ISN_GETITEM: smsg("%4d ITEM %lld",
3315 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003316 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3317 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 iptr->isn_arg.string); break;
3319 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3320
3321 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003322 case ISN_CHECKTYPE:
3323 {
3324 char *tofree;
3325
3326 smsg("%4d CHECKTYPE %s stack[%d]", current,
3327 type_name(iptr->isn_arg.type.ct_type, &tofree),
3328 iptr->isn_arg.type.ct_off);
3329 vim_free(tofree);
3330 break;
3331 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003332 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3333 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3334 iptr->isn_arg.checklen.cl_min_len);
3335 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003336 case ISN_2BOOL: if (iptr->isn_arg.number)
3337 smsg("%4d INVERT (!val)", current);
3338 else
3339 smsg("%4d 2BOOL (!!val)", current);
3340 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003341 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3342 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003343 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003344 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3345 (long long)(iptr->isn_arg.number));
3346 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003347 case ISN_PUT:
3348 smsg("%4d PUT %c %ld", current, iptr->isn_arg.put.put_regname,
3349 (long)iptr->isn_arg.put.put_lnum);
3350 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351
Bram Moolenaar389df252020-07-09 21:20:47 +02003352 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3353 iptr->isn_arg.shuffle.shfl_item,
3354 iptr->isn_arg.shuffle.shfl_up);
3355 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 case ISN_DROP: smsg("%4d DROP", current); break;
3357 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003358
3359 out_flush(); // output one line at a time
3360 ui_breakcheck();
3361 if (got_int)
3362 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003364}
3365
3366/*
3367 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3368 * list, etc. Mostly like what JavaScript does, except that empty list and
3369 * empty dictionary are FALSE.
3370 */
3371 int
3372tv2bool(typval_T *tv)
3373{
3374 switch (tv->v_type)
3375 {
3376 case VAR_NUMBER:
3377 return tv->vval.v_number != 0;
3378 case VAR_FLOAT:
3379#ifdef FEAT_FLOAT
3380 return tv->vval.v_float != 0.0;
3381#else
3382 break;
3383#endif
3384 case VAR_PARTIAL:
3385 return tv->vval.v_partial != NULL;
3386 case VAR_FUNC:
3387 case VAR_STRING:
3388 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3389 case VAR_LIST:
3390 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3391 case VAR_DICT:
3392 return tv->vval.v_dict != NULL
3393 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3394 case VAR_BOOL:
3395 case VAR_SPECIAL:
3396 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3397 case VAR_JOB:
3398#ifdef FEAT_JOB_CHANNEL
3399 return tv->vval.v_job != NULL;
3400#else
3401 break;
3402#endif
3403 case VAR_CHANNEL:
3404#ifdef FEAT_JOB_CHANNEL
3405 return tv->vval.v_channel != NULL;
3406#else
3407 break;
3408#endif
3409 case VAR_BLOB:
3410 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3411 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003412 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 case VAR_VOID:
3414 break;
3415 }
3416 return FALSE;
3417}
3418
3419/*
3420 * If "tv" is a string give an error and return FAIL.
3421 */
3422 int
3423check_not_string(typval_T *tv)
3424{
3425 if (tv->v_type == VAR_STRING)
3426 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003427 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 clear_tv(tv);
3429 return FAIL;
3430 }
3431 return OK;
3432}
3433
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434
3435#endif // FEAT_EVAL