blob: 35419dae42b91586f9be901d0703f1664b0fd4c0 [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"),
717 ufunc->uf_name_exp == NULL
718 ? ufunc->uf_name : ufunc->uf_name_exp);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200719 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200720 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200721
Bram Moolenaar09689a02020-05-09 22:50:08 +0200722 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200723 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
725 + ufunc->uf_dfunc_idx;
726 if (dfunc->df_instr == NULL)
727 return FAIL;
728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200730 CLEAR_FIELD(ectx);
731 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
732 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
733 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
734 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
736
737 // Put arguments on the stack.
738 for (idx = 0; idx < argc; ++idx)
739 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200740 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
741 && check_argtype(ufunc->uf_arg_types[idx], &argv[idx]) == FAIL)
742 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 copy_tv(&argv[idx], STACK_TV_BOT(0));
744 ++ectx.ec_stack.ga_len;
745 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200746
747 // Turn varargs into a list. Empty list if no args.
748 if (ufunc->uf_va_name != NULL)
749 {
750 int vararg_count = argc - ufunc->uf_args.ga_len;
751
752 if (vararg_count < 0)
753 vararg_count = 0;
754 else
755 argc -= vararg_count;
756 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200757 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200758 if (defcount > 0)
759 // Move varargs list to below missing default arguments.
760 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
761 --ectx.ec_stack.ga_len;
762 }
763
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100764 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200765 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100766 if (defcount > 0)
767 for (idx = 0; idx < defcount; ++idx)
768 {
769 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
770 ++ectx.ec_stack.ga_len;
771 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200772 if (ufunc->uf_va_name != NULL)
773 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
775 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200776 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
777 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200779 if (partial != NULL)
780 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200781 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
782 {
783 // TODO: is this always the right way?
784 ectx.ec_outer_stack = &current_ectx->ec_stack;
785 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
786 }
787 else
788 {
789 ectx.ec_outer_stack = partial->pt_ectx_stack;
790 ectx.ec_outer_frame = partial->pt_ectx_frame;
791 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200792 }
793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100794 // dummy frame entries
795 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
796 {
797 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
798 ++ectx.ec_stack.ga_len;
799 }
800
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200801 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200802 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200803 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
804 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200805 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100806
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200807 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200808 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200809 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200810
811 ectx.ec_instr = dfunc->df_instr;
812 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100813
Bram Moolenaara26b9702020-04-18 19:53:28 +0200814 // Commands behave like vim9script.
815 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
816
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100817 // Decide where to start execution, handles optional arguments.
818 init_instr_idx(ufunc, argc, &ectx);
819
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 for (;;)
821 {
822 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100823
Bram Moolenaar270d0382020-05-15 21:42:53 +0200824 if (++breakcheck_count >= 100)
825 {
826 line_breakcheck();
827 breakcheck_count = 0;
828 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100829 if (got_int)
830 {
831 // Turn CTRL-C into an exception.
832 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100833 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100834 goto failed;
835 did_throw = TRUE;
836 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837
Bram Moolenaara26b9702020-04-18 19:53:28 +0200838 if (did_emsg && msg_list != NULL && *msg_list != NULL)
839 {
840 // Turn an error message into an exception.
841 did_emsg = FALSE;
842 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
843 goto failed;
844 did_throw = TRUE;
845 *msg_list = NULL;
846 }
847
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 if (did_throw && !ectx.ec_in_catch)
849 {
850 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100851 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100852
853 // An exception jumps to the first catch, finally, or returns from
854 // the current function.
855 if (trystack->ga_len > 0)
856 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200857 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
859 // jump to ":catch" or ":finally"
860 ectx.ec_in_catch = TRUE;
861 ectx.ec_iidx = trycmd->tcd_catch_idx;
862 }
863 else
864 {
865 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200866 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 {
868 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200869 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 goto failed;
871 tv = STACK_TV_BOT(0);
872 tv->v_type = VAR_NUMBER;
873 tv->vval.v_number = 0;
874 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100875 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200876 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
877 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 goto done;
879 }
880
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200881 if (func_return(&ectx) == FAIL)
882 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 }
884 continue;
885 }
886
887 iptr = &ectx.ec_instr[ectx.ec_iidx++];
888 switch (iptr->isn_type)
889 {
890 // execute Ex command line
891 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200892 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100893 do_cmdline_cmd(iptr->isn_arg.string);
894 break;
895
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200896 // execute Ex command from pieces on the stack
897 case ISN_EXECCONCAT:
898 {
899 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200900 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200901 int pass;
902 int i;
903 char_u *cmd = NULL;
904 char_u *str;
905
906 for (pass = 1; pass <= 2; ++pass)
907 {
908 for (i = 0; i < count; ++i)
909 {
910 tv = STACK_TV_BOT(i - count);
911 str = tv->vval.v_string;
912 if (str != NULL && *str != NUL)
913 {
914 if (pass == 2)
915 STRCPY(cmd + len, str);
916 len += STRLEN(str);
917 }
918 if (pass == 2)
919 clear_tv(tv);
920 }
921 if (pass == 1)
922 {
923 cmd = alloc(len + 1);
924 if (cmd == NULL)
925 goto failed;
926 len = 0;
927 }
928 }
929
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200931 do_cmdline_cmd(cmd);
932 vim_free(cmd);
933 }
934 break;
935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 // execute :echo {string} ...
937 case ISN_ECHO:
938 {
939 int count = iptr->isn_arg.echo.echo_count;
940 int atstart = TRUE;
941 int needclr = TRUE;
942
943 for (idx = 0; idx < count; ++idx)
944 {
945 tv = STACK_TV_BOT(idx - count);
946 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
947 &atstart, &needclr);
948 clear_tv(tv);
949 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100950 if (needclr)
951 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100952 ectx.ec_stack.ga_len -= count;
953 }
954 break;
955
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200956 // :execute {string} ...
957 // :echomsg {string} ...
958 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100959 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200960 case ISN_ECHOMSG:
961 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100962 {
963 int count = iptr->isn_arg.number;
964 garray_T ga;
965 char_u buf[NUMBUFLEN];
966 char_u *p;
967 int len;
968 int failed = FALSE;
969
970 ga_init2(&ga, 1, 80);
971 for (idx = 0; idx < count; ++idx)
972 {
973 tv = STACK_TV_BOT(idx - count);
974 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
975 {
976 emsg(_(e_inval_string));
977 break;
978 }
979 else
980 p = tv_get_string_buf(tv, buf);
981
982 len = (int)STRLEN(p);
983 if (ga_grow(&ga, len + 2) == FAIL)
984 failed = TRUE;
985 else
986 {
987 if (ga.ga_len > 0)
988 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
989 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
990 ga.ga_len += len;
991 }
992 clear_tv(tv);
993 }
994 ectx.ec_stack.ga_len -= count;
995
996 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200997 {
998 if (iptr->isn_type == ISN_EXECUTE)
999 do_cmdline_cmd((char_u *)ga.ga_data);
1000 else
1001 {
1002 msg_sb_eol();
1003 if (iptr->isn_type == ISN_ECHOMSG)
1004 {
1005 msg_attr(ga.ga_data, echo_attr);
1006 out_flush();
1007 }
1008 else
1009 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001010 SOURCING_LNUM = iptr->isn_lnum;
1011 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001012 }
1013 }
1014 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001015 ga_clear(&ga);
1016 }
1017 break;
1018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 // load local variable or argument
1020 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001021 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 goto failed;
1023 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1024 ++ectx.ec_stack.ga_len;
1025 break;
1026
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001027 // load variable or argument from outer scope
1028 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001029 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001030 goto failed;
1031 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1032 STACK_TV_BOT(0));
1033 ++ectx.ec_stack.ga_len;
1034 break;
1035
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 // load v: variable
1037 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001038 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 goto failed;
1040 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1041 ++ectx.ec_stack.ga_len;
1042 break;
1043
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001044 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045 case ISN_LOADSCRIPT:
1046 {
1047 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001048 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 svar_T *sv;
1050
1051 sv = ((svar_T *)si->sn_var_vals.ga_data)
1052 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001053 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 goto failed;
1055 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1056 ++ectx.ec_stack.ga_len;
1057 }
1058 break;
1059
1060 // load s: variable in old script
1061 case ISN_LOADS:
1062 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001063 hashtab_T *ht = &SCRIPT_VARS(
1064 iptr->isn_arg.loadstore.ls_sid);
1065 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001067
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 if (di == NULL)
1069 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001070 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001071 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 }
1073 else
1074 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001075 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 goto failed;
1077 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1078 ++ectx.ec_stack.ga_len;
1079 }
1080 }
1081 break;
1082
Bram Moolenaard3aac292020-04-19 14:32:17 +02001083 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001085 case ISN_LOADB:
1086 case ISN_LOADW:
1087 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001089 dictitem_T *di = NULL;
1090 hashtab_T *ht = NULL;
1091 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001092
Bram Moolenaard3aac292020-04-19 14:32:17 +02001093 switch (iptr->isn_type)
1094 {
1095 case ISN_LOADG:
1096 ht = get_globvar_ht();
1097 namespace = 'g';
1098 break;
1099 case ISN_LOADB:
1100 ht = &curbuf->b_vars->dv_hashtab;
1101 namespace = 'b';
1102 break;
1103 case ISN_LOADW:
1104 ht = &curwin->w_vars->dv_hashtab;
1105 namespace = 'w';
1106 break;
1107 case ISN_LOADT:
1108 ht = &curtab->tp_vars->dv_hashtab;
1109 namespace = 't';
1110 break;
1111 default: // Cannot reach here
1112 goto failed;
1113 }
1114 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001115
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 if (di == NULL)
1117 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001118 semsg(_("E121: Undefined variable: %c:%s"),
1119 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001120 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 }
1122 else
1123 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001124 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 goto failed;
1126 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1127 ++ectx.ec_stack.ga_len;
1128 }
1129 }
1130 break;
1131
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001132 // load g:/b:/w:/t: namespace
1133 case ISN_LOADGDICT:
1134 case ISN_LOADBDICT:
1135 case ISN_LOADWDICT:
1136 case ISN_LOADTDICT:
1137 {
1138 dict_T *d = NULL;
1139
1140 switch (iptr->isn_type)
1141 {
1142 case ISN_LOADG: d = get_globvar_dict(); break;
1143 case ISN_LOADB: d = &curbuf->b_vars; break;
1144 case ISN_LOADW: d = &curwin->w_vars; break;
1145 case ISN_LOADT: d = &curtab->tp_vars; break;
1146 default: // Cannot reach here
1147 goto failed;
1148 }
1149 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1150 goto failed;
1151 tv = STACK_TV_BOT(0);
1152 tv->v_type = VAR_DICT;
1153 tv->v_lock = 0;
1154 tv->vval.v_dict = d;
1155 ++ectx.ec_stack.ga_len;
1156 }
1157 break;
1158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001159 // load &option
1160 case ISN_LOADOPT:
1161 {
1162 typval_T optval;
1163 char_u *name = iptr->isn_arg.string;
1164
Bram Moolenaara8c17702020-04-01 21:17:24 +02001165 // This is not expected to fail, name is checked during
1166 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001167 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001169 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001170 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 *STACK_TV_BOT(0) = optval;
1172 ++ectx.ec_stack.ga_len;
1173 }
1174 break;
1175
1176 // load $ENV
1177 case ISN_LOADENV:
1178 {
1179 typval_T optval;
1180 char_u *name = iptr->isn_arg.string;
1181
Bram Moolenaar270d0382020-05-15 21:42:53 +02001182 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001184 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001185 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001186 *STACK_TV_BOT(0) = optval;
1187 ++ectx.ec_stack.ga_len;
1188 }
1189 break;
1190
1191 // load @register
1192 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001193 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 goto failed;
1195 tv = STACK_TV_BOT(0);
1196 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001197 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 tv->vval.v_string = get_reg_contents(
1199 iptr->isn_arg.number, GREG_EXPR_SRC);
1200 ++ectx.ec_stack.ga_len;
1201 break;
1202
1203 // store local variable
1204 case ISN_STORE:
1205 --ectx.ec_stack.ga_len;
1206 tv = STACK_TV_VAR(iptr->isn_arg.number);
1207 clear_tv(tv);
1208 *tv = *STACK_TV_BOT(0);
1209 break;
1210
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001211 // store variable or argument in outer scope
1212 case ISN_STOREOUTER:
1213 --ectx.ec_stack.ga_len;
1214 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1215 clear_tv(tv);
1216 *tv = *STACK_TV_BOT(0);
1217 break;
1218
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001219 // store s: variable in old script
1220 case ISN_STORES:
1221 {
1222 hashtab_T *ht = &SCRIPT_VARS(
1223 iptr->isn_arg.loadstore.ls_sid);
1224 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001225 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001226
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001227 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001228 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001229 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001230 else
1231 {
1232 clear_tv(&di->di_tv);
1233 di->di_tv = *STACK_TV_BOT(0);
1234 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001235 }
1236 break;
1237
1238 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 case ISN_STORESCRIPT:
1240 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001241 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001242 iptr->isn_arg.script.script_sid);
1243 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1244 + iptr->isn_arg.script.script_idx;
1245
1246 --ectx.ec_stack.ga_len;
1247 clear_tv(sv->sv_tv);
1248 *sv->sv_tv = *STACK_TV_BOT(0);
1249 }
1250 break;
1251
1252 // store option
1253 case ISN_STOREOPT:
1254 {
1255 long n = 0;
1256 char_u *s = NULL;
1257 char *msg;
1258
1259 --ectx.ec_stack.ga_len;
1260 tv = STACK_TV_BOT(0);
1261 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001262 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001264 if (s == NULL)
1265 s = (char_u *)"";
1266 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001268 // must be VAR_NUMBER, CHECKTYPE makes sure
1269 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001270 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1271 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001272 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 if (msg != NULL)
1274 {
1275 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001276 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001278 }
1279 break;
1280
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001281 // store $ENV
1282 case ISN_STOREENV:
1283 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001284 tv = STACK_TV_BOT(0);
1285 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1286 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001287 break;
1288
1289 // store @r
1290 case ISN_STOREREG:
1291 {
1292 int reg = iptr->isn_arg.number;
1293
1294 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001295 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001296 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001297 tv_get_string(tv), -1, FALSE);
1298 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001299 }
1300 break;
1301
1302 // store v: variable
1303 case ISN_STOREV:
1304 --ectx.ec_stack.ga_len;
1305 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1306 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001307 // should not happen, type is checked when compiling
1308 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001309 break;
1310
Bram Moolenaard3aac292020-04-19 14:32:17 +02001311 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001313 case ISN_STOREB:
1314 case ISN_STOREW:
1315 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 {
1317 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001318 hashtab_T *ht;
1319 switch (iptr->isn_type)
1320 {
1321 case ISN_STOREG:
1322 ht = get_globvar_ht();
1323 break;
1324 case ISN_STOREB:
1325 ht = &curbuf->b_vars->dv_hashtab;
1326 break;
1327 case ISN_STOREW:
1328 ht = &curwin->w_vars->dv_hashtab;
1329 break;
1330 case ISN_STORET:
1331 ht = &curtab->tp_vars->dv_hashtab;
1332 break;
1333 default: // Cannot reach here
1334 goto failed;
1335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336
1337 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001338 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001340 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 else
1342 {
1343 clear_tv(&di->di_tv);
1344 di->di_tv = *STACK_TV_BOT(0);
1345 }
1346 }
1347 break;
1348
1349 // store number in local variable
1350 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001351 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 clear_tv(tv);
1353 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001354 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001355 break;
1356
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001357 // store value in list variable
1358 case ISN_STORELIST:
1359 {
1360 typval_T *tv_idx = STACK_TV_BOT(-2);
1361 varnumber_T lidx = tv_idx->vval.v_number;
1362 typval_T *tv_list = STACK_TV_BOT(-1);
1363 list_T *list = tv_list->vval.v_list;
1364
1365 if (lidx < 0 && list->lv_len + lidx >= 0)
1366 // negative index is relative to the end
1367 lidx = list->lv_len + lidx;
1368 if (lidx < 0 || lidx > list->lv_len)
1369 {
1370 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001371 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001372 }
1373 tv = STACK_TV_BOT(-3);
1374 if (lidx < list->lv_len)
1375 {
1376 listitem_T *li = list_find(list, lidx);
1377
1378 // overwrite existing list item
1379 clear_tv(&li->li_tv);
1380 li->li_tv = *tv;
1381 }
1382 else
1383 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001384 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001385 if (list_append_tv(list, tv) == FAIL)
1386 goto failed;
1387 clear_tv(tv);
1388 }
1389 clear_tv(tv_idx);
1390 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001391 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001392 }
1393 break;
1394
1395 // store value in dict variable
1396 case ISN_STOREDICT:
1397 {
1398 typval_T *tv_key = STACK_TV_BOT(-2);
1399 char_u *key = tv_key->vval.v_string;
1400 typval_T *tv_dict = STACK_TV_BOT(-1);
1401 dict_T *dict = tv_dict->vval.v_dict;
1402 dictitem_T *di;
1403
1404 if (key == NULL || *key == NUL)
1405 {
1406 emsg(_(e_emptykey));
Bram Moolenaare8593122020-07-18 15:17:02 +02001407 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001408 }
1409 tv = STACK_TV_BOT(-3);
1410 di = dict_find(dict, key, -1);
1411 if (di != NULL)
1412 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001413 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001414 clear_tv(&di->di_tv);
1415 di->di_tv = *tv;
1416 }
1417 else
1418 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001419 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001420 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1421 goto failed;
1422 clear_tv(tv);
1423 }
1424 clear_tv(tv_key);
1425 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001426 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001427 }
1428 break;
1429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 // push constant
1431 case ISN_PUSHNR:
1432 case ISN_PUSHBOOL:
1433 case ISN_PUSHSPEC:
1434 case ISN_PUSHF:
1435 case ISN_PUSHS:
1436 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001437 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001438 case ISN_PUSHCHANNEL:
1439 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001440 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 goto failed;
1442 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001443 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 ++ectx.ec_stack.ga_len;
1445 switch (iptr->isn_type)
1446 {
1447 case ISN_PUSHNR:
1448 tv->v_type = VAR_NUMBER;
1449 tv->vval.v_number = iptr->isn_arg.number;
1450 break;
1451 case ISN_PUSHBOOL:
1452 tv->v_type = VAR_BOOL;
1453 tv->vval.v_number = iptr->isn_arg.number;
1454 break;
1455 case ISN_PUSHSPEC:
1456 tv->v_type = VAR_SPECIAL;
1457 tv->vval.v_number = iptr->isn_arg.number;
1458 break;
1459#ifdef FEAT_FLOAT
1460 case ISN_PUSHF:
1461 tv->v_type = VAR_FLOAT;
1462 tv->vval.v_float = iptr->isn_arg.fnumber;
1463 break;
1464#endif
1465 case ISN_PUSHBLOB:
1466 blob_copy(iptr->isn_arg.blob, tv);
1467 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001468 case ISN_PUSHFUNC:
1469 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001470 if (iptr->isn_arg.string == NULL)
1471 tv->vval.v_string = NULL;
1472 else
1473 tv->vval.v_string =
1474 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001475 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001476 case ISN_PUSHCHANNEL:
1477#ifdef FEAT_JOB_CHANNEL
1478 tv->v_type = VAR_CHANNEL;
1479 tv->vval.v_channel = iptr->isn_arg.channel;
1480 if (tv->vval.v_channel != NULL)
1481 ++tv->vval.v_channel->ch_refcount;
1482#endif
1483 break;
1484 case ISN_PUSHJOB:
1485#ifdef FEAT_JOB_CHANNEL
1486 tv->v_type = VAR_JOB;
1487 tv->vval.v_job = iptr->isn_arg.job;
1488 if (tv->vval.v_job != NULL)
1489 ++tv->vval.v_job->jv_refcount;
1490#endif
1491 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 default:
1493 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001494 tv->vval.v_string = vim_strsave(
1495 iptr->isn_arg.string == NULL
1496 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 }
1498 break;
1499
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001500 case ISN_UNLET:
1501 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1502 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001503 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001504 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001505 case ISN_UNLETENV:
1506 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1507 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 // create a list from items on the stack; uses a single allocation
1510 // for the list header and the items
1511 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001512 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1513 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001514 break;
1515
1516 // create a dict from items on the stack
1517 case ISN_NEWDICT:
1518 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001519 int count = iptr->isn_arg.number;
1520 dict_T *dict = dict_alloc();
1521 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522
1523 if (dict == NULL)
1524 goto failed;
1525 for (idx = 0; idx < count; ++idx)
1526 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001527 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001529 // check key is unique
1530 item = dict_find(dict, tv->vval.v_string, -1);
1531 if (item != NULL)
1532 {
1533 semsg(_(e_duplicate_key), tv->vval.v_string);
1534 dict_unref(dict);
1535 goto on_error;
1536 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 item = dictitem_alloc(tv->vval.v_string);
1538 clear_tv(tv);
1539 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001540 {
1541 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001543 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1545 item->di_tv.v_lock = 0;
1546 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001547 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001548 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001549 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001551 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 }
1553
1554 if (count > 0)
1555 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001556 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 goto failed;
1558 else
1559 ++ectx.ec_stack.ga_len;
1560 tv = STACK_TV_BOT(-1);
1561 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001562 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 tv->vval.v_dict = dict;
1564 ++dict->dv_refcount;
1565 }
1566 break;
1567
1568 // call a :def function
1569 case ISN_DCALL:
1570 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1571 iptr->isn_arg.dfunc.cdf_argcount,
1572 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001573 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 break;
1575
1576 // call a builtin function
1577 case ISN_BCALL:
1578 SOURCING_LNUM = iptr->isn_lnum;
1579 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1580 iptr->isn_arg.bfunc.cbf_argcount,
1581 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001582 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 break;
1584
1585 // call a funcref or partial
1586 case ISN_PCALL:
1587 {
1588 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1589 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001590 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591
1592 SOURCING_LNUM = iptr->isn_lnum;
1593 if (pfunc->cpf_top)
1594 {
1595 // funcref is above the arguments
1596 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1597 }
1598 else
1599 {
1600 // Get the funcref from the stack.
1601 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001602 partial_tv = *STACK_TV_BOT(0);
1603 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 }
1605 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001606 if (tv == &partial_tv)
1607 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001609 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 }
1611 break;
1612
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001613 case ISN_PCALL_END:
1614 // PCALL finished, arguments have been consumed and replaced by
1615 // the return value. Now clear the funcref from the stack,
1616 // and move the return value in its place.
1617 --ectx.ec_stack.ga_len;
1618 clear_tv(STACK_TV_BOT(-1));
1619 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1620 break;
1621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 // call a user defined function or funcref/partial
1623 case ISN_UCALL:
1624 {
1625 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1626
1627 SOURCING_LNUM = iptr->isn_lnum;
1628 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001629 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001630 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 }
1632 break;
1633
1634 // return from a :def function call
1635 case ISN_RETURN:
1636 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001637 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001638 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001639
1640 if (trystack->ga_len > 0)
1641 trycmd = ((trycmd_T *)trystack->ga_data)
1642 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001643 if (trycmd != NULL
1644 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 && trycmd->tcd_finally_idx != 0)
1646 {
1647 // jump to ":finally"
1648 ectx.ec_iidx = trycmd->tcd_finally_idx;
1649 trycmd->tcd_return = TRUE;
1650 }
1651 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001652 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 }
1654 break;
1655
1656 // push a function reference to a compiled function
1657 case ISN_FUNCREF:
1658 {
1659 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001660 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661
1662 pt = ALLOC_CLEAR_ONE(partial_T);
1663 if (pt == NULL)
1664 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001665 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001666 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001667 vim_free(pt);
1668 goto failed;
1669 }
1670 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1671 + iptr->isn_arg.funcref.fr_func;
1672 pt->pt_func = pt_dfunc->df_ufunc;
1673 pt->pt_refcount = 1;
1674 ++pt_dfunc->df_ufunc->uf_refcount;
1675
1676 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1677 {
1678 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1679 + ectx.ec_dfunc_idx;
1680
1681 // The closure needs to find arguments and local
1682 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001683 pt->pt_ectx_stack = &ectx.ec_stack;
1684 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001685
1686 // If this function returns and the closure is still
1687 // used, we need to make a copy of the context
1688 // (arguments and local variables). Store a reference
1689 // to the partial so we can handle that.
1690 ++pt->pt_refcount;
1691 tv = STACK_TV_VAR(dfunc->df_varcount
1692 + iptr->isn_arg.funcref.fr_var_idx);
1693 if (tv->v_type == VAR_PARTIAL)
1694 {
1695 // TODO: use a garray_T on ectx.
1696 emsg("Multiple closures not supported yet");
1697 goto failed;
1698 }
1699 tv->v_type = VAR_PARTIAL;
1700 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001701 }
1702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 tv = STACK_TV_BOT(0);
1704 ++ectx.ec_stack.ga_len;
1705 tv->vval.v_partial = pt;
1706 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001707 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 }
1709 break;
1710
1711 // jump if a condition is met
1712 case ISN_JUMP:
1713 {
1714 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1715 int jump = TRUE;
1716
1717 if (when != JUMP_ALWAYS)
1718 {
1719 tv = STACK_TV_BOT(-1);
1720 jump = tv2bool(tv);
1721 if (when == JUMP_IF_FALSE
1722 || when == JUMP_AND_KEEP_IF_FALSE)
1723 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001724 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 {
1726 // drop the value from the stack
1727 clear_tv(tv);
1728 --ectx.ec_stack.ga_len;
1729 }
1730 }
1731 if (jump)
1732 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1733 }
1734 break;
1735
1736 // top of a for loop
1737 case ISN_FOR:
1738 {
1739 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1740 typval_T *idxtv =
1741 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1742
1743 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001744 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 goto failed;
1746 if (++idxtv->vval.v_number >= list->lv_len)
1747 // past the end of the list, jump to "endfor"
1748 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1749 else if (list->lv_first == &range_list_item)
1750 {
1751 // non-materialized range() list
1752 tv = STACK_TV_BOT(0);
1753 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001754 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001755 tv->vval.v_number = list_find_nr(
1756 list, idxtv->vval.v_number, NULL);
1757 ++ectx.ec_stack.ga_len;
1758 }
1759 else
1760 {
1761 listitem_T *li = list_find(list, idxtv->vval.v_number);
1762
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1764 ++ectx.ec_stack.ga_len;
1765 }
1766 }
1767 break;
1768
1769 // start of ":try" block
1770 case ISN_TRY:
1771 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001772 trycmd_T *trycmd = NULL;
1773
Bram Moolenaar270d0382020-05-15 21:42:53 +02001774 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 goto failed;
1776 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1777 + ectx.ec_trystack.ga_len;
1778 ++ectx.ec_trystack.ga_len;
1779 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001780 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001781 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1782 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001783 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 }
1785 break;
1786
1787 case ISN_PUSHEXC:
1788 if (current_exception == NULL)
1789 {
1790 iemsg("Evaluating catch while current_exception is NULL");
1791 goto failed;
1792 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001793 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 goto failed;
1795 tv = STACK_TV_BOT(0);
1796 ++ectx.ec_stack.ga_len;
1797 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001798 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 tv->vval.v_string = vim_strsave(
1800 (char_u *)current_exception->value);
1801 break;
1802
1803 case ISN_CATCH:
1804 {
1805 garray_T *trystack = &ectx.ec_trystack;
1806
1807 if (trystack->ga_len > 0)
1808 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001809 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 + trystack->ga_len - 1;
1811 trycmd->tcd_caught = TRUE;
1812 }
1813 did_emsg = got_int = did_throw = FALSE;
1814 catch_exception(current_exception);
1815 }
1816 break;
1817
1818 // end of ":try" block
1819 case ISN_ENDTRY:
1820 {
1821 garray_T *trystack = &ectx.ec_trystack;
1822
1823 if (trystack->ga_len > 0)
1824 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001825 trycmd_T *trycmd = NULL;
1826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 --trystack->ga_len;
1828 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001829 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 trycmd = ((trycmd_T *)trystack->ga_data)
1831 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001832 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 {
1834 // discard the exception
1835 if (caught_stack == current_exception)
1836 caught_stack = caught_stack->caught;
1837 discard_current_exception();
1838 }
1839
1840 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001841 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
1843 }
1844 break;
1845
1846 case ISN_THROW:
1847 --ectx.ec_stack.ga_len;
1848 tv = STACK_TV_BOT(0);
1849 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1850 {
1851 vim_free(tv->vval.v_string);
1852 goto failed;
1853 }
1854 did_throw = TRUE;
1855 break;
1856
1857 // compare with special values
1858 case ISN_COMPAREBOOL:
1859 case ISN_COMPARESPECIAL:
1860 {
1861 typval_T *tv1 = STACK_TV_BOT(-2);
1862 typval_T *tv2 = STACK_TV_BOT(-1);
1863 varnumber_T arg1 = tv1->vval.v_number;
1864 varnumber_T arg2 = tv2->vval.v_number;
1865 int res;
1866
1867 switch (iptr->isn_arg.op.op_type)
1868 {
1869 case EXPR_EQUAL: res = arg1 == arg2; break;
1870 case EXPR_NEQUAL: res = arg1 != arg2; break;
1871 default: res = 0; break;
1872 }
1873
1874 --ectx.ec_stack.ga_len;
1875 tv1->v_type = VAR_BOOL;
1876 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1877 }
1878 break;
1879
1880 // Operation with two number arguments
1881 case ISN_OPNR:
1882 case ISN_COMPARENR:
1883 {
1884 typval_T *tv1 = STACK_TV_BOT(-2);
1885 typval_T *tv2 = STACK_TV_BOT(-1);
1886 varnumber_T arg1 = tv1->vval.v_number;
1887 varnumber_T arg2 = tv2->vval.v_number;
1888 varnumber_T res;
1889
1890 switch (iptr->isn_arg.op.op_type)
1891 {
1892 case EXPR_MULT: res = arg1 * arg2; break;
1893 case EXPR_DIV: res = arg1 / arg2; break;
1894 case EXPR_REM: res = arg1 % arg2; break;
1895 case EXPR_SUB: res = arg1 - arg2; break;
1896 case EXPR_ADD: res = arg1 + arg2; break;
1897
1898 case EXPR_EQUAL: res = arg1 == arg2; break;
1899 case EXPR_NEQUAL: res = arg1 != arg2; break;
1900 case EXPR_GREATER: res = arg1 > arg2; break;
1901 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1902 case EXPR_SMALLER: res = arg1 < arg2; break;
1903 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1904 default: res = 0; break;
1905 }
1906
1907 --ectx.ec_stack.ga_len;
1908 if (iptr->isn_type == ISN_COMPARENR)
1909 {
1910 tv1->v_type = VAR_BOOL;
1911 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1912 }
1913 else
1914 tv1->vval.v_number = res;
1915 }
1916 break;
1917
1918 // Computation with two float arguments
1919 case ISN_OPFLOAT:
1920 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001921#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 {
1923 typval_T *tv1 = STACK_TV_BOT(-2);
1924 typval_T *tv2 = STACK_TV_BOT(-1);
1925 float_T arg1 = tv1->vval.v_float;
1926 float_T arg2 = tv2->vval.v_float;
1927 float_T res = 0;
1928 int cmp = FALSE;
1929
1930 switch (iptr->isn_arg.op.op_type)
1931 {
1932 case EXPR_MULT: res = arg1 * arg2; break;
1933 case EXPR_DIV: res = arg1 / arg2; break;
1934 case EXPR_SUB: res = arg1 - arg2; break;
1935 case EXPR_ADD: res = arg1 + arg2; break;
1936
1937 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1938 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1939 case EXPR_GREATER: cmp = arg1 > arg2; break;
1940 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1941 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1942 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1943 default: cmp = 0; break;
1944 }
1945 --ectx.ec_stack.ga_len;
1946 if (iptr->isn_type == ISN_COMPAREFLOAT)
1947 {
1948 tv1->v_type = VAR_BOOL;
1949 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1950 }
1951 else
1952 tv1->vval.v_float = res;
1953 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001954#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 break;
1956
1957 case ISN_COMPARELIST:
1958 {
1959 typval_T *tv1 = STACK_TV_BOT(-2);
1960 typval_T *tv2 = STACK_TV_BOT(-1);
1961 list_T *arg1 = tv1->vval.v_list;
1962 list_T *arg2 = tv2->vval.v_list;
1963 int cmp = FALSE;
1964 int ic = iptr->isn_arg.op.op_ic;
1965
1966 switch (iptr->isn_arg.op.op_type)
1967 {
1968 case EXPR_EQUAL: cmp =
1969 list_equal(arg1, arg2, ic, FALSE); break;
1970 case EXPR_NEQUAL: cmp =
1971 !list_equal(arg1, arg2, ic, FALSE); break;
1972 case EXPR_IS: cmp = arg1 == arg2; break;
1973 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1974 default: cmp = 0; break;
1975 }
1976 --ectx.ec_stack.ga_len;
1977 clear_tv(tv1);
1978 clear_tv(tv2);
1979 tv1->v_type = VAR_BOOL;
1980 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1981 }
1982 break;
1983
1984 case ISN_COMPAREBLOB:
1985 {
1986 typval_T *tv1 = STACK_TV_BOT(-2);
1987 typval_T *tv2 = STACK_TV_BOT(-1);
1988 blob_T *arg1 = tv1->vval.v_blob;
1989 blob_T *arg2 = tv2->vval.v_blob;
1990 int cmp = FALSE;
1991
1992 switch (iptr->isn_arg.op.op_type)
1993 {
1994 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1995 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1996 case EXPR_IS: cmp = arg1 == arg2; break;
1997 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1998 default: cmp = 0; break;
1999 }
2000 --ectx.ec_stack.ga_len;
2001 clear_tv(tv1);
2002 clear_tv(tv2);
2003 tv1->v_type = VAR_BOOL;
2004 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2005 }
2006 break;
2007
2008 // TODO: handle separately
2009 case ISN_COMPARESTRING:
2010 case ISN_COMPAREDICT:
2011 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002012 case ISN_COMPAREANY:
2013 {
2014 typval_T *tv1 = STACK_TV_BOT(-2);
2015 typval_T *tv2 = STACK_TV_BOT(-1);
2016 exptype_T exptype = iptr->isn_arg.op.op_type;
2017 int ic = iptr->isn_arg.op.op_ic;
2018
2019 typval_compare(tv1, tv2, exptype, ic);
2020 clear_tv(tv2);
2021 tv1->v_type = VAR_BOOL;
2022 tv1->vval.v_number = tv1->vval.v_number
2023 ? VVAL_TRUE : VVAL_FALSE;
2024 --ectx.ec_stack.ga_len;
2025 }
2026 break;
2027
2028 case ISN_ADDLIST:
2029 case ISN_ADDBLOB:
2030 {
2031 typval_T *tv1 = STACK_TV_BOT(-2);
2032 typval_T *tv2 = STACK_TV_BOT(-1);
2033
2034 if (iptr->isn_type == ISN_ADDLIST)
2035 eval_addlist(tv1, tv2);
2036 else
2037 eval_addblob(tv1, tv2);
2038 clear_tv(tv2);
2039 --ectx.ec_stack.ga_len;
2040 }
2041 break;
2042
2043 // Computation with two arguments of unknown type
2044 case ISN_OPANY:
2045 {
2046 typval_T *tv1 = STACK_TV_BOT(-2);
2047 typval_T *tv2 = STACK_TV_BOT(-1);
2048 varnumber_T n1, n2;
2049#ifdef FEAT_FLOAT
2050 float_T f1 = 0, f2 = 0;
2051#endif
2052 int error = FALSE;
2053
2054 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2055 {
2056 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2057 {
2058 eval_addlist(tv1, tv2);
2059 clear_tv(tv2);
2060 --ectx.ec_stack.ga_len;
2061 break;
2062 }
2063 else if (tv1->v_type == VAR_BLOB
2064 && tv2->v_type == VAR_BLOB)
2065 {
2066 eval_addblob(tv1, tv2);
2067 clear_tv(tv2);
2068 --ectx.ec_stack.ga_len;
2069 break;
2070 }
2071 }
2072#ifdef FEAT_FLOAT
2073 if (tv1->v_type == VAR_FLOAT)
2074 {
2075 f1 = tv1->vval.v_float;
2076 n1 = 0;
2077 }
2078 else
2079#endif
2080 {
2081 n1 = tv_get_number_chk(tv1, &error);
2082 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002083 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084#ifdef FEAT_FLOAT
2085 if (tv2->v_type == VAR_FLOAT)
2086 f1 = n1;
2087#endif
2088 }
2089#ifdef FEAT_FLOAT
2090 if (tv2->v_type == VAR_FLOAT)
2091 {
2092 f2 = tv2->vval.v_float;
2093 n2 = 0;
2094 }
2095 else
2096#endif
2097 {
2098 n2 = tv_get_number_chk(tv2, &error);
2099 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002100 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101#ifdef FEAT_FLOAT
2102 if (tv1->v_type == VAR_FLOAT)
2103 f2 = n2;
2104#endif
2105 }
2106#ifdef FEAT_FLOAT
2107 // if there is a float on either side the result is a float
2108 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2109 {
2110 switch (iptr->isn_arg.op.op_type)
2111 {
2112 case EXPR_MULT: f1 = f1 * f2; break;
2113 case EXPR_DIV: f1 = f1 / f2; break;
2114 case EXPR_SUB: f1 = f1 - f2; break;
2115 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002116 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002117 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 }
2119 clear_tv(tv1);
2120 clear_tv(tv2);
2121 tv1->v_type = VAR_FLOAT;
2122 tv1->vval.v_float = f1;
2123 --ectx.ec_stack.ga_len;
2124 }
2125 else
2126#endif
2127 {
2128 switch (iptr->isn_arg.op.op_type)
2129 {
2130 case EXPR_MULT: n1 = n1 * n2; break;
2131 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2132 case EXPR_SUB: n1 = n1 - n2; break;
2133 case EXPR_ADD: n1 = n1 + n2; break;
2134 default: n1 = num_modulus(n1, n2); break;
2135 }
2136 clear_tv(tv1);
2137 clear_tv(tv2);
2138 tv1->v_type = VAR_NUMBER;
2139 tv1->vval.v_number = n1;
2140 --ectx.ec_stack.ga_len;
2141 }
2142 }
2143 break;
2144
2145 case ISN_CONCAT:
2146 {
2147 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2148 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2149 char_u *res;
2150
2151 res = concat_str(str1, str2);
2152 clear_tv(STACK_TV_BOT(-2));
2153 clear_tv(STACK_TV_BOT(-1));
2154 --ectx.ec_stack.ga_len;
2155 STACK_TV_BOT(-1)->vval.v_string = res;
2156 }
2157 break;
2158
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002159 case ISN_STRINDEX:
2160 {
2161 char_u *s;
2162 varnumber_T n;
2163 char_u *res;
2164
2165 // string index: string is at stack-2, index at stack-1
2166 tv = STACK_TV_BOT(-2);
2167 if (tv->v_type != VAR_STRING)
2168 {
2169 emsg(_(e_stringreq));
2170 goto on_error;
2171 }
2172 s = tv->vval.v_string;
2173
2174 tv = STACK_TV_BOT(-1);
2175 if (tv->v_type != VAR_NUMBER)
2176 {
2177 emsg(_(e_number_exp));
2178 goto on_error;
2179 }
2180 n = tv->vval.v_number;
2181
2182 // The resulting variable is a string of a single
2183 // character. If the index is too big or negative the
2184 // result is empty.
2185 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2186 res = NULL;
2187 else
2188 res = vim_strnsave(s + n, 1);
2189 --ectx.ec_stack.ga_len;
2190 tv = STACK_TV_BOT(-1);
2191 vim_free(tv->vval.v_string);
2192 tv->vval.v_string = res;
2193 }
2194 break;
2195
2196 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 {
2198 list_T *list;
2199 varnumber_T n;
2200 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002201 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202
2203 // list index: list is at stack-2, index at stack-1
2204 tv = STACK_TV_BOT(-2);
2205 if (tv->v_type != VAR_LIST)
2206 {
2207 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002208 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 }
2210 list = tv->vval.v_list;
2211
2212 tv = STACK_TV_BOT(-1);
2213 if (tv->v_type != VAR_NUMBER)
2214 {
2215 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002216 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 }
2218 n = tv->vval.v_number;
2219 clear_tv(tv);
2220 if ((li = list_find(list, n)) == NULL)
2221 {
2222 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002223 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 }
2225 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002226 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002227 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002228 tv = STACK_TV_BOT(-1);
2229 temp_tv = *tv;
2230 copy_tv(&li->li_tv, tv);
2231 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002232 }
2233 break;
2234
Bram Moolenaar9af78762020-06-16 11:34:42 +02002235 case ISN_SLICE:
2236 {
2237 list_T *list;
2238 int count = iptr->isn_arg.number;
2239
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002240 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002241 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002242 list = tv->vval.v_list;
2243
2244 // no error for short list, expect it to be checked earlier
2245 if (list != NULL && list->lv_len >= count)
2246 {
2247 list_T *newlist = list_slice(list,
2248 count, list->lv_len - 1);
2249
2250 if (newlist != NULL)
2251 {
2252 list_unref(list);
2253 tv->vval.v_list = newlist;
2254 ++newlist->lv_refcount;
2255 }
2256 }
2257 }
2258 break;
2259
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002260 case ISN_GETITEM:
2261 {
2262 listitem_T *li;
2263 int index = iptr->isn_arg.number;
2264
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002265 // Get list item: list is at stack-1, push item.
2266 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002267 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002268 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002269
2270 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2271 goto failed;
2272 ++ectx.ec_stack.ga_len;
2273 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2274 }
2275 break;
2276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 case ISN_MEMBER:
2278 {
2279 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002280 char_u *key;
2281 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002282 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002283
2284 // dict member: dict is at stack-2, key at stack-1
2285 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002286 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002287 dict = tv->vval.v_dict;
2288
2289 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002290 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002291 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002292
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002293 if ((di = dict_find(dict, key, -1)) == NULL)
2294 {
2295 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002296 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002297 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002298 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002299 --ectx.ec_stack.ga_len;
2300 // Clear the dict after getting the item, to avoid that it
2301 // make the item invalid.
2302 tv = STACK_TV_BOT(-1);
2303 temp_tv = *tv;
2304 copy_tv(&di->di_tv, tv);
2305 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002306 }
2307 break;
2308
2309 // dict member with string key
2310 case ISN_STRINGMEMBER:
2311 {
2312 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002314 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315
2316 tv = STACK_TV_BOT(-1);
2317 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2318 {
2319 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002320 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 }
2322 dict = tv->vval.v_dict;
2323
2324 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2325 == NULL)
2326 {
2327 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002328 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002330 // Clear the dict after getting the item, to avoid that it
2331 // make the item invalid.
2332 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002334 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 }
2336 break;
2337
2338 case ISN_NEGATENR:
2339 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002340 if (tv->v_type != VAR_NUMBER
2341#ifdef FEAT_FLOAT
2342 && tv->v_type != VAR_FLOAT
2343#endif
2344 )
2345 {
2346 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002347 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002348 }
2349#ifdef FEAT_FLOAT
2350 if (tv->v_type == VAR_FLOAT)
2351 tv->vval.v_float = -tv->vval.v_float;
2352 else
2353#endif
2354 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 break;
2356
2357 case ISN_CHECKNR:
2358 {
2359 int error = FALSE;
2360
2361 tv = STACK_TV_BOT(-1);
2362 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002363 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 (void)tv_get_number_chk(tv, &error);
2365 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002366 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002367 }
2368 break;
2369
2370 case ISN_CHECKTYPE:
2371 {
2372 checktype_T *ct = &iptr->isn_arg.type;
2373
2374 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002375 // TODO: better type comparison
2376 if (tv->v_type != ct->ct_type
2377 && !((tv->v_type == VAR_PARTIAL
2378 && ct->ct_type == VAR_FUNC)
2379 || (tv->v_type == VAR_FUNC
2380 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 {
2382 semsg(_("E1029: Expected %s but got %s"),
2383 vartype_name(ct->ct_type),
2384 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002385 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 }
2387 }
2388 break;
2389
Bram Moolenaar9af78762020-06-16 11:34:42 +02002390 case ISN_CHECKLEN:
2391 {
2392 int min_len = iptr->isn_arg.checklen.cl_min_len;
2393 list_T *list = NULL;
2394
2395 tv = STACK_TV_BOT(-1);
2396 if (tv->v_type == VAR_LIST)
2397 list = tv->vval.v_list;
2398 if (list == NULL || list->lv_len < min_len
2399 || (list->lv_len > min_len
2400 && !iptr->isn_arg.checklen.cl_more_OK))
2401 {
2402 semsg(_("E1093: Expected %d items but got %d"),
2403 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002404 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002405 }
2406 }
2407 break;
2408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 case ISN_2BOOL:
2410 {
2411 int n;
2412
2413 tv = STACK_TV_BOT(-1);
2414 n = tv2bool(tv);
2415 if (iptr->isn_arg.number) // invert
2416 n = !n;
2417 clear_tv(tv);
2418 tv->v_type = VAR_BOOL;
2419 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2420 }
2421 break;
2422
2423 case ISN_2STRING:
2424 {
2425 char_u *str;
2426
2427 tv = STACK_TV_BOT(iptr->isn_arg.number);
2428 if (tv->v_type != VAR_STRING)
2429 {
2430 str = typval_tostring(tv);
2431 clear_tv(tv);
2432 tv->v_type = VAR_STRING;
2433 tv->vval.v_string = str;
2434 }
2435 }
2436 break;
2437
Bram Moolenaar389df252020-07-09 21:20:47 +02002438 case ISN_SHUFFLE:
2439 {
2440 typval_T tmp_tv;
2441 int item = iptr->isn_arg.shuffle.shfl_item;
2442 int up = iptr->isn_arg.shuffle.shfl_up;
2443
2444 tmp_tv = *STACK_TV_BOT(-item);
2445 for ( ; up > 0 && item > 1; --up)
2446 {
2447 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2448 --item;
2449 }
2450 *STACK_TV_BOT(-item) = tmp_tv;
2451 }
2452 break;
2453
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 case ISN_DROP:
2455 --ectx.ec_stack.ga_len;
2456 clear_tv(STACK_TV_BOT(0));
2457 break;
2458 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002459 continue;
2460
Bram Moolenaard032f342020-07-18 18:13:02 +02002461func_return:
2462 // Restore previous function. If the frame pointer is zero then there
2463 // is none and we are done.
2464 if (ectx.ec_frame_idx == initial_frame_idx)
2465 {
2466 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2467 // only fails when out of memory
2468 goto failed;
2469 goto done;
2470 }
2471 if (func_return(&ectx) == FAIL)
2472 // only fails when out of memory
2473 goto failed;
2474
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002475on_error:
2476 if (trylevel == 0)
2477 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 }
2479
2480done:
2481 // function finished, get result from the stack.
2482 tv = STACK_TV_BOT(-1);
2483 *rettv = *tv;
2484 tv->v_type = VAR_UNKNOWN;
2485 ret = OK;
2486
2487failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002488 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002489 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002490 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002491failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002492 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002493
2494 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2496 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002497
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002499 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 return ret;
2501}
2502
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503/*
2504 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002505 * We don't really need this at runtime, but we do have tests that require it,
2506 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 */
2508 void
2509ex_disassemble(exarg_T *eap)
2510{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002511 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002512 char_u *fname;
2513 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 dfunc_T *dfunc;
2515 isn_T *instr;
2516 int current;
2517 int line_idx = 0;
2518 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002519 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002521 if (STRNCMP(arg, "<lambda>", 8) == 0)
2522 {
2523 arg += 8;
2524 (void)getdigits(&arg);
2525 fname = vim_strnsave(eap->arg, arg - eap->arg);
2526 }
2527 else
2528 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002529 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002530 if (fname == NULL)
2531 {
2532 semsg(_(e_invarg2), eap->arg);
2533 return;
2534 }
2535
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002536 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002537 if (ufunc == NULL)
2538 {
2539 char_u *p = untrans_function_name(fname);
2540
2541 if (p != NULL)
2542 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002543 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002544 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002545 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 if (ufunc == NULL)
2547 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002548 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 return;
2550 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002551 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002552 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2553 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002554 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002556 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 return;
2558 }
2559 if (ufunc->uf_name_exp != NULL)
2560 msg((char *)ufunc->uf_name_exp);
2561 else
2562 msg((char *)ufunc->uf_name);
2563
2564 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2565 instr = dfunc->df_instr;
2566 for (current = 0; current < dfunc->df_instr_count; ++current)
2567 {
2568 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002569 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570
2571 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2572 {
2573 if (current > prev_current)
2574 {
2575 msg_puts("\n\n");
2576 prev_current = current;
2577 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002578 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2579 if (line != NULL)
2580 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 }
2582
2583 switch (iptr->isn_type)
2584 {
2585 case ISN_EXEC:
2586 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2587 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002588 case ISN_EXECCONCAT:
2589 smsg("%4d EXECCONCAT %lld", current,
2590 (long long)iptr->isn_arg.number);
2591 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 case ISN_ECHO:
2593 {
2594 echo_T *echo = &iptr->isn_arg.echo;
2595
2596 smsg("%4d %s %d", current,
2597 echo->echo_with_white ? "ECHO" : "ECHON",
2598 echo->echo_count);
2599 }
2600 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002601 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002602 smsg("%4d EXECUTE %lld", current,
2603 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002604 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002605 case ISN_ECHOMSG:
2606 smsg("%4d ECHOMSG %lld", current,
2607 (long long)(iptr->isn_arg.number));
2608 break;
2609 case ISN_ECHOERR:
2610 smsg("%4d ECHOERR %lld", current,
2611 (long long)(iptr->isn_arg.number));
2612 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002614 case ISN_LOADOUTER:
2615 {
2616 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2617
2618 if (iptr->isn_arg.number < 0)
2619 smsg("%4d LOAD%s arg[%lld]", current, add,
2620 (long long)(iptr->isn_arg.number
2621 + STACK_FRAME_SIZE));
2622 else
2623 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002624 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002625 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 break;
2627 case ISN_LOADV:
2628 smsg("%4d LOADV v:%s", current,
2629 get_vim_var_name(iptr->isn_arg.number));
2630 break;
2631 case ISN_LOADSCRIPT:
2632 {
2633 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002634 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2636 + iptr->isn_arg.script.script_idx;
2637
2638 smsg("%4d LOADSCRIPT %s from %s", current,
2639 sv->sv_name, si->sn_name);
2640 }
2641 break;
2642 case ISN_LOADS:
2643 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002644 scriptitem_T *si = SCRIPT_ITEM(
2645 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646
2647 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002648 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649 }
2650 break;
2651 case ISN_LOADG:
2652 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2653 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002654 case ISN_LOADB:
2655 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2656 break;
2657 case ISN_LOADW:
2658 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2659 break;
2660 case ISN_LOADT:
2661 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2662 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002663 case ISN_LOADGDICT:
2664 smsg("%4d LOAD g:", current);
2665 break;
2666 case ISN_LOADBDICT:
2667 smsg("%4d LOAD b:", current);
2668 break;
2669 case ISN_LOADWDICT:
2670 smsg("%4d LOAD w:", current);
2671 break;
2672 case ISN_LOADTDICT:
2673 smsg("%4d LOAD t:", current);
2674 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 case ISN_LOADOPT:
2676 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2677 break;
2678 case ISN_LOADENV:
2679 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2680 break;
2681 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002682 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 break;
2684
2685 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002686 case ISN_STOREOUTER:
2687 {
2688 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2689
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002690 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002691 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002692 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002693 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002694 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002695 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002697 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002698 case ISN_STOREV:
2699 smsg("%4d STOREV v:%s", current,
2700 get_vim_var_name(iptr->isn_arg.number));
2701 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002703 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2704 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002705 case ISN_STOREB:
2706 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2707 break;
2708 case ISN_STOREW:
2709 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2710 break;
2711 case ISN_STORET:
2712 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2713 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002714 case ISN_STORES:
2715 {
2716 scriptitem_T *si = SCRIPT_ITEM(
2717 iptr->isn_arg.loadstore.ls_sid);
2718
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002719 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002720 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002721 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 break;
2723 case ISN_STORESCRIPT:
2724 {
2725 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002726 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2728 + iptr->isn_arg.script.script_idx;
2729
2730 smsg("%4d STORESCRIPT %s in %s", current,
2731 sv->sv_name, si->sn_name);
2732 }
2733 break;
2734 case ISN_STOREOPT:
2735 smsg("%4d STOREOPT &%s", current,
2736 iptr->isn_arg.storeopt.so_name);
2737 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002738 case ISN_STOREENV:
2739 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2740 break;
2741 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002742 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002743 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744 case ISN_STORENR:
2745 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002746 iptr->isn_arg.storenr.stnr_val,
2747 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 break;
2749
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002750 case ISN_STORELIST:
2751 smsg("%4d STORELIST", current);
2752 break;
2753
2754 case ISN_STOREDICT:
2755 smsg("%4d STOREDICT", current);
2756 break;
2757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002758 // constants
2759 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002760 smsg("%4d PUSHNR %lld", current,
2761 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 break;
2763 case ISN_PUSHBOOL:
2764 case ISN_PUSHSPEC:
2765 smsg("%4d PUSH %s", current,
2766 get_var_special_name(iptr->isn_arg.number));
2767 break;
2768 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002769#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002771#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002772 break;
2773 case ISN_PUSHS:
2774 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2775 break;
2776 case ISN_PUSHBLOB:
2777 {
2778 char_u *r;
2779 char_u numbuf[NUMBUFLEN];
2780 char_u *tofree;
2781
2782 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002783 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 vim_free(tofree);
2785 }
2786 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002787 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002788 {
2789 char *name = (char *)iptr->isn_arg.string;
2790
2791 smsg("%4d PUSHFUNC \"%s\"", current,
2792 name == NULL ? "[none]" : name);
2793 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002794 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002795 case ISN_PUSHCHANNEL:
2796#ifdef FEAT_JOB_CHANNEL
2797 {
2798 channel_T *channel = iptr->isn_arg.channel;
2799
2800 smsg("%4d PUSHCHANNEL %d", current,
2801 channel == NULL ? 0 : channel->ch_id);
2802 }
2803#endif
2804 break;
2805 case ISN_PUSHJOB:
2806#ifdef FEAT_JOB_CHANNEL
2807 {
2808 typval_T tv;
2809 char_u *name;
2810
2811 tv.v_type = VAR_JOB;
2812 tv.vval.v_job = iptr->isn_arg.job;
2813 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002814 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002815 }
2816#endif
2817 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 case ISN_PUSHEXC:
2819 smsg("%4d PUSH v:exception", current);
2820 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002821 case ISN_UNLET:
2822 smsg("%4d UNLET%s %s", current,
2823 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2824 iptr->isn_arg.unlet.ul_name);
2825 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002826 case ISN_UNLETENV:
2827 smsg("%4d UNLETENV%s $%s", current,
2828 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2829 iptr->isn_arg.unlet.ul_name);
2830 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002831 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002832 smsg("%4d NEWLIST size %lld", current,
2833 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002834 break;
2835 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002836 smsg("%4d NEWDICT size %lld", current,
2837 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 break;
2839
2840 // function call
2841 case ISN_BCALL:
2842 {
2843 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2844
2845 smsg("%4d BCALL %s(argc %d)", current,
2846 internal_func_name(cbfunc->cbf_idx),
2847 cbfunc->cbf_argcount);
2848 }
2849 break;
2850 case ISN_DCALL:
2851 {
2852 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2853 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2854 + cdfunc->cdf_idx;
2855
2856 smsg("%4d DCALL %s(argc %d)", current,
2857 df->df_ufunc->uf_name_exp != NULL
2858 ? df->df_ufunc->uf_name_exp
2859 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2860 }
2861 break;
2862 case ISN_UCALL:
2863 {
2864 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2865
2866 smsg("%4d UCALL %s(argc %d)", current,
2867 cufunc->cuf_name, cufunc->cuf_argcount);
2868 }
2869 break;
2870 case ISN_PCALL:
2871 {
2872 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2873
2874 smsg("%4d PCALL%s (argc %d)", current,
2875 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2876 }
2877 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002878 case ISN_PCALL_END:
2879 smsg("%4d PCALL end", current);
2880 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 case ISN_RETURN:
2882 smsg("%4d RETURN", current);
2883 break;
2884 case ISN_FUNCREF:
2885 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002886 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002887 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002888 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002890 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002891 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 }
2893 break;
2894
2895 case ISN_JUMP:
2896 {
2897 char *when = "?";
2898
2899 switch (iptr->isn_arg.jump.jump_when)
2900 {
2901 case JUMP_ALWAYS:
2902 when = "JUMP";
2903 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 case JUMP_AND_KEEP_IF_TRUE:
2905 when = "JUMP_AND_KEEP_IF_TRUE";
2906 break;
2907 case JUMP_IF_FALSE:
2908 when = "JUMP_IF_FALSE";
2909 break;
2910 case JUMP_AND_KEEP_IF_FALSE:
2911 when = "JUMP_AND_KEEP_IF_FALSE";
2912 break;
2913 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002914 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 iptr->isn_arg.jump.jump_where);
2916 }
2917 break;
2918
2919 case ISN_FOR:
2920 {
2921 forloop_T *forloop = &iptr->isn_arg.forloop;
2922
2923 smsg("%4d FOR $%d -> %d", current,
2924 forloop->for_idx, forloop->for_end);
2925 }
2926 break;
2927
2928 case ISN_TRY:
2929 {
2930 try_T *try = &iptr->isn_arg.try;
2931
2932 smsg("%4d TRY catch -> %d, finally -> %d", current,
2933 try->try_catch, try->try_finally);
2934 }
2935 break;
2936 case ISN_CATCH:
2937 // TODO
2938 smsg("%4d CATCH", current);
2939 break;
2940 case ISN_ENDTRY:
2941 smsg("%4d ENDTRY", current);
2942 break;
2943 case ISN_THROW:
2944 smsg("%4d THROW", current);
2945 break;
2946
2947 // expression operations on number
2948 case ISN_OPNR:
2949 case ISN_OPFLOAT:
2950 case ISN_OPANY:
2951 {
2952 char *what;
2953 char *ins;
2954
2955 switch (iptr->isn_arg.op.op_type)
2956 {
2957 case EXPR_MULT: what = "*"; break;
2958 case EXPR_DIV: what = "/"; break;
2959 case EXPR_REM: what = "%"; break;
2960 case EXPR_SUB: what = "-"; break;
2961 case EXPR_ADD: what = "+"; break;
2962 default: what = "???"; break;
2963 }
2964 switch (iptr->isn_type)
2965 {
2966 case ISN_OPNR: ins = "OPNR"; break;
2967 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2968 case ISN_OPANY: ins = "OPANY"; break;
2969 default: ins = "???"; break;
2970 }
2971 smsg("%4d %s %s", current, ins, what);
2972 }
2973 break;
2974
2975 case ISN_COMPAREBOOL:
2976 case ISN_COMPARESPECIAL:
2977 case ISN_COMPARENR:
2978 case ISN_COMPAREFLOAT:
2979 case ISN_COMPARESTRING:
2980 case ISN_COMPAREBLOB:
2981 case ISN_COMPARELIST:
2982 case ISN_COMPAREDICT:
2983 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 case ISN_COMPAREANY:
2985 {
2986 char *p;
2987 char buf[10];
2988 char *type;
2989
2990 switch (iptr->isn_arg.op.op_type)
2991 {
2992 case EXPR_EQUAL: p = "=="; break;
2993 case EXPR_NEQUAL: p = "!="; break;
2994 case EXPR_GREATER: p = ">"; break;
2995 case EXPR_GEQUAL: p = ">="; break;
2996 case EXPR_SMALLER: p = "<"; break;
2997 case EXPR_SEQUAL: p = "<="; break;
2998 case EXPR_MATCH: p = "=~"; break;
2999 case EXPR_IS: p = "is"; break;
3000 case EXPR_ISNOT: p = "isnot"; break;
3001 case EXPR_NOMATCH: p = "!~"; break;
3002 default: p = "???"; break;
3003 }
3004 STRCPY(buf, p);
3005 if (iptr->isn_arg.op.op_ic == TRUE)
3006 strcat(buf, "?");
3007 switch(iptr->isn_type)
3008 {
3009 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3010 case ISN_COMPARESPECIAL:
3011 type = "COMPARESPECIAL"; break;
3012 case ISN_COMPARENR: type = "COMPARENR"; break;
3013 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3014 case ISN_COMPARESTRING:
3015 type = "COMPARESTRING"; break;
3016 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3017 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3018 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3019 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3021 default: type = "???"; break;
3022 }
3023
3024 smsg("%4d %s %s", current, type, buf);
3025 }
3026 break;
3027
3028 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3029 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3030
3031 // expression operations
3032 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003033 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3034 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003035 case ISN_SLICE: smsg("%4d SLICE %lld",
3036 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003037 case ISN_GETITEM: smsg("%4d ITEM %lld",
3038 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003039 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3040 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003041 iptr->isn_arg.string); break;
3042 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3043
3044 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3045 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3046 vartype_name(iptr->isn_arg.type.ct_type),
3047 iptr->isn_arg.type.ct_off);
3048 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003049 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3050 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3051 iptr->isn_arg.checklen.cl_min_len);
3052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 case ISN_2BOOL: if (iptr->isn_arg.number)
3054 smsg("%4d INVERT (!val)", current);
3055 else
3056 smsg("%4d 2BOOL (!!val)", current);
3057 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003058 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3059 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003060 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003061
Bram Moolenaar389df252020-07-09 21:20:47 +02003062 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3063 iptr->isn_arg.shuffle.shfl_item,
3064 iptr->isn_arg.shuffle.shfl_up);
3065 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003066 case ISN_DROP: smsg("%4d DROP", current); break;
3067 }
3068 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003069}
3070
3071/*
3072 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3073 * list, etc. Mostly like what JavaScript does, except that empty list and
3074 * empty dictionary are FALSE.
3075 */
3076 int
3077tv2bool(typval_T *tv)
3078{
3079 switch (tv->v_type)
3080 {
3081 case VAR_NUMBER:
3082 return tv->vval.v_number != 0;
3083 case VAR_FLOAT:
3084#ifdef FEAT_FLOAT
3085 return tv->vval.v_float != 0.0;
3086#else
3087 break;
3088#endif
3089 case VAR_PARTIAL:
3090 return tv->vval.v_partial != NULL;
3091 case VAR_FUNC:
3092 case VAR_STRING:
3093 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3094 case VAR_LIST:
3095 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3096 case VAR_DICT:
3097 return tv->vval.v_dict != NULL
3098 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3099 case VAR_BOOL:
3100 case VAR_SPECIAL:
3101 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3102 case VAR_JOB:
3103#ifdef FEAT_JOB_CHANNEL
3104 return tv->vval.v_job != NULL;
3105#else
3106 break;
3107#endif
3108 case VAR_CHANNEL:
3109#ifdef FEAT_JOB_CHANNEL
3110 return tv->vval.v_channel != NULL;
3111#else
3112 break;
3113#endif
3114 case VAR_BLOB:
3115 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3116 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003117 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003118 case VAR_VOID:
3119 break;
3120 }
3121 return FALSE;
3122}
3123
3124/*
3125 * If "tv" is a string give an error and return FAIL.
3126 */
3127 int
3128check_not_string(typval_T *tv)
3129{
3130 if (tv->v_type == VAR_STRING)
3131 {
3132 emsg(_("E1030: Using a String as a Number"));
3133 clear_tv(tv);
3134 return FAIL;
3135 }
3136 return OK;
3137}
3138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003139
3140#endif // FEAT_EVAL