blob: 9651f5e9b2ea5c9e4594a3075f6d5dfed1088e6b [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 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200901 SOURCING_LNUM = iptr->isn_lnum;
902 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200903 }
904 }
905 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100906 ga_clear(&ga);
907 }
908 break;
909
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 // load local variable or argument
911 case ISN_LOAD:
912 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
913 goto failed;
914 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
915 ++ectx.ec_stack.ga_len;
916 break;
917
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200918 // load variable or argument from outer scope
919 case ISN_LOADOUTER:
920 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
921 goto failed;
922 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
923 STACK_TV_BOT(0));
924 ++ectx.ec_stack.ga_len;
925 break;
926
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 // load v: variable
928 case ISN_LOADV:
929 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
930 goto failed;
931 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
932 ++ectx.ec_stack.ga_len;
933 break;
934
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100935 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 case ISN_LOADSCRIPT:
937 {
938 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100939 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 svar_T *sv;
941
942 sv = ((svar_T *)si->sn_var_vals.ga_data)
943 + iptr->isn_arg.script.script_idx;
944 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
945 goto failed;
946 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
947 ++ectx.ec_stack.ga_len;
948 }
949 break;
950
951 // load s: variable in old script
952 case ISN_LOADS:
953 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100954 hashtab_T *ht = &SCRIPT_VARS(
955 iptr->isn_arg.loadstore.ls_sid);
956 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100957 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 if (di == NULL)
960 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100961 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100962 goto failed;
963 }
964 else
965 {
966 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
967 goto failed;
968 copy_tv(&di->di_tv, STACK_TV_BOT(0));
969 ++ectx.ec_stack.ga_len;
970 }
971 }
972 break;
973
Bram Moolenaard3aac292020-04-19 14:32:17 +0200974 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100975 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200976 case ISN_LOADB:
977 case ISN_LOADW:
978 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200980 dictitem_T *di = NULL;
981 hashtab_T *ht = NULL;
982 char namespace;
983 switch (iptr->isn_type)
984 {
985 case ISN_LOADG:
986 ht = get_globvar_ht();
987 namespace = 'g';
988 break;
989 case ISN_LOADB:
990 ht = &curbuf->b_vars->dv_hashtab;
991 namespace = 'b';
992 break;
993 case ISN_LOADW:
994 ht = &curwin->w_vars->dv_hashtab;
995 namespace = 'w';
996 break;
997 case ISN_LOADT:
998 ht = &curtab->tp_vars->dv_hashtab;
999 namespace = 't';
1000 break;
1001 default: // Cannot reach here
1002 goto failed;
1003 }
1004 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 if (di == NULL)
1007 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001008 semsg(_("E121: Undefined variable: %c:%s"),
1009 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 goto failed;
1011 }
1012 else
1013 {
1014 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1015 goto failed;
1016 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1017 ++ectx.ec_stack.ga_len;
1018 }
1019 }
1020 break;
1021
1022 // load &option
1023 case ISN_LOADOPT:
1024 {
1025 typval_T optval;
1026 char_u *name = iptr->isn_arg.string;
1027
Bram Moolenaara8c17702020-04-01 21:17:24 +02001028 // This is not expected to fail, name is checked during
1029 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1031 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001032 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1033 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034 *STACK_TV_BOT(0) = optval;
1035 ++ectx.ec_stack.ga_len;
1036 }
1037 break;
1038
1039 // load $ENV
1040 case ISN_LOADENV:
1041 {
1042 typval_T optval;
1043 char_u *name = iptr->isn_arg.string;
1044
1045 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1046 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001047 // name is always valid, checked when compiling
1048 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049 *STACK_TV_BOT(0) = optval;
1050 ++ectx.ec_stack.ga_len;
1051 }
1052 break;
1053
1054 // load @register
1055 case ISN_LOADREG:
1056 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1057 goto failed;
1058 tv = STACK_TV_BOT(0);
1059 tv->v_type = VAR_STRING;
1060 tv->vval.v_string = get_reg_contents(
1061 iptr->isn_arg.number, GREG_EXPR_SRC);
1062 ++ectx.ec_stack.ga_len;
1063 break;
1064
1065 // store local variable
1066 case ISN_STORE:
1067 --ectx.ec_stack.ga_len;
1068 tv = STACK_TV_VAR(iptr->isn_arg.number);
1069 clear_tv(tv);
1070 *tv = *STACK_TV_BOT(0);
1071 break;
1072
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001073 // store variable or argument in outer scope
1074 case ISN_STOREOUTER:
1075 --ectx.ec_stack.ga_len;
1076 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1077 clear_tv(tv);
1078 *tv = *STACK_TV_BOT(0);
1079 break;
1080
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001081 // store s: variable in old script
1082 case ISN_STORES:
1083 {
1084 hashtab_T *ht = &SCRIPT_VARS(
1085 iptr->isn_arg.loadstore.ls_sid);
1086 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001087 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001088
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001089 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001090 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001091 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001092 else
1093 {
1094 clear_tv(&di->di_tv);
1095 di->di_tv = *STACK_TV_BOT(0);
1096 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001097 }
1098 break;
1099
1100 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 case ISN_STORESCRIPT:
1102 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001103 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 iptr->isn_arg.script.script_sid);
1105 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1106 + iptr->isn_arg.script.script_idx;
1107
1108 --ectx.ec_stack.ga_len;
1109 clear_tv(sv->sv_tv);
1110 *sv->sv_tv = *STACK_TV_BOT(0);
1111 }
1112 break;
1113
1114 // store option
1115 case ISN_STOREOPT:
1116 {
1117 long n = 0;
1118 char_u *s = NULL;
1119 char *msg;
1120
1121 --ectx.ec_stack.ga_len;
1122 tv = STACK_TV_BOT(0);
1123 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001124 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001126 if (s == NULL)
1127 s = (char_u *)"";
1128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 else if (tv->v_type == VAR_NUMBER)
1130 n = tv->vval.v_number;
1131 else
1132 {
1133 emsg(_("E1051: Expected string or number"));
1134 goto failed;
1135 }
1136 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1137 n, s, iptr->isn_arg.storeopt.so_flags);
1138 if (msg != NULL)
1139 {
1140 emsg(_(msg));
1141 goto failed;
1142 }
1143 clear_tv(tv);
1144 }
1145 break;
1146
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001147 // store $ENV
1148 case ISN_STOREENV:
1149 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001150 tv = STACK_TV_BOT(0);
1151 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1152 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001153 break;
1154
1155 // store @r
1156 case ISN_STOREREG:
1157 {
1158 int reg = iptr->isn_arg.number;
1159
1160 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001161 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001162 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001163 tv_get_string(tv), -1, FALSE);
1164 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001165 }
1166 break;
1167
1168 // store v: variable
1169 case ISN_STOREV:
1170 --ectx.ec_stack.ga_len;
1171 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1172 == FAIL)
1173 goto failed;
1174 break;
1175
Bram Moolenaard3aac292020-04-19 14:32:17 +02001176 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001178 case ISN_STOREB:
1179 case ISN_STOREW:
1180 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 {
1182 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001183 hashtab_T *ht;
1184 switch (iptr->isn_type)
1185 {
1186 case ISN_STOREG:
1187 ht = get_globvar_ht();
1188 break;
1189 case ISN_STOREB:
1190 ht = &curbuf->b_vars->dv_hashtab;
1191 break;
1192 case ISN_STOREW:
1193 ht = &curwin->w_vars->dv_hashtab;
1194 break;
1195 case ISN_STORET:
1196 ht = &curtab->tp_vars->dv_hashtab;
1197 break;
1198 default: // Cannot reach here
1199 goto failed;
1200 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201
1202 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001203 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001205 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 else
1207 {
1208 clear_tv(&di->di_tv);
1209 di->di_tv = *STACK_TV_BOT(0);
1210 }
1211 }
1212 break;
1213
1214 // store number in local variable
1215 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001216 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001217 clear_tv(tv);
1218 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001219 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 break;
1221
1222 // push constant
1223 case ISN_PUSHNR:
1224 case ISN_PUSHBOOL:
1225 case ISN_PUSHSPEC:
1226 case ISN_PUSHF:
1227 case ISN_PUSHS:
1228 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001229 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001230 case ISN_PUSHCHANNEL:
1231 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001232 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1233 goto failed;
1234 tv = STACK_TV_BOT(0);
1235 ++ectx.ec_stack.ga_len;
1236 switch (iptr->isn_type)
1237 {
1238 case ISN_PUSHNR:
1239 tv->v_type = VAR_NUMBER;
1240 tv->vval.v_number = iptr->isn_arg.number;
1241 break;
1242 case ISN_PUSHBOOL:
1243 tv->v_type = VAR_BOOL;
1244 tv->vval.v_number = iptr->isn_arg.number;
1245 break;
1246 case ISN_PUSHSPEC:
1247 tv->v_type = VAR_SPECIAL;
1248 tv->vval.v_number = iptr->isn_arg.number;
1249 break;
1250#ifdef FEAT_FLOAT
1251 case ISN_PUSHF:
1252 tv->v_type = VAR_FLOAT;
1253 tv->vval.v_float = iptr->isn_arg.fnumber;
1254 break;
1255#endif
1256 case ISN_PUSHBLOB:
1257 blob_copy(iptr->isn_arg.blob, tv);
1258 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001259 case ISN_PUSHFUNC:
1260 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001261 if (iptr->isn_arg.string == NULL)
1262 tv->vval.v_string = NULL;
1263 else
1264 tv->vval.v_string =
1265 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001266 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001267 case ISN_PUSHCHANNEL:
1268#ifdef FEAT_JOB_CHANNEL
1269 tv->v_type = VAR_CHANNEL;
1270 tv->vval.v_channel = iptr->isn_arg.channel;
1271 if (tv->vval.v_channel != NULL)
1272 ++tv->vval.v_channel->ch_refcount;
1273#endif
1274 break;
1275 case ISN_PUSHJOB:
1276#ifdef FEAT_JOB_CHANNEL
1277 tv->v_type = VAR_JOB;
1278 tv->vval.v_job = iptr->isn_arg.job;
1279 if (tv->vval.v_job != NULL)
1280 ++tv->vval.v_job->jv_refcount;
1281#endif
1282 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001283 default:
1284 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001285 tv->vval.v_string = vim_strsave(
1286 iptr->isn_arg.string == NULL
1287 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001288 }
1289 break;
1290
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001291 case ISN_UNLET:
1292 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1293 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1294 goto failed;
1295 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001296 case ISN_UNLETENV:
1297 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1298 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001300 // create a list from items on the stack; uses a single allocation
1301 // for the list header and the items
1302 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001303 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1304 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 break;
1306
1307 // create a dict from items on the stack
1308 case ISN_NEWDICT:
1309 {
1310 int count = iptr->isn_arg.number;
1311 dict_T *dict = dict_alloc();
1312 dictitem_T *item;
1313
1314 if (dict == NULL)
1315 goto failed;
1316 for (idx = 0; idx < count; ++idx)
1317 {
1318 // check key type is VAR_STRING
1319 tv = STACK_TV_BOT(2 * (idx - count));
1320 item = dictitem_alloc(tv->vval.v_string);
1321 clear_tv(tv);
1322 if (item == NULL)
1323 goto failed;
1324 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1325 item->di_tv.v_lock = 0;
1326 if (dict_add(dict, item) == FAIL)
1327 goto failed;
1328 }
1329
1330 if (count > 0)
1331 ectx.ec_stack.ga_len -= 2 * count - 1;
1332 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1333 goto failed;
1334 else
1335 ++ectx.ec_stack.ga_len;
1336 tv = STACK_TV_BOT(-1);
1337 tv->v_type = VAR_DICT;
1338 tv->vval.v_dict = dict;
1339 ++dict->dv_refcount;
1340 }
1341 break;
1342
1343 // call a :def function
1344 case ISN_DCALL:
1345 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1346 iptr->isn_arg.dfunc.cdf_argcount,
1347 &ectx) == FAIL)
1348 goto failed;
1349 break;
1350
1351 // call a builtin function
1352 case ISN_BCALL:
1353 SOURCING_LNUM = iptr->isn_lnum;
1354 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1355 iptr->isn_arg.bfunc.cbf_argcount,
1356 &ectx) == FAIL)
1357 goto failed;
1358 break;
1359
1360 // call a funcref or partial
1361 case ISN_PCALL:
1362 {
1363 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1364 int r;
1365 typval_T partial;
1366
1367 SOURCING_LNUM = iptr->isn_lnum;
1368 if (pfunc->cpf_top)
1369 {
1370 // funcref is above the arguments
1371 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1372 }
1373 else
1374 {
1375 // Get the funcref from the stack.
1376 --ectx.ec_stack.ga_len;
1377 partial = *STACK_TV_BOT(0);
1378 tv = &partial;
1379 }
1380 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1381 if (tv == &partial)
1382 clear_tv(&partial);
1383 if (r == FAIL)
1384 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 }
1386 break;
1387
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001388 case ISN_PCALL_END:
1389 // PCALL finished, arguments have been consumed and replaced by
1390 // the return value. Now clear the funcref from the stack,
1391 // and move the return value in its place.
1392 --ectx.ec_stack.ga_len;
1393 clear_tv(STACK_TV_BOT(-1));
1394 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1395 break;
1396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 // call a user defined function or funcref/partial
1398 case ISN_UCALL:
1399 {
1400 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1401
1402 SOURCING_LNUM = iptr->isn_lnum;
1403 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001404 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 goto failed;
1406 }
1407 break;
1408
1409 // return from a :def function call
1410 case ISN_RETURN:
1411 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001412 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001413 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001414
1415 if (trystack->ga_len > 0)
1416 trycmd = ((trycmd_T *)trystack->ga_data)
1417 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001418 if (trycmd != NULL
1419 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001420 && trycmd->tcd_finally_idx != 0)
1421 {
1422 // jump to ":finally"
1423 ectx.ec_iidx = trycmd->tcd_finally_idx;
1424 trycmd->tcd_return = TRUE;
1425 }
1426 else
1427 {
1428 // Restore previous function. If the frame pointer
1429 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001430 if (ectx.ec_frame_idx == initial_frame_idx)
1431 {
1432 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1433 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001437 if (func_return(&ectx) == FAIL)
1438 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 }
1440 }
1441 break;
1442
1443 // push a function reference to a compiled function
1444 case ISN_FUNCREF:
1445 {
1446 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001447 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448
1449 pt = ALLOC_CLEAR_ONE(partial_T);
1450 if (pt == NULL)
1451 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001452 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001453 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001454 vim_free(pt);
1455 goto failed;
1456 }
1457 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1458 + iptr->isn_arg.funcref.fr_func;
1459 pt->pt_func = pt_dfunc->df_ufunc;
1460 pt->pt_refcount = 1;
1461 ++pt_dfunc->df_ufunc->uf_refcount;
1462
1463 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1464 {
1465 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1466 + ectx.ec_dfunc_idx;
1467
1468 // The closure needs to find arguments and local
1469 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001470 pt->pt_ectx_stack = &ectx.ec_stack;
1471 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001472
1473 // If this function returns and the closure is still
1474 // used, we need to make a copy of the context
1475 // (arguments and local variables). Store a reference
1476 // to the partial so we can handle that.
1477 ++pt->pt_refcount;
1478 tv = STACK_TV_VAR(dfunc->df_varcount
1479 + iptr->isn_arg.funcref.fr_var_idx);
1480 if (tv->v_type == VAR_PARTIAL)
1481 {
1482 // TODO: use a garray_T on ectx.
1483 emsg("Multiple closures not supported yet");
1484 goto failed;
1485 }
1486 tv->v_type = VAR_PARTIAL;
1487 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001488 }
1489
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 tv = STACK_TV_BOT(0);
1491 ++ectx.ec_stack.ga_len;
1492 tv->vval.v_partial = pt;
1493 tv->v_type = VAR_PARTIAL;
1494 }
1495 break;
1496
1497 // jump if a condition is met
1498 case ISN_JUMP:
1499 {
1500 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1501 int jump = TRUE;
1502
1503 if (when != JUMP_ALWAYS)
1504 {
1505 tv = STACK_TV_BOT(-1);
1506 jump = tv2bool(tv);
1507 if (when == JUMP_IF_FALSE
1508 || when == JUMP_AND_KEEP_IF_FALSE)
1509 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001510 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 {
1512 // drop the value from the stack
1513 clear_tv(tv);
1514 --ectx.ec_stack.ga_len;
1515 }
1516 }
1517 if (jump)
1518 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1519 }
1520 break;
1521
1522 // top of a for loop
1523 case ISN_FOR:
1524 {
1525 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1526 typval_T *idxtv =
1527 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1528
1529 // push the next item from the list
1530 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1531 goto failed;
1532 if (++idxtv->vval.v_number >= list->lv_len)
1533 // past the end of the list, jump to "endfor"
1534 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1535 else if (list->lv_first == &range_list_item)
1536 {
1537 // non-materialized range() list
1538 tv = STACK_TV_BOT(0);
1539 tv->v_type = VAR_NUMBER;
1540 tv->vval.v_number = list_find_nr(
1541 list, idxtv->vval.v_number, NULL);
1542 ++ectx.ec_stack.ga_len;
1543 }
1544 else
1545 {
1546 listitem_T *li = list_find(list, idxtv->vval.v_number);
1547
1548 if (li == NULL)
1549 goto failed;
1550 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1551 ++ectx.ec_stack.ga_len;
1552 }
1553 }
1554 break;
1555
1556 // start of ":try" block
1557 case ISN_TRY:
1558 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001559 trycmd_T *trycmd = NULL;
1560
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1562 goto failed;
1563 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1564 + ectx.ec_trystack.ga_len;
1565 ++ectx.ec_trystack.ga_len;
1566 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001567 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001568 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1569 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001570 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 }
1572 break;
1573
1574 case ISN_PUSHEXC:
1575 if (current_exception == NULL)
1576 {
1577 iemsg("Evaluating catch while current_exception is NULL");
1578 goto failed;
1579 }
1580 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1581 goto failed;
1582 tv = STACK_TV_BOT(0);
1583 ++ectx.ec_stack.ga_len;
1584 tv->v_type = VAR_STRING;
1585 tv->vval.v_string = vim_strsave(
1586 (char_u *)current_exception->value);
1587 break;
1588
1589 case ISN_CATCH:
1590 {
1591 garray_T *trystack = &ectx.ec_trystack;
1592
1593 if (trystack->ga_len > 0)
1594 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001595 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 + trystack->ga_len - 1;
1597 trycmd->tcd_caught = TRUE;
1598 }
1599 did_emsg = got_int = did_throw = FALSE;
1600 catch_exception(current_exception);
1601 }
1602 break;
1603
1604 // end of ":try" block
1605 case ISN_ENDTRY:
1606 {
1607 garray_T *trystack = &ectx.ec_trystack;
1608
1609 if (trystack->ga_len > 0)
1610 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001611 trycmd_T *trycmd = NULL;
1612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 --trystack->ga_len;
1614 --trylevel;
1615 trycmd = ((trycmd_T *)trystack->ga_data)
1616 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001617 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 {
1619 // discard the exception
1620 if (caught_stack == current_exception)
1621 caught_stack = caught_stack->caught;
1622 discard_current_exception();
1623 }
1624
1625 if (trycmd->tcd_return)
1626 {
1627 // Restore previous function. If the frame pointer
1628 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001629 if (ectx.ec_frame_idx == initial_frame_idx)
1630 {
1631 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1632 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001633 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001636 if (func_return(&ectx) == FAIL)
1637 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 }
1639 }
1640 }
1641 break;
1642
1643 case ISN_THROW:
1644 --ectx.ec_stack.ga_len;
1645 tv = STACK_TV_BOT(0);
1646 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1647 {
1648 vim_free(tv->vval.v_string);
1649 goto failed;
1650 }
1651 did_throw = TRUE;
1652 break;
1653
1654 // compare with special values
1655 case ISN_COMPAREBOOL:
1656 case ISN_COMPARESPECIAL:
1657 {
1658 typval_T *tv1 = STACK_TV_BOT(-2);
1659 typval_T *tv2 = STACK_TV_BOT(-1);
1660 varnumber_T arg1 = tv1->vval.v_number;
1661 varnumber_T arg2 = tv2->vval.v_number;
1662 int res;
1663
1664 switch (iptr->isn_arg.op.op_type)
1665 {
1666 case EXPR_EQUAL: res = arg1 == arg2; break;
1667 case EXPR_NEQUAL: res = arg1 != arg2; break;
1668 default: res = 0; break;
1669 }
1670
1671 --ectx.ec_stack.ga_len;
1672 tv1->v_type = VAR_BOOL;
1673 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1674 }
1675 break;
1676
1677 // Operation with two number arguments
1678 case ISN_OPNR:
1679 case ISN_COMPARENR:
1680 {
1681 typval_T *tv1 = STACK_TV_BOT(-2);
1682 typval_T *tv2 = STACK_TV_BOT(-1);
1683 varnumber_T arg1 = tv1->vval.v_number;
1684 varnumber_T arg2 = tv2->vval.v_number;
1685 varnumber_T res;
1686
1687 switch (iptr->isn_arg.op.op_type)
1688 {
1689 case EXPR_MULT: res = arg1 * arg2; break;
1690 case EXPR_DIV: res = arg1 / arg2; break;
1691 case EXPR_REM: res = arg1 % arg2; break;
1692 case EXPR_SUB: res = arg1 - arg2; break;
1693 case EXPR_ADD: res = arg1 + arg2; break;
1694
1695 case EXPR_EQUAL: res = arg1 == arg2; break;
1696 case EXPR_NEQUAL: res = arg1 != arg2; break;
1697 case EXPR_GREATER: res = arg1 > arg2; break;
1698 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1699 case EXPR_SMALLER: res = arg1 < arg2; break;
1700 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1701 default: res = 0; break;
1702 }
1703
1704 --ectx.ec_stack.ga_len;
1705 if (iptr->isn_type == ISN_COMPARENR)
1706 {
1707 tv1->v_type = VAR_BOOL;
1708 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1709 }
1710 else
1711 tv1->vval.v_number = res;
1712 }
1713 break;
1714
1715 // Computation with two float arguments
1716 case ISN_OPFLOAT:
1717 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001718#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 {
1720 typval_T *tv1 = STACK_TV_BOT(-2);
1721 typval_T *tv2 = STACK_TV_BOT(-1);
1722 float_T arg1 = tv1->vval.v_float;
1723 float_T arg2 = tv2->vval.v_float;
1724 float_T res = 0;
1725 int cmp = FALSE;
1726
1727 switch (iptr->isn_arg.op.op_type)
1728 {
1729 case EXPR_MULT: res = arg1 * arg2; break;
1730 case EXPR_DIV: res = arg1 / arg2; break;
1731 case EXPR_SUB: res = arg1 - arg2; break;
1732 case EXPR_ADD: res = arg1 + arg2; break;
1733
1734 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1735 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1736 case EXPR_GREATER: cmp = arg1 > arg2; break;
1737 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1738 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1739 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1740 default: cmp = 0; break;
1741 }
1742 --ectx.ec_stack.ga_len;
1743 if (iptr->isn_type == ISN_COMPAREFLOAT)
1744 {
1745 tv1->v_type = VAR_BOOL;
1746 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1747 }
1748 else
1749 tv1->vval.v_float = res;
1750 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001751#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 break;
1753
1754 case ISN_COMPARELIST:
1755 {
1756 typval_T *tv1 = STACK_TV_BOT(-2);
1757 typval_T *tv2 = STACK_TV_BOT(-1);
1758 list_T *arg1 = tv1->vval.v_list;
1759 list_T *arg2 = tv2->vval.v_list;
1760 int cmp = FALSE;
1761 int ic = iptr->isn_arg.op.op_ic;
1762
1763 switch (iptr->isn_arg.op.op_type)
1764 {
1765 case EXPR_EQUAL: cmp =
1766 list_equal(arg1, arg2, ic, FALSE); break;
1767 case EXPR_NEQUAL: cmp =
1768 !list_equal(arg1, arg2, ic, FALSE); break;
1769 case EXPR_IS: cmp = arg1 == arg2; break;
1770 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1771 default: cmp = 0; break;
1772 }
1773 --ectx.ec_stack.ga_len;
1774 clear_tv(tv1);
1775 clear_tv(tv2);
1776 tv1->v_type = VAR_BOOL;
1777 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1778 }
1779 break;
1780
1781 case ISN_COMPAREBLOB:
1782 {
1783 typval_T *tv1 = STACK_TV_BOT(-2);
1784 typval_T *tv2 = STACK_TV_BOT(-1);
1785 blob_T *arg1 = tv1->vval.v_blob;
1786 blob_T *arg2 = tv2->vval.v_blob;
1787 int cmp = FALSE;
1788
1789 switch (iptr->isn_arg.op.op_type)
1790 {
1791 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1792 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1793 case EXPR_IS: cmp = arg1 == arg2; break;
1794 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1795 default: cmp = 0; break;
1796 }
1797 --ectx.ec_stack.ga_len;
1798 clear_tv(tv1);
1799 clear_tv(tv2);
1800 tv1->v_type = VAR_BOOL;
1801 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1802 }
1803 break;
1804
1805 // TODO: handle separately
1806 case ISN_COMPARESTRING:
1807 case ISN_COMPAREDICT:
1808 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 case ISN_COMPAREANY:
1810 {
1811 typval_T *tv1 = STACK_TV_BOT(-2);
1812 typval_T *tv2 = STACK_TV_BOT(-1);
1813 exptype_T exptype = iptr->isn_arg.op.op_type;
1814 int ic = iptr->isn_arg.op.op_ic;
1815
1816 typval_compare(tv1, tv2, exptype, ic);
1817 clear_tv(tv2);
1818 tv1->v_type = VAR_BOOL;
1819 tv1->vval.v_number = tv1->vval.v_number
1820 ? VVAL_TRUE : VVAL_FALSE;
1821 --ectx.ec_stack.ga_len;
1822 }
1823 break;
1824
1825 case ISN_ADDLIST:
1826 case ISN_ADDBLOB:
1827 {
1828 typval_T *tv1 = STACK_TV_BOT(-2);
1829 typval_T *tv2 = STACK_TV_BOT(-1);
1830
1831 if (iptr->isn_type == ISN_ADDLIST)
1832 eval_addlist(tv1, tv2);
1833 else
1834 eval_addblob(tv1, tv2);
1835 clear_tv(tv2);
1836 --ectx.ec_stack.ga_len;
1837 }
1838 break;
1839
1840 // Computation with two arguments of unknown type
1841 case ISN_OPANY:
1842 {
1843 typval_T *tv1 = STACK_TV_BOT(-2);
1844 typval_T *tv2 = STACK_TV_BOT(-1);
1845 varnumber_T n1, n2;
1846#ifdef FEAT_FLOAT
1847 float_T f1 = 0, f2 = 0;
1848#endif
1849 int error = FALSE;
1850
1851 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1852 {
1853 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1854 {
1855 eval_addlist(tv1, tv2);
1856 clear_tv(tv2);
1857 --ectx.ec_stack.ga_len;
1858 break;
1859 }
1860 else if (tv1->v_type == VAR_BLOB
1861 && tv2->v_type == VAR_BLOB)
1862 {
1863 eval_addblob(tv1, tv2);
1864 clear_tv(tv2);
1865 --ectx.ec_stack.ga_len;
1866 break;
1867 }
1868 }
1869#ifdef FEAT_FLOAT
1870 if (tv1->v_type == VAR_FLOAT)
1871 {
1872 f1 = tv1->vval.v_float;
1873 n1 = 0;
1874 }
1875 else
1876#endif
1877 {
1878 n1 = tv_get_number_chk(tv1, &error);
1879 if (error)
1880 goto failed;
1881#ifdef FEAT_FLOAT
1882 if (tv2->v_type == VAR_FLOAT)
1883 f1 = n1;
1884#endif
1885 }
1886#ifdef FEAT_FLOAT
1887 if (tv2->v_type == VAR_FLOAT)
1888 {
1889 f2 = tv2->vval.v_float;
1890 n2 = 0;
1891 }
1892 else
1893#endif
1894 {
1895 n2 = tv_get_number_chk(tv2, &error);
1896 if (error)
1897 goto failed;
1898#ifdef FEAT_FLOAT
1899 if (tv1->v_type == VAR_FLOAT)
1900 f2 = n2;
1901#endif
1902 }
1903#ifdef FEAT_FLOAT
1904 // if there is a float on either side the result is a float
1905 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1906 {
1907 switch (iptr->isn_arg.op.op_type)
1908 {
1909 case EXPR_MULT: f1 = f1 * f2; break;
1910 case EXPR_DIV: f1 = f1 / f2; break;
1911 case EXPR_SUB: f1 = f1 - f2; break;
1912 case EXPR_ADD: f1 = f1 + f2; break;
1913 default: emsg(_(e_modulus)); goto failed;
1914 }
1915 clear_tv(tv1);
1916 clear_tv(tv2);
1917 tv1->v_type = VAR_FLOAT;
1918 tv1->vval.v_float = f1;
1919 --ectx.ec_stack.ga_len;
1920 }
1921 else
1922#endif
1923 {
1924 switch (iptr->isn_arg.op.op_type)
1925 {
1926 case EXPR_MULT: n1 = n1 * n2; break;
1927 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1928 case EXPR_SUB: n1 = n1 - n2; break;
1929 case EXPR_ADD: n1 = n1 + n2; break;
1930 default: n1 = num_modulus(n1, n2); break;
1931 }
1932 clear_tv(tv1);
1933 clear_tv(tv2);
1934 tv1->v_type = VAR_NUMBER;
1935 tv1->vval.v_number = n1;
1936 --ectx.ec_stack.ga_len;
1937 }
1938 }
1939 break;
1940
1941 case ISN_CONCAT:
1942 {
1943 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1944 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1945 char_u *res;
1946
1947 res = concat_str(str1, str2);
1948 clear_tv(STACK_TV_BOT(-2));
1949 clear_tv(STACK_TV_BOT(-1));
1950 --ectx.ec_stack.ga_len;
1951 STACK_TV_BOT(-1)->vval.v_string = res;
1952 }
1953 break;
1954
1955 case ISN_INDEX:
1956 {
1957 list_T *list;
1958 varnumber_T n;
1959 listitem_T *li;
1960
1961 // list index: list is at stack-2, index at stack-1
1962 tv = STACK_TV_BOT(-2);
1963 if (tv->v_type != VAR_LIST)
1964 {
1965 emsg(_(e_listreq));
1966 goto failed;
1967 }
1968 list = tv->vval.v_list;
1969
1970 tv = STACK_TV_BOT(-1);
1971 if (tv->v_type != VAR_NUMBER)
1972 {
1973 emsg(_(e_number_exp));
1974 goto failed;
1975 }
1976 n = tv->vval.v_number;
1977 clear_tv(tv);
1978 if ((li = list_find(list, n)) == NULL)
1979 {
1980 semsg(_(e_listidx), n);
1981 goto failed;
1982 }
1983 --ectx.ec_stack.ga_len;
1984 clear_tv(STACK_TV_BOT(-1));
1985 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1986 }
1987 break;
1988
1989 // dict member with string key
1990 case ISN_MEMBER:
1991 {
1992 dict_T *dict;
1993 dictitem_T *di;
1994
1995 tv = STACK_TV_BOT(-1);
1996 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1997 {
1998 emsg(_(e_dictreq));
1999 goto failed;
2000 }
2001 dict = tv->vval.v_dict;
2002
2003 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2004 == NULL)
2005 {
2006 semsg(_(e_dictkey), iptr->isn_arg.string);
2007 goto failed;
2008 }
2009 clear_tv(tv);
2010 copy_tv(&di->di_tv, tv);
2011 }
2012 break;
2013
2014 case ISN_NEGATENR:
2015 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002016 if (tv->v_type != VAR_NUMBER
2017#ifdef FEAT_FLOAT
2018 && tv->v_type != VAR_FLOAT
2019#endif
2020 )
2021 {
2022 emsg(_(e_number_exp));
2023 goto failed;
2024 }
2025#ifdef FEAT_FLOAT
2026 if (tv->v_type == VAR_FLOAT)
2027 tv->vval.v_float = -tv->vval.v_float;
2028 else
2029#endif
2030 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 break;
2032
2033 case ISN_CHECKNR:
2034 {
2035 int error = FALSE;
2036
2037 tv = STACK_TV_BOT(-1);
2038 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002039 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 (void)tv_get_number_chk(tv, &error);
2041 if (error)
2042 goto failed;
2043 }
2044 break;
2045
2046 case ISN_CHECKTYPE:
2047 {
2048 checktype_T *ct = &iptr->isn_arg.type;
2049
2050 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002051 // TODO: better type comparison
2052 if (tv->v_type != ct->ct_type
2053 && !((tv->v_type == VAR_PARTIAL
2054 && ct->ct_type == VAR_FUNC)
2055 || (tv->v_type == VAR_FUNC
2056 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 {
2058 semsg(_("E1029: Expected %s but got %s"),
2059 vartype_name(ct->ct_type),
2060 vartype_name(tv->v_type));
2061 goto failed;
2062 }
2063 }
2064 break;
2065
2066 case ISN_2BOOL:
2067 {
2068 int n;
2069
2070 tv = STACK_TV_BOT(-1);
2071 n = tv2bool(tv);
2072 if (iptr->isn_arg.number) // invert
2073 n = !n;
2074 clear_tv(tv);
2075 tv->v_type = VAR_BOOL;
2076 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2077 }
2078 break;
2079
2080 case ISN_2STRING:
2081 {
2082 char_u *str;
2083
2084 tv = STACK_TV_BOT(iptr->isn_arg.number);
2085 if (tv->v_type != VAR_STRING)
2086 {
2087 str = typval_tostring(tv);
2088 clear_tv(tv);
2089 tv->v_type = VAR_STRING;
2090 tv->vval.v_string = str;
2091 }
2092 }
2093 break;
2094
2095 case ISN_DROP:
2096 --ectx.ec_stack.ga_len;
2097 clear_tv(STACK_TV_BOT(0));
2098 break;
2099 }
2100 }
2101
2102done:
2103 // function finished, get result from the stack.
2104 tv = STACK_TV_BOT(-1);
2105 *rettv = *tv;
2106 tv->v_type = VAR_UNKNOWN;
2107 ret = OK;
2108
2109failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002110 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002111 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002112 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002113failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002114 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002115
2116 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2118 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002119
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002121 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 return ret;
2123}
2124
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002125/*
2126 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002127 * We don't really need this at runtime, but we do have tests that require it,
2128 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 */
2130 void
2131ex_disassemble(exarg_T *eap)
2132{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002133 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002134 char_u *fname;
2135 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 dfunc_T *dfunc;
2137 isn_T *instr;
2138 int current;
2139 int line_idx = 0;
2140 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002141 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002143 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002144 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002145 if (fname == NULL)
2146 {
2147 semsg(_(e_invarg2), eap->arg);
2148 return;
2149 }
2150
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002151 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002152 if (ufunc == NULL)
2153 {
2154 char_u *p = untrans_function_name(fname);
2155
2156 if (p != NULL)
2157 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002158 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002159 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002160 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 if (ufunc == NULL)
2162 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002163 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 return;
2165 }
2166 if (ufunc->uf_dfunc_idx < 0)
2167 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002168 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 return;
2170 }
2171 if (ufunc->uf_name_exp != NULL)
2172 msg((char *)ufunc->uf_name_exp);
2173 else
2174 msg((char *)ufunc->uf_name);
2175
2176 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2177 instr = dfunc->df_instr;
2178 for (current = 0; current < dfunc->df_instr_count; ++current)
2179 {
2180 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002181 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182
2183 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2184 {
2185 if (current > prev_current)
2186 {
2187 msg_puts("\n\n");
2188 prev_current = current;
2189 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002190 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2191 if (line != NULL)
2192 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 }
2194
2195 switch (iptr->isn_type)
2196 {
2197 case ISN_EXEC:
2198 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2199 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002200 case ISN_EXECCONCAT:
2201 smsg("%4d EXECCONCAT %lld", current,
2202 (long long)iptr->isn_arg.number);
2203 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002204 case ISN_ECHO:
2205 {
2206 echo_T *echo = &iptr->isn_arg.echo;
2207
2208 smsg("%4d %s %d", current,
2209 echo->echo_with_white ? "ECHO" : "ECHON",
2210 echo->echo_count);
2211 }
2212 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002213 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002214 smsg("%4d EXECUTE %lld", current,
2215 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002216 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002217 case ISN_ECHOMSG:
2218 smsg("%4d ECHOMSG %lld", current,
2219 (long long)(iptr->isn_arg.number));
2220 break;
2221 case ISN_ECHOERR:
2222 smsg("%4d ECHOERR %lld", current,
2223 (long long)(iptr->isn_arg.number));
2224 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002226 case ISN_LOADOUTER:
2227 {
2228 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2229
2230 if (iptr->isn_arg.number < 0)
2231 smsg("%4d LOAD%s arg[%lld]", current, add,
2232 (long long)(iptr->isn_arg.number
2233 + STACK_FRAME_SIZE));
2234 else
2235 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002236 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002237 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 break;
2239 case ISN_LOADV:
2240 smsg("%4d LOADV v:%s", current,
2241 get_vim_var_name(iptr->isn_arg.number));
2242 break;
2243 case ISN_LOADSCRIPT:
2244 {
2245 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002246 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2248 + iptr->isn_arg.script.script_idx;
2249
2250 smsg("%4d LOADSCRIPT %s from %s", current,
2251 sv->sv_name, si->sn_name);
2252 }
2253 break;
2254 case ISN_LOADS:
2255 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002256 scriptitem_T *si = SCRIPT_ITEM(
2257 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258
2259 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002260 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002261 }
2262 break;
2263 case ISN_LOADG:
2264 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2265 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002266 case ISN_LOADB:
2267 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2268 break;
2269 case ISN_LOADW:
2270 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2271 break;
2272 case ISN_LOADT:
2273 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2274 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 case ISN_LOADOPT:
2276 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2277 break;
2278 case ISN_LOADENV:
2279 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2280 break;
2281 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002282 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 break;
2284
2285 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002286 case ISN_STOREOUTER:
2287 {
2288 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2289
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002290 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002291 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002292 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002293 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002294 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002295 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002296 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002298 case ISN_STOREV:
2299 smsg("%4d STOREV v:%s", current,
2300 get_vim_var_name(iptr->isn_arg.number));
2301 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002303 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2304 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002305 case ISN_STOREB:
2306 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2307 break;
2308 case ISN_STOREW:
2309 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2310 break;
2311 case ISN_STORET:
2312 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2313 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002314 case ISN_STORES:
2315 {
2316 scriptitem_T *si = SCRIPT_ITEM(
2317 iptr->isn_arg.loadstore.ls_sid);
2318
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002319 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002320 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002321 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 break;
2323 case ISN_STORESCRIPT:
2324 {
2325 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002326 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2328 + iptr->isn_arg.script.script_idx;
2329
2330 smsg("%4d STORESCRIPT %s in %s", current,
2331 sv->sv_name, si->sn_name);
2332 }
2333 break;
2334 case ISN_STOREOPT:
2335 smsg("%4d STOREOPT &%s", current,
2336 iptr->isn_arg.storeopt.so_name);
2337 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002338 case ISN_STOREENV:
2339 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2340 break;
2341 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002342 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002343 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 case ISN_STORENR:
2345 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002346 iptr->isn_arg.storenr.stnr_val,
2347 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 break;
2349
2350 // constants
2351 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002352 smsg("%4d PUSHNR %lld", current,
2353 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 break;
2355 case ISN_PUSHBOOL:
2356 case ISN_PUSHSPEC:
2357 smsg("%4d PUSH %s", current,
2358 get_var_special_name(iptr->isn_arg.number));
2359 break;
2360 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002361#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002363#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 break;
2365 case ISN_PUSHS:
2366 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2367 break;
2368 case ISN_PUSHBLOB:
2369 {
2370 char_u *r;
2371 char_u numbuf[NUMBUFLEN];
2372 char_u *tofree;
2373
2374 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002375 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 vim_free(tofree);
2377 }
2378 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002379 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002380 {
2381 char *name = (char *)iptr->isn_arg.string;
2382
2383 smsg("%4d PUSHFUNC \"%s\"", current,
2384 name == NULL ? "[none]" : name);
2385 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002386 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002387 case ISN_PUSHCHANNEL:
2388#ifdef FEAT_JOB_CHANNEL
2389 {
2390 channel_T *channel = iptr->isn_arg.channel;
2391
2392 smsg("%4d PUSHCHANNEL %d", current,
2393 channel == NULL ? 0 : channel->ch_id);
2394 }
2395#endif
2396 break;
2397 case ISN_PUSHJOB:
2398#ifdef FEAT_JOB_CHANNEL
2399 {
2400 typval_T tv;
2401 char_u *name;
2402
2403 tv.v_type = VAR_JOB;
2404 tv.vval.v_job = iptr->isn_arg.job;
2405 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002406 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002407 }
2408#endif
2409 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 case ISN_PUSHEXC:
2411 smsg("%4d PUSH v:exception", current);
2412 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002413 case ISN_UNLET:
2414 smsg("%4d UNLET%s %s", current,
2415 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2416 iptr->isn_arg.unlet.ul_name);
2417 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002418 case ISN_UNLETENV:
2419 smsg("%4d UNLETENV%s $%s", current,
2420 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2421 iptr->isn_arg.unlet.ul_name);
2422 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002424 smsg("%4d NEWLIST size %lld", current,
2425 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 break;
2427 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002428 smsg("%4d NEWDICT size %lld", current,
2429 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 break;
2431
2432 // function call
2433 case ISN_BCALL:
2434 {
2435 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2436
2437 smsg("%4d BCALL %s(argc %d)", current,
2438 internal_func_name(cbfunc->cbf_idx),
2439 cbfunc->cbf_argcount);
2440 }
2441 break;
2442 case ISN_DCALL:
2443 {
2444 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2445 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2446 + cdfunc->cdf_idx;
2447
2448 smsg("%4d DCALL %s(argc %d)", current,
2449 df->df_ufunc->uf_name_exp != NULL
2450 ? df->df_ufunc->uf_name_exp
2451 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2452 }
2453 break;
2454 case ISN_UCALL:
2455 {
2456 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2457
2458 smsg("%4d UCALL %s(argc %d)", current,
2459 cufunc->cuf_name, cufunc->cuf_argcount);
2460 }
2461 break;
2462 case ISN_PCALL:
2463 {
2464 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2465
2466 smsg("%4d PCALL%s (argc %d)", current,
2467 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2468 }
2469 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002470 case ISN_PCALL_END:
2471 smsg("%4d PCALL end", current);
2472 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 case ISN_RETURN:
2474 smsg("%4d RETURN", current);
2475 break;
2476 case ISN_FUNCREF:
2477 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002478 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002479 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002480 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002482 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002483 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 }
2485 break;
2486
2487 case ISN_JUMP:
2488 {
2489 char *when = "?";
2490
2491 switch (iptr->isn_arg.jump.jump_when)
2492 {
2493 case JUMP_ALWAYS:
2494 when = "JUMP";
2495 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 case JUMP_AND_KEEP_IF_TRUE:
2497 when = "JUMP_AND_KEEP_IF_TRUE";
2498 break;
2499 case JUMP_IF_FALSE:
2500 when = "JUMP_IF_FALSE";
2501 break;
2502 case JUMP_AND_KEEP_IF_FALSE:
2503 when = "JUMP_AND_KEEP_IF_FALSE";
2504 break;
2505 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002506 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 iptr->isn_arg.jump.jump_where);
2508 }
2509 break;
2510
2511 case ISN_FOR:
2512 {
2513 forloop_T *forloop = &iptr->isn_arg.forloop;
2514
2515 smsg("%4d FOR $%d -> %d", current,
2516 forloop->for_idx, forloop->for_end);
2517 }
2518 break;
2519
2520 case ISN_TRY:
2521 {
2522 try_T *try = &iptr->isn_arg.try;
2523
2524 smsg("%4d TRY catch -> %d, finally -> %d", current,
2525 try->try_catch, try->try_finally);
2526 }
2527 break;
2528 case ISN_CATCH:
2529 // TODO
2530 smsg("%4d CATCH", current);
2531 break;
2532 case ISN_ENDTRY:
2533 smsg("%4d ENDTRY", current);
2534 break;
2535 case ISN_THROW:
2536 smsg("%4d THROW", current);
2537 break;
2538
2539 // expression operations on number
2540 case ISN_OPNR:
2541 case ISN_OPFLOAT:
2542 case ISN_OPANY:
2543 {
2544 char *what;
2545 char *ins;
2546
2547 switch (iptr->isn_arg.op.op_type)
2548 {
2549 case EXPR_MULT: what = "*"; break;
2550 case EXPR_DIV: what = "/"; break;
2551 case EXPR_REM: what = "%"; break;
2552 case EXPR_SUB: what = "-"; break;
2553 case EXPR_ADD: what = "+"; break;
2554 default: what = "???"; break;
2555 }
2556 switch (iptr->isn_type)
2557 {
2558 case ISN_OPNR: ins = "OPNR"; break;
2559 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2560 case ISN_OPANY: ins = "OPANY"; break;
2561 default: ins = "???"; break;
2562 }
2563 smsg("%4d %s %s", current, ins, what);
2564 }
2565 break;
2566
2567 case ISN_COMPAREBOOL:
2568 case ISN_COMPARESPECIAL:
2569 case ISN_COMPARENR:
2570 case ISN_COMPAREFLOAT:
2571 case ISN_COMPARESTRING:
2572 case ISN_COMPAREBLOB:
2573 case ISN_COMPARELIST:
2574 case ISN_COMPAREDICT:
2575 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 case ISN_COMPAREANY:
2577 {
2578 char *p;
2579 char buf[10];
2580 char *type;
2581
2582 switch (iptr->isn_arg.op.op_type)
2583 {
2584 case EXPR_EQUAL: p = "=="; break;
2585 case EXPR_NEQUAL: p = "!="; break;
2586 case EXPR_GREATER: p = ">"; break;
2587 case EXPR_GEQUAL: p = ">="; break;
2588 case EXPR_SMALLER: p = "<"; break;
2589 case EXPR_SEQUAL: p = "<="; break;
2590 case EXPR_MATCH: p = "=~"; break;
2591 case EXPR_IS: p = "is"; break;
2592 case EXPR_ISNOT: p = "isnot"; break;
2593 case EXPR_NOMATCH: p = "!~"; break;
2594 default: p = "???"; break;
2595 }
2596 STRCPY(buf, p);
2597 if (iptr->isn_arg.op.op_ic == TRUE)
2598 strcat(buf, "?");
2599 switch(iptr->isn_type)
2600 {
2601 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2602 case ISN_COMPARESPECIAL:
2603 type = "COMPARESPECIAL"; break;
2604 case ISN_COMPARENR: type = "COMPARENR"; break;
2605 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2606 case ISN_COMPARESTRING:
2607 type = "COMPARESTRING"; break;
2608 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2609 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2610 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2611 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2613 default: type = "???"; break;
2614 }
2615
2616 smsg("%4d %s %s", current, type, buf);
2617 }
2618 break;
2619
2620 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2621 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2622
2623 // expression operations
2624 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2625 case ISN_INDEX: smsg("%4d INDEX", current); break;
2626 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2627 iptr->isn_arg.string); break;
2628 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2629
2630 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2631 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2632 vartype_name(iptr->isn_arg.type.ct_type),
2633 iptr->isn_arg.type.ct_off);
2634 break;
2635 case ISN_2BOOL: if (iptr->isn_arg.number)
2636 smsg("%4d INVERT (!val)", current);
2637 else
2638 smsg("%4d 2BOOL (!!val)", current);
2639 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002640 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2641 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002642 break;
2643
2644 case ISN_DROP: smsg("%4d DROP", current); break;
2645 }
2646 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647}
2648
2649/*
2650 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2651 * list, etc. Mostly like what JavaScript does, except that empty list and
2652 * empty dictionary are FALSE.
2653 */
2654 int
2655tv2bool(typval_T *tv)
2656{
2657 switch (tv->v_type)
2658 {
2659 case VAR_NUMBER:
2660 return tv->vval.v_number != 0;
2661 case VAR_FLOAT:
2662#ifdef FEAT_FLOAT
2663 return tv->vval.v_float != 0.0;
2664#else
2665 break;
2666#endif
2667 case VAR_PARTIAL:
2668 return tv->vval.v_partial != NULL;
2669 case VAR_FUNC:
2670 case VAR_STRING:
2671 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2672 case VAR_LIST:
2673 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2674 case VAR_DICT:
2675 return tv->vval.v_dict != NULL
2676 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2677 case VAR_BOOL:
2678 case VAR_SPECIAL:
2679 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2680 case VAR_JOB:
2681#ifdef FEAT_JOB_CHANNEL
2682 return tv->vval.v_job != NULL;
2683#else
2684 break;
2685#endif
2686 case VAR_CHANNEL:
2687#ifdef FEAT_JOB_CHANNEL
2688 return tv->vval.v_channel != NULL;
2689#else
2690 break;
2691#endif
2692 case VAR_BLOB:
2693 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2694 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002695 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 case VAR_VOID:
2697 break;
2698 }
2699 return FALSE;
2700}
2701
2702/*
2703 * If "tv" is a string give an error and return FAIL.
2704 */
2705 int
2706check_not_string(typval_T *tv)
2707{
2708 if (tv->v_type == VAR_STRING)
2709 {
2710 emsg(_("E1030: Using a String as a Number"));
2711 clear_tv(tv);
2712 return FAIL;
2713 }
2714 return OK;
2715}
2716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002717
2718#endif // FEAT_EVAL