blob: 02a895ecb7952238f8f65fc252483ac23f06622a [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
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020073#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 +010074
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020078 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020079}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200209 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200210 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200211 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200212 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 return FAIL;
214 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200215 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
216 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217 return FAIL;
218
Bram Moolenaarfe270812020-04-11 22:31:27 +0200219 // Move the vararg-list to below the missing optional arguments.
220 if (vararg_count > 0 && arg_to_add > 0)
221 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100222
223 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200226 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227
228 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
230 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200231 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
232 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233
234 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200235 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200237 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
238 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100239
240 // Set execution state to the start of the called function.
241 ectx->ec_dfunc_idx = cdf_idx;
242 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200243 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
244 if (entry != NULL)
245 {
246 // Set the script context to the script where the function was defined.
247 // TODO: save more than the SID?
248 entry->es_save_sid = current_sctx.sc_sid;
249 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
250 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100251
252 // Decide where to start execution, handles optional arguments.
253 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254
255 return OK;
256}
257
258// Get pointer to item in the stack.
259#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
260
261/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200262 * Used when returning from a function: Check if any closure is still
263 * referenced. If so then move the arguments and variables to a separate piece
264 * of stack to be used when the closure is called.
265 * When "free_arguments" is TRUE the arguments are to be freed.
266 * Returns FAIL when out of memory.
267 */
268 static int
269handle_closure_in_use(ectx_T *ectx, int free_arguments)
270{
271 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
272 + ectx->ec_dfunc_idx;
273 int argcount = ufunc_argcount(dfunc->df_ufunc);
274 int top = ectx->ec_frame_idx - argcount;
275 int idx;
276 typval_T *tv;
277 int closure_in_use = FALSE;
278
279 // Check if any created closure is still in use.
280 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
281 {
282 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
283 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
285 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200286 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200287 int refcount = tv->vval.v_partial->pt_refcount;
288 int i;
289
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200290 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200291 // unreferenced on return.
292 for (i = 0; i < dfunc->df_varcount; ++i)
293 {
294 typval_T *stv = STACK_TV(ectx->ec_frame_idx
295 + STACK_FRAME_SIZE + i);
296 if (stv->v_type == VAR_PARTIAL
297 && tv->vval.v_partial == stv->vval.v_partial)
298 --refcount;
299 }
300 if (refcount > 1)
301 {
302 closure_in_use = TRUE;
303 break;
304 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200305 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200306 }
307
308 if (closure_in_use)
309 {
310 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
311 typval_T *stack;
312
313 // A closure is using the arguments and/or local variables.
314 // Move them to the called function.
315 if (funcstack == NULL)
316 return FAIL;
317 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
318 + dfunc->df_varcount;
319 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
320 funcstack->fs_ga.ga_data = stack;
321 if (stack == NULL)
322 {
323 vim_free(funcstack);
324 return FAIL;
325 }
326
327 // Move or copy the arguments.
328 for (idx = 0; idx < argcount; ++idx)
329 {
330 tv = STACK_TV(top + idx);
331 if (free_arguments)
332 {
333 *(stack + idx) = *tv;
334 tv->v_type = VAR_UNKNOWN;
335 }
336 else
337 copy_tv(tv, stack + idx);
338 }
339 // Move the local variables.
340 for (idx = 0; idx < dfunc->df_varcount; ++idx)
341 {
342 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200343
344 // Do not copy a partial created for a local function.
345 // TODO: this won't work if the closure actually uses it. But when
346 // keeping it it gets complicated: it will create a reference cycle
347 // inside the partial, thus needs special handling for garbage
348 // collection.
349 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
350 {
351 int i;
352 typval_T *ctv;
353
354 for (i = 0; i < dfunc->df_closure_count; ++i)
355 {
356 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
357 + dfunc->df_varcount + i);
358 if (tv->vval.v_partial == ctv->vval.v_partial)
359 break;
360 }
361 if (i < dfunc->df_closure_count)
362 {
363 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
364 VAR_UNKNOWN;
365 continue;
366 }
367 }
368
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
370 tv->v_type = VAR_UNKNOWN;
371 }
372
373 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
374 {
375 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
376 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200377 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200378 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200379 partial_T *partial = tv->vval.v_partial;
380
381 if (partial->pt_refcount > 1)
382 {
383 ++funcstack->fs_refcount;
384 partial->pt_funcstack = funcstack;
385 partial->pt_ectx_stack = &funcstack->fs_ga;
386 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
387 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 }
389 }
390 }
391
392 return OK;
393}
394
395/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396 * Return from the current function.
397 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200398 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100399func_return(ectx_T *ectx)
400{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100401 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200402 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
403 + ectx->ec_dfunc_idx;
404 int argcount = ufunc_argcount(dfunc->df_ufunc);
405 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100407
408 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200409 entry = estack_pop();
410 if (entry != NULL)
411 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100412
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200413 if (handle_closure_in_use(ectx, TRUE) == FAIL)
414 return FAIL;
415
416 // Clear the arguments.
417 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
418 clear_tv(STACK_TV(idx));
419
420 // Clear local variables and temp values, but not the return value.
421 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100423 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100425 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200426 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
427 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
428 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
430 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100431
432 // Reset the stack to the position before the call, move the return value
433 // to the top of the stack.
434 idx = ectx->ec_stack.ga_len - 1;
435 ectx->ec_stack.ga_len = top + 1;
436 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437
438 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100439}
440
441#undef STACK_TV
442
443/*
444 * Prepare arguments and rettv for calling a builtin or user function.
445 */
446 static int
447call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
448{
449 int idx;
450 typval_T *tv;
451
452 // Move arguments from bottom of the stack to argvars[] and add terminator.
453 for (idx = 0; idx < argcount; ++idx)
454 argvars[idx] = *STACK_TV_BOT(idx - argcount);
455 argvars[argcount].v_type = VAR_UNKNOWN;
456
457 // Result replaces the arguments on the stack.
458 if (argcount > 0)
459 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200460 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 return FAIL;
462 else
463 ++ectx->ec_stack.ga_len;
464
465 // Default return value is zero.
466 tv = STACK_TV_BOT(-1);
467 tv->v_type = VAR_NUMBER;
468 tv->vval.v_number = 0;
469
470 return OK;
471}
472
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200473// Ugly global to avoid passing the execution context around through many
474// layers.
475static ectx_T *current_ectx = NULL;
476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477/*
478 * Call a builtin function by index.
479 */
480 static int
481call_bfunc(int func_idx, int argcount, ectx_T *ectx)
482{
483 typval_T argvars[MAX_FUNC_ARGS];
484 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200485 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 if (call_prepare(argcount, argvars, ectx) == FAIL)
489 return FAIL;
490
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200491 // Call the builtin function. Set "current_ectx" so that when it
492 // recursively invokes call_def_function() a closure context can be set.
493 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200495 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496
497 // Clear the arguments.
498 for (idx = 0; idx < argcount; ++idx)
499 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200500
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200501 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200502 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503 return OK;
504}
505
506/*
507 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509 */
510 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100511call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512{
513 typval_T argvars[MAX_FUNC_ARGS];
514 funcexe_T funcexe;
515 int error;
516 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200517 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200520 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
521 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200522 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100523 {
524 // The function has been compiled, can call it quickly. For a function
525 // that was defined later: we can call it directly next time.
526 if (iptr != NULL)
527 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100528 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100529 iptr->isn_type = ISN_DCALL;
530 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
531 iptr->isn_arg.dfunc.cdf_argcount = argcount;
532 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100534 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535
536 if (call_prepare(argcount, argvars, ectx) == FAIL)
537 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200538 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100539 funcexe.evaluate = TRUE;
540
541 // Call the user function. Result goes in last position on the stack.
542 // TODO: add selfdict if there is one
543 error = call_user_func_check(ufunc, argcount, argvars,
544 STACK_TV_BOT(-1), &funcexe, NULL);
545
546 // Clear the arguments.
547 for (idx = 0; idx < argcount; ++idx)
548 clear_tv(&argvars[idx]);
549
550 if (error != FCERR_NONE)
551 {
552 user_func_error(error, ufunc->uf_name);
553 return FAIL;
554 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200555 if (called_emsg > called_emsg_before)
556 // Error other than from calling the function itself.
557 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 return OK;
559}
560
561/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200562 * Return TRUE if an error was given or CTRL-C was pressed.
563 */
564 static int
565vim9_aborting(int prev_called_emsg)
566{
567 return called_emsg > prev_called_emsg || got_int || did_throw;
568}
569
570/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Execute a function by "name".
572 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100573 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 * Returns FAIL if not found without an error message.
575 */
576 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100577call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578{
579 ufunc_T *ufunc;
580
581 if (builtin_function(name, -1))
582 {
583 int func_idx = find_internal_func(name);
584
585 if (func_idx < 0)
586 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200587 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return FAIL;
589 return call_bfunc(func_idx, argcount, ectx);
590 }
591
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200592 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200593
594 if (ufunc == NULL)
595 {
596 int called_emsg_before = called_emsg;
597
598 if (script_autoload(name, TRUE))
599 // loaded a package, search for the function again
600 ufunc = find_func(name, FALSE, NULL);
601 if (vim9_aborting(called_emsg_before))
602 return FAIL; // bail out if loading the script caused an error
603 }
604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100606 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 return FAIL;
609}
610
611 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200612call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200614 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200615 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616 int called_emsg_before = called_emsg;
617
618 if (tv->v_type == VAR_PARTIAL)
619 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200620 partial_T *pt = tv->vval.v_partial;
621 int i;
622
623 if (pt->pt_argc > 0)
624 {
625 // Make space for arguments from the partial, shift the "argcount"
626 // arguments up.
627 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
628 return FAIL;
629 for (i = 1; i <= argcount; ++i)
630 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
631 ectx->ec_stack.ga_len += pt->pt_argc;
632 argcount += pt->pt_argc;
633
634 // copy the arguments from the partial onto the stack
635 for (i = 0; i < pt->pt_argc; ++i)
636 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
637 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638
639 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200640 {
641 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
642
643 // closure may need the function context where it was defined
644 ectx->ec_outer_stack = pt->pt_ectx_stack;
645 ectx->ec_outer_frame = pt->pt_ectx_frame;
646
647 return ret;
648 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 name = pt->pt_name;
650 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200651 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200653 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 {
655 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200656 semsg(_(e_unknownfunc),
657 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100658 return FAIL;
659 }
660 return OK;
661}
662
663/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100664 * Store "tv" in variable "name".
665 * This is for s: and g: variables.
666 */
667 static void
668store_var(char_u *name, typval_T *tv)
669{
670 funccal_entry_T entry;
671
672 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200673 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100674 restore_funccal();
675}
676
Bram Moolenaard3aac292020-04-19 14:32:17 +0200677
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100678/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 * Execute a function by "name".
680 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 */
683 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100684call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685{
Bram Moolenaared677f52020-08-12 16:38:10 +0200686 int called_emsg_before = called_emsg;
687 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688
Bram Moolenaared677f52020-08-12 16:38:10 +0200689 res = call_by_name(name, argcount, ectx, iptr);
690 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200692 dictitem_T *v;
693
694 v = find_var(name, NULL, FALSE);
695 if (v == NULL)
696 {
697 semsg(_(e_unknownfunc), name);
698 return FAIL;
699 }
700 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
701 {
702 semsg(_(e_unknownfunc), name);
703 return FAIL;
704 }
705 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200707 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708}
709
710/*
711 * Call a "def" function from old Vim script.
712 * Return OK or FAIL.
713 */
714 int
715call_def_function(
716 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200717 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200719 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 typval_T *rettv) // return value
721{
722 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200723 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200724 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100725 typval_T *tv;
726 int idx;
727 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100728 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200729 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200730 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200731 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100732
733// Get pointer to item in the stack.
734#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
735
736// Get pointer to item at the bottom of the stack, -1 is the bottom.
737#undef STACK_TV_BOT
738#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
739
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200740// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741#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 +0100742
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200743// Like STACK_TV_VAR but use the outer scope
744#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
745
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200746 if (ufunc->uf_def_status == UF_NOT_COMPILED
747 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200748 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200749 {
750 if (called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200751 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200752 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200753 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200754 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200755
Bram Moolenaar09689a02020-05-09 22:50:08 +0200756 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200757 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200758 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
759 + ufunc->uf_dfunc_idx;
760 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200761 {
762 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200763 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200764 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200765 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200767 CLEAR_FIELD(ectx);
768 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
769 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
770 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
771 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
773
774 // Put arguments on the stack.
775 for (idx = 0; idx < argc; ++idx)
776 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200777 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200778 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
779 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200780 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781 copy_tv(&argv[idx], STACK_TV_BOT(0));
782 ++ectx.ec_stack.ga_len;
783 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200784
785 // Turn varargs into a list. Empty list if no args.
786 if (ufunc->uf_va_name != NULL)
787 {
788 int vararg_count = argc - ufunc->uf_args.ga_len;
789
790 if (vararg_count < 0)
791 vararg_count = 0;
792 else
793 argc -= vararg_count;
794 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200795 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200796
797 // Check the type of the list items.
798 tv = STACK_TV_BOT(-1);
799 if (ufunc->uf_va_type != NULL
800 && ufunc->uf_va_type->tt_member != &t_any
801 && tv->vval.v_list != NULL)
802 {
803 type_T *expected = ufunc->uf_va_type->tt_member;
804 listitem_T *li = tv->vval.v_list->lv_first;
805
806 for (idx = 0; idx < vararg_count; ++idx)
807 {
808 if (check_typval_type(expected, &li->li_tv) == FAIL)
809 goto failed_early;
810 li = li->li_next;
811 }
812 }
813
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 if (defcount > 0)
815 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200816 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200817 --ectx.ec_stack.ga_len;
818 }
819
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100820 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200821 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100822 if (defcount > 0)
823 for (idx = 0; idx < defcount; ++idx)
824 {
825 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
826 ++ectx.ec_stack.ga_len;
827 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200828 if (ufunc->uf_va_name != NULL)
829 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830
831 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
833 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200835 if (partial != NULL)
836 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200837 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
838 {
839 // TODO: is this always the right way?
840 ectx.ec_outer_stack = &current_ectx->ec_stack;
841 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
842 }
843 else
844 {
845 ectx.ec_outer_stack = partial->pt_ectx_stack;
846 ectx.ec_outer_frame = partial->pt_ectx_frame;
847 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200848 }
849
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100850 // dummy frame entries
851 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
852 {
853 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
854 ++ectx.ec_stack.ga_len;
855 }
856
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200857 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
860 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200861 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200863 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200864 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200865 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200866
867 ectx.ec_instr = dfunc->df_instr;
868 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100869
Bram Moolenaara26b9702020-04-18 19:53:28 +0200870 // Commands behave like vim9script.
871 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
872
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100873 // Decide where to start execution, handles optional arguments.
874 init_instr_idx(ufunc, argc, &ectx);
875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 for (;;)
877 {
878 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100879
Bram Moolenaar270d0382020-05-15 21:42:53 +0200880 if (++breakcheck_count >= 100)
881 {
882 line_breakcheck();
883 breakcheck_count = 0;
884 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100885 if (got_int)
886 {
887 // Turn CTRL-C into an exception.
888 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100889 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100890 goto failed;
891 did_throw = TRUE;
892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893
Bram Moolenaara26b9702020-04-18 19:53:28 +0200894 if (did_emsg && msg_list != NULL && *msg_list != NULL)
895 {
896 // Turn an error message into an exception.
897 did_emsg = FALSE;
898 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
899 goto failed;
900 did_throw = TRUE;
901 *msg_list = NULL;
902 }
903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 if (did_throw && !ectx.ec_in_catch)
905 {
906 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100907 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908
909 // An exception jumps to the first catch, finally, or returns from
910 // the current function.
911 if (trystack->ga_len > 0)
912 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200913 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 {
915 // jump to ":catch" or ":finally"
916 ectx.ec_in_catch = TRUE;
917 ectx.ec_iidx = trycmd->tcd_catch_idx;
918 }
919 else
920 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200921 // Not inside try or need to return from current functions.
922 // Push a dummy return value.
923 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
924 goto failed;
925 tv = STACK_TV_BOT(0);
926 tv->v_type = VAR_NUMBER;
927 tv->vval.v_number = 0;
928 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200929 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200931 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100932 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200933 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
934 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 goto done;
936 }
937
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200938 if (func_return(&ectx) == FAIL)
939 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 }
941 continue;
942 }
943
944 iptr = &ectx.ec_instr[ectx.ec_iidx++];
945 switch (iptr->isn_type)
946 {
947 // execute Ex command line
948 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200949 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 do_cmdline_cmd(iptr->isn_arg.string);
951 break;
952
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200953 // execute Ex command from pieces on the stack
954 case ISN_EXECCONCAT:
955 {
956 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200957 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200958 int pass;
959 int i;
960 char_u *cmd = NULL;
961 char_u *str;
962
963 for (pass = 1; pass <= 2; ++pass)
964 {
965 for (i = 0; i < count; ++i)
966 {
967 tv = STACK_TV_BOT(i - count);
968 str = tv->vval.v_string;
969 if (str != NULL && *str != NUL)
970 {
971 if (pass == 2)
972 STRCPY(cmd + len, str);
973 len += STRLEN(str);
974 }
975 if (pass == 2)
976 clear_tv(tv);
977 }
978 if (pass == 1)
979 {
980 cmd = alloc(len + 1);
981 if (cmd == NULL)
982 goto failed;
983 len = 0;
984 }
985 }
986
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200987 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200988 do_cmdline_cmd(cmd);
989 vim_free(cmd);
990 }
991 break;
992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 // execute :echo {string} ...
994 case ISN_ECHO:
995 {
996 int count = iptr->isn_arg.echo.echo_count;
997 int atstart = TRUE;
998 int needclr = TRUE;
999
1000 for (idx = 0; idx < count; ++idx)
1001 {
1002 tv = STACK_TV_BOT(idx - count);
1003 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1004 &atstart, &needclr);
1005 clear_tv(tv);
1006 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001007 if (needclr)
1008 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 ectx.ec_stack.ga_len -= count;
1010 }
1011 break;
1012
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001013 // :execute {string} ...
1014 // :echomsg {string} ...
1015 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001016 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001017 case ISN_ECHOMSG:
1018 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001019 {
1020 int count = iptr->isn_arg.number;
1021 garray_T ga;
1022 char_u buf[NUMBUFLEN];
1023 char_u *p;
1024 int len;
1025 int failed = FALSE;
1026
1027 ga_init2(&ga, 1, 80);
1028 for (idx = 0; idx < count; ++idx)
1029 {
1030 tv = STACK_TV_BOT(idx - count);
1031 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1032 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001033 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001034 emsg(_(e_inval_string));
1035 break;
1036 }
1037 else
1038 p = tv_get_string_buf(tv, buf);
1039
1040 len = (int)STRLEN(p);
1041 if (ga_grow(&ga, len + 2) == FAIL)
1042 failed = TRUE;
1043 else
1044 {
1045 if (ga.ga_len > 0)
1046 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1047 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1048 ga.ga_len += len;
1049 }
1050 clear_tv(tv);
1051 }
1052 ectx.ec_stack.ga_len -= count;
1053
1054 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001055 {
1056 if (iptr->isn_type == ISN_EXECUTE)
1057 do_cmdline_cmd((char_u *)ga.ga_data);
1058 else
1059 {
1060 msg_sb_eol();
1061 if (iptr->isn_type == ISN_ECHOMSG)
1062 {
1063 msg_attr(ga.ga_data, echo_attr);
1064 out_flush();
1065 }
1066 else
1067 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001068 SOURCING_LNUM = iptr->isn_lnum;
1069 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001070 }
1071 }
1072 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001073 ga_clear(&ga);
1074 }
1075 break;
1076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 // load local variable or argument
1078 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001079 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 goto failed;
1081 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1082 ++ectx.ec_stack.ga_len;
1083 break;
1084
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001085 // load variable or argument from outer scope
1086 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001087 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001088 goto failed;
1089 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1090 STACK_TV_BOT(0));
1091 ++ectx.ec_stack.ga_len;
1092 break;
1093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 // load v: variable
1095 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001096 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 goto failed;
1098 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1099 ++ectx.ec_stack.ga_len;
1100 break;
1101
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001102 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 case ISN_LOADSCRIPT:
1104 {
1105 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001106 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 svar_T *sv;
1108
1109 sv = ((svar_T *)si->sn_var_vals.ga_data)
1110 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001111 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 goto failed;
1113 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1114 ++ectx.ec_stack.ga_len;
1115 }
1116 break;
1117
1118 // load s: variable in old script
1119 case ISN_LOADS:
1120 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001121 hashtab_T *ht = &SCRIPT_VARS(
1122 iptr->isn_arg.loadstore.ls_sid);
1123 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001125
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 if (di == NULL)
1127 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001128 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001129 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001130 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 }
1132 else
1133 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001134 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 goto failed;
1136 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1137 ++ectx.ec_stack.ga_len;
1138 }
1139 }
1140 break;
1141
Bram Moolenaard3aac292020-04-19 14:32:17 +02001142 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001143 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001144 case ISN_LOADB:
1145 case ISN_LOADW:
1146 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001148 dictitem_T *di = NULL;
1149 hashtab_T *ht = NULL;
1150 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001151
Bram Moolenaard3aac292020-04-19 14:32:17 +02001152 switch (iptr->isn_type)
1153 {
1154 case ISN_LOADG:
1155 ht = get_globvar_ht();
1156 namespace = 'g';
1157 break;
1158 case ISN_LOADB:
1159 ht = &curbuf->b_vars->dv_hashtab;
1160 namespace = 'b';
1161 break;
1162 case ISN_LOADW:
1163 ht = &curwin->w_vars->dv_hashtab;
1164 namespace = 'w';
1165 break;
1166 case ISN_LOADT:
1167 ht = &curtab->tp_vars->dv_hashtab;
1168 namespace = 't';
1169 break;
1170 default: // Cannot reach here
1171 goto failed;
1172 }
1173 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 if (di == NULL)
1176 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001177 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001178 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001179 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001180 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 }
1182 else
1183 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001184 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 goto failed;
1186 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1187 ++ectx.ec_stack.ga_len;
1188 }
1189 }
1190 break;
1191
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001192 // load g:/b:/w:/t: namespace
1193 case ISN_LOADGDICT:
1194 case ISN_LOADBDICT:
1195 case ISN_LOADWDICT:
1196 case ISN_LOADTDICT:
1197 {
1198 dict_T *d = NULL;
1199
1200 switch (iptr->isn_type)
1201 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001202 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1203 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1204 case ISN_LOADWDICT: d = curwin->w_vars; break;
1205 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001206 default: // Cannot reach here
1207 goto failed;
1208 }
1209 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1210 goto failed;
1211 tv = STACK_TV_BOT(0);
1212 tv->v_type = VAR_DICT;
1213 tv->v_lock = 0;
1214 tv->vval.v_dict = d;
1215 ++ectx.ec_stack.ga_len;
1216 }
1217 break;
1218
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 // load &option
1220 case ISN_LOADOPT:
1221 {
1222 typval_T optval;
1223 char_u *name = iptr->isn_arg.string;
1224
Bram Moolenaara8c17702020-04-01 21:17:24 +02001225 // This is not expected to fail, name is checked during
1226 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001227 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001229 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001230 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001231 *STACK_TV_BOT(0) = optval;
1232 ++ectx.ec_stack.ga_len;
1233 }
1234 break;
1235
1236 // load $ENV
1237 case ISN_LOADENV:
1238 {
1239 typval_T optval;
1240 char_u *name = iptr->isn_arg.string;
1241
Bram Moolenaar270d0382020-05-15 21:42:53 +02001242 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001244 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001245 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 *STACK_TV_BOT(0) = optval;
1247 ++ectx.ec_stack.ga_len;
1248 }
1249 break;
1250
1251 // load @register
1252 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001253 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001254 goto failed;
1255 tv = STACK_TV_BOT(0);
1256 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001257 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 tv->vval.v_string = get_reg_contents(
1259 iptr->isn_arg.number, GREG_EXPR_SRC);
1260 ++ectx.ec_stack.ga_len;
1261 break;
1262
1263 // store local variable
1264 case ISN_STORE:
1265 --ectx.ec_stack.ga_len;
1266 tv = STACK_TV_VAR(iptr->isn_arg.number);
1267 clear_tv(tv);
1268 *tv = *STACK_TV_BOT(0);
1269 break;
1270
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001271 // store variable or argument in outer scope
1272 case ISN_STOREOUTER:
1273 --ectx.ec_stack.ga_len;
1274 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1275 clear_tv(tv);
1276 *tv = *STACK_TV_BOT(0);
1277 break;
1278
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001279 // store s: variable in old script
1280 case ISN_STORES:
1281 {
1282 hashtab_T *ht = &SCRIPT_VARS(
1283 iptr->isn_arg.loadstore.ls_sid);
1284 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001285 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001286
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001288 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001289 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001290 else
1291 {
1292 clear_tv(&di->di_tv);
1293 di->di_tv = *STACK_TV_BOT(0);
1294 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001295 }
1296 break;
1297
1298 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 case ISN_STORESCRIPT:
1300 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001301 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001302 iptr->isn_arg.script.script_sid);
1303 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1304 + iptr->isn_arg.script.script_idx;
1305
1306 --ectx.ec_stack.ga_len;
1307 clear_tv(sv->sv_tv);
1308 *sv->sv_tv = *STACK_TV_BOT(0);
1309 }
1310 break;
1311
1312 // store option
1313 case ISN_STOREOPT:
1314 {
1315 long n = 0;
1316 char_u *s = NULL;
1317 char *msg;
1318
1319 --ectx.ec_stack.ga_len;
1320 tv = STACK_TV_BOT(0);
1321 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001322 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001324 if (s == NULL)
1325 s = (char_u *)"";
1326 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001328 // must be VAR_NUMBER, CHECKTYPE makes sure
1329 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1331 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001332 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 if (msg != NULL)
1334 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001335 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001337 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 }
1340 break;
1341
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001342 // store $ENV
1343 case ISN_STOREENV:
1344 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001345 tv = STACK_TV_BOT(0);
1346 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1347 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001348 break;
1349
1350 // store @r
1351 case ISN_STOREREG:
1352 {
1353 int reg = iptr->isn_arg.number;
1354
1355 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001356 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001358 tv_get_string(tv), -1, FALSE);
1359 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001360 }
1361 break;
1362
1363 // store v: variable
1364 case ISN_STOREV:
1365 --ectx.ec_stack.ga_len;
1366 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1367 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001368 // should not happen, type is checked when compiling
1369 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001370 break;
1371
Bram Moolenaard3aac292020-04-19 14:32:17 +02001372 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001374 case ISN_STOREB:
1375 case ISN_STOREW:
1376 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 {
1378 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001379 hashtab_T *ht;
1380 switch (iptr->isn_type)
1381 {
1382 case ISN_STOREG:
1383 ht = get_globvar_ht();
1384 break;
1385 case ISN_STOREB:
1386 ht = &curbuf->b_vars->dv_hashtab;
1387 break;
1388 case ISN_STOREW:
1389 ht = &curwin->w_vars->dv_hashtab;
1390 break;
1391 case ISN_STORET:
1392 ht = &curtab->tp_vars->dv_hashtab;
1393 break;
1394 default: // Cannot reach here
1395 goto failed;
1396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397
1398 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001399 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001401 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 else
1403 {
1404 clear_tv(&di->di_tv);
1405 di->di_tv = *STACK_TV_BOT(0);
1406 }
1407 }
1408 break;
1409
1410 // store number in local variable
1411 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001412 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 clear_tv(tv);
1414 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001415 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 break;
1417
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001418 // store value in list variable
1419 case ISN_STORELIST:
1420 {
1421 typval_T *tv_idx = STACK_TV_BOT(-2);
1422 varnumber_T lidx = tv_idx->vval.v_number;
1423 typval_T *tv_list = STACK_TV_BOT(-1);
1424 list_T *list = tv_list->vval.v_list;
1425
1426 if (lidx < 0 && list->lv_len + lidx >= 0)
1427 // negative index is relative to the end
1428 lidx = list->lv_len + lidx;
1429 if (lidx < 0 || lidx > list->lv_len)
1430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001432 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001433 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001434 }
1435 tv = STACK_TV_BOT(-3);
1436 if (lidx < list->lv_len)
1437 {
1438 listitem_T *li = list_find(list, lidx);
1439
1440 // overwrite existing list item
1441 clear_tv(&li->li_tv);
1442 li->li_tv = *tv;
1443 }
1444 else
1445 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001446 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001447 if (list_append_tv(list, tv) == FAIL)
1448 goto failed;
1449 clear_tv(tv);
1450 }
1451 clear_tv(tv_idx);
1452 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001453 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001454 }
1455 break;
1456
1457 // store value in dict variable
1458 case ISN_STOREDICT:
1459 {
1460 typval_T *tv_key = STACK_TV_BOT(-2);
1461 char_u *key = tv_key->vval.v_string;
1462 typval_T *tv_dict = STACK_TV_BOT(-1);
1463 dict_T *dict = tv_dict->vval.v_dict;
1464 dictitem_T *di;
1465
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001466 if (dict == NULL)
1467 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001468 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001469 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001470 goto on_error;
1471 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001472 if (key == NULL)
1473 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001474 tv = STACK_TV_BOT(-3);
1475 di = dict_find(dict, key, -1);
1476 if (di != NULL)
1477 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001478 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001479 clear_tv(&di->di_tv);
1480 di->di_tv = *tv;
1481 }
1482 else
1483 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001484 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001485 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1486 goto failed;
1487 clear_tv(tv);
1488 }
1489 clear_tv(tv_key);
1490 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001491 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001492 }
1493 break;
1494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 // push constant
1496 case ISN_PUSHNR:
1497 case ISN_PUSHBOOL:
1498 case ISN_PUSHSPEC:
1499 case ISN_PUSHF:
1500 case ISN_PUSHS:
1501 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001502 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001503 case ISN_PUSHCHANNEL:
1504 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001505 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 goto failed;
1507 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001508 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 ++ectx.ec_stack.ga_len;
1510 switch (iptr->isn_type)
1511 {
1512 case ISN_PUSHNR:
1513 tv->v_type = VAR_NUMBER;
1514 tv->vval.v_number = iptr->isn_arg.number;
1515 break;
1516 case ISN_PUSHBOOL:
1517 tv->v_type = VAR_BOOL;
1518 tv->vval.v_number = iptr->isn_arg.number;
1519 break;
1520 case ISN_PUSHSPEC:
1521 tv->v_type = VAR_SPECIAL;
1522 tv->vval.v_number = iptr->isn_arg.number;
1523 break;
1524#ifdef FEAT_FLOAT
1525 case ISN_PUSHF:
1526 tv->v_type = VAR_FLOAT;
1527 tv->vval.v_float = iptr->isn_arg.fnumber;
1528 break;
1529#endif
1530 case ISN_PUSHBLOB:
1531 blob_copy(iptr->isn_arg.blob, tv);
1532 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001533 case ISN_PUSHFUNC:
1534 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001535 if (iptr->isn_arg.string == NULL)
1536 tv->vval.v_string = NULL;
1537 else
1538 tv->vval.v_string =
1539 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001540 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001541 case ISN_PUSHCHANNEL:
1542#ifdef FEAT_JOB_CHANNEL
1543 tv->v_type = VAR_CHANNEL;
1544 tv->vval.v_channel = iptr->isn_arg.channel;
1545 if (tv->vval.v_channel != NULL)
1546 ++tv->vval.v_channel->ch_refcount;
1547#endif
1548 break;
1549 case ISN_PUSHJOB:
1550#ifdef FEAT_JOB_CHANNEL
1551 tv->v_type = VAR_JOB;
1552 tv->vval.v_job = iptr->isn_arg.job;
1553 if (tv->vval.v_job != NULL)
1554 ++tv->vval.v_job->jv_refcount;
1555#endif
1556 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 default:
1558 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001559 tv->vval.v_string = vim_strsave(
1560 iptr->isn_arg.string == NULL
1561 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 }
1563 break;
1564
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001565 case ISN_UNLET:
1566 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1567 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001568 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001569 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001570 case ISN_UNLETENV:
1571 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1572 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 // create a list from items on the stack; uses a single allocation
1575 // for the list header and the items
1576 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001577 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1578 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 break;
1580
1581 // create a dict from items on the stack
1582 case ISN_NEWDICT:
1583 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001584 int count = iptr->isn_arg.number;
1585 dict_T *dict = dict_alloc();
1586 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587
1588 if (dict == NULL)
1589 goto failed;
1590 for (idx = 0; idx < count; ++idx)
1591 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001592 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001594 // check key is unique
1595 item = dict_find(dict, tv->vval.v_string, -1);
1596 if (item != NULL)
1597 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001598 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001599 semsg(_(e_duplicate_key), tv->vval.v_string);
1600 dict_unref(dict);
1601 goto on_error;
1602 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 item = dictitem_alloc(tv->vval.v_string);
1604 clear_tv(tv);
1605 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001606 {
1607 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001609 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1611 item->di_tv.v_lock = 0;
1612 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001613 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001614 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001615 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 }
1619
1620 if (count > 0)
1621 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001622 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 goto failed;
1624 else
1625 ++ectx.ec_stack.ga_len;
1626 tv = STACK_TV_BOT(-1);
1627 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001628 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 tv->vval.v_dict = dict;
1630 ++dict->dv_refcount;
1631 }
1632 break;
1633
1634 // call a :def function
1635 case ISN_DCALL:
1636 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1637 iptr->isn_arg.dfunc.cdf_argcount,
1638 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001639 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 break;
1641
1642 // call a builtin function
1643 case ISN_BCALL:
1644 SOURCING_LNUM = iptr->isn_lnum;
1645 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1646 iptr->isn_arg.bfunc.cbf_argcount,
1647 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001648 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 break;
1650
1651 // call a funcref or partial
1652 case ISN_PCALL:
1653 {
1654 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1655 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001656 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657
1658 SOURCING_LNUM = iptr->isn_lnum;
1659 if (pfunc->cpf_top)
1660 {
1661 // funcref is above the arguments
1662 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1663 }
1664 else
1665 {
1666 // Get the funcref from the stack.
1667 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001668 partial_tv = *STACK_TV_BOT(0);
1669 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 }
1671 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001672 if (tv == &partial_tv)
1673 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001675 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 }
1677 break;
1678
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001679 case ISN_PCALL_END:
1680 // PCALL finished, arguments have been consumed and replaced by
1681 // the return value. Now clear the funcref from the stack,
1682 // and move the return value in its place.
1683 --ectx.ec_stack.ga_len;
1684 clear_tv(STACK_TV_BOT(-1));
1685 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1686 break;
1687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 // call a user defined function or funcref/partial
1689 case ISN_UCALL:
1690 {
1691 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1692
1693 SOURCING_LNUM = iptr->isn_lnum;
1694 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001695 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001696 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 }
1698 break;
1699
1700 // return from a :def function call
1701 case ISN_RETURN:
1702 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001703 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001704 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001705
1706 if (trystack->ga_len > 0)
1707 trycmd = ((trycmd_T *)trystack->ga_data)
1708 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001709 if (trycmd != NULL
1710 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 && trycmd->tcd_finally_idx != 0)
1712 {
1713 // jump to ":finally"
1714 ectx.ec_iidx = trycmd->tcd_finally_idx;
1715 trycmd->tcd_return = TRUE;
1716 }
1717 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001718 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 }
1720 break;
1721
1722 // push a function reference to a compiled function
1723 case ISN_FUNCREF:
1724 {
1725 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001726 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727
1728 pt = ALLOC_CLEAR_ONE(partial_T);
1729 if (pt == NULL)
1730 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001731 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001732 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001733 vim_free(pt);
1734 goto failed;
1735 }
1736 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1737 + iptr->isn_arg.funcref.fr_func;
1738 pt->pt_func = pt_dfunc->df_ufunc;
1739 pt->pt_refcount = 1;
1740 ++pt_dfunc->df_ufunc->uf_refcount;
1741
1742 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1743 {
1744 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1745 + ectx.ec_dfunc_idx;
1746
1747 // The closure needs to find arguments and local
1748 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001749 pt->pt_ectx_stack = &ectx.ec_stack;
1750 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001751
1752 // If this function returns and the closure is still
1753 // used, we need to make a copy of the context
1754 // (arguments and local variables). Store a reference
1755 // to the partial so we can handle that.
1756 ++pt->pt_refcount;
1757 tv = STACK_TV_VAR(dfunc->df_varcount
1758 + iptr->isn_arg.funcref.fr_var_idx);
1759 if (tv->v_type == VAR_PARTIAL)
1760 {
1761 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001762 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001763 emsg("Multiple closures not supported yet");
1764 goto failed;
1765 }
1766 tv->v_type = VAR_PARTIAL;
1767 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001768 }
1769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 tv = STACK_TV_BOT(0);
1771 ++ectx.ec_stack.ga_len;
1772 tv->vval.v_partial = pt;
1773 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001774 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 }
1776 break;
1777
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001778 // Create a global function from a lambda.
1779 case ISN_NEWFUNC:
1780 {
1781 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1782
1783 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1784 }
1785 break;
1786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 // jump if a condition is met
1788 case ISN_JUMP:
1789 {
1790 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1791 int jump = TRUE;
1792
1793 if (when != JUMP_ALWAYS)
1794 {
1795 tv = STACK_TV_BOT(-1);
1796 jump = tv2bool(tv);
1797 if (when == JUMP_IF_FALSE
1798 || when == JUMP_AND_KEEP_IF_FALSE)
1799 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001800 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 {
1802 // drop the value from the stack
1803 clear_tv(tv);
1804 --ectx.ec_stack.ga_len;
1805 }
1806 }
1807 if (jump)
1808 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1809 }
1810 break;
1811
1812 // top of a for loop
1813 case ISN_FOR:
1814 {
1815 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1816 typval_T *idxtv =
1817 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1818
1819 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001820 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 goto failed;
1822 if (++idxtv->vval.v_number >= list->lv_len)
1823 // past the end of the list, jump to "endfor"
1824 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1825 else if (list->lv_first == &range_list_item)
1826 {
1827 // non-materialized range() list
1828 tv = STACK_TV_BOT(0);
1829 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001830 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 tv->vval.v_number = list_find_nr(
1832 list, idxtv->vval.v_number, NULL);
1833 ++ectx.ec_stack.ga_len;
1834 }
1835 else
1836 {
1837 listitem_T *li = list_find(list, idxtv->vval.v_number);
1838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1840 ++ectx.ec_stack.ga_len;
1841 }
1842 }
1843 break;
1844
1845 // start of ":try" block
1846 case ISN_TRY:
1847 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001848 trycmd_T *trycmd = NULL;
1849
Bram Moolenaar270d0382020-05-15 21:42:53 +02001850 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 goto failed;
1852 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1853 + ectx.ec_trystack.ga_len;
1854 ++ectx.ec_trystack.ga_len;
1855 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001856 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1858 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001859 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 }
1861 break;
1862
1863 case ISN_PUSHEXC:
1864 if (current_exception == NULL)
1865 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001866 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001867 iemsg("Evaluating catch while current_exception is NULL");
1868 goto failed;
1869 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001870 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001871 goto failed;
1872 tv = STACK_TV_BOT(0);
1873 ++ectx.ec_stack.ga_len;
1874 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001875 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 tv->vval.v_string = vim_strsave(
1877 (char_u *)current_exception->value);
1878 break;
1879
1880 case ISN_CATCH:
1881 {
1882 garray_T *trystack = &ectx.ec_trystack;
1883
1884 if (trystack->ga_len > 0)
1885 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001886 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 + trystack->ga_len - 1;
1888 trycmd->tcd_caught = TRUE;
1889 }
1890 did_emsg = got_int = did_throw = FALSE;
1891 catch_exception(current_exception);
1892 }
1893 break;
1894
1895 // end of ":try" block
1896 case ISN_ENDTRY:
1897 {
1898 garray_T *trystack = &ectx.ec_trystack;
1899
1900 if (trystack->ga_len > 0)
1901 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001902 trycmd_T *trycmd = NULL;
1903
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 --trystack->ga_len;
1905 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001906 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 trycmd = ((trycmd_T *)trystack->ga_data)
1908 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001909 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 {
1911 // discard the exception
1912 if (caught_stack == current_exception)
1913 caught_stack = caught_stack->caught;
1914 discard_current_exception();
1915 }
1916
1917 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001918 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 }
1920 }
1921 break;
1922
1923 case ISN_THROW:
1924 --ectx.ec_stack.ga_len;
1925 tv = STACK_TV_BOT(0);
1926 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1927 {
1928 vim_free(tv->vval.v_string);
1929 goto failed;
1930 }
1931 did_throw = TRUE;
1932 break;
1933
1934 // compare with special values
1935 case ISN_COMPAREBOOL:
1936 case ISN_COMPARESPECIAL:
1937 {
1938 typval_T *tv1 = STACK_TV_BOT(-2);
1939 typval_T *tv2 = STACK_TV_BOT(-1);
1940 varnumber_T arg1 = tv1->vval.v_number;
1941 varnumber_T arg2 = tv2->vval.v_number;
1942 int res;
1943
1944 switch (iptr->isn_arg.op.op_type)
1945 {
1946 case EXPR_EQUAL: res = arg1 == arg2; break;
1947 case EXPR_NEQUAL: res = arg1 != arg2; break;
1948 default: res = 0; break;
1949 }
1950
1951 --ectx.ec_stack.ga_len;
1952 tv1->v_type = VAR_BOOL;
1953 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1954 }
1955 break;
1956
1957 // Operation with two number arguments
1958 case ISN_OPNR:
1959 case ISN_COMPARENR:
1960 {
1961 typval_T *tv1 = STACK_TV_BOT(-2);
1962 typval_T *tv2 = STACK_TV_BOT(-1);
1963 varnumber_T arg1 = tv1->vval.v_number;
1964 varnumber_T arg2 = tv2->vval.v_number;
1965 varnumber_T res;
1966
1967 switch (iptr->isn_arg.op.op_type)
1968 {
1969 case EXPR_MULT: res = arg1 * arg2; break;
1970 case EXPR_DIV: res = arg1 / arg2; break;
1971 case EXPR_REM: res = arg1 % arg2; break;
1972 case EXPR_SUB: res = arg1 - arg2; break;
1973 case EXPR_ADD: res = arg1 + arg2; break;
1974
1975 case EXPR_EQUAL: res = arg1 == arg2; break;
1976 case EXPR_NEQUAL: res = arg1 != arg2; break;
1977 case EXPR_GREATER: res = arg1 > arg2; break;
1978 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1979 case EXPR_SMALLER: res = arg1 < arg2; break;
1980 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1981 default: res = 0; break;
1982 }
1983
1984 --ectx.ec_stack.ga_len;
1985 if (iptr->isn_type == ISN_COMPARENR)
1986 {
1987 tv1->v_type = VAR_BOOL;
1988 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1989 }
1990 else
1991 tv1->vval.v_number = res;
1992 }
1993 break;
1994
1995 // Computation with two float arguments
1996 case ISN_OPFLOAT:
1997 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001998#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 {
2000 typval_T *tv1 = STACK_TV_BOT(-2);
2001 typval_T *tv2 = STACK_TV_BOT(-1);
2002 float_T arg1 = tv1->vval.v_float;
2003 float_T arg2 = tv2->vval.v_float;
2004 float_T res = 0;
2005 int cmp = FALSE;
2006
2007 switch (iptr->isn_arg.op.op_type)
2008 {
2009 case EXPR_MULT: res = arg1 * arg2; break;
2010 case EXPR_DIV: res = arg1 / arg2; break;
2011 case EXPR_SUB: res = arg1 - arg2; break;
2012 case EXPR_ADD: res = arg1 + arg2; break;
2013
2014 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2015 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2016 case EXPR_GREATER: cmp = arg1 > arg2; break;
2017 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2018 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2019 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2020 default: cmp = 0; break;
2021 }
2022 --ectx.ec_stack.ga_len;
2023 if (iptr->isn_type == ISN_COMPAREFLOAT)
2024 {
2025 tv1->v_type = VAR_BOOL;
2026 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2027 }
2028 else
2029 tv1->vval.v_float = res;
2030 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002031#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 break;
2033
2034 case ISN_COMPARELIST:
2035 {
2036 typval_T *tv1 = STACK_TV_BOT(-2);
2037 typval_T *tv2 = STACK_TV_BOT(-1);
2038 list_T *arg1 = tv1->vval.v_list;
2039 list_T *arg2 = tv2->vval.v_list;
2040 int cmp = FALSE;
2041 int ic = iptr->isn_arg.op.op_ic;
2042
2043 switch (iptr->isn_arg.op.op_type)
2044 {
2045 case EXPR_EQUAL: cmp =
2046 list_equal(arg1, arg2, ic, FALSE); break;
2047 case EXPR_NEQUAL: cmp =
2048 !list_equal(arg1, arg2, ic, FALSE); break;
2049 case EXPR_IS: cmp = arg1 == arg2; break;
2050 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2051 default: cmp = 0; break;
2052 }
2053 --ectx.ec_stack.ga_len;
2054 clear_tv(tv1);
2055 clear_tv(tv2);
2056 tv1->v_type = VAR_BOOL;
2057 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2058 }
2059 break;
2060
2061 case ISN_COMPAREBLOB:
2062 {
2063 typval_T *tv1 = STACK_TV_BOT(-2);
2064 typval_T *tv2 = STACK_TV_BOT(-1);
2065 blob_T *arg1 = tv1->vval.v_blob;
2066 blob_T *arg2 = tv2->vval.v_blob;
2067 int cmp = FALSE;
2068
2069 switch (iptr->isn_arg.op.op_type)
2070 {
2071 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2072 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2073 case EXPR_IS: cmp = arg1 == arg2; break;
2074 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2075 default: cmp = 0; break;
2076 }
2077 --ectx.ec_stack.ga_len;
2078 clear_tv(tv1);
2079 clear_tv(tv2);
2080 tv1->v_type = VAR_BOOL;
2081 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2082 }
2083 break;
2084
2085 // TODO: handle separately
2086 case ISN_COMPARESTRING:
2087 case ISN_COMPAREDICT:
2088 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 case ISN_COMPAREANY:
2090 {
2091 typval_T *tv1 = STACK_TV_BOT(-2);
2092 typval_T *tv2 = STACK_TV_BOT(-1);
2093 exptype_T exptype = iptr->isn_arg.op.op_type;
2094 int ic = iptr->isn_arg.op.op_ic;
2095
2096 typval_compare(tv1, tv2, exptype, ic);
2097 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 --ectx.ec_stack.ga_len;
2099 }
2100 break;
2101
2102 case ISN_ADDLIST:
2103 case ISN_ADDBLOB:
2104 {
2105 typval_T *tv1 = STACK_TV_BOT(-2);
2106 typval_T *tv2 = STACK_TV_BOT(-1);
2107
2108 if (iptr->isn_type == ISN_ADDLIST)
2109 eval_addlist(tv1, tv2);
2110 else
2111 eval_addblob(tv1, tv2);
2112 clear_tv(tv2);
2113 --ectx.ec_stack.ga_len;
2114 }
2115 break;
2116
2117 // Computation with two arguments of unknown type
2118 case ISN_OPANY:
2119 {
2120 typval_T *tv1 = STACK_TV_BOT(-2);
2121 typval_T *tv2 = STACK_TV_BOT(-1);
2122 varnumber_T n1, n2;
2123#ifdef FEAT_FLOAT
2124 float_T f1 = 0, f2 = 0;
2125#endif
2126 int error = FALSE;
2127
2128 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2129 {
2130 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2131 {
2132 eval_addlist(tv1, tv2);
2133 clear_tv(tv2);
2134 --ectx.ec_stack.ga_len;
2135 break;
2136 }
2137 else if (tv1->v_type == VAR_BLOB
2138 && tv2->v_type == VAR_BLOB)
2139 {
2140 eval_addblob(tv1, tv2);
2141 clear_tv(tv2);
2142 --ectx.ec_stack.ga_len;
2143 break;
2144 }
2145 }
2146#ifdef FEAT_FLOAT
2147 if (tv1->v_type == VAR_FLOAT)
2148 {
2149 f1 = tv1->vval.v_float;
2150 n1 = 0;
2151 }
2152 else
2153#endif
2154 {
2155 n1 = tv_get_number_chk(tv1, &error);
2156 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002157 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158#ifdef FEAT_FLOAT
2159 if (tv2->v_type == VAR_FLOAT)
2160 f1 = n1;
2161#endif
2162 }
2163#ifdef FEAT_FLOAT
2164 if (tv2->v_type == VAR_FLOAT)
2165 {
2166 f2 = tv2->vval.v_float;
2167 n2 = 0;
2168 }
2169 else
2170#endif
2171 {
2172 n2 = tv_get_number_chk(tv2, &error);
2173 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002174 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175#ifdef FEAT_FLOAT
2176 if (tv1->v_type == VAR_FLOAT)
2177 f2 = n2;
2178#endif
2179 }
2180#ifdef FEAT_FLOAT
2181 // if there is a float on either side the result is a float
2182 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2183 {
2184 switch (iptr->isn_arg.op.op_type)
2185 {
2186 case EXPR_MULT: f1 = f1 * f2; break;
2187 case EXPR_DIV: f1 = f1 / f2; break;
2188 case EXPR_SUB: f1 = f1 - f2; break;
2189 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002190 default: SOURCING_LNUM = iptr->isn_lnum;
2191 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002192 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 }
2194 clear_tv(tv1);
2195 clear_tv(tv2);
2196 tv1->v_type = VAR_FLOAT;
2197 tv1->vval.v_float = f1;
2198 --ectx.ec_stack.ga_len;
2199 }
2200 else
2201#endif
2202 {
2203 switch (iptr->isn_arg.op.op_type)
2204 {
2205 case EXPR_MULT: n1 = n1 * n2; break;
2206 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2207 case EXPR_SUB: n1 = n1 - n2; break;
2208 case EXPR_ADD: n1 = n1 + n2; break;
2209 default: n1 = num_modulus(n1, n2); break;
2210 }
2211 clear_tv(tv1);
2212 clear_tv(tv2);
2213 tv1->v_type = VAR_NUMBER;
2214 tv1->vval.v_number = n1;
2215 --ectx.ec_stack.ga_len;
2216 }
2217 }
2218 break;
2219
2220 case ISN_CONCAT:
2221 {
2222 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2223 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2224 char_u *res;
2225
2226 res = concat_str(str1, str2);
2227 clear_tv(STACK_TV_BOT(-2));
2228 clear_tv(STACK_TV_BOT(-1));
2229 --ectx.ec_stack.ga_len;
2230 STACK_TV_BOT(-1)->vval.v_string = res;
2231 }
2232 break;
2233
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002234 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002235 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002236 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002237 int is_slice = iptr->isn_type == ISN_STRSLICE;
2238 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002239 char_u *res;
2240
2241 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002242 // string slice: string is at stack-3, first index at
2243 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002244 if (is_slice)
2245 {
2246 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002247 n1 = tv->vval.v_number;
2248 }
2249
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002250 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002251 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002252
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002253 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002254 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002255 if (is_slice)
2256 // Slice: Select the characters from the string
2257 res = string_slice(tv->vval.v_string, n1, n2);
2258 else
2259 // Index: The resulting variable is a string of a
2260 // single character. If the index is too big or
2261 // negative the result is empty.
2262 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002263 vim_free(tv->vval.v_string);
2264 tv->vval.v_string = res;
2265 }
2266 break;
2267
2268 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002269 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 {
Bram Moolenaared591872020-08-15 22:14:53 +02002271 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002273 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274
2275 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002276 // list slice: list is at stack-3, indexes at stack-2 and
2277 // stack-1
2278 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 list = tv->vval.v_list;
2280
2281 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002282 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002284
2285 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 {
Bram Moolenaared591872020-08-15 22:14:53 +02002287 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002288 n1 = tv->vval.v_number;
2289 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 }
Bram Moolenaared591872020-08-15 22:14:53 +02002291
2292 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002293 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002294 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2295 == FAIL)
2296 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 }
2298 break;
2299
Bram Moolenaar9af78762020-06-16 11:34:42 +02002300 case ISN_SLICE:
2301 {
2302 list_T *list;
2303 int count = iptr->isn_arg.number;
2304
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002305 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002306 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002307 list = tv->vval.v_list;
2308
2309 // no error for short list, expect it to be checked earlier
2310 if (list != NULL && list->lv_len >= count)
2311 {
2312 list_T *newlist = list_slice(list,
2313 count, list->lv_len - 1);
2314
2315 if (newlist != NULL)
2316 {
2317 list_unref(list);
2318 tv->vval.v_list = newlist;
2319 ++newlist->lv_refcount;
2320 }
2321 }
2322 }
2323 break;
2324
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002325 case ISN_GETITEM:
2326 {
2327 listitem_T *li;
2328 int index = iptr->isn_arg.number;
2329
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002330 // Get list item: list is at stack-1, push item.
2331 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002332 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002333 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002334
2335 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2336 goto failed;
2337 ++ectx.ec_stack.ga_len;
2338 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2339 }
2340 break;
2341
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 case ISN_MEMBER:
2343 {
2344 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002345 char_u *key;
2346 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002347 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002348
2349 // dict member: dict is at stack-2, key at stack-1
2350 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002351 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352 dict = tv->vval.v_dict;
2353
2354 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002355 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002356 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002357
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002358 if ((di = dict_find(dict, key, -1)) == NULL)
2359 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002360 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002361 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002362 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002363 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002364 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002365 --ectx.ec_stack.ga_len;
2366 // Clear the dict after getting the item, to avoid that it
2367 // make the item invalid.
2368 tv = STACK_TV_BOT(-1);
2369 temp_tv = *tv;
2370 copy_tv(&di->di_tv, tv);
2371 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002372 }
2373 break;
2374
2375 // dict member with string key
2376 case ISN_STRINGMEMBER:
2377 {
2378 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002380 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381
2382 tv = STACK_TV_BOT(-1);
2383 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2384 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002385 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002387 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 }
2389 dict = tv->vval.v_dict;
2390
2391 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2392 == NULL)
2393 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002394 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002396 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002398 // Clear the dict after getting the item, to avoid that it
2399 // make the item invalid.
2400 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002402 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 }
2404 break;
2405
2406 case ISN_NEGATENR:
2407 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002408 if (tv->v_type != VAR_NUMBER
2409#ifdef FEAT_FLOAT
2410 && tv->v_type != VAR_FLOAT
2411#endif
2412 )
2413 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002414 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002415 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002416 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002417 }
2418#ifdef FEAT_FLOAT
2419 if (tv->v_type == VAR_FLOAT)
2420 tv->vval.v_float = -tv->vval.v_float;
2421 else
2422#endif
2423 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 break;
2425
2426 case ISN_CHECKNR:
2427 {
2428 int error = FALSE;
2429
2430 tv = STACK_TV_BOT(-1);
2431 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002432 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 (void)tv_get_number_chk(tv, &error);
2434 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002435 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 }
2437 break;
2438
2439 case ISN_CHECKTYPE:
2440 {
2441 checktype_T *ct = &iptr->isn_arg.type;
2442
2443 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002444 // TODO: better type comparison
2445 if (tv->v_type != ct->ct_type
2446 && !((tv->v_type == VAR_PARTIAL
2447 && ct->ct_type == VAR_FUNC)
2448 || (tv->v_type == VAR_FUNC
2449 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002451 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002452 semsg(_(e_expected_str_but_got_str),
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 vartype_name(ct->ct_type),
2454 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002455 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 }
2457 }
2458 break;
2459
Bram Moolenaar9af78762020-06-16 11:34:42 +02002460 case ISN_CHECKLEN:
2461 {
2462 int min_len = iptr->isn_arg.checklen.cl_min_len;
2463 list_T *list = NULL;
2464
2465 tv = STACK_TV_BOT(-1);
2466 if (tv->v_type == VAR_LIST)
2467 list = tv->vval.v_list;
2468 if (list == NULL || list->lv_len < min_len
2469 || (list->lv_len > min_len
2470 && !iptr->isn_arg.checklen.cl_more_OK))
2471 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002473 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002474 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002475 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002476 }
2477 }
2478 break;
2479
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 case ISN_2BOOL:
2481 {
2482 int n;
2483
2484 tv = STACK_TV_BOT(-1);
2485 n = tv2bool(tv);
2486 if (iptr->isn_arg.number) // invert
2487 n = !n;
2488 clear_tv(tv);
2489 tv->v_type = VAR_BOOL;
2490 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2491 }
2492 break;
2493
2494 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002495 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 {
2497 char_u *str;
2498
2499 tv = STACK_TV_BOT(iptr->isn_arg.number);
2500 if (tv->v_type != VAR_STRING)
2501 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002502 if (iptr->isn_type == ISN_2STRING_ANY)
2503 {
2504 switch (tv->v_type)
2505 {
2506 case VAR_SPECIAL:
2507 case VAR_BOOL:
2508 case VAR_NUMBER:
2509 case VAR_FLOAT:
2510 case VAR_BLOB: break;
2511 default: to_string_error(tv->v_type);
2512 goto on_error;
2513 }
2514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002515 str = typval_tostring(tv);
2516 clear_tv(tv);
2517 tv->v_type = VAR_STRING;
2518 tv->vval.v_string = str;
2519 }
2520 }
2521 break;
2522
Bram Moolenaar389df252020-07-09 21:20:47 +02002523 case ISN_SHUFFLE:
2524 {
2525 typval_T tmp_tv;
2526 int item = iptr->isn_arg.shuffle.shfl_item;
2527 int up = iptr->isn_arg.shuffle.shfl_up;
2528
2529 tmp_tv = *STACK_TV_BOT(-item);
2530 for ( ; up > 0 && item > 1; --up)
2531 {
2532 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2533 --item;
2534 }
2535 *STACK_TV_BOT(-item) = tmp_tv;
2536 }
2537 break;
2538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 case ISN_DROP:
2540 --ectx.ec_stack.ga_len;
2541 clear_tv(STACK_TV_BOT(0));
2542 break;
2543 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002544 continue;
2545
Bram Moolenaard032f342020-07-18 18:13:02 +02002546func_return:
2547 // Restore previous function. If the frame pointer is zero then there
2548 // is none and we are done.
2549 if (ectx.ec_frame_idx == initial_frame_idx)
2550 {
2551 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2552 // only fails when out of memory
2553 goto failed;
2554 goto done;
2555 }
2556 if (func_return(&ectx) == FAIL)
2557 // only fails when out of memory
2558 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002559 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002560
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002561on_error:
2562 if (trylevel == 0)
2563 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565
2566done:
2567 // function finished, get result from the stack.
2568 tv = STACK_TV_BOT(-1);
2569 *rettv = *tv;
2570 tv->v_type = VAR_UNKNOWN;
2571 ret = OK;
2572
2573failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002574 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002575 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002576 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002577failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002578 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002579
2580 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2582 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002585 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002586
2587 if (ret != OK && called_emsg == called_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002588 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002589 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 return ret;
2591}
2592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593/*
2594 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002595 * We don't really need this at runtime, but we do have tests that require it,
2596 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002597 */
2598 void
2599ex_disassemble(exarg_T *eap)
2600{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002601 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002602 char_u *fname;
2603 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 dfunc_T *dfunc;
2605 isn_T *instr;
2606 int current;
2607 int line_idx = 0;
2608 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002609 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002611 if (STRNCMP(arg, "<lambda>", 8) == 0)
2612 {
2613 arg += 8;
2614 (void)getdigits(&arg);
2615 fname = vim_strnsave(eap->arg, arg - eap->arg);
2616 }
2617 else
2618 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002619 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002620 if (fname == NULL)
2621 {
2622 semsg(_(e_invarg2), eap->arg);
2623 return;
2624 }
2625
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002626 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002627 if (ufunc == NULL)
2628 {
2629 char_u *p = untrans_function_name(fname);
2630
2631 if (p != NULL)
2632 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002633 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002634 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002635 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 if (ufunc == NULL)
2637 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002638 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639 return;
2640 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002641 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002642 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2643 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002644 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002646 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 return;
2648 }
2649 if (ufunc->uf_name_exp != NULL)
2650 msg((char *)ufunc->uf_name_exp);
2651 else
2652 msg((char *)ufunc->uf_name);
2653
2654 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2655 instr = dfunc->df_instr;
2656 for (current = 0; current < dfunc->df_instr_count; ++current)
2657 {
2658 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002659 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660
2661 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2662 {
2663 if (current > prev_current)
2664 {
2665 msg_puts("\n\n");
2666 prev_current = current;
2667 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002668 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2669 if (line != NULL)
2670 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 }
2672
2673 switch (iptr->isn_type)
2674 {
2675 case ISN_EXEC:
2676 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2677 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002678 case ISN_EXECCONCAT:
2679 smsg("%4d EXECCONCAT %lld", current,
2680 (long long)iptr->isn_arg.number);
2681 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 case ISN_ECHO:
2683 {
2684 echo_T *echo = &iptr->isn_arg.echo;
2685
2686 smsg("%4d %s %d", current,
2687 echo->echo_with_white ? "ECHO" : "ECHON",
2688 echo->echo_count);
2689 }
2690 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002691 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002692 smsg("%4d EXECUTE %lld", current,
2693 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002694 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002695 case ISN_ECHOMSG:
2696 smsg("%4d ECHOMSG %lld", current,
2697 (long long)(iptr->isn_arg.number));
2698 break;
2699 case ISN_ECHOERR:
2700 smsg("%4d ECHOERR %lld", current,
2701 (long long)(iptr->isn_arg.number));
2702 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002703 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002704 case ISN_LOADOUTER:
2705 {
2706 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2707
2708 if (iptr->isn_arg.number < 0)
2709 smsg("%4d LOAD%s arg[%lld]", current, add,
2710 (long long)(iptr->isn_arg.number
2711 + STACK_FRAME_SIZE));
2712 else
2713 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002714 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002715 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 break;
2717 case ISN_LOADV:
2718 smsg("%4d LOADV v:%s", current,
2719 get_vim_var_name(iptr->isn_arg.number));
2720 break;
2721 case ISN_LOADSCRIPT:
2722 {
2723 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002724 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2726 + iptr->isn_arg.script.script_idx;
2727
2728 smsg("%4d LOADSCRIPT %s from %s", current,
2729 sv->sv_name, si->sn_name);
2730 }
2731 break;
2732 case ISN_LOADS:
2733 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002734 scriptitem_T *si = SCRIPT_ITEM(
2735 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736
2737 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002738 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 }
2740 break;
2741 case ISN_LOADG:
2742 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2743 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002744 case ISN_LOADB:
2745 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2746 break;
2747 case ISN_LOADW:
2748 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2749 break;
2750 case ISN_LOADT:
2751 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2752 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002753 case ISN_LOADGDICT:
2754 smsg("%4d LOAD g:", current);
2755 break;
2756 case ISN_LOADBDICT:
2757 smsg("%4d LOAD b:", current);
2758 break;
2759 case ISN_LOADWDICT:
2760 smsg("%4d LOAD w:", current);
2761 break;
2762 case ISN_LOADTDICT:
2763 smsg("%4d LOAD t:", current);
2764 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002765 case ISN_LOADOPT:
2766 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2767 break;
2768 case ISN_LOADENV:
2769 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2770 break;
2771 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002772 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 break;
2774
2775 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002776 case ISN_STOREOUTER:
2777 {
2778 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2779
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002780 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002781 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002782 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002783 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002784 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002785 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002786 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002788 case ISN_STOREV:
2789 smsg("%4d STOREV v:%s", current,
2790 get_vim_var_name(iptr->isn_arg.number));
2791 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002793 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2794 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002795 case ISN_STOREB:
2796 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2797 break;
2798 case ISN_STOREW:
2799 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2800 break;
2801 case ISN_STORET:
2802 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2803 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002804 case ISN_STORES:
2805 {
2806 scriptitem_T *si = SCRIPT_ITEM(
2807 iptr->isn_arg.loadstore.ls_sid);
2808
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002809 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002810 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002811 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 break;
2813 case ISN_STORESCRIPT:
2814 {
2815 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002816 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2818 + iptr->isn_arg.script.script_idx;
2819
2820 smsg("%4d STORESCRIPT %s in %s", current,
2821 sv->sv_name, si->sn_name);
2822 }
2823 break;
2824 case ISN_STOREOPT:
2825 smsg("%4d STOREOPT &%s", current,
2826 iptr->isn_arg.storeopt.so_name);
2827 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002828 case ISN_STOREENV:
2829 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2830 break;
2831 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002832 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002833 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 case ISN_STORENR:
2835 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002836 iptr->isn_arg.storenr.stnr_val,
2837 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 break;
2839
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002840 case ISN_STORELIST:
2841 smsg("%4d STORELIST", current);
2842 break;
2843
2844 case ISN_STOREDICT:
2845 smsg("%4d STOREDICT", current);
2846 break;
2847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 // constants
2849 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002850 smsg("%4d PUSHNR %lld", current,
2851 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 break;
2853 case ISN_PUSHBOOL:
2854 case ISN_PUSHSPEC:
2855 smsg("%4d PUSH %s", current,
2856 get_var_special_name(iptr->isn_arg.number));
2857 break;
2858 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002859#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002861#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 break;
2863 case ISN_PUSHS:
2864 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2865 break;
2866 case ISN_PUSHBLOB:
2867 {
2868 char_u *r;
2869 char_u numbuf[NUMBUFLEN];
2870 char_u *tofree;
2871
2872 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002873 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 vim_free(tofree);
2875 }
2876 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002877 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002878 {
2879 char *name = (char *)iptr->isn_arg.string;
2880
2881 smsg("%4d PUSHFUNC \"%s\"", current,
2882 name == NULL ? "[none]" : name);
2883 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002884 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002885 case ISN_PUSHCHANNEL:
2886#ifdef FEAT_JOB_CHANNEL
2887 {
2888 channel_T *channel = iptr->isn_arg.channel;
2889
2890 smsg("%4d PUSHCHANNEL %d", current,
2891 channel == NULL ? 0 : channel->ch_id);
2892 }
2893#endif
2894 break;
2895 case ISN_PUSHJOB:
2896#ifdef FEAT_JOB_CHANNEL
2897 {
2898 typval_T tv;
2899 char_u *name;
2900
2901 tv.v_type = VAR_JOB;
2902 tv.vval.v_job = iptr->isn_arg.job;
2903 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002904 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002905 }
2906#endif
2907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908 case ISN_PUSHEXC:
2909 smsg("%4d PUSH v:exception", current);
2910 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002911 case ISN_UNLET:
2912 smsg("%4d UNLET%s %s", current,
2913 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2914 iptr->isn_arg.unlet.ul_name);
2915 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002916 case ISN_UNLETENV:
2917 smsg("%4d UNLETENV%s $%s", current,
2918 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2919 iptr->isn_arg.unlet.ul_name);
2920 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002922 smsg("%4d NEWLIST size %lld", current,
2923 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 break;
2925 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002926 smsg("%4d NEWDICT size %lld", current,
2927 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002928 break;
2929
2930 // function call
2931 case ISN_BCALL:
2932 {
2933 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2934
2935 smsg("%4d BCALL %s(argc %d)", current,
2936 internal_func_name(cbfunc->cbf_idx),
2937 cbfunc->cbf_argcount);
2938 }
2939 break;
2940 case ISN_DCALL:
2941 {
2942 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2943 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2944 + cdfunc->cdf_idx;
2945
2946 smsg("%4d DCALL %s(argc %d)", current,
2947 df->df_ufunc->uf_name_exp != NULL
2948 ? df->df_ufunc->uf_name_exp
2949 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2950 }
2951 break;
2952 case ISN_UCALL:
2953 {
2954 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2955
2956 smsg("%4d UCALL %s(argc %d)", current,
2957 cufunc->cuf_name, cufunc->cuf_argcount);
2958 }
2959 break;
2960 case ISN_PCALL:
2961 {
2962 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2963
2964 smsg("%4d PCALL%s (argc %d)", current,
2965 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2966 }
2967 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002968 case ISN_PCALL_END:
2969 smsg("%4d PCALL end", current);
2970 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002971 case ISN_RETURN:
2972 smsg("%4d RETURN", current);
2973 break;
2974 case ISN_FUNCREF:
2975 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002976 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002978 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002980 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002981 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 }
2983 break;
2984
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002985 case ISN_NEWFUNC:
2986 {
2987 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2988
2989 smsg("%4d NEWFUNC %s %s", current,
2990 newfunc->nf_lambda, newfunc->nf_global);
2991 }
2992 break;
2993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 case ISN_JUMP:
2995 {
2996 char *when = "?";
2997
2998 switch (iptr->isn_arg.jump.jump_when)
2999 {
3000 case JUMP_ALWAYS:
3001 when = "JUMP";
3002 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 case JUMP_AND_KEEP_IF_TRUE:
3004 when = "JUMP_AND_KEEP_IF_TRUE";
3005 break;
3006 case JUMP_IF_FALSE:
3007 when = "JUMP_IF_FALSE";
3008 break;
3009 case JUMP_AND_KEEP_IF_FALSE:
3010 when = "JUMP_AND_KEEP_IF_FALSE";
3011 break;
3012 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003013 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 iptr->isn_arg.jump.jump_where);
3015 }
3016 break;
3017
3018 case ISN_FOR:
3019 {
3020 forloop_T *forloop = &iptr->isn_arg.forloop;
3021
3022 smsg("%4d FOR $%d -> %d", current,
3023 forloop->for_idx, forloop->for_end);
3024 }
3025 break;
3026
3027 case ISN_TRY:
3028 {
3029 try_T *try = &iptr->isn_arg.try;
3030
3031 smsg("%4d TRY catch -> %d, finally -> %d", current,
3032 try->try_catch, try->try_finally);
3033 }
3034 break;
3035 case ISN_CATCH:
3036 // TODO
3037 smsg("%4d CATCH", current);
3038 break;
3039 case ISN_ENDTRY:
3040 smsg("%4d ENDTRY", current);
3041 break;
3042 case ISN_THROW:
3043 smsg("%4d THROW", current);
3044 break;
3045
3046 // expression operations on number
3047 case ISN_OPNR:
3048 case ISN_OPFLOAT:
3049 case ISN_OPANY:
3050 {
3051 char *what;
3052 char *ins;
3053
3054 switch (iptr->isn_arg.op.op_type)
3055 {
3056 case EXPR_MULT: what = "*"; break;
3057 case EXPR_DIV: what = "/"; break;
3058 case EXPR_REM: what = "%"; break;
3059 case EXPR_SUB: what = "-"; break;
3060 case EXPR_ADD: what = "+"; break;
3061 default: what = "???"; break;
3062 }
3063 switch (iptr->isn_type)
3064 {
3065 case ISN_OPNR: ins = "OPNR"; break;
3066 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3067 case ISN_OPANY: ins = "OPANY"; break;
3068 default: ins = "???"; break;
3069 }
3070 smsg("%4d %s %s", current, ins, what);
3071 }
3072 break;
3073
3074 case ISN_COMPAREBOOL:
3075 case ISN_COMPARESPECIAL:
3076 case ISN_COMPARENR:
3077 case ISN_COMPAREFLOAT:
3078 case ISN_COMPARESTRING:
3079 case ISN_COMPAREBLOB:
3080 case ISN_COMPARELIST:
3081 case ISN_COMPAREDICT:
3082 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003083 case ISN_COMPAREANY:
3084 {
3085 char *p;
3086 char buf[10];
3087 char *type;
3088
3089 switch (iptr->isn_arg.op.op_type)
3090 {
3091 case EXPR_EQUAL: p = "=="; break;
3092 case EXPR_NEQUAL: p = "!="; break;
3093 case EXPR_GREATER: p = ">"; break;
3094 case EXPR_GEQUAL: p = ">="; break;
3095 case EXPR_SMALLER: p = "<"; break;
3096 case EXPR_SEQUAL: p = "<="; break;
3097 case EXPR_MATCH: p = "=~"; break;
3098 case EXPR_IS: p = "is"; break;
3099 case EXPR_ISNOT: p = "isnot"; break;
3100 case EXPR_NOMATCH: p = "!~"; break;
3101 default: p = "???"; break;
3102 }
3103 STRCPY(buf, p);
3104 if (iptr->isn_arg.op.op_ic == TRUE)
3105 strcat(buf, "?");
3106 switch(iptr->isn_type)
3107 {
3108 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3109 case ISN_COMPARESPECIAL:
3110 type = "COMPARESPECIAL"; break;
3111 case ISN_COMPARENR: type = "COMPARENR"; break;
3112 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3113 case ISN_COMPARESTRING:
3114 type = "COMPARESTRING"; break;
3115 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3116 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3117 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3118 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003119 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3120 default: type = "???"; break;
3121 }
3122
3123 smsg("%4d %s %s", current, type, buf);
3124 }
3125 break;
3126
3127 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3128 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3129
3130 // expression operations
3131 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003132 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003133 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003134 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003135 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003136 case ISN_SLICE: smsg("%4d SLICE %lld",
3137 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003138 case ISN_GETITEM: smsg("%4d ITEM %lld",
3139 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3141 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 iptr->isn_arg.string); break;
3143 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3144
3145 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3146 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3147 vartype_name(iptr->isn_arg.type.ct_type),
3148 iptr->isn_arg.type.ct_off);
3149 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003150 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3151 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3152 iptr->isn_arg.checklen.cl_min_len);
3153 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154 case ISN_2BOOL: if (iptr->isn_arg.number)
3155 smsg("%4d INVERT (!val)", current);
3156 else
3157 smsg("%4d 2BOOL (!!val)", current);
3158 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003159 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3160 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003161 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003162 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3163 (long long)(iptr->isn_arg.number));
3164 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003165
Bram Moolenaar389df252020-07-09 21:20:47 +02003166 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3167 iptr->isn_arg.shuffle.shfl_item,
3168 iptr->isn_arg.shuffle.shfl_up);
3169 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003170 case ISN_DROP: smsg("%4d DROP", current); break;
3171 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003172
3173 out_flush(); // output one line at a time
3174 ui_breakcheck();
3175 if (got_int)
3176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178}
3179
3180/*
3181 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3182 * list, etc. Mostly like what JavaScript does, except that empty list and
3183 * empty dictionary are FALSE.
3184 */
3185 int
3186tv2bool(typval_T *tv)
3187{
3188 switch (tv->v_type)
3189 {
3190 case VAR_NUMBER:
3191 return tv->vval.v_number != 0;
3192 case VAR_FLOAT:
3193#ifdef FEAT_FLOAT
3194 return tv->vval.v_float != 0.0;
3195#else
3196 break;
3197#endif
3198 case VAR_PARTIAL:
3199 return tv->vval.v_partial != NULL;
3200 case VAR_FUNC:
3201 case VAR_STRING:
3202 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3203 case VAR_LIST:
3204 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3205 case VAR_DICT:
3206 return tv->vval.v_dict != NULL
3207 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3208 case VAR_BOOL:
3209 case VAR_SPECIAL:
3210 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3211 case VAR_JOB:
3212#ifdef FEAT_JOB_CHANNEL
3213 return tv->vval.v_job != NULL;
3214#else
3215 break;
3216#endif
3217 case VAR_CHANNEL:
3218#ifdef FEAT_JOB_CHANNEL
3219 return tv->vval.v_channel != NULL;
3220#else
3221 break;
3222#endif
3223 case VAR_BLOB:
3224 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3225 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003226 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003227 case VAR_VOID:
3228 break;
3229 }
3230 return FALSE;
3231}
3232
3233/*
3234 * If "tv" is a string give an error and return FAIL.
3235 */
3236 int
3237check_not_string(typval_T *tv)
3238{
3239 if (tv->v_type == VAR_STRING)
3240 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003241 emsg(_(e_using_string_as_number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 clear_tv(tv);
3243 return FAIL;
3244 }
3245 return OK;
3246}
3247
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248
3249#endif // FEAT_EVAL