blob: 27ad04b446a2ad2f85e9025a7bb0b31e454da766 [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
273 // A Reference in a local variables doesn't count, its get
274 // 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);
326 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
327 tv->v_type = VAR_UNKNOWN;
328 }
329
330 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
331 {
332 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
333 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200334 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200335 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200336 partial_T *partial = tv->vval.v_partial;
337
338 if (partial->pt_refcount > 1)
339 {
340 ++funcstack->fs_refcount;
341 partial->pt_funcstack = funcstack;
342 partial->pt_ectx_stack = &funcstack->fs_ga;
343 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
344 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345 }
346 }
347 }
348
349 return OK;
350}
351
352/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 * Return from the current function.
354 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200355 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356func_return(ectx_T *ectx)
357{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200359 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
360 + ectx->ec_dfunc_idx;
361 int argcount = ufunc_argcount(dfunc->df_ufunc);
362 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363
364 // execution context goes one level up
365 estack_pop();
366
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200367 if (handle_closure_in_use(ectx, TRUE) == FAIL)
368 return FAIL;
369
370 // Clear the arguments.
371 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
372 clear_tv(STACK_TV(idx));
373
374 // Clear local variables and temp values, but not the return value.
375 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100376 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100377 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100378
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100379 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200380 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
381 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
382 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100383 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
384 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100385
386 // Reset the stack to the position before the call, move the return value
387 // to the top of the stack.
388 idx = ectx->ec_stack.ga_len - 1;
389 ectx->ec_stack.ga_len = top + 1;
390 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200391
392 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393}
394
395#undef STACK_TV
396
397/*
398 * Prepare arguments and rettv for calling a builtin or user function.
399 */
400 static int
401call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
402{
403 int idx;
404 typval_T *tv;
405
406 // Move arguments from bottom of the stack to argvars[] and add terminator.
407 for (idx = 0; idx < argcount; ++idx)
408 argvars[idx] = *STACK_TV_BOT(idx - argcount);
409 argvars[argcount].v_type = VAR_UNKNOWN;
410
411 // Result replaces the arguments on the stack.
412 if (argcount > 0)
413 ectx->ec_stack.ga_len -= argcount - 1;
414 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
415 return FAIL;
416 else
417 ++ectx->ec_stack.ga_len;
418
419 // Default return value is zero.
420 tv = STACK_TV_BOT(-1);
421 tv->v_type = VAR_NUMBER;
422 tv->vval.v_number = 0;
423
424 return OK;
425}
426
427/*
428 * Call a builtin function by index.
429 */
430 static int
431call_bfunc(int func_idx, int argcount, ectx_T *ectx)
432{
433 typval_T argvars[MAX_FUNC_ARGS];
434 int idx;
Bram Moolenaar015f4262020-05-05 21:25:22 +0200435 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436
437 if (call_prepare(argcount, argvars, ectx) == FAIL)
438 return FAIL;
439
440 // Call the builtin function.
441 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
442
443 // Clear the arguments.
444 for (idx = 0; idx < argcount; ++idx)
445 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200446
447 if (called_emsg != called_emsg_before)
448 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100449 return OK;
450}
451
452/*
453 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100454 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455 */
456 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100457call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458{
459 typval_T argvars[MAX_FUNC_ARGS];
460 funcexe_T funcexe;
461 int error;
462 int idx;
463
464 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100465 {
466 // The function has been compiled, can call it quickly. For a function
467 // that was defined later: we can call it directly next time.
468 if (iptr != NULL)
469 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100470 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100471 iptr->isn_type = ISN_DCALL;
472 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
473 iptr->isn_arg.dfunc.cdf_argcount = argcount;
474 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100476 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477
478 if (call_prepare(argcount, argvars, ectx) == FAIL)
479 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200480 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 funcexe.evaluate = TRUE;
482
483 // Call the user function. Result goes in last position on the stack.
484 // TODO: add selfdict if there is one
485 error = call_user_func_check(ufunc, argcount, argvars,
486 STACK_TV_BOT(-1), &funcexe, NULL);
487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
491
492 if (error != FCERR_NONE)
493 {
494 user_func_error(error, ufunc->uf_name);
495 return FAIL;
496 }
497 return OK;
498}
499
500/*
501 * Execute a function by "name".
502 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100503 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 * Returns FAIL if not found without an error message.
505 */
506 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100507call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508{
509 ufunc_T *ufunc;
510
511 if (builtin_function(name, -1))
512 {
513 int func_idx = find_internal_func(name);
514
515 if (func_idx < 0)
516 return FAIL;
517 if (check_internal_func(func_idx, argcount) == FAIL)
518 return FAIL;
519 return call_bfunc(func_idx, argcount, ectx);
520 }
521
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200522 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100524 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 return FAIL;
527}
528
529 static int
530call_partial(typval_T *tv, int argcount, ectx_T *ectx)
531{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200532 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 int called_emsg_before = called_emsg;
534
535 if (tv->v_type == VAR_PARTIAL)
536 {
537 partial_T *pt = tv->vval.v_partial;
538
539 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200540 {
541 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
542
543 // closure may need the function context where it was defined
544 ectx->ec_outer_stack = pt->pt_ectx_stack;
545 ectx->ec_outer_frame = pt->pt_ectx_frame;
546
547 return ret;
548 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 name = pt->pt_name;
550 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200551 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200553 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 {
555 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200556 semsg(_(e_unknownfunc),
557 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 return FAIL;
559 }
560 return OK;
561}
562
563/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100564 * Store "tv" in variable "name".
565 * This is for s: and g: variables.
566 */
567 static void
568store_var(char_u *name, typval_T *tv)
569{
570 funccal_entry_T entry;
571
572 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200573 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100574 restore_funccal();
575}
576
Bram Moolenaard3aac292020-04-19 14:32:17 +0200577
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100578/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 * Execute a function by "name".
580 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100581 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100582 */
583 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100584call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585{
586 int called_emsg_before = called_emsg;
587
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100588 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589 && called_emsg == called_emsg_before)
590 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200591 dictitem_T *v;
592
593 v = find_var(name, NULL, FALSE);
594 if (v == NULL)
595 {
596 semsg(_(e_unknownfunc), name);
597 return FAIL;
598 }
599 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
600 {
601 semsg(_(e_unknownfunc), name);
602 return FAIL;
603 }
604 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605 }
606 return OK;
607}
608
609/*
610 * Call a "def" function from old Vim script.
611 * Return OK or FAIL.
612 */
613 int
614call_def_function(
615 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200616 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 typval_T *argv, // arguments
618 typval_T *rettv) // return value
619{
620 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200621 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200622 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 typval_T *tv;
624 int idx;
625 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100626 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200627 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628
629// Get pointer to item in the stack.
630#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
631
632// Get pointer to item at the bottom of the stack, -1 is the bottom.
633#undef STACK_TV_BOT
634#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
635
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200636// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200637#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 +0100638
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200639// Like STACK_TV_VAR but use the outer scope
640#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
641
Bram Moolenaara80faa82020-04-12 19:37:17 +0200642 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
644 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100645 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
647
648 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
649
650 // Put arguments on the stack.
651 for (idx = 0; idx < argc; ++idx)
652 {
653 copy_tv(&argv[idx], STACK_TV_BOT(0));
654 ++ectx.ec_stack.ga_len;
655 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200656
657 // Turn varargs into a list. Empty list if no args.
658 if (ufunc->uf_va_name != NULL)
659 {
660 int vararg_count = argc - ufunc->uf_args.ga_len;
661
662 if (vararg_count < 0)
663 vararg_count = 0;
664 else
665 argc -= vararg_count;
666 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200667 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200668 if (defcount > 0)
669 // Move varargs list to below missing default arguments.
670 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
671 --ectx.ec_stack.ga_len;
672 }
673
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100674 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200675 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100676 if (defcount > 0)
677 for (idx = 0; idx < defcount; ++idx)
678 {
679 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
680 ++ectx.ec_stack.ga_len;
681 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200682 if (ufunc->uf_va_name != NULL)
683 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684
685 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200686 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
687 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688
689 // dummy frame entries
690 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
691 {
692 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
693 ++ectx.ec_stack.ga_len;
694 }
695
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200696 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200697 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200698 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
699 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200700 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200702 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200703 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200704 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200705
706 ectx.ec_instr = dfunc->df_instr;
707 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100708
Bram Moolenaara26b9702020-04-18 19:53:28 +0200709 // Commands behave like vim9script.
710 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
711
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100712 // Decide where to start execution, handles optional arguments.
713 init_instr_idx(ufunc, argc, &ectx);
714
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 for (;;)
716 {
717 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100718
719 veryfast_breakcheck();
720 if (got_int)
721 {
722 // Turn CTRL-C into an exception.
723 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100724 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100725 goto failed;
726 did_throw = TRUE;
727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728
Bram Moolenaara26b9702020-04-18 19:53:28 +0200729 if (did_emsg && msg_list != NULL && *msg_list != NULL)
730 {
731 // Turn an error message into an exception.
732 did_emsg = FALSE;
733 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
734 goto failed;
735 did_throw = TRUE;
736 *msg_list = NULL;
737 }
738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739 if (did_throw && !ectx.ec_in_catch)
740 {
741 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100742 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 // An exception jumps to the first catch, finally, or returns from
745 // the current function.
746 if (trystack->ga_len > 0)
747 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200748 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 {
750 // jump to ":catch" or ":finally"
751 ectx.ec_in_catch = TRUE;
752 ectx.ec_iidx = trycmd->tcd_catch_idx;
753 }
754 else
755 {
756 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200757 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 {
759 // At the toplevel we are done. Push a dummy return value.
760 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
761 goto failed;
762 tv = STACK_TV_BOT(0);
763 tv->v_type = VAR_NUMBER;
764 tv->vval.v_number = 0;
765 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100766 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200767 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
768 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 goto done;
770 }
771
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200772 if (func_return(&ectx) == FAIL)
773 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774 }
775 continue;
776 }
777
778 iptr = &ectx.ec_instr[ectx.ec_iidx++];
779 switch (iptr->isn_type)
780 {
781 // execute Ex command line
782 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200783 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100784 do_cmdline_cmd(iptr->isn_arg.string);
785 break;
786
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200787 // execute Ex command from pieces on the stack
788 case ISN_EXECCONCAT:
789 {
790 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200791 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200792 int pass;
793 int i;
794 char_u *cmd = NULL;
795 char_u *str;
796
797 for (pass = 1; pass <= 2; ++pass)
798 {
799 for (i = 0; i < count; ++i)
800 {
801 tv = STACK_TV_BOT(i - count);
802 str = tv->vval.v_string;
803 if (str != NULL && *str != NUL)
804 {
805 if (pass == 2)
806 STRCPY(cmd + len, str);
807 len += STRLEN(str);
808 }
809 if (pass == 2)
810 clear_tv(tv);
811 }
812 if (pass == 1)
813 {
814 cmd = alloc(len + 1);
815 if (cmd == NULL)
816 goto failed;
817 len = 0;
818 }
819 }
820
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200821 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200822 do_cmdline_cmd(cmd);
823 vim_free(cmd);
824 }
825 break;
826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 // execute :echo {string} ...
828 case ISN_ECHO:
829 {
830 int count = iptr->isn_arg.echo.echo_count;
831 int atstart = TRUE;
832 int needclr = TRUE;
833
834 for (idx = 0; idx < count; ++idx)
835 {
836 tv = STACK_TV_BOT(idx - count);
837 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
838 &atstart, &needclr);
839 clear_tv(tv);
840 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100841 if (needclr)
842 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 ectx.ec_stack.ga_len -= count;
844 }
845 break;
846
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200847 // :execute {string} ...
848 // :echomsg {string} ...
849 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100850 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200851 case ISN_ECHOMSG:
852 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100853 {
854 int count = iptr->isn_arg.number;
855 garray_T ga;
856 char_u buf[NUMBUFLEN];
857 char_u *p;
858 int len;
859 int failed = FALSE;
860
861 ga_init2(&ga, 1, 80);
862 for (idx = 0; idx < count; ++idx)
863 {
864 tv = STACK_TV_BOT(idx - count);
865 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
866 {
867 emsg(_(e_inval_string));
868 break;
869 }
870 else
871 p = tv_get_string_buf(tv, buf);
872
873 len = (int)STRLEN(p);
874 if (ga_grow(&ga, len + 2) == FAIL)
875 failed = TRUE;
876 else
877 {
878 if (ga.ga_len > 0)
879 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
880 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
881 ga.ga_len += len;
882 }
883 clear_tv(tv);
884 }
885 ectx.ec_stack.ga_len -= count;
886
887 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200888 {
889 if (iptr->isn_type == ISN_EXECUTE)
890 do_cmdline_cmd((char_u *)ga.ga_data);
891 else
892 {
893 msg_sb_eol();
894 if (iptr->isn_type == ISN_ECHOMSG)
895 {
896 msg_attr(ga.ga_data, echo_attr);
897 out_flush();
898 }
899 else
900 {
901 int save_did_emsg = did_emsg;
902
903 SOURCING_LNUM = iptr->isn_lnum;
904 emsg(ga.ga_data);
905 if (!force_abort)
906 // We don't want to abort following
907 // commands, restore did_emsg.
908 did_emsg = save_did_emsg;
909 }
910 }
911 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100912 ga_clear(&ga);
913 }
914 break;
915
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 // load local variable or argument
917 case ISN_LOAD:
918 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
919 goto failed;
920 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
921 ++ectx.ec_stack.ga_len;
922 break;
923
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200924 // load variable or argument from outer scope
925 case ISN_LOADOUTER:
926 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
927 goto failed;
928 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
929 STACK_TV_BOT(0));
930 ++ectx.ec_stack.ga_len;
931 break;
932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 // load v: variable
934 case ISN_LOADV:
935 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
936 goto failed;
937 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
938 ++ectx.ec_stack.ga_len;
939 break;
940
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100941 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 case ISN_LOADSCRIPT:
943 {
944 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100945 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 svar_T *sv;
947
948 sv = ((svar_T *)si->sn_var_vals.ga_data)
949 + iptr->isn_arg.script.script_idx;
950 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
951 goto failed;
952 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
953 ++ectx.ec_stack.ga_len;
954 }
955 break;
956
957 // load s: variable in old script
958 case ISN_LOADS:
959 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100960 hashtab_T *ht = &SCRIPT_VARS(
961 iptr->isn_arg.loadstore.ls_sid);
962 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100964
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965 if (di == NULL)
966 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100967 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100968 goto failed;
969 }
970 else
971 {
972 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
973 goto failed;
974 copy_tv(&di->di_tv, STACK_TV_BOT(0));
975 ++ectx.ec_stack.ga_len;
976 }
977 }
978 break;
979
Bram Moolenaard3aac292020-04-19 14:32:17 +0200980 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200982 case ISN_LOADB:
983 case ISN_LOADW:
984 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200986 dictitem_T *di = NULL;
987 hashtab_T *ht = NULL;
988 char namespace;
989 switch (iptr->isn_type)
990 {
991 case ISN_LOADG:
992 ht = get_globvar_ht();
993 namespace = 'g';
994 break;
995 case ISN_LOADB:
996 ht = &curbuf->b_vars->dv_hashtab;
997 namespace = 'b';
998 break;
999 case ISN_LOADW:
1000 ht = &curwin->w_vars->dv_hashtab;
1001 namespace = 'w';
1002 break;
1003 case ISN_LOADT:
1004 ht = &curtab->tp_vars->dv_hashtab;
1005 namespace = 't';
1006 break;
1007 default: // Cannot reach here
1008 goto failed;
1009 }
1010 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 if (di == NULL)
1013 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001014 semsg(_("E121: Undefined variable: %c:%s"),
1015 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001016 goto failed;
1017 }
1018 else
1019 {
1020 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1021 goto failed;
1022 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1023 ++ectx.ec_stack.ga_len;
1024 }
1025 }
1026 break;
1027
1028 // load &option
1029 case ISN_LOADOPT:
1030 {
1031 typval_T optval;
1032 char_u *name = iptr->isn_arg.string;
1033
Bram Moolenaara8c17702020-04-01 21:17:24 +02001034 // This is not expected to fail, name is checked during
1035 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1037 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001038 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1039 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 *STACK_TV_BOT(0) = optval;
1041 ++ectx.ec_stack.ga_len;
1042 }
1043 break;
1044
1045 // load $ENV
1046 case ISN_LOADENV:
1047 {
1048 typval_T optval;
1049 char_u *name = iptr->isn_arg.string;
1050
1051 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1052 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001053 // name is always valid, checked when compiling
1054 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 *STACK_TV_BOT(0) = optval;
1056 ++ectx.ec_stack.ga_len;
1057 }
1058 break;
1059
1060 // load @register
1061 case ISN_LOADREG:
1062 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1063 goto failed;
1064 tv = STACK_TV_BOT(0);
1065 tv->v_type = VAR_STRING;
1066 tv->vval.v_string = get_reg_contents(
1067 iptr->isn_arg.number, GREG_EXPR_SRC);
1068 ++ectx.ec_stack.ga_len;
1069 break;
1070
1071 // store local variable
1072 case ISN_STORE:
1073 --ectx.ec_stack.ga_len;
1074 tv = STACK_TV_VAR(iptr->isn_arg.number);
1075 clear_tv(tv);
1076 *tv = *STACK_TV_BOT(0);
1077 break;
1078
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001079 // store s: variable in old script
1080 case ISN_STORES:
1081 {
1082 hashtab_T *ht = &SCRIPT_VARS(
1083 iptr->isn_arg.loadstore.ls_sid);
1084 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001085 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001086
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001087 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001088 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001089 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001090 else
1091 {
1092 clear_tv(&di->di_tv);
1093 di->di_tv = *STACK_TV_BOT(0);
1094 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001095 }
1096 break;
1097
1098 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 case ISN_STORESCRIPT:
1100 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001101 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 iptr->isn_arg.script.script_sid);
1103 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1104 + iptr->isn_arg.script.script_idx;
1105
1106 --ectx.ec_stack.ga_len;
1107 clear_tv(sv->sv_tv);
1108 *sv->sv_tv = *STACK_TV_BOT(0);
1109 }
1110 break;
1111
1112 // store option
1113 case ISN_STOREOPT:
1114 {
1115 long n = 0;
1116 char_u *s = NULL;
1117 char *msg;
1118
1119 --ectx.ec_stack.ga_len;
1120 tv = STACK_TV_BOT(0);
1121 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001122 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001124 if (s == NULL)
1125 s = (char_u *)"";
1126 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 else if (tv->v_type == VAR_NUMBER)
1128 n = tv->vval.v_number;
1129 else
1130 {
1131 emsg(_("E1051: Expected string or number"));
1132 goto failed;
1133 }
1134 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1135 n, s, iptr->isn_arg.storeopt.so_flags);
1136 if (msg != NULL)
1137 {
1138 emsg(_(msg));
1139 goto failed;
1140 }
1141 clear_tv(tv);
1142 }
1143 break;
1144
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001145 // store $ENV
1146 case ISN_STOREENV:
1147 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001148 tv = STACK_TV_BOT(0);
1149 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1150 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001151 break;
1152
1153 // store @r
1154 case ISN_STOREREG:
1155 {
1156 int reg = iptr->isn_arg.number;
1157
1158 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001159 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001160 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001161 tv_get_string(tv), -1, FALSE);
1162 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001163 }
1164 break;
1165
1166 // store v: variable
1167 case ISN_STOREV:
1168 --ectx.ec_stack.ga_len;
1169 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1170 == FAIL)
1171 goto failed;
1172 break;
1173
Bram Moolenaard3aac292020-04-19 14:32:17 +02001174 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001176 case ISN_STOREB:
1177 case ISN_STOREW:
1178 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 {
1180 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001181 hashtab_T *ht;
1182 switch (iptr->isn_type)
1183 {
1184 case ISN_STOREG:
1185 ht = get_globvar_ht();
1186 break;
1187 case ISN_STOREB:
1188 ht = &curbuf->b_vars->dv_hashtab;
1189 break;
1190 case ISN_STOREW:
1191 ht = &curwin->w_vars->dv_hashtab;
1192 break;
1193 case ISN_STORET:
1194 ht = &curtab->tp_vars->dv_hashtab;
1195 break;
1196 default: // Cannot reach here
1197 goto failed;
1198 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199
1200 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001202 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001203 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 else
1205 {
1206 clear_tv(&di->di_tv);
1207 di->di_tv = *STACK_TV_BOT(0);
1208 }
1209 }
1210 break;
1211
1212 // store number in local variable
1213 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001214 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 clear_tv(tv);
1216 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001217 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 break;
1219
1220 // push constant
1221 case ISN_PUSHNR:
1222 case ISN_PUSHBOOL:
1223 case ISN_PUSHSPEC:
1224 case ISN_PUSHF:
1225 case ISN_PUSHS:
1226 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001227 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001228 case ISN_PUSHCHANNEL:
1229 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1231 goto failed;
1232 tv = STACK_TV_BOT(0);
1233 ++ectx.ec_stack.ga_len;
1234 switch (iptr->isn_type)
1235 {
1236 case ISN_PUSHNR:
1237 tv->v_type = VAR_NUMBER;
1238 tv->vval.v_number = iptr->isn_arg.number;
1239 break;
1240 case ISN_PUSHBOOL:
1241 tv->v_type = VAR_BOOL;
1242 tv->vval.v_number = iptr->isn_arg.number;
1243 break;
1244 case ISN_PUSHSPEC:
1245 tv->v_type = VAR_SPECIAL;
1246 tv->vval.v_number = iptr->isn_arg.number;
1247 break;
1248#ifdef FEAT_FLOAT
1249 case ISN_PUSHF:
1250 tv->v_type = VAR_FLOAT;
1251 tv->vval.v_float = iptr->isn_arg.fnumber;
1252 break;
1253#endif
1254 case ISN_PUSHBLOB:
1255 blob_copy(iptr->isn_arg.blob, tv);
1256 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001257 case ISN_PUSHFUNC:
1258 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001259 if (iptr->isn_arg.string == NULL)
1260 tv->vval.v_string = NULL;
1261 else
1262 tv->vval.v_string =
1263 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001264 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001265 case ISN_PUSHCHANNEL:
1266#ifdef FEAT_JOB_CHANNEL
1267 tv->v_type = VAR_CHANNEL;
1268 tv->vval.v_channel = iptr->isn_arg.channel;
1269 if (tv->vval.v_channel != NULL)
1270 ++tv->vval.v_channel->ch_refcount;
1271#endif
1272 break;
1273 case ISN_PUSHJOB:
1274#ifdef FEAT_JOB_CHANNEL
1275 tv->v_type = VAR_JOB;
1276 tv->vval.v_job = iptr->isn_arg.job;
1277 if (tv->vval.v_job != NULL)
1278 ++tv->vval.v_job->jv_refcount;
1279#endif
1280 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 default:
1282 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001283 tv->vval.v_string = vim_strsave(
1284 iptr->isn_arg.string == NULL
1285 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 }
1287 break;
1288
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001289 case ISN_UNLET:
1290 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1291 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1292 goto failed;
1293 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001294 case ISN_UNLETENV:
1295 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1296 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001297
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 // create a list from items on the stack; uses a single allocation
1299 // for the list header and the items
1300 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001301 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1302 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303 break;
1304
1305 // create a dict from items on the stack
1306 case ISN_NEWDICT:
1307 {
1308 int count = iptr->isn_arg.number;
1309 dict_T *dict = dict_alloc();
1310 dictitem_T *item;
1311
1312 if (dict == NULL)
1313 goto failed;
1314 for (idx = 0; idx < count; ++idx)
1315 {
1316 // check key type is VAR_STRING
1317 tv = STACK_TV_BOT(2 * (idx - count));
1318 item = dictitem_alloc(tv->vval.v_string);
1319 clear_tv(tv);
1320 if (item == NULL)
1321 goto failed;
1322 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1323 item->di_tv.v_lock = 0;
1324 if (dict_add(dict, item) == FAIL)
1325 goto failed;
1326 }
1327
1328 if (count > 0)
1329 ectx.ec_stack.ga_len -= 2 * count - 1;
1330 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1331 goto failed;
1332 else
1333 ++ectx.ec_stack.ga_len;
1334 tv = STACK_TV_BOT(-1);
1335 tv->v_type = VAR_DICT;
1336 tv->vval.v_dict = dict;
1337 ++dict->dv_refcount;
1338 }
1339 break;
1340
1341 // call a :def function
1342 case ISN_DCALL:
1343 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1344 iptr->isn_arg.dfunc.cdf_argcount,
1345 &ectx) == FAIL)
1346 goto failed;
1347 break;
1348
1349 // call a builtin function
1350 case ISN_BCALL:
1351 SOURCING_LNUM = iptr->isn_lnum;
1352 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1353 iptr->isn_arg.bfunc.cbf_argcount,
1354 &ectx) == FAIL)
1355 goto failed;
1356 break;
1357
1358 // call a funcref or partial
1359 case ISN_PCALL:
1360 {
1361 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1362 int r;
1363 typval_T partial;
1364
1365 SOURCING_LNUM = iptr->isn_lnum;
1366 if (pfunc->cpf_top)
1367 {
1368 // funcref is above the arguments
1369 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1370 }
1371 else
1372 {
1373 // Get the funcref from the stack.
1374 --ectx.ec_stack.ga_len;
1375 partial = *STACK_TV_BOT(0);
1376 tv = &partial;
1377 }
1378 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1379 if (tv == &partial)
1380 clear_tv(&partial);
1381 if (r == FAIL)
1382 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 }
1384 break;
1385
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001386 case ISN_PCALL_END:
1387 // PCALL finished, arguments have been consumed and replaced by
1388 // the return value. Now clear the funcref from the stack,
1389 // and move the return value in its place.
1390 --ectx.ec_stack.ga_len;
1391 clear_tv(STACK_TV_BOT(-1));
1392 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1393 break;
1394
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 // call a user defined function or funcref/partial
1396 case ISN_UCALL:
1397 {
1398 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1399
1400 SOURCING_LNUM = iptr->isn_lnum;
1401 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001402 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 goto failed;
1404 }
1405 break;
1406
1407 // return from a :def function call
1408 case ISN_RETURN:
1409 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001410 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001411 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001412
1413 if (trystack->ga_len > 0)
1414 trycmd = ((trycmd_T *)trystack->ga_data)
1415 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001416 if (trycmd != NULL
1417 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 && trycmd->tcd_finally_idx != 0)
1419 {
1420 // jump to ":finally"
1421 ectx.ec_iidx = trycmd->tcd_finally_idx;
1422 trycmd->tcd_return = TRUE;
1423 }
1424 else
1425 {
1426 // Restore previous function. If the frame pointer
1427 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001428 if (ectx.ec_frame_idx == initial_frame_idx)
1429 {
1430 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1431 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001433 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001435 if (func_return(&ectx) == FAIL)
1436 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 }
1438 }
1439 break;
1440
1441 // push a function reference to a compiled function
1442 case ISN_FUNCREF:
1443 {
1444 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001445 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446
1447 pt = ALLOC_CLEAR_ONE(partial_T);
1448 if (pt == NULL)
1449 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001450 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001451 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001452 vim_free(pt);
1453 goto failed;
1454 }
1455 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1456 + iptr->isn_arg.funcref.fr_func;
1457 pt->pt_func = pt_dfunc->df_ufunc;
1458 pt->pt_refcount = 1;
1459 ++pt_dfunc->df_ufunc->uf_refcount;
1460
1461 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1462 {
1463 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1464 + ectx.ec_dfunc_idx;
1465
1466 // The closure needs to find arguments and local
1467 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001468 pt->pt_ectx_stack = &ectx.ec_stack;
1469 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001470
1471 // If this function returns and the closure is still
1472 // used, we need to make a copy of the context
1473 // (arguments and local variables). Store a reference
1474 // to the partial so we can handle that.
1475 ++pt->pt_refcount;
1476 tv = STACK_TV_VAR(dfunc->df_varcount
1477 + iptr->isn_arg.funcref.fr_var_idx);
1478 if (tv->v_type == VAR_PARTIAL)
1479 {
1480 // TODO: use a garray_T on ectx.
1481 emsg("Multiple closures not supported yet");
1482 goto failed;
1483 }
1484 tv->v_type = VAR_PARTIAL;
1485 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001486 }
1487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 tv = STACK_TV_BOT(0);
1489 ++ectx.ec_stack.ga_len;
1490 tv->vval.v_partial = pt;
1491 tv->v_type = VAR_PARTIAL;
1492 }
1493 break;
1494
1495 // jump if a condition is met
1496 case ISN_JUMP:
1497 {
1498 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1499 int jump = TRUE;
1500
1501 if (when != JUMP_ALWAYS)
1502 {
1503 tv = STACK_TV_BOT(-1);
1504 jump = tv2bool(tv);
1505 if (when == JUMP_IF_FALSE
1506 || when == JUMP_AND_KEEP_IF_FALSE)
1507 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001508 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 {
1510 // drop the value from the stack
1511 clear_tv(tv);
1512 --ectx.ec_stack.ga_len;
1513 }
1514 }
1515 if (jump)
1516 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1517 }
1518 break;
1519
1520 // top of a for loop
1521 case ISN_FOR:
1522 {
1523 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1524 typval_T *idxtv =
1525 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1526
1527 // push the next item from the list
1528 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1529 goto failed;
1530 if (++idxtv->vval.v_number >= list->lv_len)
1531 // past the end of the list, jump to "endfor"
1532 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1533 else if (list->lv_first == &range_list_item)
1534 {
1535 // non-materialized range() list
1536 tv = STACK_TV_BOT(0);
1537 tv->v_type = VAR_NUMBER;
1538 tv->vval.v_number = list_find_nr(
1539 list, idxtv->vval.v_number, NULL);
1540 ++ectx.ec_stack.ga_len;
1541 }
1542 else
1543 {
1544 listitem_T *li = list_find(list, idxtv->vval.v_number);
1545
1546 if (li == NULL)
1547 goto failed;
1548 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1549 ++ectx.ec_stack.ga_len;
1550 }
1551 }
1552 break;
1553
1554 // start of ":try" block
1555 case ISN_TRY:
1556 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001557 trycmd_T *trycmd = NULL;
1558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1560 goto failed;
1561 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1562 + ectx.ec_trystack.ga_len;
1563 ++ectx.ec_trystack.ga_len;
1564 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001565 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1567 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001568 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 }
1570 break;
1571
1572 case ISN_PUSHEXC:
1573 if (current_exception == NULL)
1574 {
1575 iemsg("Evaluating catch while current_exception is NULL");
1576 goto failed;
1577 }
1578 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1579 goto failed;
1580 tv = STACK_TV_BOT(0);
1581 ++ectx.ec_stack.ga_len;
1582 tv->v_type = VAR_STRING;
1583 tv->vval.v_string = vim_strsave(
1584 (char_u *)current_exception->value);
1585 break;
1586
1587 case ISN_CATCH:
1588 {
1589 garray_T *trystack = &ectx.ec_trystack;
1590
1591 if (trystack->ga_len > 0)
1592 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001593 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 + trystack->ga_len - 1;
1595 trycmd->tcd_caught = TRUE;
1596 }
1597 did_emsg = got_int = did_throw = FALSE;
1598 catch_exception(current_exception);
1599 }
1600 break;
1601
1602 // end of ":try" block
1603 case ISN_ENDTRY:
1604 {
1605 garray_T *trystack = &ectx.ec_trystack;
1606
1607 if (trystack->ga_len > 0)
1608 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001609 trycmd_T *trycmd = NULL;
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 --trystack->ga_len;
1612 --trylevel;
1613 trycmd = ((trycmd_T *)trystack->ga_data)
1614 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001615 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 {
1617 // discard the exception
1618 if (caught_stack == current_exception)
1619 caught_stack = caught_stack->caught;
1620 discard_current_exception();
1621 }
1622
1623 if (trycmd->tcd_return)
1624 {
1625 // Restore previous function. If the frame pointer
1626 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001627 if (ectx.ec_frame_idx == initial_frame_idx)
1628 {
1629 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1630 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001632 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001634 if (func_return(&ectx) == FAIL)
1635 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 }
1637 }
1638 }
1639 break;
1640
1641 case ISN_THROW:
1642 --ectx.ec_stack.ga_len;
1643 tv = STACK_TV_BOT(0);
1644 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1645 {
1646 vim_free(tv->vval.v_string);
1647 goto failed;
1648 }
1649 did_throw = TRUE;
1650 break;
1651
1652 // compare with special values
1653 case ISN_COMPAREBOOL:
1654 case ISN_COMPARESPECIAL:
1655 {
1656 typval_T *tv1 = STACK_TV_BOT(-2);
1657 typval_T *tv2 = STACK_TV_BOT(-1);
1658 varnumber_T arg1 = tv1->vval.v_number;
1659 varnumber_T arg2 = tv2->vval.v_number;
1660 int res;
1661
1662 switch (iptr->isn_arg.op.op_type)
1663 {
1664 case EXPR_EQUAL: res = arg1 == arg2; break;
1665 case EXPR_NEQUAL: res = arg1 != arg2; break;
1666 default: res = 0; break;
1667 }
1668
1669 --ectx.ec_stack.ga_len;
1670 tv1->v_type = VAR_BOOL;
1671 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1672 }
1673 break;
1674
1675 // Operation with two number arguments
1676 case ISN_OPNR:
1677 case ISN_COMPARENR:
1678 {
1679 typval_T *tv1 = STACK_TV_BOT(-2);
1680 typval_T *tv2 = STACK_TV_BOT(-1);
1681 varnumber_T arg1 = tv1->vval.v_number;
1682 varnumber_T arg2 = tv2->vval.v_number;
1683 varnumber_T res;
1684
1685 switch (iptr->isn_arg.op.op_type)
1686 {
1687 case EXPR_MULT: res = arg1 * arg2; break;
1688 case EXPR_DIV: res = arg1 / arg2; break;
1689 case EXPR_REM: res = arg1 % arg2; break;
1690 case EXPR_SUB: res = arg1 - arg2; break;
1691 case EXPR_ADD: res = arg1 + arg2; break;
1692
1693 case EXPR_EQUAL: res = arg1 == arg2; break;
1694 case EXPR_NEQUAL: res = arg1 != arg2; break;
1695 case EXPR_GREATER: res = arg1 > arg2; break;
1696 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1697 case EXPR_SMALLER: res = arg1 < arg2; break;
1698 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1699 default: res = 0; break;
1700 }
1701
1702 --ectx.ec_stack.ga_len;
1703 if (iptr->isn_type == ISN_COMPARENR)
1704 {
1705 tv1->v_type = VAR_BOOL;
1706 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1707 }
1708 else
1709 tv1->vval.v_number = res;
1710 }
1711 break;
1712
1713 // Computation with two float arguments
1714 case ISN_OPFLOAT:
1715 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001716#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 {
1718 typval_T *tv1 = STACK_TV_BOT(-2);
1719 typval_T *tv2 = STACK_TV_BOT(-1);
1720 float_T arg1 = tv1->vval.v_float;
1721 float_T arg2 = tv2->vval.v_float;
1722 float_T res = 0;
1723 int cmp = FALSE;
1724
1725 switch (iptr->isn_arg.op.op_type)
1726 {
1727 case EXPR_MULT: res = arg1 * arg2; break;
1728 case EXPR_DIV: res = arg1 / arg2; break;
1729 case EXPR_SUB: res = arg1 - arg2; break;
1730 case EXPR_ADD: res = arg1 + arg2; break;
1731
1732 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1733 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1734 case EXPR_GREATER: cmp = arg1 > arg2; break;
1735 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1736 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1737 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1738 default: cmp = 0; break;
1739 }
1740 --ectx.ec_stack.ga_len;
1741 if (iptr->isn_type == ISN_COMPAREFLOAT)
1742 {
1743 tv1->v_type = VAR_BOOL;
1744 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1745 }
1746 else
1747 tv1->vval.v_float = res;
1748 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001749#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 break;
1751
1752 case ISN_COMPARELIST:
1753 {
1754 typval_T *tv1 = STACK_TV_BOT(-2);
1755 typval_T *tv2 = STACK_TV_BOT(-1);
1756 list_T *arg1 = tv1->vval.v_list;
1757 list_T *arg2 = tv2->vval.v_list;
1758 int cmp = FALSE;
1759 int ic = iptr->isn_arg.op.op_ic;
1760
1761 switch (iptr->isn_arg.op.op_type)
1762 {
1763 case EXPR_EQUAL: cmp =
1764 list_equal(arg1, arg2, ic, FALSE); break;
1765 case EXPR_NEQUAL: cmp =
1766 !list_equal(arg1, arg2, ic, FALSE); break;
1767 case EXPR_IS: cmp = arg1 == arg2; break;
1768 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1769 default: cmp = 0; break;
1770 }
1771 --ectx.ec_stack.ga_len;
1772 clear_tv(tv1);
1773 clear_tv(tv2);
1774 tv1->v_type = VAR_BOOL;
1775 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1776 }
1777 break;
1778
1779 case ISN_COMPAREBLOB:
1780 {
1781 typval_T *tv1 = STACK_TV_BOT(-2);
1782 typval_T *tv2 = STACK_TV_BOT(-1);
1783 blob_T *arg1 = tv1->vval.v_blob;
1784 blob_T *arg2 = tv2->vval.v_blob;
1785 int cmp = FALSE;
1786
1787 switch (iptr->isn_arg.op.op_type)
1788 {
1789 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1790 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1791 case EXPR_IS: cmp = arg1 == arg2; break;
1792 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1793 default: cmp = 0; break;
1794 }
1795 --ectx.ec_stack.ga_len;
1796 clear_tv(tv1);
1797 clear_tv(tv2);
1798 tv1->v_type = VAR_BOOL;
1799 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1800 }
1801 break;
1802
1803 // TODO: handle separately
1804 case ISN_COMPARESTRING:
1805 case ISN_COMPAREDICT:
1806 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 case ISN_COMPAREANY:
1808 {
1809 typval_T *tv1 = STACK_TV_BOT(-2);
1810 typval_T *tv2 = STACK_TV_BOT(-1);
1811 exptype_T exptype = iptr->isn_arg.op.op_type;
1812 int ic = iptr->isn_arg.op.op_ic;
1813
1814 typval_compare(tv1, tv2, exptype, ic);
1815 clear_tv(tv2);
1816 tv1->v_type = VAR_BOOL;
1817 tv1->vval.v_number = tv1->vval.v_number
1818 ? VVAL_TRUE : VVAL_FALSE;
1819 --ectx.ec_stack.ga_len;
1820 }
1821 break;
1822
1823 case ISN_ADDLIST:
1824 case ISN_ADDBLOB:
1825 {
1826 typval_T *tv1 = STACK_TV_BOT(-2);
1827 typval_T *tv2 = STACK_TV_BOT(-1);
1828
1829 if (iptr->isn_type == ISN_ADDLIST)
1830 eval_addlist(tv1, tv2);
1831 else
1832 eval_addblob(tv1, tv2);
1833 clear_tv(tv2);
1834 --ectx.ec_stack.ga_len;
1835 }
1836 break;
1837
1838 // Computation with two arguments of unknown type
1839 case ISN_OPANY:
1840 {
1841 typval_T *tv1 = STACK_TV_BOT(-2);
1842 typval_T *tv2 = STACK_TV_BOT(-1);
1843 varnumber_T n1, n2;
1844#ifdef FEAT_FLOAT
1845 float_T f1 = 0, f2 = 0;
1846#endif
1847 int error = FALSE;
1848
1849 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1850 {
1851 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1852 {
1853 eval_addlist(tv1, tv2);
1854 clear_tv(tv2);
1855 --ectx.ec_stack.ga_len;
1856 break;
1857 }
1858 else if (tv1->v_type == VAR_BLOB
1859 && tv2->v_type == VAR_BLOB)
1860 {
1861 eval_addblob(tv1, tv2);
1862 clear_tv(tv2);
1863 --ectx.ec_stack.ga_len;
1864 break;
1865 }
1866 }
1867#ifdef FEAT_FLOAT
1868 if (tv1->v_type == VAR_FLOAT)
1869 {
1870 f1 = tv1->vval.v_float;
1871 n1 = 0;
1872 }
1873 else
1874#endif
1875 {
1876 n1 = tv_get_number_chk(tv1, &error);
1877 if (error)
1878 goto failed;
1879#ifdef FEAT_FLOAT
1880 if (tv2->v_type == VAR_FLOAT)
1881 f1 = n1;
1882#endif
1883 }
1884#ifdef FEAT_FLOAT
1885 if (tv2->v_type == VAR_FLOAT)
1886 {
1887 f2 = tv2->vval.v_float;
1888 n2 = 0;
1889 }
1890 else
1891#endif
1892 {
1893 n2 = tv_get_number_chk(tv2, &error);
1894 if (error)
1895 goto failed;
1896#ifdef FEAT_FLOAT
1897 if (tv1->v_type == VAR_FLOAT)
1898 f2 = n2;
1899#endif
1900 }
1901#ifdef FEAT_FLOAT
1902 // if there is a float on either side the result is a float
1903 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1904 {
1905 switch (iptr->isn_arg.op.op_type)
1906 {
1907 case EXPR_MULT: f1 = f1 * f2; break;
1908 case EXPR_DIV: f1 = f1 / f2; break;
1909 case EXPR_SUB: f1 = f1 - f2; break;
1910 case EXPR_ADD: f1 = f1 + f2; break;
1911 default: emsg(_(e_modulus)); goto failed;
1912 }
1913 clear_tv(tv1);
1914 clear_tv(tv2);
1915 tv1->v_type = VAR_FLOAT;
1916 tv1->vval.v_float = f1;
1917 --ectx.ec_stack.ga_len;
1918 }
1919 else
1920#endif
1921 {
1922 switch (iptr->isn_arg.op.op_type)
1923 {
1924 case EXPR_MULT: n1 = n1 * n2; break;
1925 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1926 case EXPR_SUB: n1 = n1 - n2; break;
1927 case EXPR_ADD: n1 = n1 + n2; break;
1928 default: n1 = num_modulus(n1, n2); break;
1929 }
1930 clear_tv(tv1);
1931 clear_tv(tv2);
1932 tv1->v_type = VAR_NUMBER;
1933 tv1->vval.v_number = n1;
1934 --ectx.ec_stack.ga_len;
1935 }
1936 }
1937 break;
1938
1939 case ISN_CONCAT:
1940 {
1941 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1942 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1943 char_u *res;
1944
1945 res = concat_str(str1, str2);
1946 clear_tv(STACK_TV_BOT(-2));
1947 clear_tv(STACK_TV_BOT(-1));
1948 --ectx.ec_stack.ga_len;
1949 STACK_TV_BOT(-1)->vval.v_string = res;
1950 }
1951 break;
1952
1953 case ISN_INDEX:
1954 {
1955 list_T *list;
1956 varnumber_T n;
1957 listitem_T *li;
1958
1959 // list index: list is at stack-2, index at stack-1
1960 tv = STACK_TV_BOT(-2);
1961 if (tv->v_type != VAR_LIST)
1962 {
1963 emsg(_(e_listreq));
1964 goto failed;
1965 }
1966 list = tv->vval.v_list;
1967
1968 tv = STACK_TV_BOT(-1);
1969 if (tv->v_type != VAR_NUMBER)
1970 {
1971 emsg(_(e_number_exp));
1972 goto failed;
1973 }
1974 n = tv->vval.v_number;
1975 clear_tv(tv);
1976 if ((li = list_find(list, n)) == NULL)
1977 {
1978 semsg(_(e_listidx), n);
1979 goto failed;
1980 }
1981 --ectx.ec_stack.ga_len;
1982 clear_tv(STACK_TV_BOT(-1));
1983 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1984 }
1985 break;
1986
1987 // dict member with string key
1988 case ISN_MEMBER:
1989 {
1990 dict_T *dict;
1991 dictitem_T *di;
1992
1993 tv = STACK_TV_BOT(-1);
1994 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1995 {
1996 emsg(_(e_dictreq));
1997 goto failed;
1998 }
1999 dict = tv->vval.v_dict;
2000
2001 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2002 == NULL)
2003 {
2004 semsg(_(e_dictkey), iptr->isn_arg.string);
2005 goto failed;
2006 }
2007 clear_tv(tv);
2008 copy_tv(&di->di_tv, tv);
2009 }
2010 break;
2011
2012 case ISN_NEGATENR:
2013 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002014 if (tv->v_type != VAR_NUMBER
2015#ifdef FEAT_FLOAT
2016 && tv->v_type != VAR_FLOAT
2017#endif
2018 )
2019 {
2020 emsg(_(e_number_exp));
2021 goto failed;
2022 }
2023#ifdef FEAT_FLOAT
2024 if (tv->v_type == VAR_FLOAT)
2025 tv->vval.v_float = -tv->vval.v_float;
2026 else
2027#endif
2028 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 break;
2030
2031 case ISN_CHECKNR:
2032 {
2033 int error = FALSE;
2034
2035 tv = STACK_TV_BOT(-1);
2036 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002037 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002038 (void)tv_get_number_chk(tv, &error);
2039 if (error)
2040 goto failed;
2041 }
2042 break;
2043
2044 case ISN_CHECKTYPE:
2045 {
2046 checktype_T *ct = &iptr->isn_arg.type;
2047
2048 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002049 // TODO: better type comparison
2050 if (tv->v_type != ct->ct_type
2051 && !((tv->v_type == VAR_PARTIAL
2052 && ct->ct_type == VAR_FUNC)
2053 || (tv->v_type == VAR_FUNC
2054 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055 {
2056 semsg(_("E1029: Expected %s but got %s"),
2057 vartype_name(ct->ct_type),
2058 vartype_name(tv->v_type));
2059 goto failed;
2060 }
2061 }
2062 break;
2063
2064 case ISN_2BOOL:
2065 {
2066 int n;
2067
2068 tv = STACK_TV_BOT(-1);
2069 n = tv2bool(tv);
2070 if (iptr->isn_arg.number) // invert
2071 n = !n;
2072 clear_tv(tv);
2073 tv->v_type = VAR_BOOL;
2074 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2075 }
2076 break;
2077
2078 case ISN_2STRING:
2079 {
2080 char_u *str;
2081
2082 tv = STACK_TV_BOT(iptr->isn_arg.number);
2083 if (tv->v_type != VAR_STRING)
2084 {
2085 str = typval_tostring(tv);
2086 clear_tv(tv);
2087 tv->v_type = VAR_STRING;
2088 tv->vval.v_string = str;
2089 }
2090 }
2091 break;
2092
2093 case ISN_DROP:
2094 --ectx.ec_stack.ga_len;
2095 clear_tv(STACK_TV_BOT(0));
2096 break;
2097 }
2098 }
2099
2100done:
2101 // function finished, get result from the stack.
2102 tv = STACK_TV_BOT(-1);
2103 *rettv = *tv;
2104 tv->v_type = VAR_UNKNOWN;
2105 ret = OK;
2106
2107failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002108 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002109 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002110 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002111failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002112 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002113
2114 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2116 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002119 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 return ret;
2121}
2122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002123/*
2124 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002125 * We don't really need this at runtime, but we do have tests that require it,
2126 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 */
2128 void
2129ex_disassemble(exarg_T *eap)
2130{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002131 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002132 char_u *fname;
2133 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134 dfunc_T *dfunc;
2135 isn_T *instr;
2136 int current;
2137 int line_idx = 0;
2138 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002139 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002141 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002142 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002143 if (fname == NULL)
2144 {
2145 semsg(_(e_invarg2), eap->arg);
2146 return;
2147 }
2148
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002149 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002150 if (ufunc == NULL)
2151 {
2152 char_u *p = untrans_function_name(fname);
2153
2154 if (p != NULL)
2155 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002156 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002157 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002158 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 if (ufunc == NULL)
2160 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002161 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 return;
2163 }
2164 if (ufunc->uf_dfunc_idx < 0)
2165 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002166 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002167 return;
2168 }
2169 if (ufunc->uf_name_exp != NULL)
2170 msg((char *)ufunc->uf_name_exp);
2171 else
2172 msg((char *)ufunc->uf_name);
2173
2174 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2175 instr = dfunc->df_instr;
2176 for (current = 0; current < dfunc->df_instr_count; ++current)
2177 {
2178 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002179 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180
2181 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2182 {
2183 if (current > prev_current)
2184 {
2185 msg_puts("\n\n");
2186 prev_current = current;
2187 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002188 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2189 if (line != NULL)
2190 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 }
2192
2193 switch (iptr->isn_type)
2194 {
2195 case ISN_EXEC:
2196 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2197 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002198 case ISN_EXECCONCAT:
2199 smsg("%4d EXECCONCAT %lld", current,
2200 (long long)iptr->isn_arg.number);
2201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 case ISN_ECHO:
2203 {
2204 echo_T *echo = &iptr->isn_arg.echo;
2205
2206 smsg("%4d %s %d", current,
2207 echo->echo_with_white ? "ECHO" : "ECHON",
2208 echo->echo_count);
2209 }
2210 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002211 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002212 smsg("%4d EXECUTE %lld", current,
2213 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002214 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002215 case ISN_ECHOMSG:
2216 smsg("%4d ECHOMSG %lld", current,
2217 (long long)(iptr->isn_arg.number));
2218 break;
2219 case ISN_ECHOERR:
2220 smsg("%4d ECHOERR %lld", current,
2221 (long long)(iptr->isn_arg.number));
2222 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002224 case ISN_LOADOUTER:
2225 {
2226 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2227
2228 if (iptr->isn_arg.number < 0)
2229 smsg("%4d LOAD%s arg[%lld]", current, add,
2230 (long long)(iptr->isn_arg.number
2231 + STACK_FRAME_SIZE));
2232 else
2233 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002234 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002235 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 break;
2237 case ISN_LOADV:
2238 smsg("%4d LOADV v:%s", current,
2239 get_vim_var_name(iptr->isn_arg.number));
2240 break;
2241 case ISN_LOADSCRIPT:
2242 {
2243 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002244 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2246 + iptr->isn_arg.script.script_idx;
2247
2248 smsg("%4d LOADSCRIPT %s from %s", current,
2249 sv->sv_name, si->sn_name);
2250 }
2251 break;
2252 case ISN_LOADS:
2253 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002254 scriptitem_T *si = SCRIPT_ITEM(
2255 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256
2257 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002258 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260 break;
2261 case ISN_LOADG:
2262 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2263 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002264 case ISN_LOADB:
2265 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2266 break;
2267 case ISN_LOADW:
2268 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2269 break;
2270 case ISN_LOADT:
2271 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2272 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 case ISN_LOADOPT:
2274 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2275 break;
2276 case ISN_LOADENV:
2277 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2278 break;
2279 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002280 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002281 break;
2282
2283 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002284 if (iptr->isn_arg.number < 0)
2285 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002286 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002287 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002288 smsg("%4d STORE $%lld", current,
2289 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002291 case ISN_STOREV:
2292 smsg("%4d STOREV v:%s", current,
2293 get_vim_var_name(iptr->isn_arg.number));
2294 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002295 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002296 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2297 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002298 case ISN_STOREB:
2299 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2300 break;
2301 case ISN_STOREW:
2302 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2303 break;
2304 case ISN_STORET:
2305 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2306 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002307 case ISN_STORES:
2308 {
2309 scriptitem_T *si = SCRIPT_ITEM(
2310 iptr->isn_arg.loadstore.ls_sid);
2311
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002312 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002313 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 break;
2316 case ISN_STORESCRIPT:
2317 {
2318 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002319 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2321 + iptr->isn_arg.script.script_idx;
2322
2323 smsg("%4d STORESCRIPT %s in %s", current,
2324 sv->sv_name, si->sn_name);
2325 }
2326 break;
2327 case ISN_STOREOPT:
2328 smsg("%4d STOREOPT &%s", current,
2329 iptr->isn_arg.storeopt.so_name);
2330 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002331 case ISN_STOREENV:
2332 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2333 break;
2334 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002335 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002336 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002337 case ISN_STORENR:
2338 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002339 iptr->isn_arg.storenr.stnr_val,
2340 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 break;
2342
2343 // constants
2344 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002345 smsg("%4d PUSHNR %lld", current,
2346 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 break;
2348 case ISN_PUSHBOOL:
2349 case ISN_PUSHSPEC:
2350 smsg("%4d PUSH %s", current,
2351 get_var_special_name(iptr->isn_arg.number));
2352 break;
2353 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002354#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002356#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002357 break;
2358 case ISN_PUSHS:
2359 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2360 break;
2361 case ISN_PUSHBLOB:
2362 {
2363 char_u *r;
2364 char_u numbuf[NUMBUFLEN];
2365 char_u *tofree;
2366
2367 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002368 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 vim_free(tofree);
2370 }
2371 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002372 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002373 {
2374 char *name = (char *)iptr->isn_arg.string;
2375
2376 smsg("%4d PUSHFUNC \"%s\"", current,
2377 name == NULL ? "[none]" : name);
2378 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002379 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002380 case ISN_PUSHCHANNEL:
2381#ifdef FEAT_JOB_CHANNEL
2382 {
2383 channel_T *channel = iptr->isn_arg.channel;
2384
2385 smsg("%4d PUSHCHANNEL %d", current,
2386 channel == NULL ? 0 : channel->ch_id);
2387 }
2388#endif
2389 break;
2390 case ISN_PUSHJOB:
2391#ifdef FEAT_JOB_CHANNEL
2392 {
2393 typval_T tv;
2394 char_u *name;
2395
2396 tv.v_type = VAR_JOB;
2397 tv.vval.v_job = iptr->isn_arg.job;
2398 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002399 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002400 }
2401#endif
2402 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 case ISN_PUSHEXC:
2404 smsg("%4d PUSH v:exception", current);
2405 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002406 case ISN_UNLET:
2407 smsg("%4d UNLET%s %s", current,
2408 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2409 iptr->isn_arg.unlet.ul_name);
2410 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002411 case ISN_UNLETENV:
2412 smsg("%4d UNLETENV%s $%s", current,
2413 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2414 iptr->isn_arg.unlet.ul_name);
2415 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002417 smsg("%4d NEWLIST size %lld", current,
2418 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 break;
2420 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002421 smsg("%4d NEWDICT size %lld", current,
2422 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 break;
2424
2425 // function call
2426 case ISN_BCALL:
2427 {
2428 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2429
2430 smsg("%4d BCALL %s(argc %d)", current,
2431 internal_func_name(cbfunc->cbf_idx),
2432 cbfunc->cbf_argcount);
2433 }
2434 break;
2435 case ISN_DCALL:
2436 {
2437 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2438 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2439 + cdfunc->cdf_idx;
2440
2441 smsg("%4d DCALL %s(argc %d)", current,
2442 df->df_ufunc->uf_name_exp != NULL
2443 ? df->df_ufunc->uf_name_exp
2444 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2445 }
2446 break;
2447 case ISN_UCALL:
2448 {
2449 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2450
2451 smsg("%4d UCALL %s(argc %d)", current,
2452 cufunc->cuf_name, cufunc->cuf_argcount);
2453 }
2454 break;
2455 case ISN_PCALL:
2456 {
2457 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2458
2459 smsg("%4d PCALL%s (argc %d)", current,
2460 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2461 }
2462 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002463 case ISN_PCALL_END:
2464 smsg("%4d PCALL end", current);
2465 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 case ISN_RETURN:
2467 smsg("%4d RETURN", current);
2468 break;
2469 case ISN_FUNCREF:
2470 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002471 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002473 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002475 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002476 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 }
2478 break;
2479
2480 case ISN_JUMP:
2481 {
2482 char *when = "?";
2483
2484 switch (iptr->isn_arg.jump.jump_when)
2485 {
2486 case JUMP_ALWAYS:
2487 when = "JUMP";
2488 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002489 case JUMP_AND_KEEP_IF_TRUE:
2490 when = "JUMP_AND_KEEP_IF_TRUE";
2491 break;
2492 case JUMP_IF_FALSE:
2493 when = "JUMP_IF_FALSE";
2494 break;
2495 case JUMP_AND_KEEP_IF_FALSE:
2496 when = "JUMP_AND_KEEP_IF_FALSE";
2497 break;
2498 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002499 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 iptr->isn_arg.jump.jump_where);
2501 }
2502 break;
2503
2504 case ISN_FOR:
2505 {
2506 forloop_T *forloop = &iptr->isn_arg.forloop;
2507
2508 smsg("%4d FOR $%d -> %d", current,
2509 forloop->for_idx, forloop->for_end);
2510 }
2511 break;
2512
2513 case ISN_TRY:
2514 {
2515 try_T *try = &iptr->isn_arg.try;
2516
2517 smsg("%4d TRY catch -> %d, finally -> %d", current,
2518 try->try_catch, try->try_finally);
2519 }
2520 break;
2521 case ISN_CATCH:
2522 // TODO
2523 smsg("%4d CATCH", current);
2524 break;
2525 case ISN_ENDTRY:
2526 smsg("%4d ENDTRY", current);
2527 break;
2528 case ISN_THROW:
2529 smsg("%4d THROW", current);
2530 break;
2531
2532 // expression operations on number
2533 case ISN_OPNR:
2534 case ISN_OPFLOAT:
2535 case ISN_OPANY:
2536 {
2537 char *what;
2538 char *ins;
2539
2540 switch (iptr->isn_arg.op.op_type)
2541 {
2542 case EXPR_MULT: what = "*"; break;
2543 case EXPR_DIV: what = "/"; break;
2544 case EXPR_REM: what = "%"; break;
2545 case EXPR_SUB: what = "-"; break;
2546 case EXPR_ADD: what = "+"; break;
2547 default: what = "???"; break;
2548 }
2549 switch (iptr->isn_type)
2550 {
2551 case ISN_OPNR: ins = "OPNR"; break;
2552 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2553 case ISN_OPANY: ins = "OPANY"; break;
2554 default: ins = "???"; break;
2555 }
2556 smsg("%4d %s %s", current, ins, what);
2557 }
2558 break;
2559
2560 case ISN_COMPAREBOOL:
2561 case ISN_COMPARESPECIAL:
2562 case ISN_COMPARENR:
2563 case ISN_COMPAREFLOAT:
2564 case ISN_COMPARESTRING:
2565 case ISN_COMPAREBLOB:
2566 case ISN_COMPARELIST:
2567 case ISN_COMPAREDICT:
2568 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 case ISN_COMPAREANY:
2570 {
2571 char *p;
2572 char buf[10];
2573 char *type;
2574
2575 switch (iptr->isn_arg.op.op_type)
2576 {
2577 case EXPR_EQUAL: p = "=="; break;
2578 case EXPR_NEQUAL: p = "!="; break;
2579 case EXPR_GREATER: p = ">"; break;
2580 case EXPR_GEQUAL: p = ">="; break;
2581 case EXPR_SMALLER: p = "<"; break;
2582 case EXPR_SEQUAL: p = "<="; break;
2583 case EXPR_MATCH: p = "=~"; break;
2584 case EXPR_IS: p = "is"; break;
2585 case EXPR_ISNOT: p = "isnot"; break;
2586 case EXPR_NOMATCH: p = "!~"; break;
2587 default: p = "???"; break;
2588 }
2589 STRCPY(buf, p);
2590 if (iptr->isn_arg.op.op_ic == TRUE)
2591 strcat(buf, "?");
2592 switch(iptr->isn_type)
2593 {
2594 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2595 case ISN_COMPARESPECIAL:
2596 type = "COMPARESPECIAL"; break;
2597 case ISN_COMPARENR: type = "COMPARENR"; break;
2598 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2599 case ISN_COMPARESTRING:
2600 type = "COMPARESTRING"; break;
2601 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2602 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2603 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2604 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2606 default: type = "???"; break;
2607 }
2608
2609 smsg("%4d %s %s", current, type, buf);
2610 }
2611 break;
2612
2613 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2614 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2615
2616 // expression operations
2617 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2618 case ISN_INDEX: smsg("%4d INDEX", current); break;
2619 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2620 iptr->isn_arg.string); break;
2621 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2622
2623 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2624 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2625 vartype_name(iptr->isn_arg.type.ct_type),
2626 iptr->isn_arg.type.ct_off);
2627 break;
2628 case ISN_2BOOL: if (iptr->isn_arg.number)
2629 smsg("%4d INVERT (!val)", current);
2630 else
2631 smsg("%4d 2BOOL (!!val)", current);
2632 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002633 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2634 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 break;
2636
2637 case ISN_DROP: smsg("%4d DROP", current); break;
2638 }
2639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640}
2641
2642/*
2643 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2644 * list, etc. Mostly like what JavaScript does, except that empty list and
2645 * empty dictionary are FALSE.
2646 */
2647 int
2648tv2bool(typval_T *tv)
2649{
2650 switch (tv->v_type)
2651 {
2652 case VAR_NUMBER:
2653 return tv->vval.v_number != 0;
2654 case VAR_FLOAT:
2655#ifdef FEAT_FLOAT
2656 return tv->vval.v_float != 0.0;
2657#else
2658 break;
2659#endif
2660 case VAR_PARTIAL:
2661 return tv->vval.v_partial != NULL;
2662 case VAR_FUNC:
2663 case VAR_STRING:
2664 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2665 case VAR_LIST:
2666 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2667 case VAR_DICT:
2668 return tv->vval.v_dict != NULL
2669 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2670 case VAR_BOOL:
2671 case VAR_SPECIAL:
2672 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2673 case VAR_JOB:
2674#ifdef FEAT_JOB_CHANNEL
2675 return tv->vval.v_job != NULL;
2676#else
2677 break;
2678#endif
2679 case VAR_CHANNEL:
2680#ifdef FEAT_JOB_CHANNEL
2681 return tv->vval.v_channel != NULL;
2682#else
2683 break;
2684#endif
2685 case VAR_BLOB:
2686 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2687 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002688 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002689 case VAR_VOID:
2690 break;
2691 }
2692 return FALSE;
2693}
2694
2695/*
2696 * If "tv" is a string give an error and return FAIL.
2697 */
2698 int
2699check_not_string(typval_T *tv)
2700{
2701 if (tv->v_type == VAR_STRING)
2702 {
2703 emsg(_("E1030: Using a String as a Number"));
2704 clear_tv(tv);
2705 return FAIL;
2706 }
2707 return OK;
2708}
2709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710
2711#endif // FEAT_EVAL