blob: de29988be123508ca1bba9e3f273f5248e6c19f9 [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.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200163 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
165 if (dfunc->df_deleted)
166 {
167 emsg_funcname(e_func_deleted, ufunc->uf_name);
168 return FAIL;
169 }
170
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 if (ufunc->uf_va_name != NULL)
172 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 // Stack at time of call with 2 varargs:
175 // normal_arg
176 // optional_arg
177 // vararg_1
178 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // After creating the list:
180 // normal_arg
181 // optional_arg
182 // vararg-list
183 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list
186 // normal_arg
187 // (space for optional_arg)
188 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 vararg_count = argcount - ufunc->uf_args.ga_len;
190 if (vararg_count < 0)
191 vararg_count = 0;
192 else
193 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196
197 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 }
199
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 if (arg_to_add < 0)
202 {
203 iemsg("Argument count wrong?");
204 return FAIL;
205 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 return FAIL;
209
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // Move the vararg-list to below the missing optional arguments.
211 if (vararg_count > 0 && arg_to_add > 0)
212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100213
214 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
219 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
223 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
229 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Set execution state to the start of the called function.
232 ectx->ec_dfunc_idx = cdf_idx;
233 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200234 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
235 if (entry != NULL)
236 {
237 // Set the script context to the script where the function was defined.
238 // TODO: save more than the SID?
239 entry->es_save_sid = current_sctx.sc_sid;
240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
241 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242
243 // Decide where to start execution, handles optional arguments.
244 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245
246 return OK;
247}
248
249// Get pointer to item in the stack.
250#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
251
252/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200253 * Used when returning from a function: Check if any closure is still
254 * referenced. If so then move the arguments and variables to a separate piece
255 * of stack to be used when the closure is called.
256 * When "free_arguments" is TRUE the arguments are to be freed.
257 * Returns FAIL when out of memory.
258 */
259 static int
260handle_closure_in_use(ectx_T *ectx, int free_arguments)
261{
262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
263 + ectx->ec_dfunc_idx;
264 int argcount = ufunc_argcount(dfunc->df_ufunc);
265 int top = ectx->ec_frame_idx - argcount;
266 int idx;
267 typval_T *tv;
268 int closure_in_use = FALSE;
269
270 // Check if any created closure is still in use.
271 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
272 {
273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
274 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
276 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200277 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200278 int refcount = tv->vval.v_partial->pt_refcount;
279 int i;
280
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200281 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200282 // unreferenced on return.
283 for (i = 0; i < dfunc->df_varcount; ++i)
284 {
285 typval_T *stv = STACK_TV(ectx->ec_frame_idx
286 + STACK_FRAME_SIZE + i);
287 if (stv->v_type == VAR_PARTIAL
288 && tv->vval.v_partial == stv->vval.v_partial)
289 --refcount;
290 }
291 if (refcount > 1)
292 {
293 closure_in_use = TRUE;
294 break;
295 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200296 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297 }
298
299 if (closure_in_use)
300 {
301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
302 typval_T *stack;
303
304 // A closure is using the arguments and/or local variables.
305 // Move them to the called function.
306 if (funcstack == NULL)
307 return FAIL;
308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
309 + dfunc->df_varcount;
310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
311 funcstack->fs_ga.ga_data = stack;
312 if (stack == NULL)
313 {
314 vim_free(funcstack);
315 return FAIL;
316 }
317
318 // Move or copy the arguments.
319 for (idx = 0; idx < argcount; ++idx)
320 {
321 tv = STACK_TV(top + idx);
322 if (free_arguments)
323 {
324 *(stack + idx) = *tv;
325 tv->v_type = VAR_UNKNOWN;
326 }
327 else
328 copy_tv(tv, stack + idx);
329 }
330 // Move the local variables.
331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
332 {
333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200334
335 // Do not copy a partial created for a local function.
336 // TODO: this won't work if the closure actually uses it. But when
337 // keeping it it gets complicated: it will create a reference cycle
338 // inside the partial, thus needs special handling for garbage
339 // collection.
340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
341 {
342 int i;
343 typval_T *ctv;
344
345 for (i = 0; i < dfunc->df_closure_count; ++i)
346 {
347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
348 + dfunc->df_varcount + i);
349 if (tv->vval.v_partial == ctv->vval.v_partial)
350 break;
351 }
352 if (i < dfunc->df_closure_count)
353 {
354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
355 VAR_UNKNOWN;
356 continue;
357 }
358 }
359
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
361 tv->v_type = VAR_UNKNOWN;
362 }
363
364 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
365 {
366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
367 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200370 partial_T *partial = tv->vval.v_partial;
371
372 if (partial->pt_refcount > 1)
373 {
374 ++funcstack->fs_refcount;
375 partial->pt_funcstack = funcstack;
376 partial->pt_ectx_stack = &funcstack->fs_ga;
377 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
378 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 }
380 }
381 }
382
383 return OK;
384}
385
386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Return from the current function.
388 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390func_return(ectx_T *ectx)
391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
394 + ectx->ec_dfunc_idx;
395 int argcount = ufunc_argcount(dfunc->df_ufunc);
396 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200397 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 entry = estack_pop();
401 if (entry != NULL)
402 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 if (handle_closure_in_use(ectx, TRUE) == FAIL)
405 return FAIL;
406
407 // Clear the arguments.
408 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
409 clear_tv(STACK_TV(idx));
410
411 // Clear local variables and temp values, but not the return value.
412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100413 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100416 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
421 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422
423 // Reset the stack to the position before the call, move the return value
424 // to the top of the stack.
425 idx = ectx->ec_stack.ga_len - 1;
426 ectx->ec_stack.ga_len = top + 1;
427 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428
429 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430}
431
432#undef STACK_TV
433
434/*
435 * Prepare arguments and rettv for calling a builtin or user function.
436 */
437 static int
438call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
439{
440 int idx;
441 typval_T *tv;
442
443 // Move arguments from bottom of the stack to argvars[] and add terminator.
444 for (idx = 0; idx < argcount; ++idx)
445 argvars[idx] = *STACK_TV_BOT(idx - argcount);
446 argvars[argcount].v_type = VAR_UNKNOWN;
447
448 // Result replaces the arguments on the stack.
449 if (argcount > 0)
450 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 return FAIL;
453 else
454 ++ectx->ec_stack.ga_len;
455
456 // Default return value is zero.
457 tv = STACK_TV_BOT(-1);
458 tv->v_type = VAR_NUMBER;
459 tv->vval.v_number = 0;
460
461 return OK;
462}
463
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200464// Ugly global to avoid passing the execution context around through many
465// layers.
466static ectx_T *current_ectx = NULL;
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Call a builtin function by index.
470 */
471 static int
472call_bfunc(int func_idx, int argcount, ectx_T *ectx)
473{
474 typval_T argvars[MAX_FUNC_ARGS];
475 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200476 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200477 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 if (call_prepare(argcount, argvars, ectx) == FAIL)
480 return FAIL;
481
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200482 // Call the builtin function. Set "current_ectx" so that when it
483 // recursively invokes call_def_function() a closure context can be set.
484 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200491
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200492 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return OK;
495}
496
497/*
498 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 */
501 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503{
504 typval_T argvars[MAX_FUNC_ARGS];
505 funcexe_T funcexe;
506 int error;
507 int idx;
508
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200509 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200510 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
511 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200512 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100513 {
514 // The function has been compiled, can call it quickly. For a function
515 // that was defined later: we can call it directly next time.
516 if (iptr != NULL)
517 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100518 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100519 iptr->isn_type = ISN_DCALL;
520 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
521 iptr->isn_arg.dfunc.cdf_argcount = argcount;
522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (call_prepare(argcount, argvars, ectx) == FAIL)
527 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200528 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 funcexe.evaluate = TRUE;
530
531 // Call the user function. Result goes in last position on the stack.
532 // TODO: add selfdict if there is one
533 error = call_user_func_check(ufunc, argcount, argvars,
534 STACK_TV_BOT(-1), &funcexe, NULL);
535
536 // Clear the arguments.
537 for (idx = 0; idx < argcount; ++idx)
538 clear_tv(&argvars[idx]);
539
540 if (error != FCERR_NONE)
541 {
542 user_func_error(error, ufunc->uf_name);
543 return FAIL;
544 }
545 return OK;
546}
547
548/*
549 * Execute a function by "name".
550 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100551 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 * Returns FAIL if not found without an error message.
553 */
554 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100555call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100556{
557 ufunc_T *ufunc;
558
559 if (builtin_function(name, -1))
560 {
561 int func_idx = find_internal_func(name);
562
563 if (func_idx < 0)
564 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200565 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100566 return FAIL;
567 return call_bfunc(func_idx, argcount, ectx);
568 }
569
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200570 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100572 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573
574 return FAIL;
575}
576
577 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200578call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200580 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200581 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 int called_emsg_before = called_emsg;
583
584 if (tv->v_type == VAR_PARTIAL)
585 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200586 partial_T *pt = tv->vval.v_partial;
587 int i;
588
589 if (pt->pt_argc > 0)
590 {
591 // Make space for arguments from the partial, shift the "argcount"
592 // arguments up.
593 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
594 return FAIL;
595 for (i = 1; i <= argcount; ++i)
596 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
597 ectx->ec_stack.ga_len += pt->pt_argc;
598 argcount += pt->pt_argc;
599
600 // copy the arguments from the partial onto the stack
601 for (i = 0; i < pt->pt_argc; ++i)
602 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200606 {
607 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
608
609 // closure may need the function context where it was defined
610 ectx->ec_outer_stack = pt->pt_ectx_stack;
611 ectx->ec_outer_frame = pt->pt_ectx_frame;
612
613 return ret;
614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 name = pt->pt_name;
616 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200617 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200619 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620 {
621 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200622 semsg(_(e_unknownfunc),
623 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 return FAIL;
625 }
626 return OK;
627}
628
629/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100630 * Store "tv" in variable "name".
631 * This is for s: and g: variables.
632 */
633 static void
634store_var(char_u *name, typval_T *tv)
635{
636 funccal_entry_T entry;
637
638 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200639 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100640 restore_funccal();
641}
642
Bram Moolenaard3aac292020-04-19 14:32:17 +0200643
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100644/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 * Execute a function by "name".
646 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100647 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 */
649 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100650call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651{
652 int called_emsg_before = called_emsg;
653
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100654 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 && called_emsg == called_emsg_before)
656 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200657 dictitem_T *v;
658
659 v = find_var(name, NULL, FALSE);
660 if (v == NULL)
661 {
662 semsg(_(e_unknownfunc), name);
663 return FAIL;
664 }
665 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
666 {
667 semsg(_(e_unknownfunc), name);
668 return FAIL;
669 }
670 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671 }
672 return OK;
673}
674
675/*
676 * Call a "def" function from old Vim script.
677 * Return OK or FAIL.
678 */
679 int
680call_def_function(
681 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200682 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200684 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685 typval_T *rettv) // return value
686{
687 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200688 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200689 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 typval_T *tv;
691 int idx;
692 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100693 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200694 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200695 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200696 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697
698// Get pointer to item in the stack.
699#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
700
701// Get pointer to item at the bottom of the stack, -1 is the bottom.
702#undef STACK_TV_BOT
703#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
704
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200705// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200706#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 +0100707
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200708// Like STACK_TV_VAR but use the outer scope
709#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
710
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200711 if (ufunc->uf_def_status == UF_NOT_COMPILED
712 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200713 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200714 {
715 if (called_emsg == called_emsg_before)
716 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200717 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200718 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200719 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200720
Bram Moolenaar09689a02020-05-09 22:50:08 +0200721 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200722 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200723 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
724 + ufunc->uf_dfunc_idx;
725 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200726 {
727 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200728 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200729 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200730 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200732 CLEAR_FIELD(ectx);
733 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
734 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
735 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
736 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100737 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
738
739 // Put arguments on the stack.
740 for (idx = 0; idx < argc; ++idx)
741 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200742 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200743 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
744 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200745 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 copy_tv(&argv[idx], STACK_TV_BOT(0));
747 ++ectx.ec_stack.ga_len;
748 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200749
750 // Turn varargs into a list. Empty list if no args.
751 if (ufunc->uf_va_name != NULL)
752 {
753 int vararg_count = argc - ufunc->uf_args.ga_len;
754
755 if (vararg_count < 0)
756 vararg_count = 0;
757 else
758 argc -= vararg_count;
759 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200760 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200761
762 // Check the type of the list items.
763 tv = STACK_TV_BOT(-1);
764 if (ufunc->uf_va_type != NULL
765 && ufunc->uf_va_type->tt_member != &t_any
766 && tv->vval.v_list != NULL)
767 {
768 type_T *expected = ufunc->uf_va_type->tt_member;
769 listitem_T *li = tv->vval.v_list->lv_first;
770
771 for (idx = 0; idx < vararg_count; ++idx)
772 {
773 if (check_typval_type(expected, &li->li_tv) == FAIL)
774 goto failed_early;
775 li = li->li_next;
776 }
777 }
778
Bram Moolenaar23e03252020-04-12 22:22:31 +0200779 if (defcount > 0)
780 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200781 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200782 --ectx.ec_stack.ga_len;
783 }
784
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100785 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200786 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100787 if (defcount > 0)
788 for (idx = 0; idx < defcount; ++idx)
789 {
790 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
791 ++ectx.ec_stack.ga_len;
792 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200793 if (ufunc->uf_va_name != NULL)
794 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795
796 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200797 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
798 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200800 if (partial != NULL)
801 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200802 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
803 {
804 // TODO: is this always the right way?
805 ectx.ec_outer_stack = &current_ectx->ec_stack;
806 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
807 }
808 else
809 {
810 ectx.ec_outer_stack = partial->pt_ectx_stack;
811 ectx.ec_outer_frame = partial->pt_ectx_frame;
812 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200813 }
814
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100815 // dummy frame entries
816 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
817 {
818 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
819 ++ectx.ec_stack.ga_len;
820 }
821
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200822 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200823 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200824 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
825 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200826 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200828 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200829 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200830 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200831
832 ectx.ec_instr = dfunc->df_instr;
833 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100834
Bram Moolenaara26b9702020-04-18 19:53:28 +0200835 // Commands behave like vim9script.
836 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
837
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100838 // Decide where to start execution, handles optional arguments.
839 init_instr_idx(ufunc, argc, &ectx);
840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 for (;;)
842 {
843 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100844
Bram Moolenaar270d0382020-05-15 21:42:53 +0200845 if (++breakcheck_count >= 100)
846 {
847 line_breakcheck();
848 breakcheck_count = 0;
849 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100850 if (got_int)
851 {
852 // Turn CTRL-C into an exception.
853 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100854 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100855 goto failed;
856 did_throw = TRUE;
857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858
Bram Moolenaara26b9702020-04-18 19:53:28 +0200859 if (did_emsg && msg_list != NULL && *msg_list != NULL)
860 {
861 // Turn an error message into an exception.
862 did_emsg = FALSE;
863 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
864 goto failed;
865 did_throw = TRUE;
866 *msg_list = NULL;
867 }
868
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 if (did_throw && !ectx.ec_in_catch)
870 {
871 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100872 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873
874 // An exception jumps to the first catch, finally, or returns from
875 // the current function.
876 if (trystack->ga_len > 0)
877 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200878 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 {
880 // jump to ":catch" or ":finally"
881 ectx.ec_in_catch = TRUE;
882 ectx.ec_iidx = trycmd->tcd_catch_idx;
883 }
884 else
885 {
886 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200887 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100888 {
889 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200890 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 goto failed;
892 tv = STACK_TV_BOT(0);
893 tv->v_type = VAR_NUMBER;
894 tv->vval.v_number = 0;
895 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100896 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200897 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
898 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899 goto done;
900 }
901
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200902 if (func_return(&ectx) == FAIL)
903 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 }
905 continue;
906 }
907
908 iptr = &ectx.ec_instr[ectx.ec_iidx++];
909 switch (iptr->isn_type)
910 {
911 // execute Ex command line
912 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200913 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 do_cmdline_cmd(iptr->isn_arg.string);
915 break;
916
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200917 // execute Ex command from pieces on the stack
918 case ISN_EXECCONCAT:
919 {
920 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200921 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200922 int pass;
923 int i;
924 char_u *cmd = NULL;
925 char_u *str;
926
927 for (pass = 1; pass <= 2; ++pass)
928 {
929 for (i = 0; i < count; ++i)
930 {
931 tv = STACK_TV_BOT(i - count);
932 str = tv->vval.v_string;
933 if (str != NULL && *str != NUL)
934 {
935 if (pass == 2)
936 STRCPY(cmd + len, str);
937 len += STRLEN(str);
938 }
939 if (pass == 2)
940 clear_tv(tv);
941 }
942 if (pass == 1)
943 {
944 cmd = alloc(len + 1);
945 if (cmd == NULL)
946 goto failed;
947 len = 0;
948 }
949 }
950
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200951 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200952 do_cmdline_cmd(cmd);
953 vim_free(cmd);
954 }
955 break;
956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 // execute :echo {string} ...
958 case ISN_ECHO:
959 {
960 int count = iptr->isn_arg.echo.echo_count;
961 int atstart = TRUE;
962 int needclr = TRUE;
963
964 for (idx = 0; idx < count; ++idx)
965 {
966 tv = STACK_TV_BOT(idx - count);
967 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
968 &atstart, &needclr);
969 clear_tv(tv);
970 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100971 if (needclr)
972 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 ectx.ec_stack.ga_len -= count;
974 }
975 break;
976
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200977 // :execute {string} ...
978 // :echomsg {string} ...
979 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100980 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200981 case ISN_ECHOMSG:
982 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100983 {
984 int count = iptr->isn_arg.number;
985 garray_T ga;
986 char_u buf[NUMBUFLEN];
987 char_u *p;
988 int len;
989 int failed = FALSE;
990
991 ga_init2(&ga, 1, 80);
992 for (idx = 0; idx < count; ++idx)
993 {
994 tv = STACK_TV_BOT(idx - count);
995 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
996 {
997 emsg(_(e_inval_string));
998 break;
999 }
1000 else
1001 p = tv_get_string_buf(tv, buf);
1002
1003 len = (int)STRLEN(p);
1004 if (ga_grow(&ga, len + 2) == FAIL)
1005 failed = TRUE;
1006 else
1007 {
1008 if (ga.ga_len > 0)
1009 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1010 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1011 ga.ga_len += len;
1012 }
1013 clear_tv(tv);
1014 }
1015 ectx.ec_stack.ga_len -= count;
1016
1017 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001018 {
1019 if (iptr->isn_type == ISN_EXECUTE)
1020 do_cmdline_cmd((char_u *)ga.ga_data);
1021 else
1022 {
1023 msg_sb_eol();
1024 if (iptr->isn_type == ISN_ECHOMSG)
1025 {
1026 msg_attr(ga.ga_data, echo_attr);
1027 out_flush();
1028 }
1029 else
1030 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001031 SOURCING_LNUM = iptr->isn_lnum;
1032 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001033 }
1034 }
1035 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001036 ga_clear(&ga);
1037 }
1038 break;
1039
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 // load local variable or argument
1041 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001042 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 goto failed;
1044 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1045 ++ectx.ec_stack.ga_len;
1046 break;
1047
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001048 // load variable or argument from outer scope
1049 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001050 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001051 goto failed;
1052 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1053 STACK_TV_BOT(0));
1054 ++ectx.ec_stack.ga_len;
1055 break;
1056
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 // load v: variable
1058 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001059 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 goto failed;
1061 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1062 ++ectx.ec_stack.ga_len;
1063 break;
1064
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001065 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 case ISN_LOADSCRIPT:
1067 {
1068 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001069 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 svar_T *sv;
1071
1072 sv = ((svar_T *)si->sn_var_vals.ga_data)
1073 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001074 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 goto failed;
1076 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1077 ++ectx.ec_stack.ga_len;
1078 }
1079 break;
1080
1081 // load s: variable in old script
1082 case ISN_LOADS:
1083 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001084 hashtab_T *ht = &SCRIPT_VARS(
1085 iptr->isn_arg.loadstore.ls_sid);
1086 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 if (di == NULL)
1090 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001091 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001092 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 }
1094 else
1095 {
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(&di->di_tv, STACK_TV_BOT(0));
1099 ++ectx.ec_stack.ga_len;
1100 }
1101 }
1102 break;
1103
Bram Moolenaard3aac292020-04-19 14:32:17 +02001104 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001106 case ISN_LOADB:
1107 case ISN_LOADW:
1108 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001110 dictitem_T *di = NULL;
1111 hashtab_T *ht = NULL;
1112 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001113
Bram Moolenaard3aac292020-04-19 14:32:17 +02001114 switch (iptr->isn_type)
1115 {
1116 case ISN_LOADG:
1117 ht = get_globvar_ht();
1118 namespace = 'g';
1119 break;
1120 case ISN_LOADB:
1121 ht = &curbuf->b_vars->dv_hashtab;
1122 namespace = 'b';
1123 break;
1124 case ISN_LOADW:
1125 ht = &curwin->w_vars->dv_hashtab;
1126 namespace = 'w';
1127 break;
1128 case ISN_LOADT:
1129 ht = &curtab->tp_vars->dv_hashtab;
1130 namespace = 't';
1131 break;
1132 default: // Cannot reach here
1133 goto failed;
1134 }
1135 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 if (di == NULL)
1138 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001139 semsg(_("E121: Undefined variable: %c:%s"),
1140 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001141 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 }
1143 else
1144 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001145 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001146 goto failed;
1147 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1148 ++ectx.ec_stack.ga_len;
1149 }
1150 }
1151 break;
1152
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001153 // load g:/b:/w:/t: namespace
1154 case ISN_LOADGDICT:
1155 case ISN_LOADBDICT:
1156 case ISN_LOADWDICT:
1157 case ISN_LOADTDICT:
1158 {
1159 dict_T *d = NULL;
1160
1161 switch (iptr->isn_type)
1162 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001163 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1164 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1165 case ISN_LOADWDICT: d = curwin->w_vars; break;
1166 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001167 default: // Cannot reach here
1168 goto failed;
1169 }
1170 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1171 goto failed;
1172 tv = STACK_TV_BOT(0);
1173 tv->v_type = VAR_DICT;
1174 tv->v_lock = 0;
1175 tv->vval.v_dict = d;
1176 ++ectx.ec_stack.ga_len;
1177 }
1178 break;
1179
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 // load &option
1181 case ISN_LOADOPT:
1182 {
1183 typval_T optval;
1184 char_u *name = iptr->isn_arg.string;
1185
Bram Moolenaara8c17702020-04-01 21:17:24 +02001186 // This is not expected to fail, name is checked during
1187 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001188 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001190 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001191 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 *STACK_TV_BOT(0) = optval;
1193 ++ectx.ec_stack.ga_len;
1194 }
1195 break;
1196
1197 // load $ENV
1198 case ISN_LOADENV:
1199 {
1200 typval_T optval;
1201 char_u *name = iptr->isn_arg.string;
1202
Bram Moolenaar270d0382020-05-15 21:42:53 +02001203 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001205 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001206 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 *STACK_TV_BOT(0) = optval;
1208 ++ectx.ec_stack.ga_len;
1209 }
1210 break;
1211
1212 // load @register
1213 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001214 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 goto failed;
1216 tv = STACK_TV_BOT(0);
1217 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001218 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001219 tv->vval.v_string = get_reg_contents(
1220 iptr->isn_arg.number, GREG_EXPR_SRC);
1221 ++ectx.ec_stack.ga_len;
1222 break;
1223
1224 // store local variable
1225 case ISN_STORE:
1226 --ectx.ec_stack.ga_len;
1227 tv = STACK_TV_VAR(iptr->isn_arg.number);
1228 clear_tv(tv);
1229 *tv = *STACK_TV_BOT(0);
1230 break;
1231
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001232 // store variable or argument in outer scope
1233 case ISN_STOREOUTER:
1234 --ectx.ec_stack.ga_len;
1235 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1236 clear_tv(tv);
1237 *tv = *STACK_TV_BOT(0);
1238 break;
1239
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001240 // store s: variable in old script
1241 case ISN_STORES:
1242 {
1243 hashtab_T *ht = &SCRIPT_VARS(
1244 iptr->isn_arg.loadstore.ls_sid);
1245 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001246 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001247
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001248 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001249 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001250 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001251 else
1252 {
1253 clear_tv(&di->di_tv);
1254 di->di_tv = *STACK_TV_BOT(0);
1255 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001256 }
1257 break;
1258
1259 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 case ISN_STORESCRIPT:
1261 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001262 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 iptr->isn_arg.script.script_sid);
1264 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1265 + iptr->isn_arg.script.script_idx;
1266
1267 --ectx.ec_stack.ga_len;
1268 clear_tv(sv->sv_tv);
1269 *sv->sv_tv = *STACK_TV_BOT(0);
1270 }
1271 break;
1272
1273 // store option
1274 case ISN_STOREOPT:
1275 {
1276 long n = 0;
1277 char_u *s = NULL;
1278 char *msg;
1279
1280 --ectx.ec_stack.ga_len;
1281 tv = STACK_TV_BOT(0);
1282 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001283 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001285 if (s == NULL)
1286 s = (char_u *)"";
1287 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001289 // must be VAR_NUMBER, CHECKTYPE makes sure
1290 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001291 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1292 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001293 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 if (msg != NULL)
1295 {
1296 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001297 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 }
1300 break;
1301
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001302 // store $ENV
1303 case ISN_STOREENV:
1304 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001305 tv = STACK_TV_BOT(0);
1306 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1307 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001308 break;
1309
1310 // store @r
1311 case ISN_STOREREG:
1312 {
1313 int reg = iptr->isn_arg.number;
1314
1315 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001316 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001317 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001318 tv_get_string(tv), -1, FALSE);
1319 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001320 }
1321 break;
1322
1323 // store v: variable
1324 case ISN_STOREV:
1325 --ectx.ec_stack.ga_len;
1326 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1327 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001328 // should not happen, type is checked when compiling
1329 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001330 break;
1331
Bram Moolenaard3aac292020-04-19 14:32:17 +02001332 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001334 case ISN_STOREB:
1335 case ISN_STOREW:
1336 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 {
1338 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001339 hashtab_T *ht;
1340 switch (iptr->isn_type)
1341 {
1342 case ISN_STOREG:
1343 ht = get_globvar_ht();
1344 break;
1345 case ISN_STOREB:
1346 ht = &curbuf->b_vars->dv_hashtab;
1347 break;
1348 case ISN_STOREW:
1349 ht = &curwin->w_vars->dv_hashtab;
1350 break;
1351 case ISN_STORET:
1352 ht = &curtab->tp_vars->dv_hashtab;
1353 break;
1354 default: // Cannot reach here
1355 goto failed;
1356 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357
1358 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001359 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001361 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 else
1363 {
1364 clear_tv(&di->di_tv);
1365 di->di_tv = *STACK_TV_BOT(0);
1366 }
1367 }
1368 break;
1369
1370 // store number in local variable
1371 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001372 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 clear_tv(tv);
1374 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001375 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 break;
1377
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001378 // store value in list variable
1379 case ISN_STORELIST:
1380 {
1381 typval_T *tv_idx = STACK_TV_BOT(-2);
1382 varnumber_T lidx = tv_idx->vval.v_number;
1383 typval_T *tv_list = STACK_TV_BOT(-1);
1384 list_T *list = tv_list->vval.v_list;
1385
1386 if (lidx < 0 && list->lv_len + lidx >= 0)
1387 // negative index is relative to the end
1388 lidx = list->lv_len + lidx;
1389 if (lidx < 0 || lidx > list->lv_len)
1390 {
1391 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001392 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001393 }
1394 tv = STACK_TV_BOT(-3);
1395 if (lidx < list->lv_len)
1396 {
1397 listitem_T *li = list_find(list, lidx);
1398
1399 // overwrite existing list item
1400 clear_tv(&li->li_tv);
1401 li->li_tv = *tv;
1402 }
1403 else
1404 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001405 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001406 if (list_append_tv(list, tv) == FAIL)
1407 goto failed;
1408 clear_tv(tv);
1409 }
1410 clear_tv(tv_idx);
1411 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001412 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001413 }
1414 break;
1415
1416 // store value in dict variable
1417 case ISN_STOREDICT:
1418 {
1419 typval_T *tv_key = STACK_TV_BOT(-2);
1420 char_u *key = tv_key->vval.v_string;
1421 typval_T *tv_dict = STACK_TV_BOT(-1);
1422 dict_T *dict = tv_dict->vval.v_dict;
1423 dictitem_T *di;
1424
Bram Moolenaar58626872020-08-01 14:06:38 +02001425 if (key == NULL)
1426 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001427 tv = STACK_TV_BOT(-3);
1428 di = dict_find(dict, key, -1);
1429 if (di != NULL)
1430 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001431 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001432 clear_tv(&di->di_tv);
1433 di->di_tv = *tv;
1434 }
1435 else
1436 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001437 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001438 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1439 goto failed;
1440 clear_tv(tv);
1441 }
1442 clear_tv(tv_key);
1443 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001444 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001445 }
1446 break;
1447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 // push constant
1449 case ISN_PUSHNR:
1450 case ISN_PUSHBOOL:
1451 case ISN_PUSHSPEC:
1452 case ISN_PUSHF:
1453 case ISN_PUSHS:
1454 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001455 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001456 case ISN_PUSHCHANNEL:
1457 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001458 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 goto failed;
1460 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001461 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462 ++ectx.ec_stack.ga_len;
1463 switch (iptr->isn_type)
1464 {
1465 case ISN_PUSHNR:
1466 tv->v_type = VAR_NUMBER;
1467 tv->vval.v_number = iptr->isn_arg.number;
1468 break;
1469 case ISN_PUSHBOOL:
1470 tv->v_type = VAR_BOOL;
1471 tv->vval.v_number = iptr->isn_arg.number;
1472 break;
1473 case ISN_PUSHSPEC:
1474 tv->v_type = VAR_SPECIAL;
1475 tv->vval.v_number = iptr->isn_arg.number;
1476 break;
1477#ifdef FEAT_FLOAT
1478 case ISN_PUSHF:
1479 tv->v_type = VAR_FLOAT;
1480 tv->vval.v_float = iptr->isn_arg.fnumber;
1481 break;
1482#endif
1483 case ISN_PUSHBLOB:
1484 blob_copy(iptr->isn_arg.blob, tv);
1485 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001486 case ISN_PUSHFUNC:
1487 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001488 if (iptr->isn_arg.string == NULL)
1489 tv->vval.v_string = NULL;
1490 else
1491 tv->vval.v_string =
1492 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001493 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001494 case ISN_PUSHCHANNEL:
1495#ifdef FEAT_JOB_CHANNEL
1496 tv->v_type = VAR_CHANNEL;
1497 tv->vval.v_channel = iptr->isn_arg.channel;
1498 if (tv->vval.v_channel != NULL)
1499 ++tv->vval.v_channel->ch_refcount;
1500#endif
1501 break;
1502 case ISN_PUSHJOB:
1503#ifdef FEAT_JOB_CHANNEL
1504 tv->v_type = VAR_JOB;
1505 tv->vval.v_job = iptr->isn_arg.job;
1506 if (tv->vval.v_job != NULL)
1507 ++tv->vval.v_job->jv_refcount;
1508#endif
1509 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001510 default:
1511 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001512 tv->vval.v_string = vim_strsave(
1513 iptr->isn_arg.string == NULL
1514 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001515 }
1516 break;
1517
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001518 case ISN_UNLET:
1519 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1520 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001521 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001522 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001523 case ISN_UNLETENV:
1524 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1525 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001526
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001527 // create a list from items on the stack; uses a single allocation
1528 // for the list header and the items
1529 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001530 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1531 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001532 break;
1533
1534 // create a dict from items on the stack
1535 case ISN_NEWDICT:
1536 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001537 int count = iptr->isn_arg.number;
1538 dict_T *dict = dict_alloc();
1539 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001540
1541 if (dict == NULL)
1542 goto failed;
1543 for (idx = 0; idx < count; ++idx)
1544 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001545 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001547 // check key is unique
1548 item = dict_find(dict, tv->vval.v_string, -1);
1549 if (item != NULL)
1550 {
1551 semsg(_(e_duplicate_key), tv->vval.v_string);
1552 dict_unref(dict);
1553 goto on_error;
1554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 item = dictitem_alloc(tv->vval.v_string);
1556 clear_tv(tv);
1557 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001558 {
1559 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001561 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1563 item->di_tv.v_lock = 0;
1564 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001565 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001566 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001567 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001569 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 }
1571
1572 if (count > 0)
1573 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001574 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 goto failed;
1576 else
1577 ++ectx.ec_stack.ga_len;
1578 tv = STACK_TV_BOT(-1);
1579 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001580 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 tv->vval.v_dict = dict;
1582 ++dict->dv_refcount;
1583 }
1584 break;
1585
1586 // call a :def function
1587 case ISN_DCALL:
1588 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1589 iptr->isn_arg.dfunc.cdf_argcount,
1590 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 break;
1593
1594 // call a builtin function
1595 case ISN_BCALL:
1596 SOURCING_LNUM = iptr->isn_lnum;
1597 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1598 iptr->isn_arg.bfunc.cbf_argcount,
1599 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001600 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 break;
1602
1603 // call a funcref or partial
1604 case ISN_PCALL:
1605 {
1606 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1607 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001608 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609
1610 SOURCING_LNUM = iptr->isn_lnum;
1611 if (pfunc->cpf_top)
1612 {
1613 // funcref is above the arguments
1614 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1615 }
1616 else
1617 {
1618 // Get the funcref from the stack.
1619 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001620 partial_tv = *STACK_TV_BOT(0);
1621 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
1623 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001624 if (tv == &partial_tv)
1625 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628 }
1629 break;
1630
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001631 case ISN_PCALL_END:
1632 // PCALL finished, arguments have been consumed and replaced by
1633 // the return value. Now clear the funcref from the stack,
1634 // and move the return value in its place.
1635 --ectx.ec_stack.ga_len;
1636 clear_tv(STACK_TV_BOT(-1));
1637 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1638 break;
1639
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 // call a user defined function or funcref/partial
1641 case ISN_UCALL:
1642 {
1643 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1644
1645 SOURCING_LNUM = iptr->isn_lnum;
1646 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001647 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001648 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 }
1650 break;
1651
1652 // return from a :def function call
1653 case ISN_RETURN:
1654 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001655 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001656 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001657
1658 if (trystack->ga_len > 0)
1659 trycmd = ((trycmd_T *)trystack->ga_data)
1660 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001661 if (trycmd != NULL
1662 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 && trycmd->tcd_finally_idx != 0)
1664 {
1665 // jump to ":finally"
1666 ectx.ec_iidx = trycmd->tcd_finally_idx;
1667 trycmd->tcd_return = TRUE;
1668 }
1669 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001670 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 }
1672 break;
1673
1674 // push a function reference to a compiled function
1675 case ISN_FUNCREF:
1676 {
1677 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001678 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679
1680 pt = ALLOC_CLEAR_ONE(partial_T);
1681 if (pt == NULL)
1682 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001683 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001684 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001685 vim_free(pt);
1686 goto failed;
1687 }
1688 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1689 + iptr->isn_arg.funcref.fr_func;
1690 pt->pt_func = pt_dfunc->df_ufunc;
1691 pt->pt_refcount = 1;
1692 ++pt_dfunc->df_ufunc->uf_refcount;
1693
1694 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1695 {
1696 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1697 + ectx.ec_dfunc_idx;
1698
1699 // The closure needs to find arguments and local
1700 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001701 pt->pt_ectx_stack = &ectx.ec_stack;
1702 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001703
1704 // If this function returns and the closure is still
1705 // used, we need to make a copy of the context
1706 // (arguments and local variables). Store a reference
1707 // to the partial so we can handle that.
1708 ++pt->pt_refcount;
1709 tv = STACK_TV_VAR(dfunc->df_varcount
1710 + iptr->isn_arg.funcref.fr_var_idx);
1711 if (tv->v_type == VAR_PARTIAL)
1712 {
1713 // TODO: use a garray_T on ectx.
1714 emsg("Multiple closures not supported yet");
1715 goto failed;
1716 }
1717 tv->v_type = VAR_PARTIAL;
1718 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001719 }
1720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 tv = STACK_TV_BOT(0);
1722 ++ectx.ec_stack.ga_len;
1723 tv->vval.v_partial = pt;
1724 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001725 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 }
1727 break;
1728
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001729 // Create a global function from a lambda.
1730 case ISN_NEWFUNC:
1731 {
1732 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1733
1734 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1735 }
1736 break;
1737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 // jump if a condition is met
1739 case ISN_JUMP:
1740 {
1741 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1742 int jump = TRUE;
1743
1744 if (when != JUMP_ALWAYS)
1745 {
1746 tv = STACK_TV_BOT(-1);
1747 jump = tv2bool(tv);
1748 if (when == JUMP_IF_FALSE
1749 || when == JUMP_AND_KEEP_IF_FALSE)
1750 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001751 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 {
1753 // drop the value from the stack
1754 clear_tv(tv);
1755 --ectx.ec_stack.ga_len;
1756 }
1757 }
1758 if (jump)
1759 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1760 }
1761 break;
1762
1763 // top of a for loop
1764 case ISN_FOR:
1765 {
1766 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1767 typval_T *idxtv =
1768 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1769
1770 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001771 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 goto failed;
1773 if (++idxtv->vval.v_number >= list->lv_len)
1774 // past the end of the list, jump to "endfor"
1775 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1776 else if (list->lv_first == &range_list_item)
1777 {
1778 // non-materialized range() list
1779 tv = STACK_TV_BOT(0);
1780 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001781 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 tv->vval.v_number = list_find_nr(
1783 list, idxtv->vval.v_number, NULL);
1784 ++ectx.ec_stack.ga_len;
1785 }
1786 else
1787 {
1788 listitem_T *li = list_find(list, idxtv->vval.v_number);
1789
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1791 ++ectx.ec_stack.ga_len;
1792 }
1793 }
1794 break;
1795
1796 // start of ":try" block
1797 case ISN_TRY:
1798 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001799 trycmd_T *trycmd = NULL;
1800
Bram Moolenaar270d0382020-05-15 21:42:53 +02001801 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 goto failed;
1803 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1804 + ectx.ec_trystack.ga_len;
1805 ++ectx.ec_trystack.ga_len;
1806 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001807 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1809 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001810 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 }
1812 break;
1813
1814 case ISN_PUSHEXC:
1815 if (current_exception == NULL)
1816 {
1817 iemsg("Evaluating catch while current_exception is NULL");
1818 goto failed;
1819 }
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 tv = STACK_TV_BOT(0);
1823 ++ectx.ec_stack.ga_len;
1824 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001825 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 tv->vval.v_string = vim_strsave(
1827 (char_u *)current_exception->value);
1828 break;
1829
1830 case ISN_CATCH:
1831 {
1832 garray_T *trystack = &ectx.ec_trystack;
1833
1834 if (trystack->ga_len > 0)
1835 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001836 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 + trystack->ga_len - 1;
1838 trycmd->tcd_caught = TRUE;
1839 }
1840 did_emsg = got_int = did_throw = FALSE;
1841 catch_exception(current_exception);
1842 }
1843 break;
1844
1845 // end of ":try" block
1846 case ISN_ENDTRY:
1847 {
1848 garray_T *trystack = &ectx.ec_trystack;
1849
1850 if (trystack->ga_len > 0)
1851 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001852 trycmd_T *trycmd = NULL;
1853
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 --trystack->ga_len;
1855 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001856 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 trycmd = ((trycmd_T *)trystack->ga_data)
1858 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001859 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 {
1861 // discard the exception
1862 if (caught_stack == current_exception)
1863 caught_stack = caught_stack->caught;
1864 discard_current_exception();
1865 }
1866
1867 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001868 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 }
1870 }
1871 break;
1872
1873 case ISN_THROW:
1874 --ectx.ec_stack.ga_len;
1875 tv = STACK_TV_BOT(0);
1876 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1877 {
1878 vim_free(tv->vval.v_string);
1879 goto failed;
1880 }
1881 did_throw = TRUE;
1882 break;
1883
1884 // compare with special values
1885 case ISN_COMPAREBOOL:
1886 case ISN_COMPARESPECIAL:
1887 {
1888 typval_T *tv1 = STACK_TV_BOT(-2);
1889 typval_T *tv2 = STACK_TV_BOT(-1);
1890 varnumber_T arg1 = tv1->vval.v_number;
1891 varnumber_T arg2 = tv2->vval.v_number;
1892 int res;
1893
1894 switch (iptr->isn_arg.op.op_type)
1895 {
1896 case EXPR_EQUAL: res = arg1 == arg2; break;
1897 case EXPR_NEQUAL: res = arg1 != arg2; break;
1898 default: res = 0; break;
1899 }
1900
1901 --ectx.ec_stack.ga_len;
1902 tv1->v_type = VAR_BOOL;
1903 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1904 }
1905 break;
1906
1907 // Operation with two number arguments
1908 case ISN_OPNR:
1909 case ISN_COMPARENR:
1910 {
1911 typval_T *tv1 = STACK_TV_BOT(-2);
1912 typval_T *tv2 = STACK_TV_BOT(-1);
1913 varnumber_T arg1 = tv1->vval.v_number;
1914 varnumber_T arg2 = tv2->vval.v_number;
1915 varnumber_T res;
1916
1917 switch (iptr->isn_arg.op.op_type)
1918 {
1919 case EXPR_MULT: res = arg1 * arg2; break;
1920 case EXPR_DIV: res = arg1 / arg2; break;
1921 case EXPR_REM: res = arg1 % arg2; break;
1922 case EXPR_SUB: res = arg1 - arg2; break;
1923 case EXPR_ADD: res = arg1 + arg2; break;
1924
1925 case EXPR_EQUAL: res = arg1 == arg2; break;
1926 case EXPR_NEQUAL: res = arg1 != arg2; break;
1927 case EXPR_GREATER: res = arg1 > arg2; break;
1928 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1929 case EXPR_SMALLER: res = arg1 < arg2; break;
1930 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1931 default: res = 0; break;
1932 }
1933
1934 --ectx.ec_stack.ga_len;
1935 if (iptr->isn_type == ISN_COMPARENR)
1936 {
1937 tv1->v_type = VAR_BOOL;
1938 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1939 }
1940 else
1941 tv1->vval.v_number = res;
1942 }
1943 break;
1944
1945 // Computation with two float arguments
1946 case ISN_OPFLOAT:
1947 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001948#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001949 {
1950 typval_T *tv1 = STACK_TV_BOT(-2);
1951 typval_T *tv2 = STACK_TV_BOT(-1);
1952 float_T arg1 = tv1->vval.v_float;
1953 float_T arg2 = tv2->vval.v_float;
1954 float_T res = 0;
1955 int cmp = FALSE;
1956
1957 switch (iptr->isn_arg.op.op_type)
1958 {
1959 case EXPR_MULT: res = arg1 * arg2; break;
1960 case EXPR_DIV: res = arg1 / arg2; break;
1961 case EXPR_SUB: res = arg1 - arg2; break;
1962 case EXPR_ADD: res = arg1 + arg2; break;
1963
1964 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1965 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1966 case EXPR_GREATER: cmp = arg1 > arg2; break;
1967 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1968 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1969 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1970 default: cmp = 0; break;
1971 }
1972 --ectx.ec_stack.ga_len;
1973 if (iptr->isn_type == ISN_COMPAREFLOAT)
1974 {
1975 tv1->v_type = VAR_BOOL;
1976 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1977 }
1978 else
1979 tv1->vval.v_float = res;
1980 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001981#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 break;
1983
1984 case ISN_COMPARELIST:
1985 {
1986 typval_T *tv1 = STACK_TV_BOT(-2);
1987 typval_T *tv2 = STACK_TV_BOT(-1);
1988 list_T *arg1 = tv1->vval.v_list;
1989 list_T *arg2 = tv2->vval.v_list;
1990 int cmp = FALSE;
1991 int ic = iptr->isn_arg.op.op_ic;
1992
1993 switch (iptr->isn_arg.op.op_type)
1994 {
1995 case EXPR_EQUAL: cmp =
1996 list_equal(arg1, arg2, ic, FALSE); break;
1997 case EXPR_NEQUAL: cmp =
1998 !list_equal(arg1, arg2, ic, FALSE); break;
1999 case EXPR_IS: cmp = arg1 == arg2; break;
2000 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2001 default: cmp = 0; break;
2002 }
2003 --ectx.ec_stack.ga_len;
2004 clear_tv(tv1);
2005 clear_tv(tv2);
2006 tv1->v_type = VAR_BOOL;
2007 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2008 }
2009 break;
2010
2011 case ISN_COMPAREBLOB:
2012 {
2013 typval_T *tv1 = STACK_TV_BOT(-2);
2014 typval_T *tv2 = STACK_TV_BOT(-1);
2015 blob_T *arg1 = tv1->vval.v_blob;
2016 blob_T *arg2 = tv2->vval.v_blob;
2017 int cmp = FALSE;
2018
2019 switch (iptr->isn_arg.op.op_type)
2020 {
2021 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2022 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2023 case EXPR_IS: cmp = arg1 == arg2; break;
2024 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2025 default: cmp = 0; break;
2026 }
2027 --ectx.ec_stack.ga_len;
2028 clear_tv(tv1);
2029 clear_tv(tv2);
2030 tv1->v_type = VAR_BOOL;
2031 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2032 }
2033 break;
2034
2035 // TODO: handle separately
2036 case ISN_COMPARESTRING:
2037 case ISN_COMPAREDICT:
2038 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 case ISN_COMPAREANY:
2040 {
2041 typval_T *tv1 = STACK_TV_BOT(-2);
2042 typval_T *tv2 = STACK_TV_BOT(-1);
2043 exptype_T exptype = iptr->isn_arg.op.op_type;
2044 int ic = iptr->isn_arg.op.op_ic;
2045
2046 typval_compare(tv1, tv2, exptype, ic);
2047 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 --ectx.ec_stack.ga_len;
2049 }
2050 break;
2051
2052 case ISN_ADDLIST:
2053 case ISN_ADDBLOB:
2054 {
2055 typval_T *tv1 = STACK_TV_BOT(-2);
2056 typval_T *tv2 = STACK_TV_BOT(-1);
2057
2058 if (iptr->isn_type == ISN_ADDLIST)
2059 eval_addlist(tv1, tv2);
2060 else
2061 eval_addblob(tv1, tv2);
2062 clear_tv(tv2);
2063 --ectx.ec_stack.ga_len;
2064 }
2065 break;
2066
2067 // Computation with two arguments of unknown type
2068 case ISN_OPANY:
2069 {
2070 typval_T *tv1 = STACK_TV_BOT(-2);
2071 typval_T *tv2 = STACK_TV_BOT(-1);
2072 varnumber_T n1, n2;
2073#ifdef FEAT_FLOAT
2074 float_T f1 = 0, f2 = 0;
2075#endif
2076 int error = FALSE;
2077
2078 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2079 {
2080 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2081 {
2082 eval_addlist(tv1, tv2);
2083 clear_tv(tv2);
2084 --ectx.ec_stack.ga_len;
2085 break;
2086 }
2087 else if (tv1->v_type == VAR_BLOB
2088 && tv2->v_type == VAR_BLOB)
2089 {
2090 eval_addblob(tv1, tv2);
2091 clear_tv(tv2);
2092 --ectx.ec_stack.ga_len;
2093 break;
2094 }
2095 }
2096#ifdef FEAT_FLOAT
2097 if (tv1->v_type == VAR_FLOAT)
2098 {
2099 f1 = tv1->vval.v_float;
2100 n1 = 0;
2101 }
2102 else
2103#endif
2104 {
2105 n1 = tv_get_number_chk(tv1, &error);
2106 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002107 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108#ifdef FEAT_FLOAT
2109 if (tv2->v_type == VAR_FLOAT)
2110 f1 = n1;
2111#endif
2112 }
2113#ifdef FEAT_FLOAT
2114 if (tv2->v_type == VAR_FLOAT)
2115 {
2116 f2 = tv2->vval.v_float;
2117 n2 = 0;
2118 }
2119 else
2120#endif
2121 {
2122 n2 = tv_get_number_chk(tv2, &error);
2123 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002124 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125#ifdef FEAT_FLOAT
2126 if (tv1->v_type == VAR_FLOAT)
2127 f2 = n2;
2128#endif
2129 }
2130#ifdef FEAT_FLOAT
2131 // if there is a float on either side the result is a float
2132 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2133 {
2134 switch (iptr->isn_arg.op.op_type)
2135 {
2136 case EXPR_MULT: f1 = f1 * f2; break;
2137 case EXPR_DIV: f1 = f1 / f2; break;
2138 case EXPR_SUB: f1 = f1 - f2; break;
2139 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002140 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002141 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 }
2143 clear_tv(tv1);
2144 clear_tv(tv2);
2145 tv1->v_type = VAR_FLOAT;
2146 tv1->vval.v_float = f1;
2147 --ectx.ec_stack.ga_len;
2148 }
2149 else
2150#endif
2151 {
2152 switch (iptr->isn_arg.op.op_type)
2153 {
2154 case EXPR_MULT: n1 = n1 * n2; break;
2155 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2156 case EXPR_SUB: n1 = n1 - n2; break;
2157 case EXPR_ADD: n1 = n1 + n2; break;
2158 default: n1 = num_modulus(n1, n2); break;
2159 }
2160 clear_tv(tv1);
2161 clear_tv(tv2);
2162 tv1->v_type = VAR_NUMBER;
2163 tv1->vval.v_number = n1;
2164 --ectx.ec_stack.ga_len;
2165 }
2166 }
2167 break;
2168
2169 case ISN_CONCAT:
2170 {
2171 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2172 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2173 char_u *res;
2174
2175 res = concat_str(str1, str2);
2176 clear_tv(STACK_TV_BOT(-2));
2177 clear_tv(STACK_TV_BOT(-1));
2178 --ectx.ec_stack.ga_len;
2179 STACK_TV_BOT(-1)->vval.v_string = res;
2180 }
2181 break;
2182
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002183 case ISN_STRINDEX:
2184 {
2185 char_u *s;
2186 varnumber_T n;
2187 char_u *res;
2188
2189 // string index: string is at stack-2, index at stack-1
2190 tv = STACK_TV_BOT(-2);
2191 if (tv->v_type != VAR_STRING)
2192 {
2193 emsg(_(e_stringreq));
2194 goto on_error;
2195 }
2196 s = tv->vval.v_string;
2197
2198 tv = STACK_TV_BOT(-1);
2199 if (tv->v_type != VAR_NUMBER)
2200 {
2201 emsg(_(e_number_exp));
2202 goto on_error;
2203 }
2204 n = tv->vval.v_number;
2205
2206 // The resulting variable is a string of a single
2207 // character. If the index is too big or negative the
2208 // result is empty.
2209 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2210 res = NULL;
2211 else
2212 res = vim_strnsave(s + n, 1);
2213 --ectx.ec_stack.ga_len;
2214 tv = STACK_TV_BOT(-1);
2215 vim_free(tv->vval.v_string);
2216 tv->vval.v_string = res;
2217 }
2218 break;
2219
2220 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002221 {
2222 list_T *list;
2223 varnumber_T n;
2224 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002225 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226
2227 // list index: list is at stack-2, index at stack-1
2228 tv = STACK_TV_BOT(-2);
2229 if (tv->v_type != VAR_LIST)
2230 {
2231 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 }
2234 list = tv->vval.v_list;
2235
2236 tv = STACK_TV_BOT(-1);
2237 if (tv->v_type != VAR_NUMBER)
2238 {
2239 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002240 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002241 }
2242 n = tv->vval.v_number;
2243 clear_tv(tv);
2244 if ((li = list_find(list, n)) == NULL)
2245 {
2246 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002247 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 }
2249 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002250 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002251 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002252 tv = STACK_TV_BOT(-1);
2253 temp_tv = *tv;
2254 copy_tv(&li->li_tv, tv);
2255 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 }
2257 break;
2258
Bram Moolenaar9af78762020-06-16 11:34:42 +02002259 case ISN_SLICE:
2260 {
2261 list_T *list;
2262 int count = iptr->isn_arg.number;
2263
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002264 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002265 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002266 list = tv->vval.v_list;
2267
2268 // no error for short list, expect it to be checked earlier
2269 if (list != NULL && list->lv_len >= count)
2270 {
2271 list_T *newlist = list_slice(list,
2272 count, list->lv_len - 1);
2273
2274 if (newlist != NULL)
2275 {
2276 list_unref(list);
2277 tv->vval.v_list = newlist;
2278 ++newlist->lv_refcount;
2279 }
2280 }
2281 }
2282 break;
2283
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002284 case ISN_GETITEM:
2285 {
2286 listitem_T *li;
2287 int index = iptr->isn_arg.number;
2288
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002289 // Get list item: list is at stack-1, push item.
2290 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002291 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002292 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002293
2294 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2295 goto failed;
2296 ++ectx.ec_stack.ga_len;
2297 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2298 }
2299 break;
2300
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 case ISN_MEMBER:
2302 {
2303 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002304 char_u *key;
2305 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002306 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002307
2308 // dict member: dict is at stack-2, key at stack-1
2309 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002310 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002311 dict = tv->vval.v_dict;
2312
2313 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002314 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002315 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002316
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002317 if ((di = dict_find(dict, key, -1)) == NULL)
2318 {
2319 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002320 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002321 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002322 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002323 --ectx.ec_stack.ga_len;
2324 // Clear the dict after getting the item, to avoid that it
2325 // make the item invalid.
2326 tv = STACK_TV_BOT(-1);
2327 temp_tv = *tv;
2328 copy_tv(&di->di_tv, tv);
2329 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002330 }
2331 break;
2332
2333 // dict member with string key
2334 case ISN_STRINGMEMBER:
2335 {
2336 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002338 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339
2340 tv = STACK_TV_BOT(-1);
2341 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2342 {
2343 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002344 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002345 }
2346 dict = tv->vval.v_dict;
2347
2348 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2349 == NULL)
2350 {
2351 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002352 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002354 // Clear the dict after getting the item, to avoid that it
2355 // make the item invalid.
2356 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002358 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 }
2360 break;
2361
2362 case ISN_NEGATENR:
2363 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002364 if (tv->v_type != VAR_NUMBER
2365#ifdef FEAT_FLOAT
2366 && tv->v_type != VAR_FLOAT
2367#endif
2368 )
2369 {
2370 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002371 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002372 }
2373#ifdef FEAT_FLOAT
2374 if (tv->v_type == VAR_FLOAT)
2375 tv->vval.v_float = -tv->vval.v_float;
2376 else
2377#endif
2378 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 break;
2380
2381 case ISN_CHECKNR:
2382 {
2383 int error = FALSE;
2384
2385 tv = STACK_TV_BOT(-1);
2386 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002387 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 (void)tv_get_number_chk(tv, &error);
2389 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 }
2392 break;
2393
2394 case ISN_CHECKTYPE:
2395 {
2396 checktype_T *ct = &iptr->isn_arg.type;
2397
2398 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002399 // TODO: better type comparison
2400 if (tv->v_type != ct->ct_type
2401 && !((tv->v_type == VAR_PARTIAL
2402 && ct->ct_type == VAR_FUNC)
2403 || (tv->v_type == VAR_FUNC
2404 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 {
2406 semsg(_("E1029: Expected %s but got %s"),
2407 vartype_name(ct->ct_type),
2408 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002409 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 }
2411 }
2412 break;
2413
Bram Moolenaar9af78762020-06-16 11:34:42 +02002414 case ISN_CHECKLEN:
2415 {
2416 int min_len = iptr->isn_arg.checklen.cl_min_len;
2417 list_T *list = NULL;
2418
2419 tv = STACK_TV_BOT(-1);
2420 if (tv->v_type == VAR_LIST)
2421 list = tv->vval.v_list;
2422 if (list == NULL || list->lv_len < min_len
2423 || (list->lv_len > min_len
2424 && !iptr->isn_arg.checklen.cl_more_OK))
2425 {
2426 semsg(_("E1093: Expected %d items but got %d"),
2427 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002428 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002429 }
2430 }
2431 break;
2432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 case ISN_2BOOL:
2434 {
2435 int n;
2436
2437 tv = STACK_TV_BOT(-1);
2438 n = tv2bool(tv);
2439 if (iptr->isn_arg.number) // invert
2440 n = !n;
2441 clear_tv(tv);
2442 tv->v_type = VAR_BOOL;
2443 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2444 }
2445 break;
2446
2447 case ISN_2STRING:
2448 {
2449 char_u *str;
2450
2451 tv = STACK_TV_BOT(iptr->isn_arg.number);
2452 if (tv->v_type != VAR_STRING)
2453 {
2454 str = typval_tostring(tv);
2455 clear_tv(tv);
2456 tv->v_type = VAR_STRING;
2457 tv->vval.v_string = str;
2458 }
2459 }
2460 break;
2461
Bram Moolenaar389df252020-07-09 21:20:47 +02002462 case ISN_SHUFFLE:
2463 {
2464 typval_T tmp_tv;
2465 int item = iptr->isn_arg.shuffle.shfl_item;
2466 int up = iptr->isn_arg.shuffle.shfl_up;
2467
2468 tmp_tv = *STACK_TV_BOT(-item);
2469 for ( ; up > 0 && item > 1; --up)
2470 {
2471 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2472 --item;
2473 }
2474 *STACK_TV_BOT(-item) = tmp_tv;
2475 }
2476 break;
2477
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 case ISN_DROP:
2479 --ectx.ec_stack.ga_len;
2480 clear_tv(STACK_TV_BOT(0));
2481 break;
2482 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002483 continue;
2484
Bram Moolenaard032f342020-07-18 18:13:02 +02002485func_return:
2486 // Restore previous function. If the frame pointer is zero then there
2487 // is none and we are done.
2488 if (ectx.ec_frame_idx == initial_frame_idx)
2489 {
2490 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2491 // only fails when out of memory
2492 goto failed;
2493 goto done;
2494 }
2495 if (func_return(&ectx) == FAIL)
2496 // only fails when out of memory
2497 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002498 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002499
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002500on_error:
2501 if (trylevel == 0)
2502 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 }
2504
2505done:
2506 // function finished, get result from the stack.
2507 tv = STACK_TV_BOT(-1);
2508 *rettv = *tv;
2509 tv->v_type = VAR_UNKNOWN;
2510 ret = OK;
2511
2512failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002513 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002514 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002515 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002516failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002517 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002518
2519 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2521 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002524 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002525
2526 if (ret != OK && called_emsg == called_emsg_before)
2527 semsg(_("E1099: Unknown error while executing %s"),
2528 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 return ret;
2530}
2531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532/*
2533 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002534 * We don't really need this at runtime, but we do have tests that require it,
2535 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002536 */
2537 void
2538ex_disassemble(exarg_T *eap)
2539{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002540 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002541 char_u *fname;
2542 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002543 dfunc_T *dfunc;
2544 isn_T *instr;
2545 int current;
2546 int line_idx = 0;
2547 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002548 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002550 if (STRNCMP(arg, "<lambda>", 8) == 0)
2551 {
2552 arg += 8;
2553 (void)getdigits(&arg);
2554 fname = vim_strnsave(eap->arg, arg - eap->arg);
2555 }
2556 else
2557 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002558 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002559 if (fname == NULL)
2560 {
2561 semsg(_(e_invarg2), eap->arg);
2562 return;
2563 }
2564
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002565 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002566 if (ufunc == NULL)
2567 {
2568 char_u *p = untrans_function_name(fname);
2569
2570 if (p != NULL)
2571 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002572 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002573 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002574 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 if (ufunc == NULL)
2576 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002577 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 return;
2579 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002580 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002581 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2582 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002583 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002585 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 return;
2587 }
2588 if (ufunc->uf_name_exp != NULL)
2589 msg((char *)ufunc->uf_name_exp);
2590 else
2591 msg((char *)ufunc->uf_name);
2592
2593 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2594 instr = dfunc->df_instr;
2595 for (current = 0; current < dfunc->df_instr_count; ++current)
2596 {
2597 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002598 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599
2600 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2601 {
2602 if (current > prev_current)
2603 {
2604 msg_puts("\n\n");
2605 prev_current = current;
2606 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002607 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2608 if (line != NULL)
2609 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 }
2611
2612 switch (iptr->isn_type)
2613 {
2614 case ISN_EXEC:
2615 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2616 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002617 case ISN_EXECCONCAT:
2618 smsg("%4d EXECCONCAT %lld", current,
2619 (long long)iptr->isn_arg.number);
2620 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 case ISN_ECHO:
2622 {
2623 echo_T *echo = &iptr->isn_arg.echo;
2624
2625 smsg("%4d %s %d", current,
2626 echo->echo_with_white ? "ECHO" : "ECHON",
2627 echo->echo_count);
2628 }
2629 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002630 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002631 smsg("%4d EXECUTE %lld", current,
2632 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002633 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002634 case ISN_ECHOMSG:
2635 smsg("%4d ECHOMSG %lld", current,
2636 (long long)(iptr->isn_arg.number));
2637 break;
2638 case ISN_ECHOERR:
2639 smsg("%4d ECHOERR %lld", current,
2640 (long long)(iptr->isn_arg.number));
2641 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002643 case ISN_LOADOUTER:
2644 {
2645 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2646
2647 if (iptr->isn_arg.number < 0)
2648 smsg("%4d LOAD%s arg[%lld]", current, add,
2649 (long long)(iptr->isn_arg.number
2650 + STACK_FRAME_SIZE));
2651 else
2652 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002653 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002654 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 break;
2656 case ISN_LOADV:
2657 smsg("%4d LOADV v:%s", current,
2658 get_vim_var_name(iptr->isn_arg.number));
2659 break;
2660 case ISN_LOADSCRIPT:
2661 {
2662 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002663 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2665 + iptr->isn_arg.script.script_idx;
2666
2667 smsg("%4d LOADSCRIPT %s from %s", current,
2668 sv->sv_name, si->sn_name);
2669 }
2670 break;
2671 case ISN_LOADS:
2672 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002673 scriptitem_T *si = SCRIPT_ITEM(
2674 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675
2676 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002677 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 }
2679 break;
2680 case ISN_LOADG:
2681 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2682 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002683 case ISN_LOADB:
2684 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2685 break;
2686 case ISN_LOADW:
2687 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2688 break;
2689 case ISN_LOADT:
2690 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2691 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002692 case ISN_LOADGDICT:
2693 smsg("%4d LOAD g:", current);
2694 break;
2695 case ISN_LOADBDICT:
2696 smsg("%4d LOAD b:", current);
2697 break;
2698 case ISN_LOADWDICT:
2699 smsg("%4d LOAD w:", current);
2700 break;
2701 case ISN_LOADTDICT:
2702 smsg("%4d LOAD t:", current);
2703 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 case ISN_LOADOPT:
2705 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2706 break;
2707 case ISN_LOADENV:
2708 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2709 break;
2710 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002711 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002712 break;
2713
2714 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002715 case ISN_STOREOUTER:
2716 {
2717 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2718
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002719 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002720 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002721 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002722 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002723 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002724 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002725 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002727 case ISN_STOREV:
2728 smsg("%4d STOREV v:%s", current,
2729 get_vim_var_name(iptr->isn_arg.number));
2730 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002732 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2733 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002734 case ISN_STOREB:
2735 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2736 break;
2737 case ISN_STOREW:
2738 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2739 break;
2740 case ISN_STORET:
2741 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2742 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002743 case ISN_STORES:
2744 {
2745 scriptitem_T *si = SCRIPT_ITEM(
2746 iptr->isn_arg.loadstore.ls_sid);
2747
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002748 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002749 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002750 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 break;
2752 case ISN_STORESCRIPT:
2753 {
2754 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002755 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2757 + iptr->isn_arg.script.script_idx;
2758
2759 smsg("%4d STORESCRIPT %s in %s", current,
2760 sv->sv_name, si->sn_name);
2761 }
2762 break;
2763 case ISN_STOREOPT:
2764 smsg("%4d STOREOPT &%s", current,
2765 iptr->isn_arg.storeopt.so_name);
2766 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002767 case ISN_STOREENV:
2768 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2769 break;
2770 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002771 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002772 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002773 case ISN_STORENR:
2774 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002775 iptr->isn_arg.storenr.stnr_val,
2776 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 break;
2778
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002779 case ISN_STORELIST:
2780 smsg("%4d STORELIST", current);
2781 break;
2782
2783 case ISN_STOREDICT:
2784 smsg("%4d STOREDICT", current);
2785 break;
2786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 // constants
2788 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002789 smsg("%4d PUSHNR %lld", current,
2790 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002791 break;
2792 case ISN_PUSHBOOL:
2793 case ISN_PUSHSPEC:
2794 smsg("%4d PUSH %s", current,
2795 get_var_special_name(iptr->isn_arg.number));
2796 break;
2797 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002798#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002800#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 break;
2802 case ISN_PUSHS:
2803 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2804 break;
2805 case ISN_PUSHBLOB:
2806 {
2807 char_u *r;
2808 char_u numbuf[NUMBUFLEN];
2809 char_u *tofree;
2810
2811 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002812 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 vim_free(tofree);
2814 }
2815 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002816 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002817 {
2818 char *name = (char *)iptr->isn_arg.string;
2819
2820 smsg("%4d PUSHFUNC \"%s\"", current,
2821 name == NULL ? "[none]" : name);
2822 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002823 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002824 case ISN_PUSHCHANNEL:
2825#ifdef FEAT_JOB_CHANNEL
2826 {
2827 channel_T *channel = iptr->isn_arg.channel;
2828
2829 smsg("%4d PUSHCHANNEL %d", current,
2830 channel == NULL ? 0 : channel->ch_id);
2831 }
2832#endif
2833 break;
2834 case ISN_PUSHJOB:
2835#ifdef FEAT_JOB_CHANNEL
2836 {
2837 typval_T tv;
2838 char_u *name;
2839
2840 tv.v_type = VAR_JOB;
2841 tv.vval.v_job = iptr->isn_arg.job;
2842 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002843 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002844 }
2845#endif
2846 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 case ISN_PUSHEXC:
2848 smsg("%4d PUSH v:exception", current);
2849 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002850 case ISN_UNLET:
2851 smsg("%4d UNLET%s %s", current,
2852 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2853 iptr->isn_arg.unlet.ul_name);
2854 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002855 case ISN_UNLETENV:
2856 smsg("%4d UNLETENV%s $%s", current,
2857 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2858 iptr->isn_arg.unlet.ul_name);
2859 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002861 smsg("%4d NEWLIST size %lld", current,
2862 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 break;
2864 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002865 smsg("%4d NEWDICT size %lld", current,
2866 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002867 break;
2868
2869 // function call
2870 case ISN_BCALL:
2871 {
2872 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2873
2874 smsg("%4d BCALL %s(argc %d)", current,
2875 internal_func_name(cbfunc->cbf_idx),
2876 cbfunc->cbf_argcount);
2877 }
2878 break;
2879 case ISN_DCALL:
2880 {
2881 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2882 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2883 + cdfunc->cdf_idx;
2884
2885 smsg("%4d DCALL %s(argc %d)", current,
2886 df->df_ufunc->uf_name_exp != NULL
2887 ? df->df_ufunc->uf_name_exp
2888 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2889 }
2890 break;
2891 case ISN_UCALL:
2892 {
2893 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2894
2895 smsg("%4d UCALL %s(argc %d)", current,
2896 cufunc->cuf_name, cufunc->cuf_argcount);
2897 }
2898 break;
2899 case ISN_PCALL:
2900 {
2901 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2902
2903 smsg("%4d PCALL%s (argc %d)", current,
2904 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2905 }
2906 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002907 case ISN_PCALL_END:
2908 smsg("%4d PCALL end", current);
2909 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 case ISN_RETURN:
2911 smsg("%4d RETURN", current);
2912 break;
2913 case ISN_FUNCREF:
2914 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002915 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002917 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002919 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002920 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002921 }
2922 break;
2923
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002924 case ISN_NEWFUNC:
2925 {
2926 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2927
2928 smsg("%4d NEWFUNC %s %s", current,
2929 newfunc->nf_lambda, newfunc->nf_global);
2930 }
2931 break;
2932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 case ISN_JUMP:
2934 {
2935 char *when = "?";
2936
2937 switch (iptr->isn_arg.jump.jump_when)
2938 {
2939 case JUMP_ALWAYS:
2940 when = "JUMP";
2941 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 case JUMP_AND_KEEP_IF_TRUE:
2943 when = "JUMP_AND_KEEP_IF_TRUE";
2944 break;
2945 case JUMP_IF_FALSE:
2946 when = "JUMP_IF_FALSE";
2947 break;
2948 case JUMP_AND_KEEP_IF_FALSE:
2949 when = "JUMP_AND_KEEP_IF_FALSE";
2950 break;
2951 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002952 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 iptr->isn_arg.jump.jump_where);
2954 }
2955 break;
2956
2957 case ISN_FOR:
2958 {
2959 forloop_T *forloop = &iptr->isn_arg.forloop;
2960
2961 smsg("%4d FOR $%d -> %d", current,
2962 forloop->for_idx, forloop->for_end);
2963 }
2964 break;
2965
2966 case ISN_TRY:
2967 {
2968 try_T *try = &iptr->isn_arg.try;
2969
2970 smsg("%4d TRY catch -> %d, finally -> %d", current,
2971 try->try_catch, try->try_finally);
2972 }
2973 break;
2974 case ISN_CATCH:
2975 // TODO
2976 smsg("%4d CATCH", current);
2977 break;
2978 case ISN_ENDTRY:
2979 smsg("%4d ENDTRY", current);
2980 break;
2981 case ISN_THROW:
2982 smsg("%4d THROW", current);
2983 break;
2984
2985 // expression operations on number
2986 case ISN_OPNR:
2987 case ISN_OPFLOAT:
2988 case ISN_OPANY:
2989 {
2990 char *what;
2991 char *ins;
2992
2993 switch (iptr->isn_arg.op.op_type)
2994 {
2995 case EXPR_MULT: what = "*"; break;
2996 case EXPR_DIV: what = "/"; break;
2997 case EXPR_REM: what = "%"; break;
2998 case EXPR_SUB: what = "-"; break;
2999 case EXPR_ADD: what = "+"; break;
3000 default: what = "???"; break;
3001 }
3002 switch (iptr->isn_type)
3003 {
3004 case ISN_OPNR: ins = "OPNR"; break;
3005 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3006 case ISN_OPANY: ins = "OPANY"; break;
3007 default: ins = "???"; break;
3008 }
3009 smsg("%4d %s %s", current, ins, what);
3010 }
3011 break;
3012
3013 case ISN_COMPAREBOOL:
3014 case ISN_COMPARESPECIAL:
3015 case ISN_COMPARENR:
3016 case ISN_COMPAREFLOAT:
3017 case ISN_COMPARESTRING:
3018 case ISN_COMPAREBLOB:
3019 case ISN_COMPARELIST:
3020 case ISN_COMPAREDICT:
3021 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 case ISN_COMPAREANY:
3023 {
3024 char *p;
3025 char buf[10];
3026 char *type;
3027
3028 switch (iptr->isn_arg.op.op_type)
3029 {
3030 case EXPR_EQUAL: p = "=="; break;
3031 case EXPR_NEQUAL: p = "!="; break;
3032 case EXPR_GREATER: p = ">"; break;
3033 case EXPR_GEQUAL: p = ">="; break;
3034 case EXPR_SMALLER: p = "<"; break;
3035 case EXPR_SEQUAL: p = "<="; break;
3036 case EXPR_MATCH: p = "=~"; break;
3037 case EXPR_IS: p = "is"; break;
3038 case EXPR_ISNOT: p = "isnot"; break;
3039 case EXPR_NOMATCH: p = "!~"; break;
3040 default: p = "???"; break;
3041 }
3042 STRCPY(buf, p);
3043 if (iptr->isn_arg.op.op_ic == TRUE)
3044 strcat(buf, "?");
3045 switch(iptr->isn_type)
3046 {
3047 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3048 case ISN_COMPARESPECIAL:
3049 type = "COMPARESPECIAL"; break;
3050 case ISN_COMPARENR: type = "COMPARENR"; break;
3051 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3052 case ISN_COMPARESTRING:
3053 type = "COMPARESTRING"; break;
3054 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3055 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3056 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3057 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3059 default: type = "???"; break;
3060 }
3061
3062 smsg("%4d %s %s", current, type, buf);
3063 }
3064 break;
3065
3066 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3067 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3068
3069 // expression operations
3070 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003071 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3072 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003073 case ISN_SLICE: smsg("%4d SLICE %lld",
3074 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003075 case ISN_GETITEM: smsg("%4d ITEM %lld",
3076 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003077 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3078 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003079 iptr->isn_arg.string); break;
3080 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3081
3082 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3083 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3084 vartype_name(iptr->isn_arg.type.ct_type),
3085 iptr->isn_arg.type.ct_off);
3086 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003087 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3088 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3089 iptr->isn_arg.checklen.cl_min_len);
3090 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003091 case ISN_2BOOL: if (iptr->isn_arg.number)
3092 smsg("%4d INVERT (!val)", current);
3093 else
3094 smsg("%4d 2BOOL (!!val)", current);
3095 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003096 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3097 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003098 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003099
Bram Moolenaar389df252020-07-09 21:20:47 +02003100 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3101 iptr->isn_arg.shuffle.shfl_item,
3102 iptr->isn_arg.shuffle.shfl_up);
3103 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 case ISN_DROP: smsg("%4d DROP", current); break;
3105 }
3106 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003107}
3108
3109/*
3110 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3111 * list, etc. Mostly like what JavaScript does, except that empty list and
3112 * empty dictionary are FALSE.
3113 */
3114 int
3115tv2bool(typval_T *tv)
3116{
3117 switch (tv->v_type)
3118 {
3119 case VAR_NUMBER:
3120 return tv->vval.v_number != 0;
3121 case VAR_FLOAT:
3122#ifdef FEAT_FLOAT
3123 return tv->vval.v_float != 0.0;
3124#else
3125 break;
3126#endif
3127 case VAR_PARTIAL:
3128 return tv->vval.v_partial != NULL;
3129 case VAR_FUNC:
3130 case VAR_STRING:
3131 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3132 case VAR_LIST:
3133 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3134 case VAR_DICT:
3135 return tv->vval.v_dict != NULL
3136 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3137 case VAR_BOOL:
3138 case VAR_SPECIAL:
3139 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3140 case VAR_JOB:
3141#ifdef FEAT_JOB_CHANNEL
3142 return tv->vval.v_job != NULL;
3143#else
3144 break;
3145#endif
3146 case VAR_CHANNEL:
3147#ifdef FEAT_JOB_CHANNEL
3148 return tv->vval.v_channel != NULL;
3149#else
3150 break;
3151#endif
3152 case VAR_BLOB:
3153 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3154 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003155 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003156 case VAR_VOID:
3157 break;
3158 }
3159 return FALSE;
3160}
3161
3162/*
3163 * If "tv" is a string give an error and return FAIL.
3164 */
3165 int
3166check_not_string(typval_T *tv)
3167{
3168 if (tv->v_type == VAR_STRING)
3169 {
3170 emsg(_("E1030: Using a String as a Number"));
3171 clear_tv(tv);
3172 return FAIL;
3173 }
3174 return OK;
3175}
3176
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003177
3178#endif // FEAT_EVAL