blob: 4d4df0ab60d6f28cf02dd75dd79484b90866b58f [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;
131 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
132 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;
163
164 if (dfunc->df_deleted)
165 {
166 emsg_funcname(e_func_deleted, ufunc->uf_name);
167 return FAIL;
168 }
169
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 if (ufunc->uf_va_name != NULL)
171 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200172 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200173 // Stack at time of call with 2 varargs:
174 // normal_arg
175 // optional_arg
176 // vararg_1
177 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200178 // After creating the list:
179 // normal_arg
180 // optional_arg
181 // vararg-list
182 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // After creating the list
185 // normal_arg
186 // (space for optional_arg)
187 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200188 vararg_count = argcount - ufunc->uf_args.ga_len;
189 if (vararg_count < 0)
190 vararg_count = 0;
191 else
192 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200193 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200195
196 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 }
198
Bram Moolenaarfe270812020-04-11 22:31:27 +0200199 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 if (arg_to_add < 0)
201 {
202 iemsg("Argument count wrong?");
203 return FAIL;
204 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200205 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
206 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 // Move the vararg-list to below the missing optional arguments.
210 if (vararg_count > 0 && arg_to_add > 0)
211 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100212
213 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200215 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
218 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
220 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200221 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
222 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223
224 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200225 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200227 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
228 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
230 // Set execution state to the start of the called function.
231 ectx->ec_dfunc_idx = cdf_idx;
232 ectx->ec_instr = dfunc->df_instr;
233 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100234
235 // Decide where to start execution, handles optional arguments.
236 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237
238 return OK;
239}
240
241// Get pointer to item in the stack.
242#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
243
244/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200245 * Used when returning from a function: Check if any closure is still
246 * referenced. If so then move the arguments and variables to a separate piece
247 * of stack to be used when the closure is called.
248 * When "free_arguments" is TRUE the arguments are to be freed.
249 * Returns FAIL when out of memory.
250 */
251 static int
252handle_closure_in_use(ectx_T *ectx, int free_arguments)
253{
254 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
255 + ectx->ec_dfunc_idx;
256 int argcount = ufunc_argcount(dfunc->df_ufunc);
257 int top = ectx->ec_frame_idx - argcount;
258 int idx;
259 typval_T *tv;
260 int closure_in_use = FALSE;
261
262 // Check if any created closure is still in use.
263 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
264 {
265 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
266 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200267 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
268 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200269 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200270 int refcount = tv->vval.v_partial->pt_refcount;
271 int i;
272
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200273 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200274 // unreferenced on return.
275 for (i = 0; i < dfunc->df_varcount; ++i)
276 {
277 typval_T *stv = STACK_TV(ectx->ec_frame_idx
278 + STACK_FRAME_SIZE + i);
279 if (stv->v_type == VAR_PARTIAL
280 && tv->vval.v_partial == stv->vval.v_partial)
281 --refcount;
282 }
283 if (refcount > 1)
284 {
285 closure_in_use = TRUE;
286 break;
287 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200288 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200289 }
290
291 if (closure_in_use)
292 {
293 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
294 typval_T *stack;
295
296 // A closure is using the arguments and/or local variables.
297 // Move them to the called function.
298 if (funcstack == NULL)
299 return FAIL;
300 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
301 + dfunc->df_varcount;
302 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
303 funcstack->fs_ga.ga_data = stack;
304 if (stack == NULL)
305 {
306 vim_free(funcstack);
307 return FAIL;
308 }
309
310 // Move or copy the arguments.
311 for (idx = 0; idx < argcount; ++idx)
312 {
313 tv = STACK_TV(top + idx);
314 if (free_arguments)
315 {
316 *(stack + idx) = *tv;
317 tv->v_type = VAR_UNKNOWN;
318 }
319 else
320 copy_tv(tv, stack + idx);
321 }
322 // Move the local variables.
323 for (idx = 0; idx < dfunc->df_varcount; ++idx)
324 {
325 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200326
327 // Do not copy a partial created for a local function.
328 // TODO: this won't work if the closure actually uses it. But when
329 // keeping it it gets complicated: it will create a reference cycle
330 // inside the partial, thus needs special handling for garbage
331 // collection.
332 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
333 {
334 int i;
335 typval_T *ctv;
336
337 for (i = 0; i < dfunc->df_closure_count; ++i)
338 {
339 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
340 + dfunc->df_varcount + i);
341 if (tv->vval.v_partial == ctv->vval.v_partial)
342 break;
343 }
344 if (i < dfunc->df_closure_count)
345 {
346 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
347 VAR_UNKNOWN;
348 continue;
349 }
350 }
351
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200352 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
353 tv->v_type = VAR_UNKNOWN;
354 }
355
356 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
357 {
358 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
359 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200360 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200362 partial_T *partial = tv->vval.v_partial;
363
364 if (partial->pt_refcount > 1)
365 {
366 ++funcstack->fs_refcount;
367 partial->pt_funcstack = funcstack;
368 partial->pt_ectx_stack = &funcstack->fs_ga;
369 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
370 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200371 }
372 }
373 }
374
375 return OK;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Return from the current function.
380 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382func_return(ectx_T *ectx)
383{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
386 + ectx->ec_dfunc_idx;
387 int argcount = ufunc_argcount(dfunc->df_ufunc);
388 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389
390 // execution context goes one level up
391 estack_pop();
392
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 if (handle_closure_in_use(ectx, TRUE) == FAIL)
394 return FAIL;
395
396 // Clear the arguments.
397 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
398 clear_tv(STACK_TV(idx));
399
400 // Clear local variables and temp values, but not the return value.
401 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100402 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100404
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100405 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
407 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
408 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
410 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100411
412 // Reset the stack to the position before the call, move the return value
413 // to the top of the stack.
414 idx = ectx->ec_stack.ga_len - 1;
415 ectx->ec_stack.ga_len = top + 1;
416 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417
418 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
421#undef STACK_TV
422
423/*
424 * Prepare arguments and rettv for calling a builtin or user function.
425 */
426 static int
427call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
428{
429 int idx;
430 typval_T *tv;
431
432 // Move arguments from bottom of the stack to argvars[] and add terminator.
433 for (idx = 0; idx < argcount; ++idx)
434 argvars[idx] = *STACK_TV_BOT(idx - argcount);
435 argvars[argcount].v_type = VAR_UNKNOWN;
436
437 // Result replaces the arguments on the stack.
438 if (argcount > 0)
439 ectx->ec_stack.ga_len -= argcount - 1;
440 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
441 return FAIL;
442 else
443 ++ectx->ec_stack.ga_len;
444
445 // Default return value is zero.
446 tv = STACK_TV_BOT(-1);
447 tv->v_type = VAR_NUMBER;
448 tv->vval.v_number = 0;
449
450 return OK;
451}
452
453/*
454 * Call a builtin function by index.
455 */
456 static int
457call_bfunc(int func_idx, int argcount, ectx_T *ectx)
458{
459 typval_T argvars[MAX_FUNC_ARGS];
460 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200461 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462
463 if (call_prepare(argcount, argvars, ectx) == FAIL)
464 return FAIL;
465
466 // Call the builtin function.
467 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
468
469 // Clear the arguments.
470 for (idx = 0; idx < argcount; ++idx)
471 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200472
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200473 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200474 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 return OK;
476}
477
478/*
479 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100480 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 */
482 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100483call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484{
485 typval_T argvars[MAX_FUNC_ARGS];
486 funcexe_T funcexe;
487 int error;
488 int idx;
489
490 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100491 {
492 // The function has been compiled, can call it quickly. For a function
493 // that was defined later: we can call it directly next time.
494 if (iptr != NULL)
495 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100496 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100497 iptr->isn_type = ISN_DCALL;
498 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
499 iptr->isn_arg.dfunc.cdf_argcount = argcount;
500 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503
504 if (call_prepare(argcount, argvars, ectx) == FAIL)
505 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200506 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 funcexe.evaluate = TRUE;
508
509 // Call the user function. Result goes in last position on the stack.
510 // TODO: add selfdict if there is one
511 error = call_user_func_check(ufunc, argcount, argvars,
512 STACK_TV_BOT(-1), &funcexe, NULL);
513
514 // Clear the arguments.
515 for (idx = 0; idx < argcount; ++idx)
516 clear_tv(&argvars[idx]);
517
518 if (error != FCERR_NONE)
519 {
520 user_func_error(error, ufunc->uf_name);
521 return FAIL;
522 }
523 return OK;
524}
525
526/*
527 * Execute a function by "name".
528 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100529 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 * Returns FAIL if not found without an error message.
531 */
532 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100533call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534{
535 ufunc_T *ufunc;
536
537 if (builtin_function(name, -1))
538 {
539 int func_idx = find_internal_func(name);
540
541 if (func_idx < 0)
542 return FAIL;
543 if (check_internal_func(func_idx, argcount) == FAIL)
544 return FAIL;
545 return call_bfunc(func_idx, argcount, ectx);
546 }
547
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200548 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100550 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551
552 return FAIL;
553}
554
555 static int
556call_partial(typval_T *tv, int argcount, ectx_T *ectx)
557{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200558 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 int called_emsg_before = called_emsg;
560
561 if (tv->v_type == VAR_PARTIAL)
562 {
563 partial_T *pt = tv->vval.v_partial;
564
565 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200566 {
567 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
568
569 // closure may need the function context where it was defined
570 ectx->ec_outer_stack = pt->pt_ectx_stack;
571 ectx->ec_outer_frame = pt->pt_ectx_frame;
572
573 return ret;
574 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 name = pt->pt_name;
576 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200577 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200579 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580 {
581 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200582 semsg(_(e_unknownfunc),
583 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 return FAIL;
585 }
586 return OK;
587}
588
589/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100590 * Store "tv" in variable "name".
591 * This is for s: and g: variables.
592 */
593 static void
594store_var(char_u *name, typval_T *tv)
595{
596 funccal_entry_T entry;
597
598 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200599 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100600 restore_funccal();
601}
602
Bram Moolenaard3aac292020-04-19 14:32:17 +0200603
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100604/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 * Execute a function by "name".
606 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100607 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 */
609 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100610call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611{
612 int called_emsg_before = called_emsg;
613
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100614 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 && called_emsg == called_emsg_before)
616 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200617 dictitem_T *v;
618
619 v = find_var(name, NULL, FALSE);
620 if (v == NULL)
621 {
622 semsg(_(e_unknownfunc), name);
623 return FAIL;
624 }
625 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
626 {
627 semsg(_(e_unknownfunc), name);
628 return FAIL;
629 }
630 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631 }
632 return OK;
633}
634
635/*
636 * Call a "def" function from old Vim script.
637 * Return OK or FAIL.
638 */
639 int
640call_def_function(
641 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200642 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 typval_T *argv, // arguments
644 typval_T *rettv) // return value
645{
646 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200647 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200648 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 typval_T *tv;
650 int idx;
651 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100652 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200653 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654
655// Get pointer to item in the stack.
656#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
657
658// Get pointer to item at the bottom of the stack, -1 is the bottom.
659#undef STACK_TV_BOT
660#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
661
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200662// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200663#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 +0100664
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200665// Like STACK_TV_VAR but use the outer scope
666#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
667
Bram Moolenaar09689a02020-05-09 22:50:08 +0200668 {
669 // Check the function was compiled, it is postponed in ex_vim9script().
670 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
671 + ufunc->uf_dfunc_idx;
672 if (dfunc->df_instr == NULL)
673 return FAIL;
674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200676 CLEAR_FIELD(ectx);
677 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
678 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
679 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
680 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100681 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
682
683 // Put arguments on the stack.
684 for (idx = 0; idx < argc; ++idx)
685 {
686 copy_tv(&argv[idx], STACK_TV_BOT(0));
687 ++ectx.ec_stack.ga_len;
688 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200689
690 // Turn varargs into a list. Empty list if no args.
691 if (ufunc->uf_va_name != NULL)
692 {
693 int vararg_count = argc - ufunc->uf_args.ga_len;
694
695 if (vararg_count < 0)
696 vararg_count = 0;
697 else
698 argc -= vararg_count;
699 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200700 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200701 if (defcount > 0)
702 // Move varargs list to below missing default arguments.
703 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
704 --ectx.ec_stack.ga_len;
705 }
706
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100707 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200708 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100709 if (defcount > 0)
710 for (idx = 0; idx < defcount; ++idx)
711 {
712 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
713 ++ectx.ec_stack.ga_len;
714 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200715 if (ufunc->uf_va_name != NULL)
716 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717
718 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200719 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
720 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
722 // dummy frame entries
723 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
724 {
725 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
726 ++ectx.ec_stack.ga_len;
727 }
728
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200729 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200730 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200731 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
732 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200733 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200735 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200736 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200737 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200738
739 ectx.ec_instr = dfunc->df_instr;
740 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100741
Bram Moolenaara26b9702020-04-18 19:53:28 +0200742 // Commands behave like vim9script.
743 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
744
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100745 // Decide where to start execution, handles optional arguments.
746 init_instr_idx(ufunc, argc, &ectx);
747
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 for (;;)
749 {
750 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100751
752 veryfast_breakcheck();
753 if (got_int)
754 {
755 // Turn CTRL-C into an exception.
756 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100757 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100758 goto failed;
759 did_throw = TRUE;
760 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761
Bram Moolenaara26b9702020-04-18 19:53:28 +0200762 if (did_emsg && msg_list != NULL && *msg_list != NULL)
763 {
764 // Turn an error message into an exception.
765 did_emsg = FALSE;
766 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
767 goto failed;
768 did_throw = TRUE;
769 *msg_list = NULL;
770 }
771
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 if (did_throw && !ectx.ec_in_catch)
773 {
774 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100775 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776
777 // An exception jumps to the first catch, finally, or returns from
778 // the current function.
779 if (trystack->ga_len > 0)
780 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200781 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 {
783 // jump to ":catch" or ":finally"
784 ectx.ec_in_catch = TRUE;
785 ectx.ec_iidx = trycmd->tcd_catch_idx;
786 }
787 else
788 {
789 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200790 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 {
792 // At the toplevel we are done. Push a dummy return value.
793 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
794 goto failed;
795 tv = STACK_TV_BOT(0);
796 tv->v_type = VAR_NUMBER;
797 tv->vval.v_number = 0;
798 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100799 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200800 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
801 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 goto done;
803 }
804
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200805 if (func_return(&ectx) == FAIL)
806 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807 }
808 continue;
809 }
810
811 iptr = &ectx.ec_instr[ectx.ec_iidx++];
812 switch (iptr->isn_type)
813 {
814 // execute Ex command line
815 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 do_cmdline_cmd(iptr->isn_arg.string);
818 break;
819
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200820 // execute Ex command from pieces on the stack
821 case ISN_EXECCONCAT:
822 {
823 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200824 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200825 int pass;
826 int i;
827 char_u *cmd = NULL;
828 char_u *str;
829
830 for (pass = 1; pass <= 2; ++pass)
831 {
832 for (i = 0; i < count; ++i)
833 {
834 tv = STACK_TV_BOT(i - count);
835 str = tv->vval.v_string;
836 if (str != NULL && *str != NUL)
837 {
838 if (pass == 2)
839 STRCPY(cmd + len, str);
840 len += STRLEN(str);
841 }
842 if (pass == 2)
843 clear_tv(tv);
844 }
845 if (pass == 1)
846 {
847 cmd = alloc(len + 1);
848 if (cmd == NULL)
849 goto failed;
850 len = 0;
851 }
852 }
853
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200854 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200855 do_cmdline_cmd(cmd);
856 vim_free(cmd);
857 }
858 break;
859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 // execute :echo {string} ...
861 case ISN_ECHO:
862 {
863 int count = iptr->isn_arg.echo.echo_count;
864 int atstart = TRUE;
865 int needclr = TRUE;
866
867 for (idx = 0; idx < count; ++idx)
868 {
869 tv = STACK_TV_BOT(idx - count);
870 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
871 &atstart, &needclr);
872 clear_tv(tv);
873 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100874 if (needclr)
875 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876 ectx.ec_stack.ga_len -= count;
877 }
878 break;
879
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200880 // :execute {string} ...
881 // :echomsg {string} ...
882 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100883 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200884 case ISN_ECHOMSG:
885 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100886 {
887 int count = iptr->isn_arg.number;
888 garray_T ga;
889 char_u buf[NUMBUFLEN];
890 char_u *p;
891 int len;
892 int failed = FALSE;
893
894 ga_init2(&ga, 1, 80);
895 for (idx = 0; idx < count; ++idx)
896 {
897 tv = STACK_TV_BOT(idx - count);
898 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
899 {
900 emsg(_(e_inval_string));
901 break;
902 }
903 else
904 p = tv_get_string_buf(tv, buf);
905
906 len = (int)STRLEN(p);
907 if (ga_grow(&ga, len + 2) == FAIL)
908 failed = TRUE;
909 else
910 {
911 if (ga.ga_len > 0)
912 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
913 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
914 ga.ga_len += len;
915 }
916 clear_tv(tv);
917 }
918 ectx.ec_stack.ga_len -= count;
919
920 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200921 {
922 if (iptr->isn_type == ISN_EXECUTE)
923 do_cmdline_cmd((char_u *)ga.ga_data);
924 else
925 {
926 msg_sb_eol();
927 if (iptr->isn_type == ISN_ECHOMSG)
928 {
929 msg_attr(ga.ga_data, echo_attr);
930 out_flush();
931 }
932 else
933 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200934 SOURCING_LNUM = iptr->isn_lnum;
935 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200936 }
937 }
938 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100939 ga_clear(&ga);
940 }
941 break;
942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 // load local variable or argument
944 case ISN_LOAD:
945 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
946 goto failed;
947 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
948 ++ectx.ec_stack.ga_len;
949 break;
950
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200951 // load variable or argument from outer scope
952 case ISN_LOADOUTER:
953 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
954 goto failed;
955 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
956 STACK_TV_BOT(0));
957 ++ectx.ec_stack.ga_len;
958 break;
959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 // load v: variable
961 case ISN_LOADV:
962 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
963 goto failed;
964 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
965 ++ectx.ec_stack.ga_len;
966 break;
967
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100968 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 case ISN_LOADSCRIPT:
970 {
971 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100972 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 svar_T *sv;
974
975 sv = ((svar_T *)si->sn_var_vals.ga_data)
976 + iptr->isn_arg.script.script_idx;
977 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
978 goto failed;
979 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
980 ++ectx.ec_stack.ga_len;
981 }
982 break;
983
984 // load s: variable in old script
985 case ISN_LOADS:
986 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100987 hashtab_T *ht = &SCRIPT_VARS(
988 iptr->isn_arg.loadstore.ls_sid);
989 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100991
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100992 if (di == NULL)
993 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100994 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100995 goto failed;
996 }
997 else
998 {
999 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1000 goto failed;
1001 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1002 ++ectx.ec_stack.ga_len;
1003 }
1004 }
1005 break;
1006
Bram Moolenaard3aac292020-04-19 14:32:17 +02001007 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001008 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001009 case ISN_LOADB:
1010 case ISN_LOADW:
1011 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001013 dictitem_T *di = NULL;
1014 hashtab_T *ht = NULL;
1015 char namespace;
1016 switch (iptr->isn_type)
1017 {
1018 case ISN_LOADG:
1019 ht = get_globvar_ht();
1020 namespace = 'g';
1021 break;
1022 case ISN_LOADB:
1023 ht = &curbuf->b_vars->dv_hashtab;
1024 namespace = 'b';
1025 break;
1026 case ISN_LOADW:
1027 ht = &curwin->w_vars->dv_hashtab;
1028 namespace = 'w';
1029 break;
1030 case ISN_LOADT:
1031 ht = &curtab->tp_vars->dv_hashtab;
1032 namespace = 't';
1033 break;
1034 default: // Cannot reach here
1035 goto failed;
1036 }
1037 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001038
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 if (di == NULL)
1040 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001041 semsg(_("E121: Undefined variable: %c:%s"),
1042 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 goto failed;
1044 }
1045 else
1046 {
1047 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1048 goto failed;
1049 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1050 ++ectx.ec_stack.ga_len;
1051 }
1052 }
1053 break;
1054
1055 // load &option
1056 case ISN_LOADOPT:
1057 {
1058 typval_T optval;
1059 char_u *name = iptr->isn_arg.string;
1060
Bram Moolenaara8c17702020-04-01 21:17:24 +02001061 // This is not expected to fail, name is checked during
1062 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1064 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001065 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1066 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 *STACK_TV_BOT(0) = optval;
1068 ++ectx.ec_stack.ga_len;
1069 }
1070 break;
1071
1072 // load $ENV
1073 case ISN_LOADENV:
1074 {
1075 typval_T optval;
1076 char_u *name = iptr->isn_arg.string;
1077
1078 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1079 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001080 // name is always valid, checked when compiling
1081 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001082 *STACK_TV_BOT(0) = optval;
1083 ++ectx.ec_stack.ga_len;
1084 }
1085 break;
1086
1087 // load @register
1088 case ISN_LOADREG:
1089 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1090 goto failed;
1091 tv = STACK_TV_BOT(0);
1092 tv->v_type = VAR_STRING;
1093 tv->vval.v_string = get_reg_contents(
1094 iptr->isn_arg.number, GREG_EXPR_SRC);
1095 ++ectx.ec_stack.ga_len;
1096 break;
1097
1098 // store local variable
1099 case ISN_STORE:
1100 --ectx.ec_stack.ga_len;
1101 tv = STACK_TV_VAR(iptr->isn_arg.number);
1102 clear_tv(tv);
1103 *tv = *STACK_TV_BOT(0);
1104 break;
1105
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001106 // store variable or argument in outer scope
1107 case ISN_STOREOUTER:
1108 --ectx.ec_stack.ga_len;
1109 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1110 clear_tv(tv);
1111 *tv = *STACK_TV_BOT(0);
1112 break;
1113
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001114 // store s: variable in old script
1115 case ISN_STORES:
1116 {
1117 hashtab_T *ht = &SCRIPT_VARS(
1118 iptr->isn_arg.loadstore.ls_sid);
1119 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001120 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001121
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001122 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001123 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001124 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001125 else
1126 {
1127 clear_tv(&di->di_tv);
1128 di->di_tv = *STACK_TV_BOT(0);
1129 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001130 }
1131 break;
1132
1133 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134 case ISN_STORESCRIPT:
1135 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001136 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 iptr->isn_arg.script.script_sid);
1138 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1139 + iptr->isn_arg.script.script_idx;
1140
1141 --ectx.ec_stack.ga_len;
1142 clear_tv(sv->sv_tv);
1143 *sv->sv_tv = *STACK_TV_BOT(0);
1144 }
1145 break;
1146
1147 // store option
1148 case ISN_STOREOPT:
1149 {
1150 long n = 0;
1151 char_u *s = NULL;
1152 char *msg;
1153
1154 --ectx.ec_stack.ga_len;
1155 tv = STACK_TV_BOT(0);
1156 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001157 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001159 if (s == NULL)
1160 s = (char_u *)"";
1161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 else if (tv->v_type == VAR_NUMBER)
1163 n = tv->vval.v_number;
1164 else
1165 {
1166 emsg(_("E1051: Expected string or number"));
1167 goto failed;
1168 }
1169 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1170 n, s, iptr->isn_arg.storeopt.so_flags);
1171 if (msg != NULL)
1172 {
1173 emsg(_(msg));
1174 goto failed;
1175 }
1176 clear_tv(tv);
1177 }
1178 break;
1179
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001180 // store $ENV
1181 case ISN_STOREENV:
1182 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001183 tv = STACK_TV_BOT(0);
1184 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1185 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001186 break;
1187
1188 // store @r
1189 case ISN_STOREREG:
1190 {
1191 int reg = iptr->isn_arg.number;
1192
1193 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001194 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001195 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001196 tv_get_string(tv), -1, FALSE);
1197 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001198 }
1199 break;
1200
1201 // store v: variable
1202 case ISN_STOREV:
1203 --ectx.ec_stack.ga_len;
1204 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1205 == FAIL)
1206 goto failed;
1207 break;
1208
Bram Moolenaard3aac292020-04-19 14:32:17 +02001209 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001211 case ISN_STOREB:
1212 case ISN_STOREW:
1213 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001214 {
1215 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001216 hashtab_T *ht;
1217 switch (iptr->isn_type)
1218 {
1219 case ISN_STOREG:
1220 ht = get_globvar_ht();
1221 break;
1222 case ISN_STOREB:
1223 ht = &curbuf->b_vars->dv_hashtab;
1224 break;
1225 case ISN_STOREW:
1226 ht = &curwin->w_vars->dv_hashtab;
1227 break;
1228 case ISN_STORET:
1229 ht = &curtab->tp_vars->dv_hashtab;
1230 break;
1231 default: // Cannot reach here
1232 goto failed;
1233 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234
1235 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001236 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001238 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 else
1240 {
1241 clear_tv(&di->di_tv);
1242 di->di_tv = *STACK_TV_BOT(0);
1243 }
1244 }
1245 break;
1246
1247 // store number in local variable
1248 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001249 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 clear_tv(tv);
1251 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001252 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 break;
1254
1255 // push constant
1256 case ISN_PUSHNR:
1257 case ISN_PUSHBOOL:
1258 case ISN_PUSHSPEC:
1259 case ISN_PUSHF:
1260 case ISN_PUSHS:
1261 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001262 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001263 case ISN_PUSHCHANNEL:
1264 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1266 goto failed;
1267 tv = STACK_TV_BOT(0);
1268 ++ectx.ec_stack.ga_len;
1269 switch (iptr->isn_type)
1270 {
1271 case ISN_PUSHNR:
1272 tv->v_type = VAR_NUMBER;
1273 tv->vval.v_number = iptr->isn_arg.number;
1274 break;
1275 case ISN_PUSHBOOL:
1276 tv->v_type = VAR_BOOL;
1277 tv->vval.v_number = iptr->isn_arg.number;
1278 break;
1279 case ISN_PUSHSPEC:
1280 tv->v_type = VAR_SPECIAL;
1281 tv->vval.v_number = iptr->isn_arg.number;
1282 break;
1283#ifdef FEAT_FLOAT
1284 case ISN_PUSHF:
1285 tv->v_type = VAR_FLOAT;
1286 tv->vval.v_float = iptr->isn_arg.fnumber;
1287 break;
1288#endif
1289 case ISN_PUSHBLOB:
1290 blob_copy(iptr->isn_arg.blob, tv);
1291 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001292 case ISN_PUSHFUNC:
1293 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001294 if (iptr->isn_arg.string == NULL)
1295 tv->vval.v_string = NULL;
1296 else
1297 tv->vval.v_string =
1298 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001299 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001300 case ISN_PUSHCHANNEL:
1301#ifdef FEAT_JOB_CHANNEL
1302 tv->v_type = VAR_CHANNEL;
1303 tv->vval.v_channel = iptr->isn_arg.channel;
1304 if (tv->vval.v_channel != NULL)
1305 ++tv->vval.v_channel->ch_refcount;
1306#endif
1307 break;
1308 case ISN_PUSHJOB:
1309#ifdef FEAT_JOB_CHANNEL
1310 tv->v_type = VAR_JOB;
1311 tv->vval.v_job = iptr->isn_arg.job;
1312 if (tv->vval.v_job != NULL)
1313 ++tv->vval.v_job->jv_refcount;
1314#endif
1315 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 default:
1317 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001318 tv->vval.v_string = vim_strsave(
1319 iptr->isn_arg.string == NULL
1320 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 }
1322 break;
1323
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001324 case ISN_UNLET:
1325 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1326 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1327 goto failed;
1328 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001329 case ISN_UNLETENV:
1330 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1331 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001332
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 // create a list from items on the stack; uses a single allocation
1334 // for the list header and the items
1335 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001336 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1337 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338 break;
1339
1340 // create a dict from items on the stack
1341 case ISN_NEWDICT:
1342 {
1343 int count = iptr->isn_arg.number;
1344 dict_T *dict = dict_alloc();
1345 dictitem_T *item;
1346
1347 if (dict == NULL)
1348 goto failed;
1349 for (idx = 0; idx < count; ++idx)
1350 {
1351 // check key type is VAR_STRING
1352 tv = STACK_TV_BOT(2 * (idx - count));
1353 item = dictitem_alloc(tv->vval.v_string);
1354 clear_tv(tv);
1355 if (item == NULL)
1356 goto failed;
1357 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1358 item->di_tv.v_lock = 0;
1359 if (dict_add(dict, item) == FAIL)
1360 goto failed;
1361 }
1362
1363 if (count > 0)
1364 ectx.ec_stack.ga_len -= 2 * count - 1;
1365 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1366 goto failed;
1367 else
1368 ++ectx.ec_stack.ga_len;
1369 tv = STACK_TV_BOT(-1);
1370 tv->v_type = VAR_DICT;
1371 tv->vval.v_dict = dict;
1372 ++dict->dv_refcount;
1373 }
1374 break;
1375
1376 // call a :def function
1377 case ISN_DCALL:
1378 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1379 iptr->isn_arg.dfunc.cdf_argcount,
1380 &ectx) == FAIL)
1381 goto failed;
1382 break;
1383
1384 // call a builtin function
1385 case ISN_BCALL:
1386 SOURCING_LNUM = iptr->isn_lnum;
1387 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1388 iptr->isn_arg.bfunc.cbf_argcount,
1389 &ectx) == FAIL)
1390 goto failed;
1391 break;
1392
1393 // call a funcref or partial
1394 case ISN_PCALL:
1395 {
1396 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1397 int r;
1398 typval_T partial;
1399
1400 SOURCING_LNUM = iptr->isn_lnum;
1401 if (pfunc->cpf_top)
1402 {
1403 // funcref is above the arguments
1404 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1405 }
1406 else
1407 {
1408 // Get the funcref from the stack.
1409 --ectx.ec_stack.ga_len;
1410 partial = *STACK_TV_BOT(0);
1411 tv = &partial;
1412 }
1413 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1414 if (tv == &partial)
1415 clear_tv(&partial);
1416 if (r == FAIL)
1417 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 }
1419 break;
1420
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001421 case ISN_PCALL_END:
1422 // PCALL finished, arguments have been consumed and replaced by
1423 // the return value. Now clear the funcref from the stack,
1424 // and move the return value in its place.
1425 --ectx.ec_stack.ga_len;
1426 clear_tv(STACK_TV_BOT(-1));
1427 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1428 break;
1429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 // call a user defined function or funcref/partial
1431 case ISN_UCALL:
1432 {
1433 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1434
1435 SOURCING_LNUM = iptr->isn_lnum;
1436 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001437 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438 goto failed;
1439 }
1440 break;
1441
1442 // return from a :def function call
1443 case ISN_RETURN:
1444 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001445 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001446 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001447
1448 if (trystack->ga_len > 0)
1449 trycmd = ((trycmd_T *)trystack->ga_data)
1450 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001451 if (trycmd != NULL
1452 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453 && trycmd->tcd_finally_idx != 0)
1454 {
1455 // jump to ":finally"
1456 ectx.ec_iidx = trycmd->tcd_finally_idx;
1457 trycmd->tcd_return = TRUE;
1458 }
1459 else
1460 {
1461 // Restore previous function. If the frame pointer
1462 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001463 if (ectx.ec_frame_idx == initial_frame_idx)
1464 {
1465 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1466 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001468 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001470 if (func_return(&ectx) == FAIL)
1471 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001472 }
1473 }
1474 break;
1475
1476 // push a function reference to a compiled function
1477 case ISN_FUNCREF:
1478 {
1479 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001480 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001481
1482 pt = ALLOC_CLEAR_ONE(partial_T);
1483 if (pt == NULL)
1484 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001485 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001486 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001487 vim_free(pt);
1488 goto failed;
1489 }
1490 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1491 + iptr->isn_arg.funcref.fr_func;
1492 pt->pt_func = pt_dfunc->df_ufunc;
1493 pt->pt_refcount = 1;
1494 ++pt_dfunc->df_ufunc->uf_refcount;
1495
1496 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1497 {
1498 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1499 + ectx.ec_dfunc_idx;
1500
1501 // The closure needs to find arguments and local
1502 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001503 pt->pt_ectx_stack = &ectx.ec_stack;
1504 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001505
1506 // If this function returns and the closure is still
1507 // used, we need to make a copy of the context
1508 // (arguments and local variables). Store a reference
1509 // to the partial so we can handle that.
1510 ++pt->pt_refcount;
1511 tv = STACK_TV_VAR(dfunc->df_varcount
1512 + iptr->isn_arg.funcref.fr_var_idx);
1513 if (tv->v_type == VAR_PARTIAL)
1514 {
1515 // TODO: use a garray_T on ectx.
1516 emsg("Multiple closures not supported yet");
1517 goto failed;
1518 }
1519 tv->v_type = VAR_PARTIAL;
1520 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001521 }
1522
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001523 tv = STACK_TV_BOT(0);
1524 ++ectx.ec_stack.ga_len;
1525 tv->vval.v_partial = pt;
1526 tv->v_type = VAR_PARTIAL;
1527 }
1528 break;
1529
1530 // jump if a condition is met
1531 case ISN_JUMP:
1532 {
1533 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1534 int jump = TRUE;
1535
1536 if (when != JUMP_ALWAYS)
1537 {
1538 tv = STACK_TV_BOT(-1);
1539 jump = tv2bool(tv);
1540 if (when == JUMP_IF_FALSE
1541 || when == JUMP_AND_KEEP_IF_FALSE)
1542 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001543 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 {
1545 // drop the value from the stack
1546 clear_tv(tv);
1547 --ectx.ec_stack.ga_len;
1548 }
1549 }
1550 if (jump)
1551 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1552 }
1553 break;
1554
1555 // top of a for loop
1556 case ISN_FOR:
1557 {
1558 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1559 typval_T *idxtv =
1560 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1561
1562 // push the next item from the list
1563 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1564 goto failed;
1565 if (++idxtv->vval.v_number >= list->lv_len)
1566 // past the end of the list, jump to "endfor"
1567 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1568 else if (list->lv_first == &range_list_item)
1569 {
1570 // non-materialized range() list
1571 tv = STACK_TV_BOT(0);
1572 tv->v_type = VAR_NUMBER;
1573 tv->vval.v_number = list_find_nr(
1574 list, idxtv->vval.v_number, NULL);
1575 ++ectx.ec_stack.ga_len;
1576 }
1577 else
1578 {
1579 listitem_T *li = list_find(list, idxtv->vval.v_number);
1580
1581 if (li == NULL)
1582 goto failed;
1583 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1584 ++ectx.ec_stack.ga_len;
1585 }
1586 }
1587 break;
1588
1589 // start of ":try" block
1590 case ISN_TRY:
1591 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001592 trycmd_T *trycmd = NULL;
1593
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1595 goto failed;
1596 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1597 + ectx.ec_trystack.ga_len;
1598 ++ectx.ec_trystack.ga_len;
1599 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001600 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1602 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001603 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 }
1605 break;
1606
1607 case ISN_PUSHEXC:
1608 if (current_exception == NULL)
1609 {
1610 iemsg("Evaluating catch while current_exception is NULL");
1611 goto failed;
1612 }
1613 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1614 goto failed;
1615 tv = STACK_TV_BOT(0);
1616 ++ectx.ec_stack.ga_len;
1617 tv->v_type = VAR_STRING;
1618 tv->vval.v_string = vim_strsave(
1619 (char_u *)current_exception->value);
1620 break;
1621
1622 case ISN_CATCH:
1623 {
1624 garray_T *trystack = &ectx.ec_trystack;
1625
1626 if (trystack->ga_len > 0)
1627 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001628 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 + trystack->ga_len - 1;
1630 trycmd->tcd_caught = TRUE;
1631 }
1632 did_emsg = got_int = did_throw = FALSE;
1633 catch_exception(current_exception);
1634 }
1635 break;
1636
1637 // end of ":try" block
1638 case ISN_ENDTRY:
1639 {
1640 garray_T *trystack = &ectx.ec_trystack;
1641
1642 if (trystack->ga_len > 0)
1643 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001644 trycmd_T *trycmd = NULL;
1645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 --trystack->ga_len;
1647 --trylevel;
1648 trycmd = ((trycmd_T *)trystack->ga_data)
1649 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001650 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 {
1652 // discard the exception
1653 if (caught_stack == current_exception)
1654 caught_stack = caught_stack->caught;
1655 discard_current_exception();
1656 }
1657
1658 if (trycmd->tcd_return)
1659 {
1660 // Restore previous function. If the frame pointer
1661 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001662 if (ectx.ec_frame_idx == initial_frame_idx)
1663 {
1664 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1665 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001669 if (func_return(&ectx) == FAIL)
1670 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 }
1672 }
1673 }
1674 break;
1675
1676 case ISN_THROW:
1677 --ectx.ec_stack.ga_len;
1678 tv = STACK_TV_BOT(0);
1679 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1680 {
1681 vim_free(tv->vval.v_string);
1682 goto failed;
1683 }
1684 did_throw = TRUE;
1685 break;
1686
1687 // compare with special values
1688 case ISN_COMPAREBOOL:
1689 case ISN_COMPARESPECIAL:
1690 {
1691 typval_T *tv1 = STACK_TV_BOT(-2);
1692 typval_T *tv2 = STACK_TV_BOT(-1);
1693 varnumber_T arg1 = tv1->vval.v_number;
1694 varnumber_T arg2 = tv2->vval.v_number;
1695 int res;
1696
1697 switch (iptr->isn_arg.op.op_type)
1698 {
1699 case EXPR_EQUAL: res = arg1 == arg2; break;
1700 case EXPR_NEQUAL: res = arg1 != arg2; break;
1701 default: res = 0; break;
1702 }
1703
1704 --ectx.ec_stack.ga_len;
1705 tv1->v_type = VAR_BOOL;
1706 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1707 }
1708 break;
1709
1710 // Operation with two number arguments
1711 case ISN_OPNR:
1712 case ISN_COMPARENR:
1713 {
1714 typval_T *tv1 = STACK_TV_BOT(-2);
1715 typval_T *tv2 = STACK_TV_BOT(-1);
1716 varnumber_T arg1 = tv1->vval.v_number;
1717 varnumber_T arg2 = tv2->vval.v_number;
1718 varnumber_T res;
1719
1720 switch (iptr->isn_arg.op.op_type)
1721 {
1722 case EXPR_MULT: res = arg1 * arg2; break;
1723 case EXPR_DIV: res = arg1 / arg2; break;
1724 case EXPR_REM: res = arg1 % arg2; break;
1725 case EXPR_SUB: res = arg1 - arg2; break;
1726 case EXPR_ADD: res = arg1 + arg2; break;
1727
1728 case EXPR_EQUAL: res = arg1 == arg2; break;
1729 case EXPR_NEQUAL: res = arg1 != arg2; break;
1730 case EXPR_GREATER: res = arg1 > arg2; break;
1731 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1732 case EXPR_SMALLER: res = arg1 < arg2; break;
1733 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1734 default: res = 0; break;
1735 }
1736
1737 --ectx.ec_stack.ga_len;
1738 if (iptr->isn_type == ISN_COMPARENR)
1739 {
1740 tv1->v_type = VAR_BOOL;
1741 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1742 }
1743 else
1744 tv1->vval.v_number = res;
1745 }
1746 break;
1747
1748 // Computation with two float arguments
1749 case ISN_OPFLOAT:
1750 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001751#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 {
1753 typval_T *tv1 = STACK_TV_BOT(-2);
1754 typval_T *tv2 = STACK_TV_BOT(-1);
1755 float_T arg1 = tv1->vval.v_float;
1756 float_T arg2 = tv2->vval.v_float;
1757 float_T res = 0;
1758 int cmp = FALSE;
1759
1760 switch (iptr->isn_arg.op.op_type)
1761 {
1762 case EXPR_MULT: res = arg1 * arg2; break;
1763 case EXPR_DIV: res = arg1 / arg2; break;
1764 case EXPR_SUB: res = arg1 - arg2; break;
1765 case EXPR_ADD: res = arg1 + arg2; break;
1766
1767 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1768 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1769 case EXPR_GREATER: cmp = arg1 > arg2; break;
1770 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1771 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1772 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1773 default: cmp = 0; break;
1774 }
1775 --ectx.ec_stack.ga_len;
1776 if (iptr->isn_type == ISN_COMPAREFLOAT)
1777 {
1778 tv1->v_type = VAR_BOOL;
1779 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1780 }
1781 else
1782 tv1->vval.v_float = res;
1783 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001784#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 break;
1786
1787 case ISN_COMPARELIST:
1788 {
1789 typval_T *tv1 = STACK_TV_BOT(-2);
1790 typval_T *tv2 = STACK_TV_BOT(-1);
1791 list_T *arg1 = tv1->vval.v_list;
1792 list_T *arg2 = tv2->vval.v_list;
1793 int cmp = FALSE;
1794 int ic = iptr->isn_arg.op.op_ic;
1795
1796 switch (iptr->isn_arg.op.op_type)
1797 {
1798 case EXPR_EQUAL: cmp =
1799 list_equal(arg1, arg2, ic, FALSE); break;
1800 case EXPR_NEQUAL: cmp =
1801 !list_equal(arg1, arg2, ic, FALSE); break;
1802 case EXPR_IS: cmp = arg1 == arg2; break;
1803 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1804 default: cmp = 0; break;
1805 }
1806 --ectx.ec_stack.ga_len;
1807 clear_tv(tv1);
1808 clear_tv(tv2);
1809 tv1->v_type = VAR_BOOL;
1810 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1811 }
1812 break;
1813
1814 case ISN_COMPAREBLOB:
1815 {
1816 typval_T *tv1 = STACK_TV_BOT(-2);
1817 typval_T *tv2 = STACK_TV_BOT(-1);
1818 blob_T *arg1 = tv1->vval.v_blob;
1819 blob_T *arg2 = tv2->vval.v_blob;
1820 int cmp = FALSE;
1821
1822 switch (iptr->isn_arg.op.op_type)
1823 {
1824 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1825 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1826 case EXPR_IS: cmp = arg1 == arg2; break;
1827 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1828 default: cmp = 0; break;
1829 }
1830 --ectx.ec_stack.ga_len;
1831 clear_tv(tv1);
1832 clear_tv(tv2);
1833 tv1->v_type = VAR_BOOL;
1834 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1835 }
1836 break;
1837
1838 // TODO: handle separately
1839 case ISN_COMPARESTRING:
1840 case ISN_COMPAREDICT:
1841 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 case ISN_COMPAREANY:
1843 {
1844 typval_T *tv1 = STACK_TV_BOT(-2);
1845 typval_T *tv2 = STACK_TV_BOT(-1);
1846 exptype_T exptype = iptr->isn_arg.op.op_type;
1847 int ic = iptr->isn_arg.op.op_ic;
1848
1849 typval_compare(tv1, tv2, exptype, ic);
1850 clear_tv(tv2);
1851 tv1->v_type = VAR_BOOL;
1852 tv1->vval.v_number = tv1->vval.v_number
1853 ? VVAL_TRUE : VVAL_FALSE;
1854 --ectx.ec_stack.ga_len;
1855 }
1856 break;
1857
1858 case ISN_ADDLIST:
1859 case ISN_ADDBLOB:
1860 {
1861 typval_T *tv1 = STACK_TV_BOT(-2);
1862 typval_T *tv2 = STACK_TV_BOT(-1);
1863
1864 if (iptr->isn_type == ISN_ADDLIST)
1865 eval_addlist(tv1, tv2);
1866 else
1867 eval_addblob(tv1, tv2);
1868 clear_tv(tv2);
1869 --ectx.ec_stack.ga_len;
1870 }
1871 break;
1872
1873 // Computation with two arguments of unknown type
1874 case ISN_OPANY:
1875 {
1876 typval_T *tv1 = STACK_TV_BOT(-2);
1877 typval_T *tv2 = STACK_TV_BOT(-1);
1878 varnumber_T n1, n2;
1879#ifdef FEAT_FLOAT
1880 float_T f1 = 0, f2 = 0;
1881#endif
1882 int error = FALSE;
1883
1884 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1885 {
1886 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1887 {
1888 eval_addlist(tv1, tv2);
1889 clear_tv(tv2);
1890 --ectx.ec_stack.ga_len;
1891 break;
1892 }
1893 else if (tv1->v_type == VAR_BLOB
1894 && tv2->v_type == VAR_BLOB)
1895 {
1896 eval_addblob(tv1, tv2);
1897 clear_tv(tv2);
1898 --ectx.ec_stack.ga_len;
1899 break;
1900 }
1901 }
1902#ifdef FEAT_FLOAT
1903 if (tv1->v_type == VAR_FLOAT)
1904 {
1905 f1 = tv1->vval.v_float;
1906 n1 = 0;
1907 }
1908 else
1909#endif
1910 {
1911 n1 = tv_get_number_chk(tv1, &error);
1912 if (error)
1913 goto failed;
1914#ifdef FEAT_FLOAT
1915 if (tv2->v_type == VAR_FLOAT)
1916 f1 = n1;
1917#endif
1918 }
1919#ifdef FEAT_FLOAT
1920 if (tv2->v_type == VAR_FLOAT)
1921 {
1922 f2 = tv2->vval.v_float;
1923 n2 = 0;
1924 }
1925 else
1926#endif
1927 {
1928 n2 = tv_get_number_chk(tv2, &error);
1929 if (error)
1930 goto failed;
1931#ifdef FEAT_FLOAT
1932 if (tv1->v_type == VAR_FLOAT)
1933 f2 = n2;
1934#endif
1935 }
1936#ifdef FEAT_FLOAT
1937 // if there is a float on either side the result is a float
1938 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1939 {
1940 switch (iptr->isn_arg.op.op_type)
1941 {
1942 case EXPR_MULT: f1 = f1 * f2; break;
1943 case EXPR_DIV: f1 = f1 / f2; break;
1944 case EXPR_SUB: f1 = f1 - f2; break;
1945 case EXPR_ADD: f1 = f1 + f2; break;
1946 default: emsg(_(e_modulus)); goto failed;
1947 }
1948 clear_tv(tv1);
1949 clear_tv(tv2);
1950 tv1->v_type = VAR_FLOAT;
1951 tv1->vval.v_float = f1;
1952 --ectx.ec_stack.ga_len;
1953 }
1954 else
1955#endif
1956 {
1957 switch (iptr->isn_arg.op.op_type)
1958 {
1959 case EXPR_MULT: n1 = n1 * n2; break;
1960 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1961 case EXPR_SUB: n1 = n1 - n2; break;
1962 case EXPR_ADD: n1 = n1 + n2; break;
1963 default: n1 = num_modulus(n1, n2); break;
1964 }
1965 clear_tv(tv1);
1966 clear_tv(tv2);
1967 tv1->v_type = VAR_NUMBER;
1968 tv1->vval.v_number = n1;
1969 --ectx.ec_stack.ga_len;
1970 }
1971 }
1972 break;
1973
1974 case ISN_CONCAT:
1975 {
1976 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1977 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1978 char_u *res;
1979
1980 res = concat_str(str1, str2);
1981 clear_tv(STACK_TV_BOT(-2));
1982 clear_tv(STACK_TV_BOT(-1));
1983 --ectx.ec_stack.ga_len;
1984 STACK_TV_BOT(-1)->vval.v_string = res;
1985 }
1986 break;
1987
1988 case ISN_INDEX:
1989 {
1990 list_T *list;
1991 varnumber_T n;
1992 listitem_T *li;
1993
1994 // list index: list is at stack-2, index at stack-1
1995 tv = STACK_TV_BOT(-2);
1996 if (tv->v_type != VAR_LIST)
1997 {
1998 emsg(_(e_listreq));
1999 goto failed;
2000 }
2001 list = tv->vval.v_list;
2002
2003 tv = STACK_TV_BOT(-1);
2004 if (tv->v_type != VAR_NUMBER)
2005 {
2006 emsg(_(e_number_exp));
2007 goto failed;
2008 }
2009 n = tv->vval.v_number;
2010 clear_tv(tv);
2011 if ((li = list_find(list, n)) == NULL)
2012 {
2013 semsg(_(e_listidx), n);
2014 goto failed;
2015 }
2016 --ectx.ec_stack.ga_len;
2017 clear_tv(STACK_TV_BOT(-1));
2018 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2019 }
2020 break;
2021
2022 // dict member with string key
2023 case ISN_MEMBER:
2024 {
2025 dict_T *dict;
2026 dictitem_T *di;
2027
2028 tv = STACK_TV_BOT(-1);
2029 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2030 {
2031 emsg(_(e_dictreq));
2032 goto failed;
2033 }
2034 dict = tv->vval.v_dict;
2035
2036 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2037 == NULL)
2038 {
2039 semsg(_(e_dictkey), iptr->isn_arg.string);
2040 goto failed;
2041 }
2042 clear_tv(tv);
2043 copy_tv(&di->di_tv, tv);
2044 }
2045 break;
2046
2047 case ISN_NEGATENR:
2048 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002049 if (tv->v_type != VAR_NUMBER
2050#ifdef FEAT_FLOAT
2051 && tv->v_type != VAR_FLOAT
2052#endif
2053 )
2054 {
2055 emsg(_(e_number_exp));
2056 goto failed;
2057 }
2058#ifdef FEAT_FLOAT
2059 if (tv->v_type == VAR_FLOAT)
2060 tv->vval.v_float = -tv->vval.v_float;
2061 else
2062#endif
2063 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002064 break;
2065
2066 case ISN_CHECKNR:
2067 {
2068 int error = FALSE;
2069
2070 tv = STACK_TV_BOT(-1);
2071 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002073 (void)tv_get_number_chk(tv, &error);
2074 if (error)
2075 goto failed;
2076 }
2077 break;
2078
2079 case ISN_CHECKTYPE:
2080 {
2081 checktype_T *ct = &iptr->isn_arg.type;
2082
2083 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002084 // TODO: better type comparison
2085 if (tv->v_type != ct->ct_type
2086 && !((tv->v_type == VAR_PARTIAL
2087 && ct->ct_type == VAR_FUNC)
2088 || (tv->v_type == VAR_FUNC
2089 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 {
2091 semsg(_("E1029: Expected %s but got %s"),
2092 vartype_name(ct->ct_type),
2093 vartype_name(tv->v_type));
2094 goto failed;
2095 }
2096 }
2097 break;
2098
2099 case ISN_2BOOL:
2100 {
2101 int n;
2102
2103 tv = STACK_TV_BOT(-1);
2104 n = tv2bool(tv);
2105 if (iptr->isn_arg.number) // invert
2106 n = !n;
2107 clear_tv(tv);
2108 tv->v_type = VAR_BOOL;
2109 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2110 }
2111 break;
2112
2113 case ISN_2STRING:
2114 {
2115 char_u *str;
2116
2117 tv = STACK_TV_BOT(iptr->isn_arg.number);
2118 if (tv->v_type != VAR_STRING)
2119 {
2120 str = typval_tostring(tv);
2121 clear_tv(tv);
2122 tv->v_type = VAR_STRING;
2123 tv->vval.v_string = str;
2124 }
2125 }
2126 break;
2127
2128 case ISN_DROP:
2129 --ectx.ec_stack.ga_len;
2130 clear_tv(STACK_TV_BOT(0));
2131 break;
2132 }
2133 }
2134
2135done:
2136 // function finished, get result from the stack.
2137 tv = STACK_TV_BOT(-1);
2138 *rettv = *tv;
2139 tv->v_type = VAR_UNKNOWN;
2140 ret = OK;
2141
2142failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002143 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002144 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002145 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002146failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002147 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002148
2149 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2151 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002154 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 return ret;
2156}
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158/*
2159 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002160 * We don't really need this at runtime, but we do have tests that require it,
2161 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 */
2163 void
2164ex_disassemble(exarg_T *eap)
2165{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002166 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002167 char_u *fname;
2168 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 dfunc_T *dfunc;
2170 isn_T *instr;
2171 int current;
2172 int line_idx = 0;
2173 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002174 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002176 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002177 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002178 if (fname == NULL)
2179 {
2180 semsg(_(e_invarg2), eap->arg);
2181 return;
2182 }
2183
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002184 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002185 if (ufunc == NULL)
2186 {
2187 char_u *p = untrans_function_name(fname);
2188
2189 if (p != NULL)
2190 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002191 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002192 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002193 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194 if (ufunc == NULL)
2195 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002196 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 return;
2198 }
2199 if (ufunc->uf_dfunc_idx < 0)
2200 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002201 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 return;
2203 }
2204 if (ufunc->uf_name_exp != NULL)
2205 msg((char *)ufunc->uf_name_exp);
2206 else
2207 msg((char *)ufunc->uf_name);
2208
2209 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2210 instr = dfunc->df_instr;
2211 for (current = 0; current < dfunc->df_instr_count; ++current)
2212 {
2213 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002214 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215
2216 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2217 {
2218 if (current > prev_current)
2219 {
2220 msg_puts("\n\n");
2221 prev_current = current;
2222 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002223 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2224 if (line != NULL)
2225 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002226 }
2227
2228 switch (iptr->isn_type)
2229 {
2230 case ISN_EXEC:
2231 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2232 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002233 case ISN_EXECCONCAT:
2234 smsg("%4d EXECCONCAT %lld", current,
2235 (long long)iptr->isn_arg.number);
2236 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 case ISN_ECHO:
2238 {
2239 echo_T *echo = &iptr->isn_arg.echo;
2240
2241 smsg("%4d %s %d", current,
2242 echo->echo_with_white ? "ECHO" : "ECHON",
2243 echo->echo_count);
2244 }
2245 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002246 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002247 smsg("%4d EXECUTE %lld", current,
2248 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002249 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002250 case ISN_ECHOMSG:
2251 smsg("%4d ECHOMSG %lld", current,
2252 (long long)(iptr->isn_arg.number));
2253 break;
2254 case ISN_ECHOERR:
2255 smsg("%4d ECHOERR %lld", current,
2256 (long long)(iptr->isn_arg.number));
2257 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002259 case ISN_LOADOUTER:
2260 {
2261 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2262
2263 if (iptr->isn_arg.number < 0)
2264 smsg("%4d LOAD%s arg[%lld]", current, add,
2265 (long long)(iptr->isn_arg.number
2266 + STACK_FRAME_SIZE));
2267 else
2268 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002269 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002270 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 break;
2272 case ISN_LOADV:
2273 smsg("%4d LOADV v:%s", current,
2274 get_vim_var_name(iptr->isn_arg.number));
2275 break;
2276 case ISN_LOADSCRIPT:
2277 {
2278 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002279 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002280 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2281 + iptr->isn_arg.script.script_idx;
2282
2283 smsg("%4d LOADSCRIPT %s from %s", current,
2284 sv->sv_name, si->sn_name);
2285 }
2286 break;
2287 case ISN_LOADS:
2288 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002289 scriptitem_T *si = SCRIPT_ITEM(
2290 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291
2292 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002293 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 }
2295 break;
2296 case ISN_LOADG:
2297 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2298 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002299 case ISN_LOADB:
2300 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2301 break;
2302 case ISN_LOADW:
2303 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2304 break;
2305 case ISN_LOADT:
2306 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2307 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 case ISN_LOADOPT:
2309 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2310 break;
2311 case ISN_LOADENV:
2312 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2313 break;
2314 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002315 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 break;
2317
2318 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002319 case ISN_STOREOUTER:
2320 {
2321 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2322
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002323 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002324 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002325 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002326 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002327 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002328 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002330 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002331 case ISN_STOREV:
2332 smsg("%4d STOREV v:%s", current,
2333 get_vim_var_name(iptr->isn_arg.number));
2334 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002336 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2337 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002338 case ISN_STOREB:
2339 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2340 break;
2341 case ISN_STOREW:
2342 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2343 break;
2344 case ISN_STORET:
2345 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2346 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002347 case ISN_STORES:
2348 {
2349 scriptitem_T *si = SCRIPT_ITEM(
2350 iptr->isn_arg.loadstore.ls_sid);
2351
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002352 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002353 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002354 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 break;
2356 case ISN_STORESCRIPT:
2357 {
2358 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002359 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002360 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2361 + iptr->isn_arg.script.script_idx;
2362
2363 smsg("%4d STORESCRIPT %s in %s", current,
2364 sv->sv_name, si->sn_name);
2365 }
2366 break;
2367 case ISN_STOREOPT:
2368 smsg("%4d STOREOPT &%s", current,
2369 iptr->isn_arg.storeopt.so_name);
2370 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002371 case ISN_STOREENV:
2372 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2373 break;
2374 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002375 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002376 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377 case ISN_STORENR:
2378 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002379 iptr->isn_arg.storenr.stnr_val,
2380 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 break;
2382
2383 // constants
2384 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002385 smsg("%4d PUSHNR %lld", current,
2386 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 break;
2388 case ISN_PUSHBOOL:
2389 case ISN_PUSHSPEC:
2390 smsg("%4d PUSH %s", current,
2391 get_var_special_name(iptr->isn_arg.number));
2392 break;
2393 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002394#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002396#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 break;
2398 case ISN_PUSHS:
2399 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2400 break;
2401 case ISN_PUSHBLOB:
2402 {
2403 char_u *r;
2404 char_u numbuf[NUMBUFLEN];
2405 char_u *tofree;
2406
2407 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002408 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 vim_free(tofree);
2410 }
2411 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002412 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002413 {
2414 char *name = (char *)iptr->isn_arg.string;
2415
2416 smsg("%4d PUSHFUNC \"%s\"", current,
2417 name == NULL ? "[none]" : name);
2418 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002419 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002420 case ISN_PUSHCHANNEL:
2421#ifdef FEAT_JOB_CHANNEL
2422 {
2423 channel_T *channel = iptr->isn_arg.channel;
2424
2425 smsg("%4d PUSHCHANNEL %d", current,
2426 channel == NULL ? 0 : channel->ch_id);
2427 }
2428#endif
2429 break;
2430 case ISN_PUSHJOB:
2431#ifdef FEAT_JOB_CHANNEL
2432 {
2433 typval_T tv;
2434 char_u *name;
2435
2436 tv.v_type = VAR_JOB;
2437 tv.vval.v_job = iptr->isn_arg.job;
2438 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002439 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002440 }
2441#endif
2442 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 case ISN_PUSHEXC:
2444 smsg("%4d PUSH v:exception", current);
2445 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002446 case ISN_UNLET:
2447 smsg("%4d UNLET%s %s", current,
2448 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2449 iptr->isn_arg.unlet.ul_name);
2450 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002451 case ISN_UNLETENV:
2452 smsg("%4d UNLETENV%s $%s", current,
2453 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2454 iptr->isn_arg.unlet.ul_name);
2455 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002457 smsg("%4d NEWLIST size %lld", current,
2458 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 break;
2460 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002461 smsg("%4d NEWDICT size %lld", current,
2462 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 break;
2464
2465 // function call
2466 case ISN_BCALL:
2467 {
2468 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2469
2470 smsg("%4d BCALL %s(argc %d)", current,
2471 internal_func_name(cbfunc->cbf_idx),
2472 cbfunc->cbf_argcount);
2473 }
2474 break;
2475 case ISN_DCALL:
2476 {
2477 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2478 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2479 + cdfunc->cdf_idx;
2480
2481 smsg("%4d DCALL %s(argc %d)", current,
2482 df->df_ufunc->uf_name_exp != NULL
2483 ? df->df_ufunc->uf_name_exp
2484 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2485 }
2486 break;
2487 case ISN_UCALL:
2488 {
2489 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2490
2491 smsg("%4d UCALL %s(argc %d)", current,
2492 cufunc->cuf_name, cufunc->cuf_argcount);
2493 }
2494 break;
2495 case ISN_PCALL:
2496 {
2497 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2498
2499 smsg("%4d PCALL%s (argc %d)", current,
2500 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2501 }
2502 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002503 case ISN_PCALL_END:
2504 smsg("%4d PCALL end", current);
2505 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 case ISN_RETURN:
2507 smsg("%4d RETURN", current);
2508 break;
2509 case ISN_FUNCREF:
2510 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002511 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002512 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002513 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002515 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002516 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002517 }
2518 break;
2519
2520 case ISN_JUMP:
2521 {
2522 char *when = "?";
2523
2524 switch (iptr->isn_arg.jump.jump_when)
2525 {
2526 case JUMP_ALWAYS:
2527 when = "JUMP";
2528 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 case JUMP_AND_KEEP_IF_TRUE:
2530 when = "JUMP_AND_KEEP_IF_TRUE";
2531 break;
2532 case JUMP_IF_FALSE:
2533 when = "JUMP_IF_FALSE";
2534 break;
2535 case JUMP_AND_KEEP_IF_FALSE:
2536 when = "JUMP_AND_KEEP_IF_FALSE";
2537 break;
2538 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002539 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540 iptr->isn_arg.jump.jump_where);
2541 }
2542 break;
2543
2544 case ISN_FOR:
2545 {
2546 forloop_T *forloop = &iptr->isn_arg.forloop;
2547
2548 smsg("%4d FOR $%d -> %d", current,
2549 forloop->for_idx, forloop->for_end);
2550 }
2551 break;
2552
2553 case ISN_TRY:
2554 {
2555 try_T *try = &iptr->isn_arg.try;
2556
2557 smsg("%4d TRY catch -> %d, finally -> %d", current,
2558 try->try_catch, try->try_finally);
2559 }
2560 break;
2561 case ISN_CATCH:
2562 // TODO
2563 smsg("%4d CATCH", current);
2564 break;
2565 case ISN_ENDTRY:
2566 smsg("%4d ENDTRY", current);
2567 break;
2568 case ISN_THROW:
2569 smsg("%4d THROW", current);
2570 break;
2571
2572 // expression operations on number
2573 case ISN_OPNR:
2574 case ISN_OPFLOAT:
2575 case ISN_OPANY:
2576 {
2577 char *what;
2578 char *ins;
2579
2580 switch (iptr->isn_arg.op.op_type)
2581 {
2582 case EXPR_MULT: what = "*"; break;
2583 case EXPR_DIV: what = "/"; break;
2584 case EXPR_REM: what = "%"; break;
2585 case EXPR_SUB: what = "-"; break;
2586 case EXPR_ADD: what = "+"; break;
2587 default: what = "???"; break;
2588 }
2589 switch (iptr->isn_type)
2590 {
2591 case ISN_OPNR: ins = "OPNR"; break;
2592 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2593 case ISN_OPANY: ins = "OPANY"; break;
2594 default: ins = "???"; break;
2595 }
2596 smsg("%4d %s %s", current, ins, what);
2597 }
2598 break;
2599
2600 case ISN_COMPAREBOOL:
2601 case ISN_COMPARESPECIAL:
2602 case ISN_COMPARENR:
2603 case ISN_COMPAREFLOAT:
2604 case ISN_COMPARESTRING:
2605 case ISN_COMPAREBLOB:
2606 case ISN_COMPARELIST:
2607 case ISN_COMPAREDICT:
2608 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 case ISN_COMPAREANY:
2610 {
2611 char *p;
2612 char buf[10];
2613 char *type;
2614
2615 switch (iptr->isn_arg.op.op_type)
2616 {
2617 case EXPR_EQUAL: p = "=="; break;
2618 case EXPR_NEQUAL: p = "!="; break;
2619 case EXPR_GREATER: p = ">"; break;
2620 case EXPR_GEQUAL: p = ">="; break;
2621 case EXPR_SMALLER: p = "<"; break;
2622 case EXPR_SEQUAL: p = "<="; break;
2623 case EXPR_MATCH: p = "=~"; break;
2624 case EXPR_IS: p = "is"; break;
2625 case EXPR_ISNOT: p = "isnot"; break;
2626 case EXPR_NOMATCH: p = "!~"; break;
2627 default: p = "???"; break;
2628 }
2629 STRCPY(buf, p);
2630 if (iptr->isn_arg.op.op_ic == TRUE)
2631 strcat(buf, "?");
2632 switch(iptr->isn_type)
2633 {
2634 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2635 case ISN_COMPARESPECIAL:
2636 type = "COMPARESPECIAL"; break;
2637 case ISN_COMPARENR: type = "COMPARENR"; break;
2638 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2639 case ISN_COMPARESTRING:
2640 type = "COMPARESTRING"; break;
2641 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2642 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2643 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2644 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2646 default: type = "???"; break;
2647 }
2648
2649 smsg("%4d %s %s", current, type, buf);
2650 }
2651 break;
2652
2653 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2654 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2655
2656 // expression operations
2657 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2658 case ISN_INDEX: smsg("%4d INDEX", current); break;
2659 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2660 iptr->isn_arg.string); break;
2661 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2662
2663 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2664 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2665 vartype_name(iptr->isn_arg.type.ct_type),
2666 iptr->isn_arg.type.ct_off);
2667 break;
2668 case ISN_2BOOL: if (iptr->isn_arg.number)
2669 smsg("%4d INVERT (!val)", current);
2670 else
2671 smsg("%4d 2BOOL (!!val)", current);
2672 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002673 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2674 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 break;
2676
2677 case ISN_DROP: smsg("%4d DROP", current); break;
2678 }
2679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002680}
2681
2682/*
2683 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2684 * list, etc. Mostly like what JavaScript does, except that empty list and
2685 * empty dictionary are FALSE.
2686 */
2687 int
2688tv2bool(typval_T *tv)
2689{
2690 switch (tv->v_type)
2691 {
2692 case VAR_NUMBER:
2693 return tv->vval.v_number != 0;
2694 case VAR_FLOAT:
2695#ifdef FEAT_FLOAT
2696 return tv->vval.v_float != 0.0;
2697#else
2698 break;
2699#endif
2700 case VAR_PARTIAL:
2701 return tv->vval.v_partial != NULL;
2702 case VAR_FUNC:
2703 case VAR_STRING:
2704 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2705 case VAR_LIST:
2706 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2707 case VAR_DICT:
2708 return tv->vval.v_dict != NULL
2709 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2710 case VAR_BOOL:
2711 case VAR_SPECIAL:
2712 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2713 case VAR_JOB:
2714#ifdef FEAT_JOB_CHANNEL
2715 return tv->vval.v_job != NULL;
2716#else
2717 break;
2718#endif
2719 case VAR_CHANNEL:
2720#ifdef FEAT_JOB_CHANNEL
2721 return tv->vval.v_channel != NULL;
2722#else
2723 break;
2724#endif
2725 case VAR_BLOB:
2726 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2727 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002728 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 case VAR_VOID:
2730 break;
2731 }
2732 return FALSE;
2733}
2734
2735/*
2736 * If "tv" is a string give an error and return FAIL.
2737 */
2738 int
2739check_not_string(typval_T *tv)
2740{
2741 if (tv->v_type == VAR_STRING)
2742 {
2743 emsg(_("E1030: Using a String as a Number"));
2744 clear_tv(tv);
2745 return FAIL;
2746 }
2747 return OK;
2748}
2749
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002750
2751#endif // FEAT_EVAL