blob: cc677b001344c8fcfc4711996c71e8042d5d1f6c [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 Moolenaar015f4262020-05-05 21:25:22 +0200461 int called_emsg_before = called_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
473 if (called_emsg != called_emsg_before)
474 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 Moolenaara80faa82020-04-12 19:37:17 +0200668 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
670 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
673
674 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
675
676 // Put arguments on the stack.
677 for (idx = 0; idx < argc; ++idx)
678 {
679 copy_tv(&argv[idx], STACK_TV_BOT(0));
680 ++ectx.ec_stack.ga_len;
681 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200682
683 // Turn varargs into a list. Empty list if no args.
684 if (ufunc->uf_va_name != NULL)
685 {
686 int vararg_count = argc - ufunc->uf_args.ga_len;
687
688 if (vararg_count < 0)
689 vararg_count = 0;
690 else
691 argc -= vararg_count;
692 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200693 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200694 if (defcount > 0)
695 // Move varargs list to below missing default arguments.
696 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
697 --ectx.ec_stack.ga_len;
698 }
699
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100700 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200701 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100702 if (defcount > 0)
703 for (idx = 0; idx < defcount; ++idx)
704 {
705 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
706 ++ectx.ec_stack.ga_len;
707 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200708 if (ufunc->uf_va_name != NULL)
709 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710
711 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200712 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
713 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714
715 // dummy frame entries
716 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
717 {
718 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
719 ++ectx.ec_stack.ga_len;
720 }
721
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200722 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200723 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200724 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
725 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200728 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200729 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200730 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200731
732 ectx.ec_instr = dfunc->df_instr;
733 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100734
Bram Moolenaara26b9702020-04-18 19:53:28 +0200735 // Commands behave like vim9script.
736 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
737
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100738 // Decide where to start execution, handles optional arguments.
739 init_instr_idx(ufunc, argc, &ectx);
740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100741 for (;;)
742 {
743 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100744
745 veryfast_breakcheck();
746 if (got_int)
747 {
748 // Turn CTRL-C into an exception.
749 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100750 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100751 goto failed;
752 did_throw = TRUE;
753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754
Bram Moolenaara26b9702020-04-18 19:53:28 +0200755 if (did_emsg && msg_list != NULL && *msg_list != NULL)
756 {
757 // Turn an error message into an exception.
758 did_emsg = FALSE;
759 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
760 goto failed;
761 did_throw = TRUE;
762 *msg_list = NULL;
763 }
764
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 if (did_throw && !ectx.ec_in_catch)
766 {
767 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100768 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769
770 // An exception jumps to the first catch, finally, or returns from
771 // the current function.
772 if (trystack->ga_len > 0)
773 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200774 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 {
776 // jump to ":catch" or ":finally"
777 ectx.ec_in_catch = TRUE;
778 ectx.ec_iidx = trycmd->tcd_catch_idx;
779 }
780 else
781 {
782 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200783 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 {
785 // At the toplevel we are done. Push a dummy return value.
786 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
787 goto failed;
788 tv = STACK_TV_BOT(0);
789 tv->v_type = VAR_NUMBER;
790 tv->vval.v_number = 0;
791 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100792 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200793 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
794 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795 goto done;
796 }
797
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200798 if (func_return(&ectx) == FAIL)
799 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 }
801 continue;
802 }
803
804 iptr = &ectx.ec_instr[ectx.ec_iidx++];
805 switch (iptr->isn_type)
806 {
807 // execute Ex command line
808 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200809 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 do_cmdline_cmd(iptr->isn_arg.string);
811 break;
812
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200813 // execute Ex command from pieces on the stack
814 case ISN_EXECCONCAT:
815 {
816 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200817 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200818 int pass;
819 int i;
820 char_u *cmd = NULL;
821 char_u *str;
822
823 for (pass = 1; pass <= 2; ++pass)
824 {
825 for (i = 0; i < count; ++i)
826 {
827 tv = STACK_TV_BOT(i - count);
828 str = tv->vval.v_string;
829 if (str != NULL && *str != NUL)
830 {
831 if (pass == 2)
832 STRCPY(cmd + len, str);
833 len += STRLEN(str);
834 }
835 if (pass == 2)
836 clear_tv(tv);
837 }
838 if (pass == 1)
839 {
840 cmd = alloc(len + 1);
841 if (cmd == NULL)
842 goto failed;
843 len = 0;
844 }
845 }
846
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200847 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200848 do_cmdline_cmd(cmd);
849 vim_free(cmd);
850 }
851 break;
852
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 // execute :echo {string} ...
854 case ISN_ECHO:
855 {
856 int count = iptr->isn_arg.echo.echo_count;
857 int atstart = TRUE;
858 int needclr = TRUE;
859
860 for (idx = 0; idx < count; ++idx)
861 {
862 tv = STACK_TV_BOT(idx - count);
863 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
864 &atstart, &needclr);
865 clear_tv(tv);
866 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100867 if (needclr)
868 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100869 ectx.ec_stack.ga_len -= count;
870 }
871 break;
872
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200873 // :execute {string} ...
874 // :echomsg {string} ...
875 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100876 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200877 case ISN_ECHOMSG:
878 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100879 {
880 int count = iptr->isn_arg.number;
881 garray_T ga;
882 char_u buf[NUMBUFLEN];
883 char_u *p;
884 int len;
885 int failed = FALSE;
886
887 ga_init2(&ga, 1, 80);
888 for (idx = 0; idx < count; ++idx)
889 {
890 tv = STACK_TV_BOT(idx - count);
891 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
892 {
893 emsg(_(e_inval_string));
894 break;
895 }
896 else
897 p = tv_get_string_buf(tv, buf);
898
899 len = (int)STRLEN(p);
900 if (ga_grow(&ga, len + 2) == FAIL)
901 failed = TRUE;
902 else
903 {
904 if (ga.ga_len > 0)
905 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
906 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
907 ga.ga_len += len;
908 }
909 clear_tv(tv);
910 }
911 ectx.ec_stack.ga_len -= count;
912
913 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200914 {
915 if (iptr->isn_type == ISN_EXECUTE)
916 do_cmdline_cmd((char_u *)ga.ga_data);
917 else
918 {
919 msg_sb_eol();
920 if (iptr->isn_type == ISN_ECHOMSG)
921 {
922 msg_attr(ga.ga_data, echo_attr);
923 out_flush();
924 }
925 else
926 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200927 SOURCING_LNUM = iptr->isn_lnum;
928 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200929 }
930 }
931 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100932 ga_clear(&ga);
933 }
934 break;
935
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 // load local variable or argument
937 case ISN_LOAD:
938 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
939 goto failed;
940 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
941 ++ectx.ec_stack.ga_len;
942 break;
943
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200944 // load variable or argument from outer scope
945 case ISN_LOADOUTER:
946 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
947 goto failed;
948 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
949 STACK_TV_BOT(0));
950 ++ectx.ec_stack.ga_len;
951 break;
952
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 // load v: variable
954 case ISN_LOADV:
955 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
956 goto failed;
957 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
958 ++ectx.ec_stack.ga_len;
959 break;
960
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100961 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 case ISN_LOADSCRIPT:
963 {
964 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100965 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 svar_T *sv;
967
968 sv = ((svar_T *)si->sn_var_vals.ga_data)
969 + iptr->isn_arg.script.script_idx;
970 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
971 goto failed;
972 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
973 ++ectx.ec_stack.ga_len;
974 }
975 break;
976
977 // load s: variable in old script
978 case ISN_LOADS:
979 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100980 hashtab_T *ht = &SCRIPT_VARS(
981 iptr->isn_arg.loadstore.ls_sid);
982 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 if (di == NULL)
986 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100987 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100988 goto failed;
989 }
990 else
991 {
992 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
993 goto failed;
994 copy_tv(&di->di_tv, STACK_TV_BOT(0));
995 ++ectx.ec_stack.ga_len;
996 }
997 }
998 break;
999
Bram Moolenaard3aac292020-04-19 14:32:17 +02001000 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001001 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001002 case ISN_LOADB:
1003 case ISN_LOADW:
1004 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001006 dictitem_T *di = NULL;
1007 hashtab_T *ht = NULL;
1008 char namespace;
1009 switch (iptr->isn_type)
1010 {
1011 case ISN_LOADG:
1012 ht = get_globvar_ht();
1013 namespace = 'g';
1014 break;
1015 case ISN_LOADB:
1016 ht = &curbuf->b_vars->dv_hashtab;
1017 namespace = 'b';
1018 break;
1019 case ISN_LOADW:
1020 ht = &curwin->w_vars->dv_hashtab;
1021 namespace = 'w';
1022 break;
1023 case ISN_LOADT:
1024 ht = &curtab->tp_vars->dv_hashtab;
1025 namespace = 't';
1026 break;
1027 default: // Cannot reach here
1028 goto failed;
1029 }
1030 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001031
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 if (di == NULL)
1033 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001034 semsg(_("E121: Undefined variable: %c:%s"),
1035 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 goto failed;
1037 }
1038 else
1039 {
1040 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1041 goto failed;
1042 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1043 ++ectx.ec_stack.ga_len;
1044 }
1045 }
1046 break;
1047
1048 // load &option
1049 case ISN_LOADOPT:
1050 {
1051 typval_T optval;
1052 char_u *name = iptr->isn_arg.string;
1053
Bram Moolenaara8c17702020-04-01 21:17:24 +02001054 // This is not expected to fail, name is checked during
1055 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001056 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1057 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001058 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1059 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001060 *STACK_TV_BOT(0) = optval;
1061 ++ectx.ec_stack.ga_len;
1062 }
1063 break;
1064
1065 // load $ENV
1066 case ISN_LOADENV:
1067 {
1068 typval_T optval;
1069 char_u *name = iptr->isn_arg.string;
1070
1071 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1072 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001073 // name is always valid, checked when compiling
1074 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 *STACK_TV_BOT(0) = optval;
1076 ++ectx.ec_stack.ga_len;
1077 }
1078 break;
1079
1080 // load @register
1081 case ISN_LOADREG:
1082 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1083 goto failed;
1084 tv = STACK_TV_BOT(0);
1085 tv->v_type = VAR_STRING;
1086 tv->vval.v_string = get_reg_contents(
1087 iptr->isn_arg.number, GREG_EXPR_SRC);
1088 ++ectx.ec_stack.ga_len;
1089 break;
1090
1091 // store local variable
1092 case ISN_STORE:
1093 --ectx.ec_stack.ga_len;
1094 tv = STACK_TV_VAR(iptr->isn_arg.number);
1095 clear_tv(tv);
1096 *tv = *STACK_TV_BOT(0);
1097 break;
1098
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001099 // store variable or argument in outer scope
1100 case ISN_STOREOUTER:
1101 --ectx.ec_stack.ga_len;
1102 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1103 clear_tv(tv);
1104 *tv = *STACK_TV_BOT(0);
1105 break;
1106
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001107 // store s: variable in old script
1108 case ISN_STORES:
1109 {
1110 hashtab_T *ht = &SCRIPT_VARS(
1111 iptr->isn_arg.loadstore.ls_sid);
1112 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001113 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001114
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001115 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001116 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001117 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001118 else
1119 {
1120 clear_tv(&di->di_tv);
1121 di->di_tv = *STACK_TV_BOT(0);
1122 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 }
1124 break;
1125
1126 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 case ISN_STORESCRIPT:
1128 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001129 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 iptr->isn_arg.script.script_sid);
1131 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1132 + iptr->isn_arg.script.script_idx;
1133
1134 --ectx.ec_stack.ga_len;
1135 clear_tv(sv->sv_tv);
1136 *sv->sv_tv = *STACK_TV_BOT(0);
1137 }
1138 break;
1139
1140 // store option
1141 case ISN_STOREOPT:
1142 {
1143 long n = 0;
1144 char_u *s = NULL;
1145 char *msg;
1146
1147 --ectx.ec_stack.ga_len;
1148 tv = STACK_TV_BOT(0);
1149 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001150 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001152 if (s == NULL)
1153 s = (char_u *)"";
1154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001155 else if (tv->v_type == VAR_NUMBER)
1156 n = tv->vval.v_number;
1157 else
1158 {
1159 emsg(_("E1051: Expected string or number"));
1160 goto failed;
1161 }
1162 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1163 n, s, iptr->isn_arg.storeopt.so_flags);
1164 if (msg != NULL)
1165 {
1166 emsg(_(msg));
1167 goto failed;
1168 }
1169 clear_tv(tv);
1170 }
1171 break;
1172
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001173 // store $ENV
1174 case ISN_STOREENV:
1175 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001176 tv = STACK_TV_BOT(0);
1177 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1178 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001179 break;
1180
1181 // store @r
1182 case ISN_STOREREG:
1183 {
1184 int reg = iptr->isn_arg.number;
1185
1186 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001187 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001188 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001189 tv_get_string(tv), -1, FALSE);
1190 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001191 }
1192 break;
1193
1194 // store v: variable
1195 case ISN_STOREV:
1196 --ectx.ec_stack.ga_len;
1197 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1198 == FAIL)
1199 goto failed;
1200 break;
1201
Bram Moolenaard3aac292020-04-19 14:32:17 +02001202 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001204 case ISN_STOREB:
1205 case ISN_STOREW:
1206 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 {
1208 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001209 hashtab_T *ht;
1210 switch (iptr->isn_type)
1211 {
1212 case ISN_STOREG:
1213 ht = get_globvar_ht();
1214 break;
1215 case ISN_STOREB:
1216 ht = &curbuf->b_vars->dv_hashtab;
1217 break;
1218 case ISN_STOREW:
1219 ht = &curwin->w_vars->dv_hashtab;
1220 break;
1221 case ISN_STORET:
1222 ht = &curtab->tp_vars->dv_hashtab;
1223 break;
1224 default: // Cannot reach here
1225 goto failed;
1226 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001227
1228 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001229 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001231 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 else
1233 {
1234 clear_tv(&di->di_tv);
1235 di->di_tv = *STACK_TV_BOT(0);
1236 }
1237 }
1238 break;
1239
1240 // store number in local variable
1241 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001242 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 clear_tv(tv);
1244 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001245 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 break;
1247
1248 // push constant
1249 case ISN_PUSHNR:
1250 case ISN_PUSHBOOL:
1251 case ISN_PUSHSPEC:
1252 case ISN_PUSHF:
1253 case ISN_PUSHS:
1254 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001255 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001256 case ISN_PUSHCHANNEL:
1257 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1259 goto failed;
1260 tv = STACK_TV_BOT(0);
1261 ++ectx.ec_stack.ga_len;
1262 switch (iptr->isn_type)
1263 {
1264 case ISN_PUSHNR:
1265 tv->v_type = VAR_NUMBER;
1266 tv->vval.v_number = iptr->isn_arg.number;
1267 break;
1268 case ISN_PUSHBOOL:
1269 tv->v_type = VAR_BOOL;
1270 tv->vval.v_number = iptr->isn_arg.number;
1271 break;
1272 case ISN_PUSHSPEC:
1273 tv->v_type = VAR_SPECIAL;
1274 tv->vval.v_number = iptr->isn_arg.number;
1275 break;
1276#ifdef FEAT_FLOAT
1277 case ISN_PUSHF:
1278 tv->v_type = VAR_FLOAT;
1279 tv->vval.v_float = iptr->isn_arg.fnumber;
1280 break;
1281#endif
1282 case ISN_PUSHBLOB:
1283 blob_copy(iptr->isn_arg.blob, tv);
1284 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001285 case ISN_PUSHFUNC:
1286 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001287 if (iptr->isn_arg.string == NULL)
1288 tv->vval.v_string = NULL;
1289 else
1290 tv->vval.v_string =
1291 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001292 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001293 case ISN_PUSHCHANNEL:
1294#ifdef FEAT_JOB_CHANNEL
1295 tv->v_type = VAR_CHANNEL;
1296 tv->vval.v_channel = iptr->isn_arg.channel;
1297 if (tv->vval.v_channel != NULL)
1298 ++tv->vval.v_channel->ch_refcount;
1299#endif
1300 break;
1301 case ISN_PUSHJOB:
1302#ifdef FEAT_JOB_CHANNEL
1303 tv->v_type = VAR_JOB;
1304 tv->vval.v_job = iptr->isn_arg.job;
1305 if (tv->vval.v_job != NULL)
1306 ++tv->vval.v_job->jv_refcount;
1307#endif
1308 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 default:
1310 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001311 tv->vval.v_string = vim_strsave(
1312 iptr->isn_arg.string == NULL
1313 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 }
1315 break;
1316
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001317 case ISN_UNLET:
1318 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1319 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1320 goto failed;
1321 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001322 case ISN_UNLETENV:
1323 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1324 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 // create a list from items on the stack; uses a single allocation
1327 // for the list header and the items
1328 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001329 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1330 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 break;
1332
1333 // create a dict from items on the stack
1334 case ISN_NEWDICT:
1335 {
1336 int count = iptr->isn_arg.number;
1337 dict_T *dict = dict_alloc();
1338 dictitem_T *item;
1339
1340 if (dict == NULL)
1341 goto failed;
1342 for (idx = 0; idx < count; ++idx)
1343 {
1344 // check key type is VAR_STRING
1345 tv = STACK_TV_BOT(2 * (idx - count));
1346 item = dictitem_alloc(tv->vval.v_string);
1347 clear_tv(tv);
1348 if (item == NULL)
1349 goto failed;
1350 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1351 item->di_tv.v_lock = 0;
1352 if (dict_add(dict, item) == FAIL)
1353 goto failed;
1354 }
1355
1356 if (count > 0)
1357 ectx.ec_stack.ga_len -= 2 * count - 1;
1358 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1359 goto failed;
1360 else
1361 ++ectx.ec_stack.ga_len;
1362 tv = STACK_TV_BOT(-1);
1363 tv->v_type = VAR_DICT;
1364 tv->vval.v_dict = dict;
1365 ++dict->dv_refcount;
1366 }
1367 break;
1368
1369 // call a :def function
1370 case ISN_DCALL:
1371 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1372 iptr->isn_arg.dfunc.cdf_argcount,
1373 &ectx) == FAIL)
1374 goto failed;
1375 break;
1376
1377 // call a builtin function
1378 case ISN_BCALL:
1379 SOURCING_LNUM = iptr->isn_lnum;
1380 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1381 iptr->isn_arg.bfunc.cbf_argcount,
1382 &ectx) == FAIL)
1383 goto failed;
1384 break;
1385
1386 // call a funcref or partial
1387 case ISN_PCALL:
1388 {
1389 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1390 int r;
1391 typval_T partial;
1392
1393 SOURCING_LNUM = iptr->isn_lnum;
1394 if (pfunc->cpf_top)
1395 {
1396 // funcref is above the arguments
1397 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1398 }
1399 else
1400 {
1401 // Get the funcref from the stack.
1402 --ectx.ec_stack.ga_len;
1403 partial = *STACK_TV_BOT(0);
1404 tv = &partial;
1405 }
1406 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1407 if (tv == &partial)
1408 clear_tv(&partial);
1409 if (r == FAIL)
1410 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 }
1412 break;
1413
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001414 case ISN_PCALL_END:
1415 // PCALL finished, arguments have been consumed and replaced by
1416 // the return value. Now clear the funcref from the stack,
1417 // and move the return value in its place.
1418 --ectx.ec_stack.ga_len;
1419 clear_tv(STACK_TV_BOT(-1));
1420 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1421 break;
1422
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 // call a user defined function or funcref/partial
1424 case ISN_UCALL:
1425 {
1426 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1427
1428 SOURCING_LNUM = iptr->isn_lnum;
1429 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001430 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 goto failed;
1432 }
1433 break;
1434
1435 // return from a :def function call
1436 case ISN_RETURN:
1437 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001438 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001439 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001440
1441 if (trystack->ga_len > 0)
1442 trycmd = ((trycmd_T *)trystack->ga_data)
1443 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001444 if (trycmd != NULL
1445 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 && trycmd->tcd_finally_idx != 0)
1447 {
1448 // jump to ":finally"
1449 ectx.ec_iidx = trycmd->tcd_finally_idx;
1450 trycmd->tcd_return = TRUE;
1451 }
1452 else
1453 {
1454 // Restore previous function. If the frame pointer
1455 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001456 if (ectx.ec_frame_idx == initial_frame_idx)
1457 {
1458 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1459 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001461 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001462
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001463 if (func_return(&ectx) == FAIL)
1464 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001465 }
1466 }
1467 break;
1468
1469 // push a function reference to a compiled function
1470 case ISN_FUNCREF:
1471 {
1472 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001473 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474
1475 pt = ALLOC_CLEAR_ONE(partial_T);
1476 if (pt == NULL)
1477 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001478 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001479 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001480 vim_free(pt);
1481 goto failed;
1482 }
1483 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1484 + iptr->isn_arg.funcref.fr_func;
1485 pt->pt_func = pt_dfunc->df_ufunc;
1486 pt->pt_refcount = 1;
1487 ++pt_dfunc->df_ufunc->uf_refcount;
1488
1489 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1490 {
1491 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1492 + ectx.ec_dfunc_idx;
1493
1494 // The closure needs to find arguments and local
1495 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001496 pt->pt_ectx_stack = &ectx.ec_stack;
1497 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001498
1499 // If this function returns and the closure is still
1500 // used, we need to make a copy of the context
1501 // (arguments and local variables). Store a reference
1502 // to the partial so we can handle that.
1503 ++pt->pt_refcount;
1504 tv = STACK_TV_VAR(dfunc->df_varcount
1505 + iptr->isn_arg.funcref.fr_var_idx);
1506 if (tv->v_type == VAR_PARTIAL)
1507 {
1508 // TODO: use a garray_T on ectx.
1509 emsg("Multiple closures not supported yet");
1510 goto failed;
1511 }
1512 tv->v_type = VAR_PARTIAL;
1513 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001514 }
1515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001516 tv = STACK_TV_BOT(0);
1517 ++ectx.ec_stack.ga_len;
1518 tv->vval.v_partial = pt;
1519 tv->v_type = VAR_PARTIAL;
1520 }
1521 break;
1522
1523 // jump if a condition is met
1524 case ISN_JUMP:
1525 {
1526 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1527 int jump = TRUE;
1528
1529 if (when != JUMP_ALWAYS)
1530 {
1531 tv = STACK_TV_BOT(-1);
1532 jump = tv2bool(tv);
1533 if (when == JUMP_IF_FALSE
1534 || when == JUMP_AND_KEEP_IF_FALSE)
1535 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001536 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 {
1538 // drop the value from the stack
1539 clear_tv(tv);
1540 --ectx.ec_stack.ga_len;
1541 }
1542 }
1543 if (jump)
1544 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1545 }
1546 break;
1547
1548 // top of a for loop
1549 case ISN_FOR:
1550 {
1551 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1552 typval_T *idxtv =
1553 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1554
1555 // push the next item from the list
1556 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1557 goto failed;
1558 if (++idxtv->vval.v_number >= list->lv_len)
1559 // past the end of the list, jump to "endfor"
1560 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1561 else if (list->lv_first == &range_list_item)
1562 {
1563 // non-materialized range() list
1564 tv = STACK_TV_BOT(0);
1565 tv->v_type = VAR_NUMBER;
1566 tv->vval.v_number = list_find_nr(
1567 list, idxtv->vval.v_number, NULL);
1568 ++ectx.ec_stack.ga_len;
1569 }
1570 else
1571 {
1572 listitem_T *li = list_find(list, idxtv->vval.v_number);
1573
1574 if (li == NULL)
1575 goto failed;
1576 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1577 ++ectx.ec_stack.ga_len;
1578 }
1579 }
1580 break;
1581
1582 // start of ":try" block
1583 case ISN_TRY:
1584 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001585 trycmd_T *trycmd = NULL;
1586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1588 goto failed;
1589 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1590 + ectx.ec_trystack.ga_len;
1591 ++ectx.ec_trystack.ga_len;
1592 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001593 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1595 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001596 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 }
1598 break;
1599
1600 case ISN_PUSHEXC:
1601 if (current_exception == NULL)
1602 {
1603 iemsg("Evaluating catch while current_exception is NULL");
1604 goto failed;
1605 }
1606 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1607 goto failed;
1608 tv = STACK_TV_BOT(0);
1609 ++ectx.ec_stack.ga_len;
1610 tv->v_type = VAR_STRING;
1611 tv->vval.v_string = vim_strsave(
1612 (char_u *)current_exception->value);
1613 break;
1614
1615 case ISN_CATCH:
1616 {
1617 garray_T *trystack = &ectx.ec_trystack;
1618
1619 if (trystack->ga_len > 0)
1620 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001621 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 + trystack->ga_len - 1;
1623 trycmd->tcd_caught = TRUE;
1624 }
1625 did_emsg = got_int = did_throw = FALSE;
1626 catch_exception(current_exception);
1627 }
1628 break;
1629
1630 // end of ":try" block
1631 case ISN_ENDTRY:
1632 {
1633 garray_T *trystack = &ectx.ec_trystack;
1634
1635 if (trystack->ga_len > 0)
1636 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001637 trycmd_T *trycmd = NULL;
1638
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 --trystack->ga_len;
1640 --trylevel;
1641 trycmd = ((trycmd_T *)trystack->ga_data)
1642 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001643 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 {
1645 // discard the exception
1646 if (caught_stack == current_exception)
1647 caught_stack = caught_stack->caught;
1648 discard_current_exception();
1649 }
1650
1651 if (trycmd->tcd_return)
1652 {
1653 // Restore previous function. If the frame pointer
1654 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001655 if (ectx.ec_frame_idx == initial_frame_idx)
1656 {
1657 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1658 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001660 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001661
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001662 if (func_return(&ectx) == FAIL)
1663 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 }
1665 }
1666 }
1667 break;
1668
1669 case ISN_THROW:
1670 --ectx.ec_stack.ga_len;
1671 tv = STACK_TV_BOT(0);
1672 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1673 {
1674 vim_free(tv->vval.v_string);
1675 goto failed;
1676 }
1677 did_throw = TRUE;
1678 break;
1679
1680 // compare with special values
1681 case ISN_COMPAREBOOL:
1682 case ISN_COMPARESPECIAL:
1683 {
1684 typval_T *tv1 = STACK_TV_BOT(-2);
1685 typval_T *tv2 = STACK_TV_BOT(-1);
1686 varnumber_T arg1 = tv1->vval.v_number;
1687 varnumber_T arg2 = tv2->vval.v_number;
1688 int res;
1689
1690 switch (iptr->isn_arg.op.op_type)
1691 {
1692 case EXPR_EQUAL: res = arg1 == arg2; break;
1693 case EXPR_NEQUAL: res = arg1 != arg2; break;
1694 default: res = 0; break;
1695 }
1696
1697 --ectx.ec_stack.ga_len;
1698 tv1->v_type = VAR_BOOL;
1699 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1700 }
1701 break;
1702
1703 // Operation with two number arguments
1704 case ISN_OPNR:
1705 case ISN_COMPARENR:
1706 {
1707 typval_T *tv1 = STACK_TV_BOT(-2);
1708 typval_T *tv2 = STACK_TV_BOT(-1);
1709 varnumber_T arg1 = tv1->vval.v_number;
1710 varnumber_T arg2 = tv2->vval.v_number;
1711 varnumber_T res;
1712
1713 switch (iptr->isn_arg.op.op_type)
1714 {
1715 case EXPR_MULT: res = arg1 * arg2; break;
1716 case EXPR_DIV: res = arg1 / arg2; break;
1717 case EXPR_REM: res = arg1 % arg2; break;
1718 case EXPR_SUB: res = arg1 - arg2; break;
1719 case EXPR_ADD: res = arg1 + arg2; break;
1720
1721 case EXPR_EQUAL: res = arg1 == arg2; break;
1722 case EXPR_NEQUAL: res = arg1 != arg2; break;
1723 case EXPR_GREATER: res = arg1 > arg2; break;
1724 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1725 case EXPR_SMALLER: res = arg1 < arg2; break;
1726 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1727 default: res = 0; break;
1728 }
1729
1730 --ectx.ec_stack.ga_len;
1731 if (iptr->isn_type == ISN_COMPARENR)
1732 {
1733 tv1->v_type = VAR_BOOL;
1734 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1735 }
1736 else
1737 tv1->vval.v_number = res;
1738 }
1739 break;
1740
1741 // Computation with two float arguments
1742 case ISN_OPFLOAT:
1743 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001744#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 {
1746 typval_T *tv1 = STACK_TV_BOT(-2);
1747 typval_T *tv2 = STACK_TV_BOT(-1);
1748 float_T arg1 = tv1->vval.v_float;
1749 float_T arg2 = tv2->vval.v_float;
1750 float_T res = 0;
1751 int cmp = FALSE;
1752
1753 switch (iptr->isn_arg.op.op_type)
1754 {
1755 case EXPR_MULT: res = arg1 * arg2; break;
1756 case EXPR_DIV: res = arg1 / arg2; break;
1757 case EXPR_SUB: res = arg1 - arg2; break;
1758 case EXPR_ADD: res = arg1 + arg2; break;
1759
1760 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1761 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1762 case EXPR_GREATER: cmp = arg1 > arg2; break;
1763 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1764 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1765 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1766 default: cmp = 0; break;
1767 }
1768 --ectx.ec_stack.ga_len;
1769 if (iptr->isn_type == ISN_COMPAREFLOAT)
1770 {
1771 tv1->v_type = VAR_BOOL;
1772 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1773 }
1774 else
1775 tv1->vval.v_float = res;
1776 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001777#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 break;
1779
1780 case ISN_COMPARELIST:
1781 {
1782 typval_T *tv1 = STACK_TV_BOT(-2);
1783 typval_T *tv2 = STACK_TV_BOT(-1);
1784 list_T *arg1 = tv1->vval.v_list;
1785 list_T *arg2 = tv2->vval.v_list;
1786 int cmp = FALSE;
1787 int ic = iptr->isn_arg.op.op_ic;
1788
1789 switch (iptr->isn_arg.op.op_type)
1790 {
1791 case EXPR_EQUAL: cmp =
1792 list_equal(arg1, arg2, ic, FALSE); break;
1793 case EXPR_NEQUAL: cmp =
1794 !list_equal(arg1, arg2, ic, FALSE); break;
1795 case EXPR_IS: cmp = arg1 == arg2; break;
1796 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1797 default: cmp = 0; break;
1798 }
1799 --ectx.ec_stack.ga_len;
1800 clear_tv(tv1);
1801 clear_tv(tv2);
1802 tv1->v_type = VAR_BOOL;
1803 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1804 }
1805 break;
1806
1807 case ISN_COMPAREBLOB:
1808 {
1809 typval_T *tv1 = STACK_TV_BOT(-2);
1810 typval_T *tv2 = STACK_TV_BOT(-1);
1811 blob_T *arg1 = tv1->vval.v_blob;
1812 blob_T *arg2 = tv2->vval.v_blob;
1813 int cmp = FALSE;
1814
1815 switch (iptr->isn_arg.op.op_type)
1816 {
1817 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1818 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1819 case EXPR_IS: cmp = arg1 == arg2; break;
1820 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1821 default: cmp = 0; break;
1822 }
1823 --ectx.ec_stack.ga_len;
1824 clear_tv(tv1);
1825 clear_tv(tv2);
1826 tv1->v_type = VAR_BOOL;
1827 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1828 }
1829 break;
1830
1831 // TODO: handle separately
1832 case ISN_COMPARESTRING:
1833 case ISN_COMPAREDICT:
1834 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 case ISN_COMPAREANY:
1836 {
1837 typval_T *tv1 = STACK_TV_BOT(-2);
1838 typval_T *tv2 = STACK_TV_BOT(-1);
1839 exptype_T exptype = iptr->isn_arg.op.op_type;
1840 int ic = iptr->isn_arg.op.op_ic;
1841
1842 typval_compare(tv1, tv2, exptype, ic);
1843 clear_tv(tv2);
1844 tv1->v_type = VAR_BOOL;
1845 tv1->vval.v_number = tv1->vval.v_number
1846 ? VVAL_TRUE : VVAL_FALSE;
1847 --ectx.ec_stack.ga_len;
1848 }
1849 break;
1850
1851 case ISN_ADDLIST:
1852 case ISN_ADDBLOB:
1853 {
1854 typval_T *tv1 = STACK_TV_BOT(-2);
1855 typval_T *tv2 = STACK_TV_BOT(-1);
1856
1857 if (iptr->isn_type == ISN_ADDLIST)
1858 eval_addlist(tv1, tv2);
1859 else
1860 eval_addblob(tv1, tv2);
1861 clear_tv(tv2);
1862 --ectx.ec_stack.ga_len;
1863 }
1864 break;
1865
1866 // Computation with two arguments of unknown type
1867 case ISN_OPANY:
1868 {
1869 typval_T *tv1 = STACK_TV_BOT(-2);
1870 typval_T *tv2 = STACK_TV_BOT(-1);
1871 varnumber_T n1, n2;
1872#ifdef FEAT_FLOAT
1873 float_T f1 = 0, f2 = 0;
1874#endif
1875 int error = FALSE;
1876
1877 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1878 {
1879 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1880 {
1881 eval_addlist(tv1, tv2);
1882 clear_tv(tv2);
1883 --ectx.ec_stack.ga_len;
1884 break;
1885 }
1886 else if (tv1->v_type == VAR_BLOB
1887 && tv2->v_type == VAR_BLOB)
1888 {
1889 eval_addblob(tv1, tv2);
1890 clear_tv(tv2);
1891 --ectx.ec_stack.ga_len;
1892 break;
1893 }
1894 }
1895#ifdef FEAT_FLOAT
1896 if (tv1->v_type == VAR_FLOAT)
1897 {
1898 f1 = tv1->vval.v_float;
1899 n1 = 0;
1900 }
1901 else
1902#endif
1903 {
1904 n1 = tv_get_number_chk(tv1, &error);
1905 if (error)
1906 goto failed;
1907#ifdef FEAT_FLOAT
1908 if (tv2->v_type == VAR_FLOAT)
1909 f1 = n1;
1910#endif
1911 }
1912#ifdef FEAT_FLOAT
1913 if (tv2->v_type == VAR_FLOAT)
1914 {
1915 f2 = tv2->vval.v_float;
1916 n2 = 0;
1917 }
1918 else
1919#endif
1920 {
1921 n2 = tv_get_number_chk(tv2, &error);
1922 if (error)
1923 goto failed;
1924#ifdef FEAT_FLOAT
1925 if (tv1->v_type == VAR_FLOAT)
1926 f2 = n2;
1927#endif
1928 }
1929#ifdef FEAT_FLOAT
1930 // if there is a float on either side the result is a float
1931 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1932 {
1933 switch (iptr->isn_arg.op.op_type)
1934 {
1935 case EXPR_MULT: f1 = f1 * f2; break;
1936 case EXPR_DIV: f1 = f1 / f2; break;
1937 case EXPR_SUB: f1 = f1 - f2; break;
1938 case EXPR_ADD: f1 = f1 + f2; break;
1939 default: emsg(_(e_modulus)); goto failed;
1940 }
1941 clear_tv(tv1);
1942 clear_tv(tv2);
1943 tv1->v_type = VAR_FLOAT;
1944 tv1->vval.v_float = f1;
1945 --ectx.ec_stack.ga_len;
1946 }
1947 else
1948#endif
1949 {
1950 switch (iptr->isn_arg.op.op_type)
1951 {
1952 case EXPR_MULT: n1 = n1 * n2; break;
1953 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1954 case EXPR_SUB: n1 = n1 - n2; break;
1955 case EXPR_ADD: n1 = n1 + n2; break;
1956 default: n1 = num_modulus(n1, n2); break;
1957 }
1958 clear_tv(tv1);
1959 clear_tv(tv2);
1960 tv1->v_type = VAR_NUMBER;
1961 tv1->vval.v_number = n1;
1962 --ectx.ec_stack.ga_len;
1963 }
1964 }
1965 break;
1966
1967 case ISN_CONCAT:
1968 {
1969 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1970 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1971 char_u *res;
1972
1973 res = concat_str(str1, str2);
1974 clear_tv(STACK_TV_BOT(-2));
1975 clear_tv(STACK_TV_BOT(-1));
1976 --ectx.ec_stack.ga_len;
1977 STACK_TV_BOT(-1)->vval.v_string = res;
1978 }
1979 break;
1980
1981 case ISN_INDEX:
1982 {
1983 list_T *list;
1984 varnumber_T n;
1985 listitem_T *li;
1986
1987 // list index: list is at stack-2, index at stack-1
1988 tv = STACK_TV_BOT(-2);
1989 if (tv->v_type != VAR_LIST)
1990 {
1991 emsg(_(e_listreq));
1992 goto failed;
1993 }
1994 list = tv->vval.v_list;
1995
1996 tv = STACK_TV_BOT(-1);
1997 if (tv->v_type != VAR_NUMBER)
1998 {
1999 emsg(_(e_number_exp));
2000 goto failed;
2001 }
2002 n = tv->vval.v_number;
2003 clear_tv(tv);
2004 if ((li = list_find(list, n)) == NULL)
2005 {
2006 semsg(_(e_listidx), n);
2007 goto failed;
2008 }
2009 --ectx.ec_stack.ga_len;
2010 clear_tv(STACK_TV_BOT(-1));
2011 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2012 }
2013 break;
2014
2015 // dict member with string key
2016 case ISN_MEMBER:
2017 {
2018 dict_T *dict;
2019 dictitem_T *di;
2020
2021 tv = STACK_TV_BOT(-1);
2022 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2023 {
2024 emsg(_(e_dictreq));
2025 goto failed;
2026 }
2027 dict = tv->vval.v_dict;
2028
2029 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2030 == NULL)
2031 {
2032 semsg(_(e_dictkey), iptr->isn_arg.string);
2033 goto failed;
2034 }
2035 clear_tv(tv);
2036 copy_tv(&di->di_tv, tv);
2037 }
2038 break;
2039
2040 case ISN_NEGATENR:
2041 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002042 if (tv->v_type != VAR_NUMBER
2043#ifdef FEAT_FLOAT
2044 && tv->v_type != VAR_FLOAT
2045#endif
2046 )
2047 {
2048 emsg(_(e_number_exp));
2049 goto failed;
2050 }
2051#ifdef FEAT_FLOAT
2052 if (tv->v_type == VAR_FLOAT)
2053 tv->vval.v_float = -tv->vval.v_float;
2054 else
2055#endif
2056 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 break;
2058
2059 case ISN_CHECKNR:
2060 {
2061 int error = FALSE;
2062
2063 tv = STACK_TV_BOT(-1);
2064 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002066 (void)tv_get_number_chk(tv, &error);
2067 if (error)
2068 goto failed;
2069 }
2070 break;
2071
2072 case ISN_CHECKTYPE:
2073 {
2074 checktype_T *ct = &iptr->isn_arg.type;
2075
2076 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002077 // TODO: better type comparison
2078 if (tv->v_type != ct->ct_type
2079 && !((tv->v_type == VAR_PARTIAL
2080 && ct->ct_type == VAR_FUNC)
2081 || (tv->v_type == VAR_FUNC
2082 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 {
2084 semsg(_("E1029: Expected %s but got %s"),
2085 vartype_name(ct->ct_type),
2086 vartype_name(tv->v_type));
2087 goto failed;
2088 }
2089 }
2090 break;
2091
2092 case ISN_2BOOL:
2093 {
2094 int n;
2095
2096 tv = STACK_TV_BOT(-1);
2097 n = tv2bool(tv);
2098 if (iptr->isn_arg.number) // invert
2099 n = !n;
2100 clear_tv(tv);
2101 tv->v_type = VAR_BOOL;
2102 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2103 }
2104 break;
2105
2106 case ISN_2STRING:
2107 {
2108 char_u *str;
2109
2110 tv = STACK_TV_BOT(iptr->isn_arg.number);
2111 if (tv->v_type != VAR_STRING)
2112 {
2113 str = typval_tostring(tv);
2114 clear_tv(tv);
2115 tv->v_type = VAR_STRING;
2116 tv->vval.v_string = str;
2117 }
2118 }
2119 break;
2120
2121 case ISN_DROP:
2122 --ectx.ec_stack.ga_len;
2123 clear_tv(STACK_TV_BOT(0));
2124 break;
2125 }
2126 }
2127
2128done:
2129 // function finished, get result from the stack.
2130 tv = STACK_TV_BOT(-1);
2131 *rettv = *tv;
2132 tv->v_type = VAR_UNKNOWN;
2133 ret = OK;
2134
2135failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002136 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002137 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002138 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002139failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002140 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002141
2142 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2144 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002145
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002147 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 return ret;
2149}
2150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151/*
2152 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002153 * We don't really need this at runtime, but we do have tests that require it,
2154 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 */
2156 void
2157ex_disassemble(exarg_T *eap)
2158{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002159 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002160 char_u *fname;
2161 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 dfunc_T *dfunc;
2163 isn_T *instr;
2164 int current;
2165 int line_idx = 0;
2166 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002167 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002169 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002170 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002171 if (fname == NULL)
2172 {
2173 semsg(_(e_invarg2), eap->arg);
2174 return;
2175 }
2176
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002177 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002178 if (ufunc == NULL)
2179 {
2180 char_u *p = untrans_function_name(fname);
2181
2182 if (p != NULL)
2183 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002184 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002185 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002186 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 if (ufunc == NULL)
2188 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002189 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 return;
2191 }
2192 if (ufunc->uf_dfunc_idx < 0)
2193 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002194 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 return;
2196 }
2197 if (ufunc->uf_name_exp != NULL)
2198 msg((char *)ufunc->uf_name_exp);
2199 else
2200 msg((char *)ufunc->uf_name);
2201
2202 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2203 instr = dfunc->df_instr;
2204 for (current = 0; current < dfunc->df_instr_count; ++current)
2205 {
2206 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002207 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208
2209 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2210 {
2211 if (current > prev_current)
2212 {
2213 msg_puts("\n\n");
2214 prev_current = current;
2215 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002216 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2217 if (line != NULL)
2218 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 }
2220
2221 switch (iptr->isn_type)
2222 {
2223 case ISN_EXEC:
2224 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2225 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002226 case ISN_EXECCONCAT:
2227 smsg("%4d EXECCONCAT %lld", current,
2228 (long long)iptr->isn_arg.number);
2229 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 case ISN_ECHO:
2231 {
2232 echo_T *echo = &iptr->isn_arg.echo;
2233
2234 smsg("%4d %s %d", current,
2235 echo->echo_with_white ? "ECHO" : "ECHON",
2236 echo->echo_count);
2237 }
2238 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002239 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002240 smsg("%4d EXECUTE %lld", current,
2241 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002242 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002243 case ISN_ECHOMSG:
2244 smsg("%4d ECHOMSG %lld", current,
2245 (long long)(iptr->isn_arg.number));
2246 break;
2247 case ISN_ECHOERR:
2248 smsg("%4d ECHOERR %lld", current,
2249 (long long)(iptr->isn_arg.number));
2250 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002252 case ISN_LOADOUTER:
2253 {
2254 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2255
2256 if (iptr->isn_arg.number < 0)
2257 smsg("%4d LOAD%s arg[%lld]", current, add,
2258 (long long)(iptr->isn_arg.number
2259 + STACK_FRAME_SIZE));
2260 else
2261 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002262 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002263 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 break;
2265 case ISN_LOADV:
2266 smsg("%4d LOADV v:%s", current,
2267 get_vim_var_name(iptr->isn_arg.number));
2268 break;
2269 case ISN_LOADSCRIPT:
2270 {
2271 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002272 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2274 + iptr->isn_arg.script.script_idx;
2275
2276 smsg("%4d LOADSCRIPT %s from %s", current,
2277 sv->sv_name, si->sn_name);
2278 }
2279 break;
2280 case ISN_LOADS:
2281 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002282 scriptitem_T *si = SCRIPT_ITEM(
2283 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284
2285 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002286 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 }
2288 break;
2289 case ISN_LOADG:
2290 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2291 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002292 case ISN_LOADB:
2293 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2294 break;
2295 case ISN_LOADW:
2296 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2297 break;
2298 case ISN_LOADT:
2299 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2300 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 case ISN_LOADOPT:
2302 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2303 break;
2304 case ISN_LOADENV:
2305 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2306 break;
2307 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002308 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 break;
2310
2311 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002312 case ISN_STOREOUTER:
2313 {
2314 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2315
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002316 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002317 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002318 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002319 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002320 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002321 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002322 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002323 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002324 case ISN_STOREV:
2325 smsg("%4d STOREV v:%s", current,
2326 get_vim_var_name(iptr->isn_arg.number));
2327 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002329 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2330 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002331 case ISN_STOREB:
2332 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2333 break;
2334 case ISN_STOREW:
2335 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2336 break;
2337 case ISN_STORET:
2338 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2339 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002340 case ISN_STORES:
2341 {
2342 scriptitem_T *si = SCRIPT_ITEM(
2343 iptr->isn_arg.loadstore.ls_sid);
2344
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002345 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002346 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002347 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 break;
2349 case ISN_STORESCRIPT:
2350 {
2351 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002352 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002353 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2354 + iptr->isn_arg.script.script_idx;
2355
2356 smsg("%4d STORESCRIPT %s in %s", current,
2357 sv->sv_name, si->sn_name);
2358 }
2359 break;
2360 case ISN_STOREOPT:
2361 smsg("%4d STOREOPT &%s", current,
2362 iptr->isn_arg.storeopt.so_name);
2363 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002364 case ISN_STOREENV:
2365 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2366 break;
2367 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002368 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002369 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 case ISN_STORENR:
2371 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002372 iptr->isn_arg.storenr.stnr_val,
2373 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 break;
2375
2376 // constants
2377 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002378 smsg("%4d PUSHNR %lld", current,
2379 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 break;
2381 case ISN_PUSHBOOL:
2382 case ISN_PUSHSPEC:
2383 smsg("%4d PUSH %s", current,
2384 get_var_special_name(iptr->isn_arg.number));
2385 break;
2386 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002387#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002389#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 break;
2391 case ISN_PUSHS:
2392 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2393 break;
2394 case ISN_PUSHBLOB:
2395 {
2396 char_u *r;
2397 char_u numbuf[NUMBUFLEN];
2398 char_u *tofree;
2399
2400 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002401 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 vim_free(tofree);
2403 }
2404 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002405 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002406 {
2407 char *name = (char *)iptr->isn_arg.string;
2408
2409 smsg("%4d PUSHFUNC \"%s\"", current,
2410 name == NULL ? "[none]" : name);
2411 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002412 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002413 case ISN_PUSHCHANNEL:
2414#ifdef FEAT_JOB_CHANNEL
2415 {
2416 channel_T *channel = iptr->isn_arg.channel;
2417
2418 smsg("%4d PUSHCHANNEL %d", current,
2419 channel == NULL ? 0 : channel->ch_id);
2420 }
2421#endif
2422 break;
2423 case ISN_PUSHJOB:
2424#ifdef FEAT_JOB_CHANNEL
2425 {
2426 typval_T tv;
2427 char_u *name;
2428
2429 tv.v_type = VAR_JOB;
2430 tv.vval.v_job = iptr->isn_arg.job;
2431 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002432 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002433 }
2434#endif
2435 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 case ISN_PUSHEXC:
2437 smsg("%4d PUSH v:exception", current);
2438 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002439 case ISN_UNLET:
2440 smsg("%4d UNLET%s %s", current,
2441 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2442 iptr->isn_arg.unlet.ul_name);
2443 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002444 case ISN_UNLETENV:
2445 smsg("%4d UNLETENV%s $%s", current,
2446 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2447 iptr->isn_arg.unlet.ul_name);
2448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002450 smsg("%4d NEWLIST size %lld", current,
2451 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452 break;
2453 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002454 smsg("%4d NEWDICT size %lld", current,
2455 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 break;
2457
2458 // function call
2459 case ISN_BCALL:
2460 {
2461 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2462
2463 smsg("%4d BCALL %s(argc %d)", current,
2464 internal_func_name(cbfunc->cbf_idx),
2465 cbfunc->cbf_argcount);
2466 }
2467 break;
2468 case ISN_DCALL:
2469 {
2470 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2471 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2472 + cdfunc->cdf_idx;
2473
2474 smsg("%4d DCALL %s(argc %d)", current,
2475 df->df_ufunc->uf_name_exp != NULL
2476 ? df->df_ufunc->uf_name_exp
2477 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2478 }
2479 break;
2480 case ISN_UCALL:
2481 {
2482 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2483
2484 smsg("%4d UCALL %s(argc %d)", current,
2485 cufunc->cuf_name, cufunc->cuf_argcount);
2486 }
2487 break;
2488 case ISN_PCALL:
2489 {
2490 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2491
2492 smsg("%4d PCALL%s (argc %d)", current,
2493 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2494 }
2495 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002496 case ISN_PCALL_END:
2497 smsg("%4d PCALL end", current);
2498 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 case ISN_RETURN:
2500 smsg("%4d RETURN", current);
2501 break;
2502 case ISN_FUNCREF:
2503 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002504 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002506 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002508 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002509 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 }
2511 break;
2512
2513 case ISN_JUMP:
2514 {
2515 char *when = "?";
2516
2517 switch (iptr->isn_arg.jump.jump_when)
2518 {
2519 case JUMP_ALWAYS:
2520 when = "JUMP";
2521 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 case JUMP_AND_KEEP_IF_TRUE:
2523 when = "JUMP_AND_KEEP_IF_TRUE";
2524 break;
2525 case JUMP_IF_FALSE:
2526 when = "JUMP_IF_FALSE";
2527 break;
2528 case JUMP_AND_KEEP_IF_FALSE:
2529 when = "JUMP_AND_KEEP_IF_FALSE";
2530 break;
2531 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002532 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533 iptr->isn_arg.jump.jump_where);
2534 }
2535 break;
2536
2537 case ISN_FOR:
2538 {
2539 forloop_T *forloop = &iptr->isn_arg.forloop;
2540
2541 smsg("%4d FOR $%d -> %d", current,
2542 forloop->for_idx, forloop->for_end);
2543 }
2544 break;
2545
2546 case ISN_TRY:
2547 {
2548 try_T *try = &iptr->isn_arg.try;
2549
2550 smsg("%4d TRY catch -> %d, finally -> %d", current,
2551 try->try_catch, try->try_finally);
2552 }
2553 break;
2554 case ISN_CATCH:
2555 // TODO
2556 smsg("%4d CATCH", current);
2557 break;
2558 case ISN_ENDTRY:
2559 smsg("%4d ENDTRY", current);
2560 break;
2561 case ISN_THROW:
2562 smsg("%4d THROW", current);
2563 break;
2564
2565 // expression operations on number
2566 case ISN_OPNR:
2567 case ISN_OPFLOAT:
2568 case ISN_OPANY:
2569 {
2570 char *what;
2571 char *ins;
2572
2573 switch (iptr->isn_arg.op.op_type)
2574 {
2575 case EXPR_MULT: what = "*"; break;
2576 case EXPR_DIV: what = "/"; break;
2577 case EXPR_REM: what = "%"; break;
2578 case EXPR_SUB: what = "-"; break;
2579 case EXPR_ADD: what = "+"; break;
2580 default: what = "???"; break;
2581 }
2582 switch (iptr->isn_type)
2583 {
2584 case ISN_OPNR: ins = "OPNR"; break;
2585 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2586 case ISN_OPANY: ins = "OPANY"; break;
2587 default: ins = "???"; break;
2588 }
2589 smsg("%4d %s %s", current, ins, what);
2590 }
2591 break;
2592
2593 case ISN_COMPAREBOOL:
2594 case ISN_COMPARESPECIAL:
2595 case ISN_COMPARENR:
2596 case ISN_COMPAREFLOAT:
2597 case ISN_COMPARESTRING:
2598 case ISN_COMPAREBLOB:
2599 case ISN_COMPARELIST:
2600 case ISN_COMPAREDICT:
2601 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 case ISN_COMPAREANY:
2603 {
2604 char *p;
2605 char buf[10];
2606 char *type;
2607
2608 switch (iptr->isn_arg.op.op_type)
2609 {
2610 case EXPR_EQUAL: p = "=="; break;
2611 case EXPR_NEQUAL: p = "!="; break;
2612 case EXPR_GREATER: p = ">"; break;
2613 case EXPR_GEQUAL: p = ">="; break;
2614 case EXPR_SMALLER: p = "<"; break;
2615 case EXPR_SEQUAL: p = "<="; break;
2616 case EXPR_MATCH: p = "=~"; break;
2617 case EXPR_IS: p = "is"; break;
2618 case EXPR_ISNOT: p = "isnot"; break;
2619 case EXPR_NOMATCH: p = "!~"; break;
2620 default: p = "???"; break;
2621 }
2622 STRCPY(buf, p);
2623 if (iptr->isn_arg.op.op_ic == TRUE)
2624 strcat(buf, "?");
2625 switch(iptr->isn_type)
2626 {
2627 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2628 case ISN_COMPARESPECIAL:
2629 type = "COMPARESPECIAL"; break;
2630 case ISN_COMPARENR: type = "COMPARENR"; break;
2631 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2632 case ISN_COMPARESTRING:
2633 type = "COMPARESTRING"; break;
2634 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2635 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2636 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2637 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2639 default: type = "???"; break;
2640 }
2641
2642 smsg("%4d %s %s", current, type, buf);
2643 }
2644 break;
2645
2646 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2647 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2648
2649 // expression operations
2650 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2651 case ISN_INDEX: smsg("%4d INDEX", current); break;
2652 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2653 iptr->isn_arg.string); break;
2654 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2655
2656 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2657 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2658 vartype_name(iptr->isn_arg.type.ct_type),
2659 iptr->isn_arg.type.ct_off);
2660 break;
2661 case ISN_2BOOL: if (iptr->isn_arg.number)
2662 smsg("%4d INVERT (!val)", current);
2663 else
2664 smsg("%4d 2BOOL (!!val)", current);
2665 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002666 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2667 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 break;
2669
2670 case ISN_DROP: smsg("%4d DROP", current); break;
2671 }
2672 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673}
2674
2675/*
2676 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2677 * list, etc. Mostly like what JavaScript does, except that empty list and
2678 * empty dictionary are FALSE.
2679 */
2680 int
2681tv2bool(typval_T *tv)
2682{
2683 switch (tv->v_type)
2684 {
2685 case VAR_NUMBER:
2686 return tv->vval.v_number != 0;
2687 case VAR_FLOAT:
2688#ifdef FEAT_FLOAT
2689 return tv->vval.v_float != 0.0;
2690#else
2691 break;
2692#endif
2693 case VAR_PARTIAL:
2694 return tv->vval.v_partial != NULL;
2695 case VAR_FUNC:
2696 case VAR_STRING:
2697 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2698 case VAR_LIST:
2699 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2700 case VAR_DICT:
2701 return tv->vval.v_dict != NULL
2702 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2703 case VAR_BOOL:
2704 case VAR_SPECIAL:
2705 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2706 case VAR_JOB:
2707#ifdef FEAT_JOB_CHANNEL
2708 return tv->vval.v_job != NULL;
2709#else
2710 break;
2711#endif
2712 case VAR_CHANNEL:
2713#ifdef FEAT_JOB_CHANNEL
2714 return tv->vval.v_channel != NULL;
2715#else
2716 break;
2717#endif
2718 case VAR_BLOB:
2719 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2720 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002721 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722 case VAR_VOID:
2723 break;
2724 }
2725 return FALSE;
2726}
2727
2728/*
2729 * If "tv" is a string give an error and return FAIL.
2730 */
2731 int
2732check_not_string(typval_T *tv)
2733{
2734 if (tv->v_type == VAR_STRING)
2735 {
2736 emsg(_("E1030: Using a String as a Number"));
2737 clear_tv(tv);
2738 return FAIL;
2739 }
2740 return OK;
2741}
2742
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743
2744#endif // FEAT_EVAL