blob: a3cea806ee31a668c6a142ed89dd3661fff6d0d0 [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
464/*
465 * Call a builtin function by index.
466 */
467 static int
468call_bfunc(int func_idx, int argcount, ectx_T *ectx)
469{
470 typval_T argvars[MAX_FUNC_ARGS];
471 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200472 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473
474 if (call_prepare(argcount, argvars, ectx) == FAIL)
475 return FAIL;
476
477 // Call the builtin function.
478 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
479
480 // Clear the arguments.
481 for (idx = 0; idx < argcount; ++idx)
482 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200483
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200484 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200485 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486 return OK;
487}
488
489/*
490 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100491 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 */
493 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100494call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495{
496 typval_T argvars[MAX_FUNC_ARGS];
497 funcexe_T funcexe;
498 int error;
499 int idx;
500
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200501 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200502 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
503 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200504 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100505 {
506 // The function has been compiled, can call it quickly. For a function
507 // that was defined later: we can call it directly next time.
508 if (iptr != NULL)
509 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100510 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100511 iptr->isn_type = ISN_DCALL;
512 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
513 iptr->isn_arg.dfunc.cdf_argcount = argcount;
514 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100517
518 if (call_prepare(argcount, argvars, ectx) == FAIL)
519 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200520 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521 funcexe.evaluate = TRUE;
522
523 // Call the user function. Result goes in last position on the stack.
524 // TODO: add selfdict if there is one
525 error = call_user_func_check(ufunc, argcount, argvars,
526 STACK_TV_BOT(-1), &funcexe, NULL);
527
528 // Clear the arguments.
529 for (idx = 0; idx < argcount; ++idx)
530 clear_tv(&argvars[idx]);
531
532 if (error != FCERR_NONE)
533 {
534 user_func_error(error, ufunc->uf_name);
535 return FAIL;
536 }
537 return OK;
538}
539
540/*
541 * Execute a function by "name".
542 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100543 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544 * Returns FAIL if not found without an error message.
545 */
546 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100547call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548{
549 ufunc_T *ufunc;
550
551 if (builtin_function(name, -1))
552 {
553 int func_idx = find_internal_func(name);
554
555 if (func_idx < 0)
556 return FAIL;
557 if (check_internal_func(func_idx, argcount) == FAIL)
558 return FAIL;
559 return call_bfunc(func_idx, argcount, ectx);
560 }
561
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200562 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100564 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565
566 return FAIL;
567}
568
569 static int
570call_partial(typval_T *tv, int argcount, ectx_T *ectx)
571{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200572 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100573 int called_emsg_before = called_emsg;
574
575 if (tv->v_type == VAR_PARTIAL)
576 {
577 partial_T *pt = tv->vval.v_partial;
578
579 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200580 {
581 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
582
583 // closure may need the function context where it was defined
584 ectx->ec_outer_stack = pt->pt_ectx_stack;
585 ectx->ec_outer_frame = pt->pt_ectx_frame;
586
587 return ret;
588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 name = pt->pt_name;
590 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200591 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200593 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 {
595 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200596 semsg(_(e_unknownfunc),
597 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598 return FAIL;
599 }
600 return OK;
601}
602
603/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100604 * Store "tv" in variable "name".
605 * This is for s: and g: variables.
606 */
607 static void
608store_var(char_u *name, typval_T *tv)
609{
610 funccal_entry_T entry;
611
612 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200613 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100614 restore_funccal();
615}
616
Bram Moolenaard3aac292020-04-19 14:32:17 +0200617
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100618/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619 * Execute a function by "name".
620 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100621 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100622 */
623 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100624call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625{
626 int called_emsg_before = called_emsg;
627
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100628 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 && called_emsg == called_emsg_before)
630 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200631 dictitem_T *v;
632
633 v = find_var(name, NULL, FALSE);
634 if (v == NULL)
635 {
636 semsg(_(e_unknownfunc), name);
637 return FAIL;
638 }
639 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
640 {
641 semsg(_(e_unknownfunc), name);
642 return FAIL;
643 }
644 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 }
646 return OK;
647}
648
649/*
650 * Call a "def" function from old Vim script.
651 * Return OK or FAIL.
652 */
653 int
654call_def_function(
655 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200656 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200658 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 typval_T *rettv) // return value
660{
661 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200662 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664 typval_T *tv;
665 int idx;
666 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100667 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200668 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200669 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200670 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100671
672// Get pointer to item in the stack.
673#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
674
675// Get pointer to item at the bottom of the stack, -1 is the bottom.
676#undef STACK_TV_BOT
677#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
678
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200679// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200680#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 +0100681
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200682// Like STACK_TV_VAR but use the outer scope
683#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
684
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200685 if (ufunc->uf_def_status == UF_NOT_COMPILED
686 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200687 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200688 {
689 if (called_emsg == called_emsg_before)
690 semsg(_("E1091: Function is not compiled: %s"),
691 ufunc->uf_name_exp == NULL
692 ? ufunc->uf_name : ufunc->uf_name_exp);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200693 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200694 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200695
Bram Moolenaar09689a02020-05-09 22:50:08 +0200696 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200697 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200698 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
699 + ufunc->uf_dfunc_idx;
700 if (dfunc->df_instr == NULL)
701 return FAIL;
702 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200704 CLEAR_FIELD(ectx);
705 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
706 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
707 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
708 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
710
711 // Put arguments on the stack.
712 for (idx = 0; idx < argc; ++idx)
713 {
714 copy_tv(&argv[idx], STACK_TV_BOT(0));
715 ++ectx.ec_stack.ga_len;
716 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200717
718 // Turn varargs into a list. Empty list if no args.
719 if (ufunc->uf_va_name != NULL)
720 {
721 int vararg_count = argc - ufunc->uf_args.ga_len;
722
723 if (vararg_count < 0)
724 vararg_count = 0;
725 else
726 argc -= vararg_count;
727 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200728 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200729 if (defcount > 0)
730 // Move varargs list to below missing default arguments.
731 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
732 --ectx.ec_stack.ga_len;
733 }
734
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100735 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200736 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100737 if (defcount > 0)
738 for (idx = 0; idx < defcount; ++idx)
739 {
740 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
741 ++ectx.ec_stack.ga_len;
742 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200743 if (ufunc->uf_va_name != NULL)
744 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745
746 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200747 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
748 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200750 if (partial != NULL)
751 {
752 ectx.ec_outer_stack = partial->pt_ectx_stack;
753 ectx.ec_outer_frame = partial->pt_ectx_frame;
754 }
755
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 // dummy frame entries
757 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
758 {
759 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
760 ++ectx.ec_stack.ga_len;
761 }
762
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200763 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200764 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200765 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
766 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200767 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200769 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200770 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200771 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200772
773 ectx.ec_instr = dfunc->df_instr;
774 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100775
Bram Moolenaara26b9702020-04-18 19:53:28 +0200776 // Commands behave like vim9script.
777 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
778
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100779 // Decide where to start execution, handles optional arguments.
780 init_instr_idx(ufunc, argc, &ectx);
781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 for (;;)
783 {
784 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100785
Bram Moolenaar270d0382020-05-15 21:42:53 +0200786 if (++breakcheck_count >= 100)
787 {
788 line_breakcheck();
789 breakcheck_count = 0;
790 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100791 if (got_int)
792 {
793 // Turn CTRL-C into an exception.
794 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100795 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100796 goto failed;
797 did_throw = TRUE;
798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799
Bram Moolenaara26b9702020-04-18 19:53:28 +0200800 if (did_emsg && msg_list != NULL && *msg_list != NULL)
801 {
802 // Turn an error message into an exception.
803 did_emsg = FALSE;
804 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
805 goto failed;
806 did_throw = TRUE;
807 *msg_list = NULL;
808 }
809
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 if (did_throw && !ectx.ec_in_catch)
811 {
812 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100813 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814
815 // An exception jumps to the first catch, finally, or returns from
816 // the current function.
817 if (trystack->ga_len > 0)
818 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200819 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 {
821 // jump to ":catch" or ":finally"
822 ectx.ec_in_catch = TRUE;
823 ectx.ec_iidx = trycmd->tcd_catch_idx;
824 }
825 else
826 {
827 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200828 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 {
830 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200831 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832 goto failed;
833 tv = STACK_TV_BOT(0);
834 tv->v_type = VAR_NUMBER;
835 tv->vval.v_number = 0;
836 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100837 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200838 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
839 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 goto done;
841 }
842
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200843 if (func_return(&ectx) == FAIL)
844 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845 }
846 continue;
847 }
848
849 iptr = &ectx.ec_instr[ectx.ec_iidx++];
850 switch (iptr->isn_type)
851 {
852 // execute Ex command line
853 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200854 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855 do_cmdline_cmd(iptr->isn_arg.string);
856 break;
857
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200858 // execute Ex command from pieces on the stack
859 case ISN_EXECCONCAT:
860 {
861 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200862 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200863 int pass;
864 int i;
865 char_u *cmd = NULL;
866 char_u *str;
867
868 for (pass = 1; pass <= 2; ++pass)
869 {
870 for (i = 0; i < count; ++i)
871 {
872 tv = STACK_TV_BOT(i - count);
873 str = tv->vval.v_string;
874 if (str != NULL && *str != NUL)
875 {
876 if (pass == 2)
877 STRCPY(cmd + len, str);
878 len += STRLEN(str);
879 }
880 if (pass == 2)
881 clear_tv(tv);
882 }
883 if (pass == 1)
884 {
885 cmd = alloc(len + 1);
886 if (cmd == NULL)
887 goto failed;
888 len = 0;
889 }
890 }
891
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200892 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200893 do_cmdline_cmd(cmd);
894 vim_free(cmd);
895 }
896 break;
897
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 // execute :echo {string} ...
899 case ISN_ECHO:
900 {
901 int count = iptr->isn_arg.echo.echo_count;
902 int atstart = TRUE;
903 int needclr = TRUE;
904
905 for (idx = 0; idx < count; ++idx)
906 {
907 tv = STACK_TV_BOT(idx - count);
908 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
909 &atstart, &needclr);
910 clear_tv(tv);
911 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100912 if (needclr)
913 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 ectx.ec_stack.ga_len -= count;
915 }
916 break;
917
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200918 // :execute {string} ...
919 // :echomsg {string} ...
920 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100921 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200922 case ISN_ECHOMSG:
923 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100924 {
925 int count = iptr->isn_arg.number;
926 garray_T ga;
927 char_u buf[NUMBUFLEN];
928 char_u *p;
929 int len;
930 int failed = FALSE;
931
932 ga_init2(&ga, 1, 80);
933 for (idx = 0; idx < count; ++idx)
934 {
935 tv = STACK_TV_BOT(idx - count);
936 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
937 {
938 emsg(_(e_inval_string));
939 break;
940 }
941 else
942 p = tv_get_string_buf(tv, buf);
943
944 len = (int)STRLEN(p);
945 if (ga_grow(&ga, len + 2) == FAIL)
946 failed = TRUE;
947 else
948 {
949 if (ga.ga_len > 0)
950 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
951 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
952 ga.ga_len += len;
953 }
954 clear_tv(tv);
955 }
956 ectx.ec_stack.ga_len -= count;
957
958 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200959 {
960 if (iptr->isn_type == ISN_EXECUTE)
961 do_cmdline_cmd((char_u *)ga.ga_data);
962 else
963 {
964 msg_sb_eol();
965 if (iptr->isn_type == ISN_ECHOMSG)
966 {
967 msg_attr(ga.ga_data, echo_attr);
968 out_flush();
969 }
970 else
971 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200972 SOURCING_LNUM = iptr->isn_lnum;
973 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200974 }
975 }
976 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100977 ga_clear(&ga);
978 }
979 break;
980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 // load local variable or argument
982 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200983 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 goto failed;
985 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
986 ++ectx.ec_stack.ga_len;
987 break;
988
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200989 // load variable or argument from outer scope
990 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200991 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200992 goto failed;
993 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
994 STACK_TV_BOT(0));
995 ++ectx.ec_stack.ga_len;
996 break;
997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 // load v: variable
999 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001000 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 goto failed;
1002 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1003 ++ectx.ec_stack.ga_len;
1004 break;
1005
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001006 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 case ISN_LOADSCRIPT:
1008 {
1009 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001010 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 svar_T *sv;
1012
1013 sv = ((svar_T *)si->sn_var_vals.ga_data)
1014 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001015 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016 goto failed;
1017 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1018 ++ectx.ec_stack.ga_len;
1019 }
1020 break;
1021
1022 // load s: variable in old script
1023 case ISN_LOADS:
1024 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001025 hashtab_T *ht = &SCRIPT_VARS(
1026 iptr->isn_arg.loadstore.ls_sid);
1027 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001029
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if (di == NULL)
1031 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001032 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 goto failed;
1034 }
1035 else
1036 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001037 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001038 goto failed;
1039 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1040 ++ectx.ec_stack.ga_len;
1041 }
1042 }
1043 break;
1044
Bram Moolenaard3aac292020-04-19 14:32:17 +02001045 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001047 case ISN_LOADB:
1048 case ISN_LOADW:
1049 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001051 dictitem_T *di = NULL;
1052 hashtab_T *ht = NULL;
1053 char namespace;
1054 switch (iptr->isn_type)
1055 {
1056 case ISN_LOADG:
1057 ht = get_globvar_ht();
1058 namespace = 'g';
1059 break;
1060 case ISN_LOADB:
1061 ht = &curbuf->b_vars->dv_hashtab;
1062 namespace = 'b';
1063 break;
1064 case ISN_LOADW:
1065 ht = &curwin->w_vars->dv_hashtab;
1066 namespace = 'w';
1067 break;
1068 case ISN_LOADT:
1069 ht = &curtab->tp_vars->dv_hashtab;
1070 namespace = 't';
1071 break;
1072 default: // Cannot reach here
1073 goto failed;
1074 }
1075 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001076
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 if (di == NULL)
1078 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001079 semsg(_("E121: Undefined variable: %c:%s"),
1080 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 goto failed;
1082 }
1083 else
1084 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001085 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 goto failed;
1087 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1088 ++ectx.ec_stack.ga_len;
1089 }
1090 }
1091 break;
1092
1093 // load &option
1094 case ISN_LOADOPT:
1095 {
1096 typval_T optval;
1097 char_u *name = iptr->isn_arg.string;
1098
Bram Moolenaara8c17702020-04-01 21:17:24 +02001099 // This is not expected to fail, name is checked during
1100 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001101 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001103 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001104 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 *STACK_TV_BOT(0) = optval;
1106 ++ectx.ec_stack.ga_len;
1107 }
1108 break;
1109
1110 // load $ENV
1111 case ISN_LOADENV:
1112 {
1113 typval_T optval;
1114 char_u *name = iptr->isn_arg.string;
1115
Bram Moolenaar270d0382020-05-15 21:42:53 +02001116 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001118 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001119 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 *STACK_TV_BOT(0) = optval;
1121 ++ectx.ec_stack.ga_len;
1122 }
1123 break;
1124
1125 // load @register
1126 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001127 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 goto failed;
1129 tv = STACK_TV_BOT(0);
1130 tv->v_type = VAR_STRING;
1131 tv->vval.v_string = get_reg_contents(
1132 iptr->isn_arg.number, GREG_EXPR_SRC);
1133 ++ectx.ec_stack.ga_len;
1134 break;
1135
1136 // store local variable
1137 case ISN_STORE:
1138 --ectx.ec_stack.ga_len;
1139 tv = STACK_TV_VAR(iptr->isn_arg.number);
1140 clear_tv(tv);
1141 *tv = *STACK_TV_BOT(0);
1142 break;
1143
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001144 // store variable or argument in outer scope
1145 case ISN_STOREOUTER:
1146 --ectx.ec_stack.ga_len;
1147 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1148 clear_tv(tv);
1149 *tv = *STACK_TV_BOT(0);
1150 break;
1151
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001152 // store s: variable in old script
1153 case ISN_STORES:
1154 {
1155 hashtab_T *ht = &SCRIPT_VARS(
1156 iptr->isn_arg.loadstore.ls_sid);
1157 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001158 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001159
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001160 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001161 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001162 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001163 else
1164 {
1165 clear_tv(&di->di_tv);
1166 di->di_tv = *STACK_TV_BOT(0);
1167 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001168 }
1169 break;
1170
1171 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 case ISN_STORESCRIPT:
1173 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001174 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 iptr->isn_arg.script.script_sid);
1176 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1177 + iptr->isn_arg.script.script_idx;
1178
1179 --ectx.ec_stack.ga_len;
1180 clear_tv(sv->sv_tv);
1181 *sv->sv_tv = *STACK_TV_BOT(0);
1182 }
1183 break;
1184
1185 // store option
1186 case ISN_STOREOPT:
1187 {
1188 long n = 0;
1189 char_u *s = NULL;
1190 char *msg;
1191
1192 --ectx.ec_stack.ga_len;
1193 tv = STACK_TV_BOT(0);
1194 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001195 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001197 if (s == NULL)
1198 s = (char_u *)"";
1199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001201 // must be VAR_NUMBER, CHECKTYPE makes sure
1202 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1204 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001205 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 if (msg != NULL)
1207 {
1208 emsg(_(msg));
1209 goto failed;
1210 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001211 }
1212 break;
1213
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001214 // store $ENV
1215 case ISN_STOREENV:
1216 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001217 tv = STACK_TV_BOT(0);
1218 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1219 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001220 break;
1221
1222 // store @r
1223 case ISN_STOREREG:
1224 {
1225 int reg = iptr->isn_arg.number;
1226
1227 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001228 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001229 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001230 tv_get_string(tv), -1, FALSE);
1231 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001232 }
1233 break;
1234
1235 // store v: variable
1236 case ISN_STOREV:
1237 --ectx.ec_stack.ga_len;
1238 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1239 == FAIL)
1240 goto failed;
1241 break;
1242
Bram Moolenaard3aac292020-04-19 14:32:17 +02001243 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001244 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001245 case ISN_STOREB:
1246 case ISN_STOREW:
1247 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 {
1249 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001250 hashtab_T *ht;
1251 switch (iptr->isn_type)
1252 {
1253 case ISN_STOREG:
1254 ht = get_globvar_ht();
1255 break;
1256 case ISN_STOREB:
1257 ht = &curbuf->b_vars->dv_hashtab;
1258 break;
1259 case ISN_STOREW:
1260 ht = &curwin->w_vars->dv_hashtab;
1261 break;
1262 case ISN_STORET:
1263 ht = &curtab->tp_vars->dv_hashtab;
1264 break;
1265 default: // Cannot reach here
1266 goto failed;
1267 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001268
1269 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001270 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001272 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 else
1274 {
1275 clear_tv(&di->di_tv);
1276 di->di_tv = *STACK_TV_BOT(0);
1277 }
1278 }
1279 break;
1280
1281 // store number in local variable
1282 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001283 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 clear_tv(tv);
1285 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001286 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 break;
1288
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001289 // store value in list variable
1290 case ISN_STORELIST:
1291 {
1292 typval_T *tv_idx = STACK_TV_BOT(-2);
1293 varnumber_T lidx = tv_idx->vval.v_number;
1294 typval_T *tv_list = STACK_TV_BOT(-1);
1295 list_T *list = tv_list->vval.v_list;
1296
1297 if (lidx < 0 && list->lv_len + lidx >= 0)
1298 // negative index is relative to the end
1299 lidx = list->lv_len + lidx;
1300 if (lidx < 0 || lidx > list->lv_len)
1301 {
1302 semsg(_(e_listidx), lidx);
1303 goto failed;
1304 }
1305 tv = STACK_TV_BOT(-3);
1306 if (lidx < list->lv_len)
1307 {
1308 listitem_T *li = list_find(list, lidx);
1309
1310 // overwrite existing list item
1311 clear_tv(&li->li_tv);
1312 li->li_tv = *tv;
1313 }
1314 else
1315 {
1316 // append to list
1317 if (list_append_tv(list, tv) == FAIL)
1318 goto failed;
1319 clear_tv(tv);
1320 }
1321 clear_tv(tv_idx);
1322 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001323 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001324 }
1325 break;
1326
1327 // store value in dict variable
1328 case ISN_STOREDICT:
1329 {
1330 typval_T *tv_key = STACK_TV_BOT(-2);
1331 char_u *key = tv_key->vval.v_string;
1332 typval_T *tv_dict = STACK_TV_BOT(-1);
1333 dict_T *dict = tv_dict->vval.v_dict;
1334 dictitem_T *di;
1335
1336 if (key == NULL || *key == NUL)
1337 {
1338 emsg(_(e_emptykey));
1339 goto failed;
1340 }
1341 tv = STACK_TV_BOT(-3);
1342 di = dict_find(dict, key, -1);
1343 if (di != NULL)
1344 {
1345 clear_tv(&di->di_tv);
1346 di->di_tv = *tv;
1347 }
1348 else
1349 {
1350 // add to dict
1351 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1352 goto failed;
1353 clear_tv(tv);
1354 }
1355 clear_tv(tv_key);
1356 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001357 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001358 }
1359 break;
1360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 // push constant
1362 case ISN_PUSHNR:
1363 case ISN_PUSHBOOL:
1364 case ISN_PUSHSPEC:
1365 case ISN_PUSHF:
1366 case ISN_PUSHS:
1367 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001368 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001369 case ISN_PUSHCHANNEL:
1370 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001371 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 goto failed;
1373 tv = STACK_TV_BOT(0);
1374 ++ectx.ec_stack.ga_len;
1375 switch (iptr->isn_type)
1376 {
1377 case ISN_PUSHNR:
1378 tv->v_type = VAR_NUMBER;
1379 tv->vval.v_number = iptr->isn_arg.number;
1380 break;
1381 case ISN_PUSHBOOL:
1382 tv->v_type = VAR_BOOL;
1383 tv->vval.v_number = iptr->isn_arg.number;
1384 break;
1385 case ISN_PUSHSPEC:
1386 tv->v_type = VAR_SPECIAL;
1387 tv->vval.v_number = iptr->isn_arg.number;
1388 break;
1389#ifdef FEAT_FLOAT
1390 case ISN_PUSHF:
1391 tv->v_type = VAR_FLOAT;
1392 tv->vval.v_float = iptr->isn_arg.fnumber;
1393 break;
1394#endif
1395 case ISN_PUSHBLOB:
1396 blob_copy(iptr->isn_arg.blob, tv);
1397 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001398 case ISN_PUSHFUNC:
1399 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001400 if (iptr->isn_arg.string == NULL)
1401 tv->vval.v_string = NULL;
1402 else
1403 tv->vval.v_string =
1404 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001405 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001406 case ISN_PUSHCHANNEL:
1407#ifdef FEAT_JOB_CHANNEL
1408 tv->v_type = VAR_CHANNEL;
1409 tv->vval.v_channel = iptr->isn_arg.channel;
1410 if (tv->vval.v_channel != NULL)
1411 ++tv->vval.v_channel->ch_refcount;
1412#endif
1413 break;
1414 case ISN_PUSHJOB:
1415#ifdef FEAT_JOB_CHANNEL
1416 tv->v_type = VAR_JOB;
1417 tv->vval.v_job = iptr->isn_arg.job;
1418 if (tv->vval.v_job != NULL)
1419 ++tv->vval.v_job->jv_refcount;
1420#endif
1421 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001422 default:
1423 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001424 tv->vval.v_string = vim_strsave(
1425 iptr->isn_arg.string == NULL
1426 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 }
1428 break;
1429
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001430 case ISN_UNLET:
1431 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1432 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1433 goto failed;
1434 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001435 case ISN_UNLETENV:
1436 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1437 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 // create a list from items on the stack; uses a single allocation
1440 // for the list header and the items
1441 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001442 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1443 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 break;
1445
1446 // create a dict from items on the stack
1447 case ISN_NEWDICT:
1448 {
1449 int count = iptr->isn_arg.number;
1450 dict_T *dict = dict_alloc();
1451 dictitem_T *item;
1452
1453 if (dict == NULL)
1454 goto failed;
1455 for (idx = 0; idx < count; ++idx)
1456 {
1457 // check key type is VAR_STRING
1458 tv = STACK_TV_BOT(2 * (idx - count));
1459 item = dictitem_alloc(tv->vval.v_string);
1460 clear_tv(tv);
1461 if (item == NULL)
1462 goto failed;
1463 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1464 item->di_tv.v_lock = 0;
1465 if (dict_add(dict, item) == FAIL)
1466 goto failed;
1467 }
1468
1469 if (count > 0)
1470 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001471 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 goto failed;
1473 else
1474 ++ectx.ec_stack.ga_len;
1475 tv = STACK_TV_BOT(-1);
1476 tv->v_type = VAR_DICT;
1477 tv->vval.v_dict = dict;
1478 ++dict->dv_refcount;
1479 }
1480 break;
1481
1482 // call a :def function
1483 case ISN_DCALL:
1484 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1485 iptr->isn_arg.dfunc.cdf_argcount,
1486 &ectx) == FAIL)
1487 goto failed;
1488 break;
1489
1490 // call a builtin function
1491 case ISN_BCALL:
1492 SOURCING_LNUM = iptr->isn_lnum;
1493 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1494 iptr->isn_arg.bfunc.cbf_argcount,
1495 &ectx) == FAIL)
1496 goto failed;
1497 break;
1498
1499 // call a funcref or partial
1500 case ISN_PCALL:
1501 {
1502 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1503 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001504 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001505
1506 SOURCING_LNUM = iptr->isn_lnum;
1507 if (pfunc->cpf_top)
1508 {
1509 // funcref is above the arguments
1510 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1511 }
1512 else
1513 {
1514 // Get the funcref from the stack.
1515 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001516 partial_tv = *STACK_TV_BOT(0);
1517 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 }
1519 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001520 if (tv == &partial_tv)
1521 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 if (r == FAIL)
1523 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001524 }
1525 break;
1526
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001527 case ISN_PCALL_END:
1528 // PCALL finished, arguments have been consumed and replaced by
1529 // the return value. Now clear the funcref from the stack,
1530 // and move the return value in its place.
1531 --ectx.ec_stack.ga_len;
1532 clear_tv(STACK_TV_BOT(-1));
1533 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1534 break;
1535
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 // call a user defined function or funcref/partial
1537 case ISN_UCALL:
1538 {
1539 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1540
1541 SOURCING_LNUM = iptr->isn_lnum;
1542 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001543 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 goto failed;
1545 }
1546 break;
1547
1548 // return from a :def function call
1549 case ISN_RETURN:
1550 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001551 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001552 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001553
1554 if (trystack->ga_len > 0)
1555 trycmd = ((trycmd_T *)trystack->ga_data)
1556 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001557 if (trycmd != NULL
1558 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 && trycmd->tcd_finally_idx != 0)
1560 {
1561 // jump to ":finally"
1562 ectx.ec_iidx = trycmd->tcd_finally_idx;
1563 trycmd->tcd_return = TRUE;
1564 }
1565 else
1566 {
1567 // Restore previous function. If the frame pointer
1568 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001569 if (ectx.ec_frame_idx == initial_frame_idx)
1570 {
1571 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1572 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001576 if (func_return(&ectx) == FAIL)
1577 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578 }
1579 }
1580 break;
1581
1582 // push a function reference to a compiled function
1583 case ISN_FUNCREF:
1584 {
1585 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001586 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587
1588 pt = ALLOC_CLEAR_ONE(partial_T);
1589 if (pt == NULL)
1590 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001591 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001592 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001593 vim_free(pt);
1594 goto failed;
1595 }
1596 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1597 + iptr->isn_arg.funcref.fr_func;
1598 pt->pt_func = pt_dfunc->df_ufunc;
1599 pt->pt_refcount = 1;
1600 ++pt_dfunc->df_ufunc->uf_refcount;
1601
1602 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1603 {
1604 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1605 + ectx.ec_dfunc_idx;
1606
1607 // The closure needs to find arguments and local
1608 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001609 pt->pt_ectx_stack = &ectx.ec_stack;
1610 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001611
1612 // If this function returns and the closure is still
1613 // used, we need to make a copy of the context
1614 // (arguments and local variables). Store a reference
1615 // to the partial so we can handle that.
1616 ++pt->pt_refcount;
1617 tv = STACK_TV_VAR(dfunc->df_varcount
1618 + iptr->isn_arg.funcref.fr_var_idx);
1619 if (tv->v_type == VAR_PARTIAL)
1620 {
1621 // TODO: use a garray_T on ectx.
1622 emsg("Multiple closures not supported yet");
1623 goto failed;
1624 }
1625 tv->v_type = VAR_PARTIAL;
1626 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001627 }
1628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 tv = STACK_TV_BOT(0);
1630 ++ectx.ec_stack.ga_len;
1631 tv->vval.v_partial = pt;
1632 tv->v_type = VAR_PARTIAL;
1633 }
1634 break;
1635
1636 // jump if a condition is met
1637 case ISN_JUMP:
1638 {
1639 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1640 int jump = TRUE;
1641
1642 if (when != JUMP_ALWAYS)
1643 {
1644 tv = STACK_TV_BOT(-1);
1645 jump = tv2bool(tv);
1646 if (when == JUMP_IF_FALSE
1647 || when == JUMP_AND_KEEP_IF_FALSE)
1648 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001649 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 {
1651 // drop the value from the stack
1652 clear_tv(tv);
1653 --ectx.ec_stack.ga_len;
1654 }
1655 }
1656 if (jump)
1657 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1658 }
1659 break;
1660
1661 // top of a for loop
1662 case ISN_FOR:
1663 {
1664 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1665 typval_T *idxtv =
1666 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1667
1668 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001669 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 goto failed;
1671 if (++idxtv->vval.v_number >= list->lv_len)
1672 // past the end of the list, jump to "endfor"
1673 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1674 else if (list->lv_first == &range_list_item)
1675 {
1676 // non-materialized range() list
1677 tv = STACK_TV_BOT(0);
1678 tv->v_type = VAR_NUMBER;
1679 tv->vval.v_number = list_find_nr(
1680 list, idxtv->vval.v_number, NULL);
1681 ++ectx.ec_stack.ga_len;
1682 }
1683 else
1684 {
1685 listitem_T *li = list_find(list, idxtv->vval.v_number);
1686
1687 if (li == NULL)
1688 goto failed;
1689 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1690 ++ectx.ec_stack.ga_len;
1691 }
1692 }
1693 break;
1694
1695 // start of ":try" block
1696 case ISN_TRY:
1697 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001698 trycmd_T *trycmd = NULL;
1699
Bram Moolenaar270d0382020-05-15 21:42:53 +02001700 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 goto failed;
1702 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1703 + ectx.ec_trystack.ga_len;
1704 ++ectx.ec_trystack.ga_len;
1705 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001706 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1708 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001709 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710 }
1711 break;
1712
1713 case ISN_PUSHEXC:
1714 if (current_exception == NULL)
1715 {
1716 iemsg("Evaluating catch while current_exception is NULL");
1717 goto failed;
1718 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001719 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 goto failed;
1721 tv = STACK_TV_BOT(0);
1722 ++ectx.ec_stack.ga_len;
1723 tv->v_type = VAR_STRING;
1724 tv->vval.v_string = vim_strsave(
1725 (char_u *)current_exception->value);
1726 break;
1727
1728 case ISN_CATCH:
1729 {
1730 garray_T *trystack = &ectx.ec_trystack;
1731
1732 if (trystack->ga_len > 0)
1733 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001734 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 + trystack->ga_len - 1;
1736 trycmd->tcd_caught = TRUE;
1737 }
1738 did_emsg = got_int = did_throw = FALSE;
1739 catch_exception(current_exception);
1740 }
1741 break;
1742
1743 // end of ":try" block
1744 case ISN_ENDTRY:
1745 {
1746 garray_T *trystack = &ectx.ec_trystack;
1747
1748 if (trystack->ga_len > 0)
1749 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001750 trycmd_T *trycmd = NULL;
1751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 --trystack->ga_len;
1753 --trylevel;
1754 trycmd = ((trycmd_T *)trystack->ga_data)
1755 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001756 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 {
1758 // discard the exception
1759 if (caught_stack == current_exception)
1760 caught_stack = caught_stack->caught;
1761 discard_current_exception();
1762 }
1763
1764 if (trycmd->tcd_return)
1765 {
1766 // Restore previous function. If the frame pointer
1767 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001768 if (ectx.ec_frame_idx == initial_frame_idx)
1769 {
1770 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1771 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001773 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001774
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001775 if (func_return(&ectx) == FAIL)
1776 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 }
1778 }
1779 }
1780 break;
1781
1782 case ISN_THROW:
1783 --ectx.ec_stack.ga_len;
1784 tv = STACK_TV_BOT(0);
1785 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1786 {
1787 vim_free(tv->vval.v_string);
1788 goto failed;
1789 }
1790 did_throw = TRUE;
1791 break;
1792
1793 // compare with special values
1794 case ISN_COMPAREBOOL:
1795 case ISN_COMPARESPECIAL:
1796 {
1797 typval_T *tv1 = STACK_TV_BOT(-2);
1798 typval_T *tv2 = STACK_TV_BOT(-1);
1799 varnumber_T arg1 = tv1->vval.v_number;
1800 varnumber_T arg2 = tv2->vval.v_number;
1801 int res;
1802
1803 switch (iptr->isn_arg.op.op_type)
1804 {
1805 case EXPR_EQUAL: res = arg1 == arg2; break;
1806 case EXPR_NEQUAL: res = arg1 != arg2; break;
1807 default: res = 0; break;
1808 }
1809
1810 --ectx.ec_stack.ga_len;
1811 tv1->v_type = VAR_BOOL;
1812 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1813 }
1814 break;
1815
1816 // Operation with two number arguments
1817 case ISN_OPNR:
1818 case ISN_COMPARENR:
1819 {
1820 typval_T *tv1 = STACK_TV_BOT(-2);
1821 typval_T *tv2 = STACK_TV_BOT(-1);
1822 varnumber_T arg1 = tv1->vval.v_number;
1823 varnumber_T arg2 = tv2->vval.v_number;
1824 varnumber_T res;
1825
1826 switch (iptr->isn_arg.op.op_type)
1827 {
1828 case EXPR_MULT: res = arg1 * arg2; break;
1829 case EXPR_DIV: res = arg1 / arg2; break;
1830 case EXPR_REM: res = arg1 % arg2; break;
1831 case EXPR_SUB: res = arg1 - arg2; break;
1832 case EXPR_ADD: res = arg1 + arg2; break;
1833
1834 case EXPR_EQUAL: res = arg1 == arg2; break;
1835 case EXPR_NEQUAL: res = arg1 != arg2; break;
1836 case EXPR_GREATER: res = arg1 > arg2; break;
1837 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1838 case EXPR_SMALLER: res = arg1 < arg2; break;
1839 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1840 default: res = 0; break;
1841 }
1842
1843 --ectx.ec_stack.ga_len;
1844 if (iptr->isn_type == ISN_COMPARENR)
1845 {
1846 tv1->v_type = VAR_BOOL;
1847 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1848 }
1849 else
1850 tv1->vval.v_number = res;
1851 }
1852 break;
1853
1854 // Computation with two float arguments
1855 case ISN_OPFLOAT:
1856 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001857#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 {
1859 typval_T *tv1 = STACK_TV_BOT(-2);
1860 typval_T *tv2 = STACK_TV_BOT(-1);
1861 float_T arg1 = tv1->vval.v_float;
1862 float_T arg2 = tv2->vval.v_float;
1863 float_T res = 0;
1864 int cmp = FALSE;
1865
1866 switch (iptr->isn_arg.op.op_type)
1867 {
1868 case EXPR_MULT: res = arg1 * arg2; break;
1869 case EXPR_DIV: res = arg1 / arg2; break;
1870 case EXPR_SUB: res = arg1 - arg2; break;
1871 case EXPR_ADD: res = arg1 + arg2; break;
1872
1873 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1874 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1875 case EXPR_GREATER: cmp = arg1 > arg2; break;
1876 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1877 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1878 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1879 default: cmp = 0; break;
1880 }
1881 --ectx.ec_stack.ga_len;
1882 if (iptr->isn_type == ISN_COMPAREFLOAT)
1883 {
1884 tv1->v_type = VAR_BOOL;
1885 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1886 }
1887 else
1888 tv1->vval.v_float = res;
1889 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001890#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 break;
1892
1893 case ISN_COMPARELIST:
1894 {
1895 typval_T *tv1 = STACK_TV_BOT(-2);
1896 typval_T *tv2 = STACK_TV_BOT(-1);
1897 list_T *arg1 = tv1->vval.v_list;
1898 list_T *arg2 = tv2->vval.v_list;
1899 int cmp = FALSE;
1900 int ic = iptr->isn_arg.op.op_ic;
1901
1902 switch (iptr->isn_arg.op.op_type)
1903 {
1904 case EXPR_EQUAL: cmp =
1905 list_equal(arg1, arg2, ic, FALSE); break;
1906 case EXPR_NEQUAL: cmp =
1907 !list_equal(arg1, arg2, ic, FALSE); break;
1908 case EXPR_IS: cmp = arg1 == arg2; break;
1909 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1910 default: cmp = 0; break;
1911 }
1912 --ectx.ec_stack.ga_len;
1913 clear_tv(tv1);
1914 clear_tv(tv2);
1915 tv1->v_type = VAR_BOOL;
1916 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1917 }
1918 break;
1919
1920 case ISN_COMPAREBLOB:
1921 {
1922 typval_T *tv1 = STACK_TV_BOT(-2);
1923 typval_T *tv2 = STACK_TV_BOT(-1);
1924 blob_T *arg1 = tv1->vval.v_blob;
1925 blob_T *arg2 = tv2->vval.v_blob;
1926 int cmp = FALSE;
1927
1928 switch (iptr->isn_arg.op.op_type)
1929 {
1930 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1931 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1932 case EXPR_IS: cmp = arg1 == arg2; break;
1933 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1934 default: cmp = 0; break;
1935 }
1936 --ectx.ec_stack.ga_len;
1937 clear_tv(tv1);
1938 clear_tv(tv2);
1939 tv1->v_type = VAR_BOOL;
1940 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1941 }
1942 break;
1943
1944 // TODO: handle separately
1945 case ISN_COMPARESTRING:
1946 case ISN_COMPAREDICT:
1947 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 case ISN_COMPAREANY:
1949 {
1950 typval_T *tv1 = STACK_TV_BOT(-2);
1951 typval_T *tv2 = STACK_TV_BOT(-1);
1952 exptype_T exptype = iptr->isn_arg.op.op_type;
1953 int ic = iptr->isn_arg.op.op_ic;
1954
1955 typval_compare(tv1, tv2, exptype, ic);
1956 clear_tv(tv2);
1957 tv1->v_type = VAR_BOOL;
1958 tv1->vval.v_number = tv1->vval.v_number
1959 ? VVAL_TRUE : VVAL_FALSE;
1960 --ectx.ec_stack.ga_len;
1961 }
1962 break;
1963
1964 case ISN_ADDLIST:
1965 case ISN_ADDBLOB:
1966 {
1967 typval_T *tv1 = STACK_TV_BOT(-2);
1968 typval_T *tv2 = STACK_TV_BOT(-1);
1969
1970 if (iptr->isn_type == ISN_ADDLIST)
1971 eval_addlist(tv1, tv2);
1972 else
1973 eval_addblob(tv1, tv2);
1974 clear_tv(tv2);
1975 --ectx.ec_stack.ga_len;
1976 }
1977 break;
1978
1979 // Computation with two arguments of unknown type
1980 case ISN_OPANY:
1981 {
1982 typval_T *tv1 = STACK_TV_BOT(-2);
1983 typval_T *tv2 = STACK_TV_BOT(-1);
1984 varnumber_T n1, n2;
1985#ifdef FEAT_FLOAT
1986 float_T f1 = 0, f2 = 0;
1987#endif
1988 int error = FALSE;
1989
1990 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1991 {
1992 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1993 {
1994 eval_addlist(tv1, tv2);
1995 clear_tv(tv2);
1996 --ectx.ec_stack.ga_len;
1997 break;
1998 }
1999 else if (tv1->v_type == VAR_BLOB
2000 && tv2->v_type == VAR_BLOB)
2001 {
2002 eval_addblob(tv1, tv2);
2003 clear_tv(tv2);
2004 --ectx.ec_stack.ga_len;
2005 break;
2006 }
2007 }
2008#ifdef FEAT_FLOAT
2009 if (tv1->v_type == VAR_FLOAT)
2010 {
2011 f1 = tv1->vval.v_float;
2012 n1 = 0;
2013 }
2014 else
2015#endif
2016 {
2017 n1 = tv_get_number_chk(tv1, &error);
2018 if (error)
2019 goto failed;
2020#ifdef FEAT_FLOAT
2021 if (tv2->v_type == VAR_FLOAT)
2022 f1 = n1;
2023#endif
2024 }
2025#ifdef FEAT_FLOAT
2026 if (tv2->v_type == VAR_FLOAT)
2027 {
2028 f2 = tv2->vval.v_float;
2029 n2 = 0;
2030 }
2031 else
2032#endif
2033 {
2034 n2 = tv_get_number_chk(tv2, &error);
2035 if (error)
2036 goto failed;
2037#ifdef FEAT_FLOAT
2038 if (tv1->v_type == VAR_FLOAT)
2039 f2 = n2;
2040#endif
2041 }
2042#ifdef FEAT_FLOAT
2043 // if there is a float on either side the result is a float
2044 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2045 {
2046 switch (iptr->isn_arg.op.op_type)
2047 {
2048 case EXPR_MULT: f1 = f1 * f2; break;
2049 case EXPR_DIV: f1 = f1 / f2; break;
2050 case EXPR_SUB: f1 = f1 - f2; break;
2051 case EXPR_ADD: f1 = f1 + f2; break;
2052 default: emsg(_(e_modulus)); goto failed;
2053 }
2054 clear_tv(tv1);
2055 clear_tv(tv2);
2056 tv1->v_type = VAR_FLOAT;
2057 tv1->vval.v_float = f1;
2058 --ectx.ec_stack.ga_len;
2059 }
2060 else
2061#endif
2062 {
2063 switch (iptr->isn_arg.op.op_type)
2064 {
2065 case EXPR_MULT: n1 = n1 * n2; break;
2066 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2067 case EXPR_SUB: n1 = n1 - n2; break;
2068 case EXPR_ADD: n1 = n1 + n2; break;
2069 default: n1 = num_modulus(n1, n2); break;
2070 }
2071 clear_tv(tv1);
2072 clear_tv(tv2);
2073 tv1->v_type = VAR_NUMBER;
2074 tv1->vval.v_number = n1;
2075 --ectx.ec_stack.ga_len;
2076 }
2077 }
2078 break;
2079
2080 case ISN_CONCAT:
2081 {
2082 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2083 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2084 char_u *res;
2085
2086 res = concat_str(str1, str2);
2087 clear_tv(STACK_TV_BOT(-2));
2088 clear_tv(STACK_TV_BOT(-1));
2089 --ectx.ec_stack.ga_len;
2090 STACK_TV_BOT(-1)->vval.v_string = res;
2091 }
2092 break;
2093
2094 case ISN_INDEX:
2095 {
2096 list_T *list;
2097 varnumber_T n;
2098 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002099 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100
2101 // list index: list is at stack-2, index at stack-1
2102 tv = STACK_TV_BOT(-2);
2103 if (tv->v_type != VAR_LIST)
2104 {
2105 emsg(_(e_listreq));
2106 goto failed;
2107 }
2108 list = tv->vval.v_list;
2109
2110 tv = STACK_TV_BOT(-1);
2111 if (tv->v_type != VAR_NUMBER)
2112 {
2113 emsg(_(e_number_exp));
2114 goto failed;
2115 }
2116 n = tv->vval.v_number;
2117 clear_tv(tv);
2118 if ((li = list_find(list, n)) == NULL)
2119 {
2120 semsg(_(e_listidx), n);
2121 goto failed;
2122 }
2123 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002124 // Clear the list after getting the item, to avoid that it
2125 // make the item invalid.
2126 tv = STACK_TV_BOT(-1);
2127 temp_tv = *tv;
2128 copy_tv(&li->li_tv, tv);
2129 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 }
2131 break;
2132
Bram Moolenaar9af78762020-06-16 11:34:42 +02002133 case ISN_SLICE:
2134 {
2135 list_T *list;
2136 int count = iptr->isn_arg.number;
2137
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002138 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002139 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002140 list = tv->vval.v_list;
2141
2142 // no error for short list, expect it to be checked earlier
2143 if (list != NULL && list->lv_len >= count)
2144 {
2145 list_T *newlist = list_slice(list,
2146 count, list->lv_len - 1);
2147
2148 if (newlist != NULL)
2149 {
2150 list_unref(list);
2151 tv->vval.v_list = newlist;
2152 ++newlist->lv_refcount;
2153 }
2154 }
2155 }
2156 break;
2157
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002158 case ISN_GETITEM:
2159 {
2160 listitem_T *li;
2161 int index = iptr->isn_arg.number;
2162
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002163 // Get list item: list is at stack-1, push item.
2164 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002165 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002166 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002167
2168 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2169 goto failed;
2170 ++ectx.ec_stack.ga_len;
2171 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2172 }
2173 break;
2174
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175 case ISN_MEMBER:
2176 {
2177 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002178 char_u *key;
2179 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002180 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002181
2182 // dict member: dict is at stack-2, key at stack-1
2183 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002184 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002185 dict = tv->vval.v_dict;
2186
2187 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002188 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002189 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002190
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002191 if ((di = dict_find(dict, key, -1)) == NULL)
2192 {
2193 semsg(_(e_dictkey), key);
2194 goto failed;
2195 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002196 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002197 --ectx.ec_stack.ga_len;
2198 // Clear the dict after getting the item, to avoid that it
2199 // make the item invalid.
2200 tv = STACK_TV_BOT(-1);
2201 temp_tv = *tv;
2202 copy_tv(&di->di_tv, tv);
2203 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002204 }
2205 break;
2206
2207 // dict member with string key
2208 case ISN_STRINGMEMBER:
2209 {
2210 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002212 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002213
2214 tv = STACK_TV_BOT(-1);
2215 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2216 {
2217 emsg(_(e_dictreq));
2218 goto failed;
2219 }
2220 dict = tv->vval.v_dict;
2221
2222 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2223 == NULL)
2224 {
2225 semsg(_(e_dictkey), iptr->isn_arg.string);
2226 goto failed;
2227 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002228 // Clear the dict after getting the item, to avoid that it
2229 // make the item invalid.
2230 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002232 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002233 }
2234 break;
2235
2236 case ISN_NEGATENR:
2237 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002238 if (tv->v_type != VAR_NUMBER
2239#ifdef FEAT_FLOAT
2240 && tv->v_type != VAR_FLOAT
2241#endif
2242 )
2243 {
2244 emsg(_(e_number_exp));
2245 goto failed;
2246 }
2247#ifdef FEAT_FLOAT
2248 if (tv->v_type == VAR_FLOAT)
2249 tv->vval.v_float = -tv->vval.v_float;
2250 else
2251#endif
2252 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 break;
2254
2255 case ISN_CHECKNR:
2256 {
2257 int error = FALSE;
2258
2259 tv = STACK_TV_BOT(-1);
2260 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 (void)tv_get_number_chk(tv, &error);
2263 if (error)
2264 goto failed;
2265 }
2266 break;
2267
2268 case ISN_CHECKTYPE:
2269 {
2270 checktype_T *ct = &iptr->isn_arg.type;
2271
2272 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002273 // TODO: better type comparison
2274 if (tv->v_type != ct->ct_type
2275 && !((tv->v_type == VAR_PARTIAL
2276 && ct->ct_type == VAR_FUNC)
2277 || (tv->v_type == VAR_FUNC
2278 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 {
2280 semsg(_("E1029: Expected %s but got %s"),
2281 vartype_name(ct->ct_type),
2282 vartype_name(tv->v_type));
2283 goto failed;
2284 }
2285 }
2286 break;
2287
Bram Moolenaar9af78762020-06-16 11:34:42 +02002288 case ISN_CHECKLEN:
2289 {
2290 int min_len = iptr->isn_arg.checklen.cl_min_len;
2291 list_T *list = NULL;
2292
2293 tv = STACK_TV_BOT(-1);
2294 if (tv->v_type == VAR_LIST)
2295 list = tv->vval.v_list;
2296 if (list == NULL || list->lv_len < min_len
2297 || (list->lv_len > min_len
2298 && !iptr->isn_arg.checklen.cl_more_OK))
2299 {
2300 semsg(_("E1093: Expected %d items but got %d"),
2301 min_len, list == NULL ? 0 : list->lv_len);
2302 goto failed;
2303 }
2304 }
2305 break;
2306
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 case ISN_2BOOL:
2308 {
2309 int n;
2310
2311 tv = STACK_TV_BOT(-1);
2312 n = tv2bool(tv);
2313 if (iptr->isn_arg.number) // invert
2314 n = !n;
2315 clear_tv(tv);
2316 tv->v_type = VAR_BOOL;
2317 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2318 }
2319 break;
2320
2321 case ISN_2STRING:
2322 {
2323 char_u *str;
2324
2325 tv = STACK_TV_BOT(iptr->isn_arg.number);
2326 if (tv->v_type != VAR_STRING)
2327 {
2328 str = typval_tostring(tv);
2329 clear_tv(tv);
2330 tv->v_type = VAR_STRING;
2331 tv->vval.v_string = str;
2332 }
2333 }
2334 break;
2335
2336 case ISN_DROP:
2337 --ectx.ec_stack.ga_len;
2338 clear_tv(STACK_TV_BOT(0));
2339 break;
2340 }
2341 }
2342
2343done:
2344 // function finished, get result from the stack.
2345 tv = STACK_TV_BOT(-1);
2346 *rettv = *tv;
2347 tv->v_type = VAR_UNKNOWN;
2348 ret = OK;
2349
2350failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002351 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002352 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002353 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002354failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002355 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002356
2357 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002358 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2359 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002360
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002362 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 return ret;
2364}
2365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366/*
2367 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002368 * We don't really need this at runtime, but we do have tests that require it,
2369 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 */
2371 void
2372ex_disassemble(exarg_T *eap)
2373{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002374 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002375 char_u *fname;
2376 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 dfunc_T *dfunc;
2378 isn_T *instr;
2379 int current;
2380 int line_idx = 0;
2381 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002382 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002384 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002385 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002386 if (fname == NULL)
2387 {
2388 semsg(_(e_invarg2), eap->arg);
2389 return;
2390 }
2391
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002392 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002393 if (ufunc == NULL)
2394 {
2395 char_u *p = untrans_function_name(fname);
2396
2397 if (p != NULL)
2398 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002399 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002400 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002401 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 if (ufunc == NULL)
2403 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002404 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 return;
2406 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002407 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002408 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2409 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002410 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002412 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 return;
2414 }
2415 if (ufunc->uf_name_exp != NULL)
2416 msg((char *)ufunc->uf_name_exp);
2417 else
2418 msg((char *)ufunc->uf_name);
2419
2420 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2421 instr = dfunc->df_instr;
2422 for (current = 0; current < dfunc->df_instr_count; ++current)
2423 {
2424 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002425 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426
2427 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2428 {
2429 if (current > prev_current)
2430 {
2431 msg_puts("\n\n");
2432 prev_current = current;
2433 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002434 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2435 if (line != NULL)
2436 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 }
2438
2439 switch (iptr->isn_type)
2440 {
2441 case ISN_EXEC:
2442 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2443 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002444 case ISN_EXECCONCAT:
2445 smsg("%4d EXECCONCAT %lld", current,
2446 (long long)iptr->isn_arg.number);
2447 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 case ISN_ECHO:
2449 {
2450 echo_T *echo = &iptr->isn_arg.echo;
2451
2452 smsg("%4d %s %d", current,
2453 echo->echo_with_white ? "ECHO" : "ECHON",
2454 echo->echo_count);
2455 }
2456 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002457 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002458 smsg("%4d EXECUTE %lld", current,
2459 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002460 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002461 case ISN_ECHOMSG:
2462 smsg("%4d ECHOMSG %lld", current,
2463 (long long)(iptr->isn_arg.number));
2464 break;
2465 case ISN_ECHOERR:
2466 smsg("%4d ECHOERR %lld", current,
2467 (long long)(iptr->isn_arg.number));
2468 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002470 case ISN_LOADOUTER:
2471 {
2472 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2473
2474 if (iptr->isn_arg.number < 0)
2475 smsg("%4d LOAD%s arg[%lld]", current, add,
2476 (long long)(iptr->isn_arg.number
2477 + STACK_FRAME_SIZE));
2478 else
2479 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002480 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002481 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 break;
2483 case ISN_LOADV:
2484 smsg("%4d LOADV v:%s", current,
2485 get_vim_var_name(iptr->isn_arg.number));
2486 break;
2487 case ISN_LOADSCRIPT:
2488 {
2489 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002490 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2492 + iptr->isn_arg.script.script_idx;
2493
2494 smsg("%4d LOADSCRIPT %s from %s", current,
2495 sv->sv_name, si->sn_name);
2496 }
2497 break;
2498 case ISN_LOADS:
2499 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002500 scriptitem_T *si = SCRIPT_ITEM(
2501 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502
2503 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002504 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 }
2506 break;
2507 case ISN_LOADG:
2508 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2509 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002510 case ISN_LOADB:
2511 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2512 break;
2513 case ISN_LOADW:
2514 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2515 break;
2516 case ISN_LOADT:
2517 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2518 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002519 case ISN_LOADOPT:
2520 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2521 break;
2522 case ISN_LOADENV:
2523 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2524 break;
2525 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002526 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 break;
2528
2529 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002530 case ISN_STOREOUTER:
2531 {
2532 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2533
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002534 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002535 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002536 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002537 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002538 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002539 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002540 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002542 case ISN_STOREV:
2543 smsg("%4d STOREV v:%s", current,
2544 get_vim_var_name(iptr->isn_arg.number));
2545 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002547 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2548 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002549 case ISN_STOREB:
2550 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2551 break;
2552 case ISN_STOREW:
2553 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2554 break;
2555 case ISN_STORET:
2556 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2557 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002558 case ISN_STORES:
2559 {
2560 scriptitem_T *si = SCRIPT_ITEM(
2561 iptr->isn_arg.loadstore.ls_sid);
2562
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002563 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002564 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002565 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 break;
2567 case ISN_STORESCRIPT:
2568 {
2569 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002570 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2572 + iptr->isn_arg.script.script_idx;
2573
2574 smsg("%4d STORESCRIPT %s in %s", current,
2575 sv->sv_name, si->sn_name);
2576 }
2577 break;
2578 case ISN_STOREOPT:
2579 smsg("%4d STOREOPT &%s", current,
2580 iptr->isn_arg.storeopt.so_name);
2581 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002582 case ISN_STOREENV:
2583 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2584 break;
2585 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002586 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002587 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 case ISN_STORENR:
2589 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002590 iptr->isn_arg.storenr.stnr_val,
2591 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002592 break;
2593
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002594 case ISN_STORELIST:
2595 smsg("%4d STORELIST", current);
2596 break;
2597
2598 case ISN_STOREDICT:
2599 smsg("%4d STOREDICT", current);
2600 break;
2601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 // constants
2603 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002604 smsg("%4d PUSHNR %lld", current,
2605 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 break;
2607 case ISN_PUSHBOOL:
2608 case ISN_PUSHSPEC:
2609 smsg("%4d PUSH %s", current,
2610 get_var_special_name(iptr->isn_arg.number));
2611 break;
2612 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002613#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002615#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 break;
2617 case ISN_PUSHS:
2618 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2619 break;
2620 case ISN_PUSHBLOB:
2621 {
2622 char_u *r;
2623 char_u numbuf[NUMBUFLEN];
2624 char_u *tofree;
2625
2626 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002627 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 vim_free(tofree);
2629 }
2630 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002631 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002632 {
2633 char *name = (char *)iptr->isn_arg.string;
2634
2635 smsg("%4d PUSHFUNC \"%s\"", current,
2636 name == NULL ? "[none]" : name);
2637 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002638 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002639 case ISN_PUSHCHANNEL:
2640#ifdef FEAT_JOB_CHANNEL
2641 {
2642 channel_T *channel = iptr->isn_arg.channel;
2643
2644 smsg("%4d PUSHCHANNEL %d", current,
2645 channel == NULL ? 0 : channel->ch_id);
2646 }
2647#endif
2648 break;
2649 case ISN_PUSHJOB:
2650#ifdef FEAT_JOB_CHANNEL
2651 {
2652 typval_T tv;
2653 char_u *name;
2654
2655 tv.v_type = VAR_JOB;
2656 tv.vval.v_job = iptr->isn_arg.job;
2657 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002658 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002659 }
2660#endif
2661 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 case ISN_PUSHEXC:
2663 smsg("%4d PUSH v:exception", current);
2664 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002665 case ISN_UNLET:
2666 smsg("%4d UNLET%s %s", current,
2667 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2668 iptr->isn_arg.unlet.ul_name);
2669 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002670 case ISN_UNLETENV:
2671 smsg("%4d UNLETENV%s $%s", current,
2672 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2673 iptr->isn_arg.unlet.ul_name);
2674 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002676 smsg("%4d NEWLIST size %lld", current,
2677 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 break;
2679 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002680 smsg("%4d NEWDICT size %lld", current,
2681 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 break;
2683
2684 // function call
2685 case ISN_BCALL:
2686 {
2687 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2688
2689 smsg("%4d BCALL %s(argc %d)", current,
2690 internal_func_name(cbfunc->cbf_idx),
2691 cbfunc->cbf_argcount);
2692 }
2693 break;
2694 case ISN_DCALL:
2695 {
2696 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2697 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2698 + cdfunc->cdf_idx;
2699
2700 smsg("%4d DCALL %s(argc %d)", current,
2701 df->df_ufunc->uf_name_exp != NULL
2702 ? df->df_ufunc->uf_name_exp
2703 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2704 }
2705 break;
2706 case ISN_UCALL:
2707 {
2708 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2709
2710 smsg("%4d UCALL %s(argc %d)", current,
2711 cufunc->cuf_name, cufunc->cuf_argcount);
2712 }
2713 break;
2714 case ISN_PCALL:
2715 {
2716 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2717
2718 smsg("%4d PCALL%s (argc %d)", current,
2719 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2720 }
2721 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002722 case ISN_PCALL_END:
2723 smsg("%4d PCALL end", current);
2724 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 case ISN_RETURN:
2726 smsg("%4d RETURN", current);
2727 break;
2728 case ISN_FUNCREF:
2729 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002730 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002732 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002734 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002735 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002736 }
2737 break;
2738
2739 case ISN_JUMP:
2740 {
2741 char *when = "?";
2742
2743 switch (iptr->isn_arg.jump.jump_when)
2744 {
2745 case JUMP_ALWAYS:
2746 when = "JUMP";
2747 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 case JUMP_AND_KEEP_IF_TRUE:
2749 when = "JUMP_AND_KEEP_IF_TRUE";
2750 break;
2751 case JUMP_IF_FALSE:
2752 when = "JUMP_IF_FALSE";
2753 break;
2754 case JUMP_AND_KEEP_IF_FALSE:
2755 when = "JUMP_AND_KEEP_IF_FALSE";
2756 break;
2757 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002758 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002759 iptr->isn_arg.jump.jump_where);
2760 }
2761 break;
2762
2763 case ISN_FOR:
2764 {
2765 forloop_T *forloop = &iptr->isn_arg.forloop;
2766
2767 smsg("%4d FOR $%d -> %d", current,
2768 forloop->for_idx, forloop->for_end);
2769 }
2770 break;
2771
2772 case ISN_TRY:
2773 {
2774 try_T *try = &iptr->isn_arg.try;
2775
2776 smsg("%4d TRY catch -> %d, finally -> %d", current,
2777 try->try_catch, try->try_finally);
2778 }
2779 break;
2780 case ISN_CATCH:
2781 // TODO
2782 smsg("%4d CATCH", current);
2783 break;
2784 case ISN_ENDTRY:
2785 smsg("%4d ENDTRY", current);
2786 break;
2787 case ISN_THROW:
2788 smsg("%4d THROW", current);
2789 break;
2790
2791 // expression operations on number
2792 case ISN_OPNR:
2793 case ISN_OPFLOAT:
2794 case ISN_OPANY:
2795 {
2796 char *what;
2797 char *ins;
2798
2799 switch (iptr->isn_arg.op.op_type)
2800 {
2801 case EXPR_MULT: what = "*"; break;
2802 case EXPR_DIV: what = "/"; break;
2803 case EXPR_REM: what = "%"; break;
2804 case EXPR_SUB: what = "-"; break;
2805 case EXPR_ADD: what = "+"; break;
2806 default: what = "???"; break;
2807 }
2808 switch (iptr->isn_type)
2809 {
2810 case ISN_OPNR: ins = "OPNR"; break;
2811 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2812 case ISN_OPANY: ins = "OPANY"; break;
2813 default: ins = "???"; break;
2814 }
2815 smsg("%4d %s %s", current, ins, what);
2816 }
2817 break;
2818
2819 case ISN_COMPAREBOOL:
2820 case ISN_COMPARESPECIAL:
2821 case ISN_COMPARENR:
2822 case ISN_COMPAREFLOAT:
2823 case ISN_COMPARESTRING:
2824 case ISN_COMPAREBLOB:
2825 case ISN_COMPARELIST:
2826 case ISN_COMPAREDICT:
2827 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 case ISN_COMPAREANY:
2829 {
2830 char *p;
2831 char buf[10];
2832 char *type;
2833
2834 switch (iptr->isn_arg.op.op_type)
2835 {
2836 case EXPR_EQUAL: p = "=="; break;
2837 case EXPR_NEQUAL: p = "!="; break;
2838 case EXPR_GREATER: p = ">"; break;
2839 case EXPR_GEQUAL: p = ">="; break;
2840 case EXPR_SMALLER: p = "<"; break;
2841 case EXPR_SEQUAL: p = "<="; break;
2842 case EXPR_MATCH: p = "=~"; break;
2843 case EXPR_IS: p = "is"; break;
2844 case EXPR_ISNOT: p = "isnot"; break;
2845 case EXPR_NOMATCH: p = "!~"; break;
2846 default: p = "???"; break;
2847 }
2848 STRCPY(buf, p);
2849 if (iptr->isn_arg.op.op_ic == TRUE)
2850 strcat(buf, "?");
2851 switch(iptr->isn_type)
2852 {
2853 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2854 case ISN_COMPARESPECIAL:
2855 type = "COMPARESPECIAL"; break;
2856 case ISN_COMPARENR: type = "COMPARENR"; break;
2857 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2858 case ISN_COMPARESTRING:
2859 type = "COMPARESTRING"; break;
2860 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2861 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2862 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2863 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2865 default: type = "???"; break;
2866 }
2867
2868 smsg("%4d %s %s", current, type, buf);
2869 }
2870 break;
2871
2872 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2873 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2874
2875 // expression operations
2876 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2877 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002878 case ISN_SLICE: smsg("%4d SLICE %lld",
2879 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002880 case ISN_GETITEM: smsg("%4d ITEM %lld",
2881 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002882 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2883 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002884 iptr->isn_arg.string); break;
2885 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2886
2887 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2888 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2889 vartype_name(iptr->isn_arg.type.ct_type),
2890 iptr->isn_arg.type.ct_off);
2891 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002892 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
2893 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
2894 iptr->isn_arg.checklen.cl_min_len);
2895 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 case ISN_2BOOL: if (iptr->isn_arg.number)
2897 smsg("%4d INVERT (!val)", current);
2898 else
2899 smsg("%4d 2BOOL (!!val)", current);
2900 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002901 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2902 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002903 break;
2904
2905 case ISN_DROP: smsg("%4d DROP", current); break;
2906 }
2907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002908}
2909
2910/*
2911 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2912 * list, etc. Mostly like what JavaScript does, except that empty list and
2913 * empty dictionary are FALSE.
2914 */
2915 int
2916tv2bool(typval_T *tv)
2917{
2918 switch (tv->v_type)
2919 {
2920 case VAR_NUMBER:
2921 return tv->vval.v_number != 0;
2922 case VAR_FLOAT:
2923#ifdef FEAT_FLOAT
2924 return tv->vval.v_float != 0.0;
2925#else
2926 break;
2927#endif
2928 case VAR_PARTIAL:
2929 return tv->vval.v_partial != NULL;
2930 case VAR_FUNC:
2931 case VAR_STRING:
2932 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2933 case VAR_LIST:
2934 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2935 case VAR_DICT:
2936 return tv->vval.v_dict != NULL
2937 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2938 case VAR_BOOL:
2939 case VAR_SPECIAL:
2940 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2941 case VAR_JOB:
2942#ifdef FEAT_JOB_CHANNEL
2943 return tv->vval.v_job != NULL;
2944#else
2945 break;
2946#endif
2947 case VAR_CHANNEL:
2948#ifdef FEAT_JOB_CHANNEL
2949 return tv->vval.v_channel != NULL;
2950#else
2951 break;
2952#endif
2953 case VAR_BLOB:
2954 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2955 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002956 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 case VAR_VOID:
2958 break;
2959 }
2960 return FALSE;
2961}
2962
2963/*
2964 * If "tv" is a string give an error and return FAIL.
2965 */
2966 int
2967check_not_string(typval_T *tv)
2968{
2969 if (tv->v_type == VAR_STRING)
2970 {
2971 emsg(_("E1030: Using a String as a Number"));
2972 clear_tv(tv);
2973 return FAIL;
2974 }
2975 return OK;
2976}
2977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002978
2979#endif // FEAT_EVAL