blob: 386b8491f933cdea3c84c9b2e2b81022148855b3 [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;
435
436 if (call_prepare(argcount, argvars, ectx) == FAIL)
437 return FAIL;
438
439 // Call the builtin function.
440 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
441
442 // Clear the arguments.
443 for (idx = 0; idx < argcount; ++idx)
444 clear_tv(&argvars[idx]);
445 return OK;
446}
447
448/*
449 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100450 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 */
452 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100453call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454{
455 typval_T argvars[MAX_FUNC_ARGS];
456 funcexe_T funcexe;
457 int error;
458 int idx;
459
460 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100461 {
462 // The function has been compiled, can call it quickly. For a function
463 // that was defined later: we can call it directly next time.
464 if (iptr != NULL)
465 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100466 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100467 iptr->isn_type = ISN_DCALL;
468 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
469 iptr->isn_arg.dfunc.cdf_argcount = argcount;
470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100471 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473
474 if (call_prepare(argcount, argvars, ectx) == FAIL)
475 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200476 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 funcexe.evaluate = TRUE;
478
479 // Call the user function. Result goes in last position on the stack.
480 // TODO: add selfdict if there is one
481 error = call_user_func_check(ufunc, argcount, argvars,
482 STACK_TV_BOT(-1), &funcexe, NULL);
483
484 // Clear the arguments.
485 for (idx = 0; idx < argcount; ++idx)
486 clear_tv(&argvars[idx]);
487
488 if (error != FCERR_NONE)
489 {
490 user_func_error(error, ufunc->uf_name);
491 return FAIL;
492 }
493 return OK;
494}
495
496/*
497 * Execute a function by "name".
498 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 * Returns FAIL if not found without an error message.
501 */
502 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100503call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504{
505 ufunc_T *ufunc;
506
507 if (builtin_function(name, -1))
508 {
509 int func_idx = find_internal_func(name);
510
511 if (func_idx < 0)
512 return FAIL;
513 if (check_internal_func(func_idx, argcount) == FAIL)
514 return FAIL;
515 return call_bfunc(func_idx, argcount, ectx);
516 }
517
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200518 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100520 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100521
522 return FAIL;
523}
524
525 static int
526call_partial(typval_T *tv, int argcount, ectx_T *ectx)
527{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200528 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 int called_emsg_before = called_emsg;
530
531 if (tv->v_type == VAR_PARTIAL)
532 {
533 partial_T *pt = tv->vval.v_partial;
534
535 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200536 {
537 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
538
539 // closure may need the function context where it was defined
540 ectx->ec_outer_stack = pt->pt_ectx_stack;
541 ectx->ec_outer_frame = pt->pt_ectx_frame;
542
543 return ret;
544 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100545 name = pt->pt_name;
546 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200547 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200549 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100550 {
551 if (called_emsg == called_emsg_before)
552 semsg(_(e_unknownfunc), name);
553 return FAIL;
554 }
555 return OK;
556}
557
558/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100559 * Store "tv" in variable "name".
560 * This is for s: and g: variables.
561 */
562 static void
563store_var(char_u *name, typval_T *tv)
564{
565 funccal_entry_T entry;
566
567 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200568 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100569 restore_funccal();
570}
571
Bram Moolenaard3aac292020-04-19 14:32:17 +0200572
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100573/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 * Execute a function by "name".
575 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100576 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100577 */
578 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100579call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580{
581 int called_emsg_before = called_emsg;
582
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100583 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100584 && called_emsg == called_emsg_before)
585 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200586 dictitem_T *v;
587
588 v = find_var(name, NULL, FALSE);
589 if (v == NULL)
590 {
591 semsg(_(e_unknownfunc), name);
592 return FAIL;
593 }
594 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
595 {
596 semsg(_(e_unknownfunc), name);
597 return FAIL;
598 }
599 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 }
601 return OK;
602}
603
604/*
605 * Call a "def" function from old Vim script.
606 * Return OK or FAIL.
607 */
608 int
609call_def_function(
610 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200611 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 typval_T *argv, // arguments
613 typval_T *rettv) // return value
614{
615 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200616 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200617 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 typval_T *tv;
619 int idx;
620 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100621 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200622 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623
624// Get pointer to item in the stack.
625#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
626
627// Get pointer to item at the bottom of the stack, -1 is the bottom.
628#undef STACK_TV_BOT
629#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
630
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200631// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200632#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 +0100633
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200634// Like STACK_TV_VAR but use the outer scope
635#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
636
Bram Moolenaara80faa82020-04-12 19:37:17 +0200637 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100638 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
639 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100640 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
642
643 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
644
645 // Put arguments on the stack.
646 for (idx = 0; idx < argc; ++idx)
647 {
648 copy_tv(&argv[idx], STACK_TV_BOT(0));
649 ++ectx.ec_stack.ga_len;
650 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200651
652 // Turn varargs into a list. Empty list if no args.
653 if (ufunc->uf_va_name != NULL)
654 {
655 int vararg_count = argc - ufunc->uf_args.ga_len;
656
657 if (vararg_count < 0)
658 vararg_count = 0;
659 else
660 argc -= vararg_count;
661 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200662 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200663 if (defcount > 0)
664 // Move varargs list to below missing default arguments.
665 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
666 --ectx.ec_stack.ga_len;
667 }
668
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100669 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200670 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100671 if (defcount > 0)
672 for (idx = 0; idx < defcount; ++idx)
673 {
674 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
675 ++ectx.ec_stack.ga_len;
676 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200677 if (ufunc->uf_va_name != NULL)
678 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
680 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200681 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
682 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683
684 // dummy frame entries
685 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
686 {
687 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
688 ++ectx.ec_stack.ga_len;
689 }
690
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200691 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200692 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200693 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
694 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200695 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200697 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200698 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200699 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200700
701 ectx.ec_instr = dfunc->df_instr;
702 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100703
Bram Moolenaara26b9702020-04-18 19:53:28 +0200704 // Commands behave like vim9script.
705 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
706
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100707 // Decide where to start execution, handles optional arguments.
708 init_instr_idx(ufunc, argc, &ectx);
709
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100710 for (;;)
711 {
712 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100713
714 veryfast_breakcheck();
715 if (got_int)
716 {
717 // Turn CTRL-C into an exception.
718 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100719 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100720 goto failed;
721 did_throw = TRUE;
722 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723
Bram Moolenaara26b9702020-04-18 19:53:28 +0200724 if (did_emsg && msg_list != NULL && *msg_list != NULL)
725 {
726 // Turn an error message into an exception.
727 did_emsg = FALSE;
728 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
729 goto failed;
730 did_throw = TRUE;
731 *msg_list = NULL;
732 }
733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734 if (did_throw && !ectx.ec_in_catch)
735 {
736 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100737 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738
739 // An exception jumps to the first catch, finally, or returns from
740 // the current function.
741 if (trystack->ga_len > 0)
742 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200743 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 {
745 // jump to ":catch" or ":finally"
746 ectx.ec_in_catch = TRUE;
747 ectx.ec_iidx = trycmd->tcd_catch_idx;
748 }
749 else
750 {
751 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200752 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 {
754 // At the toplevel we are done. Push a dummy return value.
755 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
756 goto failed;
757 tv = STACK_TV_BOT(0);
758 tv->v_type = VAR_NUMBER;
759 tv->vval.v_number = 0;
760 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100761 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200762 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
763 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 goto done;
765 }
766
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200767 if (func_return(&ectx) == FAIL)
768 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 }
770 continue;
771 }
772
773 iptr = &ectx.ec_instr[ectx.ec_iidx++];
774 switch (iptr->isn_type)
775 {
776 // execute Ex command line
777 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200778 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 do_cmdline_cmd(iptr->isn_arg.string);
780 break;
781
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200782 // execute Ex command from pieces on the stack
783 case ISN_EXECCONCAT:
784 {
785 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200786 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200787 int pass;
788 int i;
789 char_u *cmd = NULL;
790 char_u *str;
791
792 for (pass = 1; pass <= 2; ++pass)
793 {
794 for (i = 0; i < count; ++i)
795 {
796 tv = STACK_TV_BOT(i - count);
797 str = tv->vval.v_string;
798 if (str != NULL && *str != NUL)
799 {
800 if (pass == 2)
801 STRCPY(cmd + len, str);
802 len += STRLEN(str);
803 }
804 if (pass == 2)
805 clear_tv(tv);
806 }
807 if (pass == 1)
808 {
809 cmd = alloc(len + 1);
810 if (cmd == NULL)
811 goto failed;
812 len = 0;
813 }
814 }
815
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200816 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200817 do_cmdline_cmd(cmd);
818 vim_free(cmd);
819 }
820 break;
821
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 // execute :echo {string} ...
823 case ISN_ECHO:
824 {
825 int count = iptr->isn_arg.echo.echo_count;
826 int atstart = TRUE;
827 int needclr = TRUE;
828
829 for (idx = 0; idx < count; ++idx)
830 {
831 tv = STACK_TV_BOT(idx - count);
832 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
833 &atstart, &needclr);
834 clear_tv(tv);
835 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100836 if (needclr)
837 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 ectx.ec_stack.ga_len -= count;
839 }
840 break;
841
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200842 // :execute {string} ...
843 // :echomsg {string} ...
844 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100845 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200846 case ISN_ECHOMSG:
847 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100848 {
849 int count = iptr->isn_arg.number;
850 garray_T ga;
851 char_u buf[NUMBUFLEN];
852 char_u *p;
853 int len;
854 int failed = FALSE;
855
856 ga_init2(&ga, 1, 80);
857 for (idx = 0; idx < count; ++idx)
858 {
859 tv = STACK_TV_BOT(idx - count);
860 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
861 {
862 emsg(_(e_inval_string));
863 break;
864 }
865 else
866 p = tv_get_string_buf(tv, buf);
867
868 len = (int)STRLEN(p);
869 if (ga_grow(&ga, len + 2) == FAIL)
870 failed = TRUE;
871 else
872 {
873 if (ga.ga_len > 0)
874 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
875 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
876 ga.ga_len += len;
877 }
878 clear_tv(tv);
879 }
880 ectx.ec_stack.ga_len -= count;
881
882 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200883 {
884 if (iptr->isn_type == ISN_EXECUTE)
885 do_cmdline_cmd((char_u *)ga.ga_data);
886 else
887 {
888 msg_sb_eol();
889 if (iptr->isn_type == ISN_ECHOMSG)
890 {
891 msg_attr(ga.ga_data, echo_attr);
892 out_flush();
893 }
894 else
895 {
896 int save_did_emsg = did_emsg;
897
898 SOURCING_LNUM = iptr->isn_lnum;
899 emsg(ga.ga_data);
900 if (!force_abort)
901 // We don't want to abort following
902 // commands, restore did_emsg.
903 did_emsg = save_did_emsg;
904 }
905 }
906 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100907 ga_clear(&ga);
908 }
909 break;
910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 // load local variable or argument
912 case ISN_LOAD:
913 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
914 goto failed;
915 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
916 ++ectx.ec_stack.ga_len;
917 break;
918
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200919 // load variable or argument from outer scope
920 case ISN_LOADOUTER:
921 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
922 goto failed;
923 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
924 STACK_TV_BOT(0));
925 ++ectx.ec_stack.ga_len;
926 break;
927
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100928 // load v: variable
929 case ISN_LOADV:
930 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
931 goto failed;
932 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
933 ++ectx.ec_stack.ga_len;
934 break;
935
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100936 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 case ISN_LOADSCRIPT:
938 {
939 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100940 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 svar_T *sv;
942
943 sv = ((svar_T *)si->sn_var_vals.ga_data)
944 + iptr->isn_arg.script.script_idx;
945 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
946 goto failed;
947 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
948 ++ectx.ec_stack.ga_len;
949 }
950 break;
951
952 // load s: variable in old script
953 case ISN_LOADS:
954 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100955 hashtab_T *ht = &SCRIPT_VARS(
956 iptr->isn_arg.loadstore.ls_sid);
957 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100958 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100959
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 if (di == NULL)
961 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100962 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 goto failed;
964 }
965 else
966 {
967 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
968 goto failed;
969 copy_tv(&di->di_tv, STACK_TV_BOT(0));
970 ++ectx.ec_stack.ga_len;
971 }
972 }
973 break;
974
Bram Moolenaard3aac292020-04-19 14:32:17 +0200975 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100976 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200977 case ISN_LOADB:
978 case ISN_LOADW:
979 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200981 dictitem_T *di = NULL;
982 hashtab_T *ht = NULL;
983 char namespace;
984 switch (iptr->isn_type)
985 {
986 case ISN_LOADG:
987 ht = get_globvar_ht();
988 namespace = 'g';
989 break;
990 case ISN_LOADB:
991 ht = &curbuf->b_vars->dv_hashtab;
992 namespace = 'b';
993 break;
994 case ISN_LOADW:
995 ht = &curwin->w_vars->dv_hashtab;
996 namespace = 'w';
997 break;
998 case ISN_LOADT:
999 ht = &curtab->tp_vars->dv_hashtab;
1000 namespace = 't';
1001 break;
1002 default: // Cannot reach here
1003 goto failed;
1004 }
1005 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001007 if (di == NULL)
1008 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001009 semsg(_("E121: Undefined variable: %c:%s"),
1010 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 goto failed;
1012 }
1013 else
1014 {
1015 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1016 goto failed;
1017 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1018 ++ectx.ec_stack.ga_len;
1019 }
1020 }
1021 break;
1022
1023 // load &option
1024 case ISN_LOADOPT:
1025 {
1026 typval_T optval;
1027 char_u *name = iptr->isn_arg.string;
1028
Bram Moolenaara8c17702020-04-01 21:17:24 +02001029 // This is not expected to fail, name is checked during
1030 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001031 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1032 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001033 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1034 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 *STACK_TV_BOT(0) = optval;
1036 ++ectx.ec_stack.ga_len;
1037 }
1038 break;
1039
1040 // load $ENV
1041 case ISN_LOADENV:
1042 {
1043 typval_T optval;
1044 char_u *name = iptr->isn_arg.string;
1045
1046 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1047 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001048 // name is always valid, checked when compiling
1049 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 *STACK_TV_BOT(0) = optval;
1051 ++ectx.ec_stack.ga_len;
1052 }
1053 break;
1054
1055 // load @register
1056 case ISN_LOADREG:
1057 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1058 goto failed;
1059 tv = STACK_TV_BOT(0);
1060 tv->v_type = VAR_STRING;
1061 tv->vval.v_string = get_reg_contents(
1062 iptr->isn_arg.number, GREG_EXPR_SRC);
1063 ++ectx.ec_stack.ga_len;
1064 break;
1065
1066 // store local variable
1067 case ISN_STORE:
1068 --ectx.ec_stack.ga_len;
1069 tv = STACK_TV_VAR(iptr->isn_arg.number);
1070 clear_tv(tv);
1071 *tv = *STACK_TV_BOT(0);
1072 break;
1073
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001074 // store s: variable in old script
1075 case ISN_STORES:
1076 {
1077 hashtab_T *ht = &SCRIPT_VARS(
1078 iptr->isn_arg.loadstore.ls_sid);
1079 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001080 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001081
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001082 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001083 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001084 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001085 else
1086 {
1087 clear_tv(&di->di_tv);
1088 di->di_tv = *STACK_TV_BOT(0);
1089 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001090 }
1091 break;
1092
1093 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 case ISN_STORESCRIPT:
1095 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001096 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 iptr->isn_arg.script.script_sid);
1098 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1099 + iptr->isn_arg.script.script_idx;
1100
1101 --ectx.ec_stack.ga_len;
1102 clear_tv(sv->sv_tv);
1103 *sv->sv_tv = *STACK_TV_BOT(0);
1104 }
1105 break;
1106
1107 // store option
1108 case ISN_STOREOPT:
1109 {
1110 long n = 0;
1111 char_u *s = NULL;
1112 char *msg;
1113
1114 --ectx.ec_stack.ga_len;
1115 tv = STACK_TV_BOT(0);
1116 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001117 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001119 if (s == NULL)
1120 s = (char_u *)"";
1121 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 else if (tv->v_type == VAR_NUMBER)
1123 n = tv->vval.v_number;
1124 else
1125 {
1126 emsg(_("E1051: Expected string or number"));
1127 goto failed;
1128 }
1129 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1130 n, s, iptr->isn_arg.storeopt.so_flags);
1131 if (msg != NULL)
1132 {
1133 emsg(_(msg));
1134 goto failed;
1135 }
1136 clear_tv(tv);
1137 }
1138 break;
1139
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001140 // store $ENV
1141 case ISN_STOREENV:
1142 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001143 tv = STACK_TV_BOT(0);
1144 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1145 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001146 break;
1147
1148 // store @r
1149 case ISN_STOREREG:
1150 {
1151 int reg = iptr->isn_arg.number;
1152
1153 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001154 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001155 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001156 tv_get_string(tv), -1, FALSE);
1157 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001158 }
1159 break;
1160
1161 // store v: variable
1162 case ISN_STOREV:
1163 --ectx.ec_stack.ga_len;
1164 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1165 == FAIL)
1166 goto failed;
1167 break;
1168
Bram Moolenaard3aac292020-04-19 14:32:17 +02001169 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001171 case ISN_STOREB:
1172 case ISN_STOREW:
1173 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 {
1175 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001176 hashtab_T *ht;
1177 switch (iptr->isn_type)
1178 {
1179 case ISN_STOREG:
1180 ht = get_globvar_ht();
1181 break;
1182 case ISN_STOREB:
1183 ht = &curbuf->b_vars->dv_hashtab;
1184 break;
1185 case ISN_STOREW:
1186 ht = &curwin->w_vars->dv_hashtab;
1187 break;
1188 case ISN_STORET:
1189 ht = &curtab->tp_vars->dv_hashtab;
1190 break;
1191 default: // Cannot reach here
1192 goto failed;
1193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194
1195 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001196 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001198 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 else
1200 {
1201 clear_tv(&di->di_tv);
1202 di->di_tv = *STACK_TV_BOT(0);
1203 }
1204 }
1205 break;
1206
1207 // store number in local variable
1208 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001209 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 clear_tv(tv);
1211 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001212 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 break;
1214
1215 // push constant
1216 case ISN_PUSHNR:
1217 case ISN_PUSHBOOL:
1218 case ISN_PUSHSPEC:
1219 case ISN_PUSHF:
1220 case ISN_PUSHS:
1221 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001222 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001223 case ISN_PUSHCHANNEL:
1224 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1226 goto failed;
1227 tv = STACK_TV_BOT(0);
1228 ++ectx.ec_stack.ga_len;
1229 switch (iptr->isn_type)
1230 {
1231 case ISN_PUSHNR:
1232 tv->v_type = VAR_NUMBER;
1233 tv->vval.v_number = iptr->isn_arg.number;
1234 break;
1235 case ISN_PUSHBOOL:
1236 tv->v_type = VAR_BOOL;
1237 tv->vval.v_number = iptr->isn_arg.number;
1238 break;
1239 case ISN_PUSHSPEC:
1240 tv->v_type = VAR_SPECIAL;
1241 tv->vval.v_number = iptr->isn_arg.number;
1242 break;
1243#ifdef FEAT_FLOAT
1244 case ISN_PUSHF:
1245 tv->v_type = VAR_FLOAT;
1246 tv->vval.v_float = iptr->isn_arg.fnumber;
1247 break;
1248#endif
1249 case ISN_PUSHBLOB:
1250 blob_copy(iptr->isn_arg.blob, tv);
1251 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001252 case ISN_PUSHFUNC:
1253 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001254 if (iptr->isn_arg.string == NULL)
1255 tv->vval.v_string = NULL;
1256 else
1257 tv->vval.v_string =
1258 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001259 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001260 case ISN_PUSHCHANNEL:
1261#ifdef FEAT_JOB_CHANNEL
1262 tv->v_type = VAR_CHANNEL;
1263 tv->vval.v_channel = iptr->isn_arg.channel;
1264 if (tv->vval.v_channel != NULL)
1265 ++tv->vval.v_channel->ch_refcount;
1266#endif
1267 break;
1268 case ISN_PUSHJOB:
1269#ifdef FEAT_JOB_CHANNEL
1270 tv->v_type = VAR_JOB;
1271 tv->vval.v_job = iptr->isn_arg.job;
1272 if (tv->vval.v_job != NULL)
1273 ++tv->vval.v_job->jv_refcount;
1274#endif
1275 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 default:
1277 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001278 tv->vval.v_string = vim_strsave(
1279 iptr->isn_arg.string == NULL
1280 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 }
1282 break;
1283
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001284 case ISN_UNLET:
1285 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1286 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1287 goto failed;
1288 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001289 case ISN_UNLETENV:
1290 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1291 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 // create a list from items on the stack; uses a single allocation
1294 // for the list header and the items
1295 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001296 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1297 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001298 break;
1299
1300 // create a dict from items on the stack
1301 case ISN_NEWDICT:
1302 {
1303 int count = iptr->isn_arg.number;
1304 dict_T *dict = dict_alloc();
1305 dictitem_T *item;
1306
1307 if (dict == NULL)
1308 goto failed;
1309 for (idx = 0; idx < count; ++idx)
1310 {
1311 // check key type is VAR_STRING
1312 tv = STACK_TV_BOT(2 * (idx - count));
1313 item = dictitem_alloc(tv->vval.v_string);
1314 clear_tv(tv);
1315 if (item == NULL)
1316 goto failed;
1317 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1318 item->di_tv.v_lock = 0;
1319 if (dict_add(dict, item) == FAIL)
1320 goto failed;
1321 }
1322
1323 if (count > 0)
1324 ectx.ec_stack.ga_len -= 2 * count - 1;
1325 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1326 goto failed;
1327 else
1328 ++ectx.ec_stack.ga_len;
1329 tv = STACK_TV_BOT(-1);
1330 tv->v_type = VAR_DICT;
1331 tv->vval.v_dict = dict;
1332 ++dict->dv_refcount;
1333 }
1334 break;
1335
1336 // call a :def function
1337 case ISN_DCALL:
1338 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1339 iptr->isn_arg.dfunc.cdf_argcount,
1340 &ectx) == FAIL)
1341 goto failed;
1342 break;
1343
1344 // call a builtin function
1345 case ISN_BCALL:
1346 SOURCING_LNUM = iptr->isn_lnum;
1347 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1348 iptr->isn_arg.bfunc.cbf_argcount,
1349 &ectx) == FAIL)
1350 goto failed;
1351 break;
1352
1353 // call a funcref or partial
1354 case ISN_PCALL:
1355 {
1356 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1357 int r;
1358 typval_T partial;
1359
1360 SOURCING_LNUM = iptr->isn_lnum;
1361 if (pfunc->cpf_top)
1362 {
1363 // funcref is above the arguments
1364 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1365 }
1366 else
1367 {
1368 // Get the funcref from the stack.
1369 --ectx.ec_stack.ga_len;
1370 partial = *STACK_TV_BOT(0);
1371 tv = &partial;
1372 }
1373 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1374 if (tv == &partial)
1375 clear_tv(&partial);
1376 if (r == FAIL)
1377 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 }
1379 break;
1380
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001381 case ISN_PCALL_END:
1382 // PCALL finished, arguments have been consumed and replaced by
1383 // the return value. Now clear the funcref from the stack,
1384 // and move the return value in its place.
1385 --ectx.ec_stack.ga_len;
1386 clear_tv(STACK_TV_BOT(-1));
1387 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1388 break;
1389
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390 // call a user defined function or funcref/partial
1391 case ISN_UCALL:
1392 {
1393 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1394
1395 SOURCING_LNUM = iptr->isn_lnum;
1396 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001397 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 goto failed;
1399 }
1400 break;
1401
1402 // return from a :def function call
1403 case ISN_RETURN:
1404 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001405 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001406 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001407
1408 if (trystack->ga_len > 0)
1409 trycmd = ((trycmd_T *)trystack->ga_data)
1410 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001411 if (trycmd != NULL
1412 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 && trycmd->tcd_finally_idx != 0)
1414 {
1415 // jump to ":finally"
1416 ectx.ec_iidx = trycmd->tcd_finally_idx;
1417 trycmd->tcd_return = TRUE;
1418 }
1419 else
1420 {
1421 // Restore previous function. If the frame pointer
1422 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001423 if (ectx.ec_frame_idx == initial_frame_idx)
1424 {
1425 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1426 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001428 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001430 if (func_return(&ectx) == FAIL)
1431 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 }
1433 }
1434 break;
1435
1436 // push a function reference to a compiled function
1437 case ISN_FUNCREF:
1438 {
1439 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001440 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441
1442 pt = ALLOC_CLEAR_ONE(partial_T);
1443 if (pt == NULL)
1444 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001445 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001446 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001447 vim_free(pt);
1448 goto failed;
1449 }
1450 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1451 + iptr->isn_arg.funcref.fr_func;
1452 pt->pt_func = pt_dfunc->df_ufunc;
1453 pt->pt_refcount = 1;
1454 ++pt_dfunc->df_ufunc->uf_refcount;
1455
1456 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1457 {
1458 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1459 + ectx.ec_dfunc_idx;
1460
1461 // The closure needs to find arguments and local
1462 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001463 pt->pt_ectx_stack = &ectx.ec_stack;
1464 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001465
1466 // If this function returns and the closure is still
1467 // used, we need to make a copy of the context
1468 // (arguments and local variables). Store a reference
1469 // to the partial so we can handle that.
1470 ++pt->pt_refcount;
1471 tv = STACK_TV_VAR(dfunc->df_varcount
1472 + iptr->isn_arg.funcref.fr_var_idx);
1473 if (tv->v_type == VAR_PARTIAL)
1474 {
1475 // TODO: use a garray_T on ectx.
1476 emsg("Multiple closures not supported yet");
1477 goto failed;
1478 }
1479 tv->v_type = VAR_PARTIAL;
1480 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001481 }
1482
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 tv = STACK_TV_BOT(0);
1484 ++ectx.ec_stack.ga_len;
1485 tv->vval.v_partial = pt;
1486 tv->v_type = VAR_PARTIAL;
1487 }
1488 break;
1489
1490 // jump if a condition is met
1491 case ISN_JUMP:
1492 {
1493 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1494 int jump = TRUE;
1495
1496 if (when != JUMP_ALWAYS)
1497 {
1498 tv = STACK_TV_BOT(-1);
1499 jump = tv2bool(tv);
1500 if (when == JUMP_IF_FALSE
1501 || when == JUMP_AND_KEEP_IF_FALSE)
1502 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001503 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 {
1505 // drop the value from the stack
1506 clear_tv(tv);
1507 --ectx.ec_stack.ga_len;
1508 }
1509 }
1510 if (jump)
1511 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1512 }
1513 break;
1514
1515 // top of a for loop
1516 case ISN_FOR:
1517 {
1518 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1519 typval_T *idxtv =
1520 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1521
1522 // push the next item from the list
1523 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1524 goto failed;
1525 if (++idxtv->vval.v_number >= list->lv_len)
1526 // past the end of the list, jump to "endfor"
1527 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1528 else if (list->lv_first == &range_list_item)
1529 {
1530 // non-materialized range() list
1531 tv = STACK_TV_BOT(0);
1532 tv->v_type = VAR_NUMBER;
1533 tv->vval.v_number = list_find_nr(
1534 list, idxtv->vval.v_number, NULL);
1535 ++ectx.ec_stack.ga_len;
1536 }
1537 else
1538 {
1539 listitem_T *li = list_find(list, idxtv->vval.v_number);
1540
1541 if (li == NULL)
1542 goto failed;
1543 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1544 ++ectx.ec_stack.ga_len;
1545 }
1546 }
1547 break;
1548
1549 // start of ":try" block
1550 case ISN_TRY:
1551 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001552 trycmd_T *trycmd = NULL;
1553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1555 goto failed;
1556 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1557 + ectx.ec_trystack.ga_len;
1558 ++ectx.ec_trystack.ga_len;
1559 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001560 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1562 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001563 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 }
1565 break;
1566
1567 case ISN_PUSHEXC:
1568 if (current_exception == NULL)
1569 {
1570 iemsg("Evaluating catch while current_exception is NULL");
1571 goto failed;
1572 }
1573 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1574 goto failed;
1575 tv = STACK_TV_BOT(0);
1576 ++ectx.ec_stack.ga_len;
1577 tv->v_type = VAR_STRING;
1578 tv->vval.v_string = vim_strsave(
1579 (char_u *)current_exception->value);
1580 break;
1581
1582 case ISN_CATCH:
1583 {
1584 garray_T *trystack = &ectx.ec_trystack;
1585
1586 if (trystack->ga_len > 0)
1587 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001588 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 + trystack->ga_len - 1;
1590 trycmd->tcd_caught = TRUE;
1591 }
1592 did_emsg = got_int = did_throw = FALSE;
1593 catch_exception(current_exception);
1594 }
1595 break;
1596
1597 // end of ":try" block
1598 case ISN_ENDTRY:
1599 {
1600 garray_T *trystack = &ectx.ec_trystack;
1601
1602 if (trystack->ga_len > 0)
1603 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001604 trycmd_T *trycmd = NULL;
1605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 --trystack->ga_len;
1607 --trylevel;
1608 trycmd = ((trycmd_T *)trystack->ga_data)
1609 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001610 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 {
1612 // discard the exception
1613 if (caught_stack == current_exception)
1614 caught_stack = caught_stack->caught;
1615 discard_current_exception();
1616 }
1617
1618 if (trycmd->tcd_return)
1619 {
1620 // Restore previous function. If the frame pointer
1621 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001622 if (ectx.ec_frame_idx == initial_frame_idx)
1623 {
1624 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1625 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001628
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001629 if (func_return(&ectx) == FAIL)
1630 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001631 }
1632 }
1633 }
1634 break;
1635
1636 case ISN_THROW:
1637 --ectx.ec_stack.ga_len;
1638 tv = STACK_TV_BOT(0);
1639 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1640 {
1641 vim_free(tv->vval.v_string);
1642 goto failed;
1643 }
1644 did_throw = TRUE;
1645 break;
1646
1647 // compare with special values
1648 case ISN_COMPAREBOOL:
1649 case ISN_COMPARESPECIAL:
1650 {
1651 typval_T *tv1 = STACK_TV_BOT(-2);
1652 typval_T *tv2 = STACK_TV_BOT(-1);
1653 varnumber_T arg1 = tv1->vval.v_number;
1654 varnumber_T arg2 = tv2->vval.v_number;
1655 int res;
1656
1657 switch (iptr->isn_arg.op.op_type)
1658 {
1659 case EXPR_EQUAL: res = arg1 == arg2; break;
1660 case EXPR_NEQUAL: res = arg1 != arg2; break;
1661 default: res = 0; break;
1662 }
1663
1664 --ectx.ec_stack.ga_len;
1665 tv1->v_type = VAR_BOOL;
1666 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1667 }
1668 break;
1669
1670 // Operation with two number arguments
1671 case ISN_OPNR:
1672 case ISN_COMPARENR:
1673 {
1674 typval_T *tv1 = STACK_TV_BOT(-2);
1675 typval_T *tv2 = STACK_TV_BOT(-1);
1676 varnumber_T arg1 = tv1->vval.v_number;
1677 varnumber_T arg2 = tv2->vval.v_number;
1678 varnumber_T res;
1679
1680 switch (iptr->isn_arg.op.op_type)
1681 {
1682 case EXPR_MULT: res = arg1 * arg2; break;
1683 case EXPR_DIV: res = arg1 / arg2; break;
1684 case EXPR_REM: res = arg1 % arg2; break;
1685 case EXPR_SUB: res = arg1 - arg2; break;
1686 case EXPR_ADD: res = arg1 + arg2; break;
1687
1688 case EXPR_EQUAL: res = arg1 == arg2; break;
1689 case EXPR_NEQUAL: res = arg1 != arg2; break;
1690 case EXPR_GREATER: res = arg1 > arg2; break;
1691 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1692 case EXPR_SMALLER: res = arg1 < arg2; break;
1693 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1694 default: res = 0; break;
1695 }
1696
1697 --ectx.ec_stack.ga_len;
1698 if (iptr->isn_type == ISN_COMPARENR)
1699 {
1700 tv1->v_type = VAR_BOOL;
1701 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1702 }
1703 else
1704 tv1->vval.v_number = res;
1705 }
1706 break;
1707
1708 // Computation with two float arguments
1709 case ISN_OPFLOAT:
1710 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001711#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 {
1713 typval_T *tv1 = STACK_TV_BOT(-2);
1714 typval_T *tv2 = STACK_TV_BOT(-1);
1715 float_T arg1 = tv1->vval.v_float;
1716 float_T arg2 = tv2->vval.v_float;
1717 float_T res = 0;
1718 int cmp = FALSE;
1719
1720 switch (iptr->isn_arg.op.op_type)
1721 {
1722 case EXPR_MULT: res = arg1 * arg2; break;
1723 case EXPR_DIV: res = arg1 / arg2; break;
1724 case EXPR_SUB: res = arg1 - arg2; break;
1725 case EXPR_ADD: res = arg1 + arg2; break;
1726
1727 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1728 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1729 case EXPR_GREATER: cmp = arg1 > arg2; break;
1730 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1731 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1732 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1733 default: cmp = 0; break;
1734 }
1735 --ectx.ec_stack.ga_len;
1736 if (iptr->isn_type == ISN_COMPAREFLOAT)
1737 {
1738 tv1->v_type = VAR_BOOL;
1739 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1740 }
1741 else
1742 tv1->vval.v_float = res;
1743 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001744#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 break;
1746
1747 case ISN_COMPARELIST:
1748 {
1749 typval_T *tv1 = STACK_TV_BOT(-2);
1750 typval_T *tv2 = STACK_TV_BOT(-1);
1751 list_T *arg1 = tv1->vval.v_list;
1752 list_T *arg2 = tv2->vval.v_list;
1753 int cmp = FALSE;
1754 int ic = iptr->isn_arg.op.op_ic;
1755
1756 switch (iptr->isn_arg.op.op_type)
1757 {
1758 case EXPR_EQUAL: cmp =
1759 list_equal(arg1, arg2, ic, FALSE); break;
1760 case EXPR_NEQUAL: cmp =
1761 !list_equal(arg1, arg2, ic, FALSE); break;
1762 case EXPR_IS: cmp = arg1 == arg2; break;
1763 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1764 default: cmp = 0; break;
1765 }
1766 --ectx.ec_stack.ga_len;
1767 clear_tv(tv1);
1768 clear_tv(tv2);
1769 tv1->v_type = VAR_BOOL;
1770 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1771 }
1772 break;
1773
1774 case ISN_COMPAREBLOB:
1775 {
1776 typval_T *tv1 = STACK_TV_BOT(-2);
1777 typval_T *tv2 = STACK_TV_BOT(-1);
1778 blob_T *arg1 = tv1->vval.v_blob;
1779 blob_T *arg2 = tv2->vval.v_blob;
1780 int cmp = FALSE;
1781
1782 switch (iptr->isn_arg.op.op_type)
1783 {
1784 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1785 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1786 case EXPR_IS: cmp = arg1 == arg2; break;
1787 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1788 default: cmp = 0; break;
1789 }
1790 --ectx.ec_stack.ga_len;
1791 clear_tv(tv1);
1792 clear_tv(tv2);
1793 tv1->v_type = VAR_BOOL;
1794 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1795 }
1796 break;
1797
1798 // TODO: handle separately
1799 case ISN_COMPARESTRING:
1800 case ISN_COMPAREDICT:
1801 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 case ISN_COMPAREANY:
1803 {
1804 typval_T *tv1 = STACK_TV_BOT(-2);
1805 typval_T *tv2 = STACK_TV_BOT(-1);
1806 exptype_T exptype = iptr->isn_arg.op.op_type;
1807 int ic = iptr->isn_arg.op.op_ic;
1808
1809 typval_compare(tv1, tv2, exptype, ic);
1810 clear_tv(tv2);
1811 tv1->v_type = VAR_BOOL;
1812 tv1->vval.v_number = tv1->vval.v_number
1813 ? VVAL_TRUE : VVAL_FALSE;
1814 --ectx.ec_stack.ga_len;
1815 }
1816 break;
1817
1818 case ISN_ADDLIST:
1819 case ISN_ADDBLOB:
1820 {
1821 typval_T *tv1 = STACK_TV_BOT(-2);
1822 typval_T *tv2 = STACK_TV_BOT(-1);
1823
1824 if (iptr->isn_type == ISN_ADDLIST)
1825 eval_addlist(tv1, tv2);
1826 else
1827 eval_addblob(tv1, tv2);
1828 clear_tv(tv2);
1829 --ectx.ec_stack.ga_len;
1830 }
1831 break;
1832
1833 // Computation with two arguments of unknown type
1834 case ISN_OPANY:
1835 {
1836 typval_T *tv1 = STACK_TV_BOT(-2);
1837 typval_T *tv2 = STACK_TV_BOT(-1);
1838 varnumber_T n1, n2;
1839#ifdef FEAT_FLOAT
1840 float_T f1 = 0, f2 = 0;
1841#endif
1842 int error = FALSE;
1843
1844 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1845 {
1846 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1847 {
1848 eval_addlist(tv1, tv2);
1849 clear_tv(tv2);
1850 --ectx.ec_stack.ga_len;
1851 break;
1852 }
1853 else if (tv1->v_type == VAR_BLOB
1854 && tv2->v_type == VAR_BLOB)
1855 {
1856 eval_addblob(tv1, tv2);
1857 clear_tv(tv2);
1858 --ectx.ec_stack.ga_len;
1859 break;
1860 }
1861 }
1862#ifdef FEAT_FLOAT
1863 if (tv1->v_type == VAR_FLOAT)
1864 {
1865 f1 = tv1->vval.v_float;
1866 n1 = 0;
1867 }
1868 else
1869#endif
1870 {
1871 n1 = tv_get_number_chk(tv1, &error);
1872 if (error)
1873 goto failed;
1874#ifdef FEAT_FLOAT
1875 if (tv2->v_type == VAR_FLOAT)
1876 f1 = n1;
1877#endif
1878 }
1879#ifdef FEAT_FLOAT
1880 if (tv2->v_type == VAR_FLOAT)
1881 {
1882 f2 = tv2->vval.v_float;
1883 n2 = 0;
1884 }
1885 else
1886#endif
1887 {
1888 n2 = tv_get_number_chk(tv2, &error);
1889 if (error)
1890 goto failed;
1891#ifdef FEAT_FLOAT
1892 if (tv1->v_type == VAR_FLOAT)
1893 f2 = n2;
1894#endif
1895 }
1896#ifdef FEAT_FLOAT
1897 // if there is a float on either side the result is a float
1898 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1899 {
1900 switch (iptr->isn_arg.op.op_type)
1901 {
1902 case EXPR_MULT: f1 = f1 * f2; break;
1903 case EXPR_DIV: f1 = f1 / f2; break;
1904 case EXPR_SUB: f1 = f1 - f2; break;
1905 case EXPR_ADD: f1 = f1 + f2; break;
1906 default: emsg(_(e_modulus)); goto failed;
1907 }
1908 clear_tv(tv1);
1909 clear_tv(tv2);
1910 tv1->v_type = VAR_FLOAT;
1911 tv1->vval.v_float = f1;
1912 --ectx.ec_stack.ga_len;
1913 }
1914 else
1915#endif
1916 {
1917 switch (iptr->isn_arg.op.op_type)
1918 {
1919 case EXPR_MULT: n1 = n1 * n2; break;
1920 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1921 case EXPR_SUB: n1 = n1 - n2; break;
1922 case EXPR_ADD: n1 = n1 + n2; break;
1923 default: n1 = num_modulus(n1, n2); break;
1924 }
1925 clear_tv(tv1);
1926 clear_tv(tv2);
1927 tv1->v_type = VAR_NUMBER;
1928 tv1->vval.v_number = n1;
1929 --ectx.ec_stack.ga_len;
1930 }
1931 }
1932 break;
1933
1934 case ISN_CONCAT:
1935 {
1936 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1937 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1938 char_u *res;
1939
1940 res = concat_str(str1, str2);
1941 clear_tv(STACK_TV_BOT(-2));
1942 clear_tv(STACK_TV_BOT(-1));
1943 --ectx.ec_stack.ga_len;
1944 STACK_TV_BOT(-1)->vval.v_string = res;
1945 }
1946 break;
1947
1948 case ISN_INDEX:
1949 {
1950 list_T *list;
1951 varnumber_T n;
1952 listitem_T *li;
1953
1954 // list index: list is at stack-2, index at stack-1
1955 tv = STACK_TV_BOT(-2);
1956 if (tv->v_type != VAR_LIST)
1957 {
1958 emsg(_(e_listreq));
1959 goto failed;
1960 }
1961 list = tv->vval.v_list;
1962
1963 tv = STACK_TV_BOT(-1);
1964 if (tv->v_type != VAR_NUMBER)
1965 {
1966 emsg(_(e_number_exp));
1967 goto failed;
1968 }
1969 n = tv->vval.v_number;
1970 clear_tv(tv);
1971 if ((li = list_find(list, n)) == NULL)
1972 {
1973 semsg(_(e_listidx), n);
1974 goto failed;
1975 }
1976 --ectx.ec_stack.ga_len;
1977 clear_tv(STACK_TV_BOT(-1));
1978 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1979 }
1980 break;
1981
1982 // dict member with string key
1983 case ISN_MEMBER:
1984 {
1985 dict_T *dict;
1986 dictitem_T *di;
1987
1988 tv = STACK_TV_BOT(-1);
1989 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1990 {
1991 emsg(_(e_dictreq));
1992 goto failed;
1993 }
1994 dict = tv->vval.v_dict;
1995
1996 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1997 == NULL)
1998 {
1999 semsg(_(e_dictkey), iptr->isn_arg.string);
2000 goto failed;
2001 }
2002 clear_tv(tv);
2003 copy_tv(&di->di_tv, tv);
2004 }
2005 break;
2006
2007 case ISN_NEGATENR:
2008 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002009 if (tv->v_type != VAR_NUMBER
2010#ifdef FEAT_FLOAT
2011 && tv->v_type != VAR_FLOAT
2012#endif
2013 )
2014 {
2015 emsg(_(e_number_exp));
2016 goto failed;
2017 }
2018#ifdef FEAT_FLOAT
2019 if (tv->v_type == VAR_FLOAT)
2020 tv->vval.v_float = -tv->vval.v_float;
2021 else
2022#endif
2023 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 break;
2025
2026 case ISN_CHECKNR:
2027 {
2028 int error = FALSE;
2029
2030 tv = STACK_TV_BOT(-1);
2031 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 (void)tv_get_number_chk(tv, &error);
2034 if (error)
2035 goto failed;
2036 }
2037 break;
2038
2039 case ISN_CHECKTYPE:
2040 {
2041 checktype_T *ct = &iptr->isn_arg.type;
2042
2043 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002044 // TODO: better type comparison
2045 if (tv->v_type != ct->ct_type
2046 && !((tv->v_type == VAR_PARTIAL
2047 && ct->ct_type == VAR_FUNC)
2048 || (tv->v_type == VAR_FUNC
2049 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 {
2051 semsg(_("E1029: Expected %s but got %s"),
2052 vartype_name(ct->ct_type),
2053 vartype_name(tv->v_type));
2054 goto failed;
2055 }
2056 }
2057 break;
2058
2059 case ISN_2BOOL:
2060 {
2061 int n;
2062
2063 tv = STACK_TV_BOT(-1);
2064 n = tv2bool(tv);
2065 if (iptr->isn_arg.number) // invert
2066 n = !n;
2067 clear_tv(tv);
2068 tv->v_type = VAR_BOOL;
2069 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2070 }
2071 break;
2072
2073 case ISN_2STRING:
2074 {
2075 char_u *str;
2076
2077 tv = STACK_TV_BOT(iptr->isn_arg.number);
2078 if (tv->v_type != VAR_STRING)
2079 {
2080 str = typval_tostring(tv);
2081 clear_tv(tv);
2082 tv->v_type = VAR_STRING;
2083 tv->vval.v_string = str;
2084 }
2085 }
2086 break;
2087
2088 case ISN_DROP:
2089 --ectx.ec_stack.ga_len;
2090 clear_tv(STACK_TV_BOT(0));
2091 break;
2092 }
2093 }
2094
2095done:
2096 // function finished, get result from the stack.
2097 tv = STACK_TV_BOT(-1);
2098 *rettv = *tv;
2099 tv->v_type = VAR_UNKNOWN;
2100 ret = OK;
2101
2102failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002103 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002104 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002105 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002106failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002107 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002108
2109 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2111 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002114 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 return ret;
2116}
2117
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118/*
2119 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002120 * We don't really need this at runtime, but we do have tests that require it,
2121 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002122 */
2123 void
2124ex_disassemble(exarg_T *eap)
2125{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002126 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002127 char_u *fname;
2128 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 dfunc_T *dfunc;
2130 isn_T *instr;
2131 int current;
2132 int line_idx = 0;
2133 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002134 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002136 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002137 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002138 if (fname == NULL)
2139 {
2140 semsg(_(e_invarg2), eap->arg);
2141 return;
2142 }
2143
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002144 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002145 if (ufunc == NULL)
2146 {
2147 char_u *p = untrans_function_name(fname);
2148
2149 if (p != NULL)
2150 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002151 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002152 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002153 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 if (ufunc == NULL)
2155 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002156 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002157 return;
2158 }
2159 if (ufunc->uf_dfunc_idx < 0)
2160 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002161 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162 return;
2163 }
2164 if (ufunc->uf_name_exp != NULL)
2165 msg((char *)ufunc->uf_name_exp);
2166 else
2167 msg((char *)ufunc->uf_name);
2168
2169 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2170 instr = dfunc->df_instr;
2171 for (current = 0; current < dfunc->df_instr_count; ++current)
2172 {
2173 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002174 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002175
2176 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2177 {
2178 if (current > prev_current)
2179 {
2180 msg_puts("\n\n");
2181 prev_current = current;
2182 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002183 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2184 if (line != NULL)
2185 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 }
2187
2188 switch (iptr->isn_type)
2189 {
2190 case ISN_EXEC:
2191 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2192 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002193 case ISN_EXECCONCAT:
2194 smsg("%4d EXECCONCAT %lld", current,
2195 (long long)iptr->isn_arg.number);
2196 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 case ISN_ECHO:
2198 {
2199 echo_T *echo = &iptr->isn_arg.echo;
2200
2201 smsg("%4d %s %d", current,
2202 echo->echo_with_white ? "ECHO" : "ECHON",
2203 echo->echo_count);
2204 }
2205 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002206 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002207 smsg("%4d EXECUTE %lld", current,
2208 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002209 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002210 case ISN_ECHOMSG:
2211 smsg("%4d ECHOMSG %lld", current,
2212 (long long)(iptr->isn_arg.number));
2213 break;
2214 case ISN_ECHOERR:
2215 smsg("%4d ECHOERR %lld", current,
2216 (long long)(iptr->isn_arg.number));
2217 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002219 case ISN_LOADOUTER:
2220 {
2221 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2222
2223 if (iptr->isn_arg.number < 0)
2224 smsg("%4d LOAD%s arg[%lld]", current, add,
2225 (long long)(iptr->isn_arg.number
2226 + STACK_FRAME_SIZE));
2227 else
2228 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002229 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002230 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 break;
2232 case ISN_LOADV:
2233 smsg("%4d LOADV v:%s", current,
2234 get_vim_var_name(iptr->isn_arg.number));
2235 break;
2236 case ISN_LOADSCRIPT:
2237 {
2238 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002239 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2241 + iptr->isn_arg.script.script_idx;
2242
2243 smsg("%4d LOADSCRIPT %s from %s", current,
2244 sv->sv_name, si->sn_name);
2245 }
2246 break;
2247 case ISN_LOADS:
2248 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002249 scriptitem_T *si = SCRIPT_ITEM(
2250 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251
2252 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002253 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002254 }
2255 break;
2256 case ISN_LOADG:
2257 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2258 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002259 case ISN_LOADB:
2260 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2261 break;
2262 case ISN_LOADW:
2263 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2264 break;
2265 case ISN_LOADT:
2266 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2267 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 case ISN_LOADOPT:
2269 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2270 break;
2271 case ISN_LOADENV:
2272 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2273 break;
2274 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002275 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276 break;
2277
2278 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002279 if (iptr->isn_arg.number < 0)
2280 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002281 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002282 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002283 smsg("%4d STORE $%lld", current,
2284 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002286 case ISN_STOREV:
2287 smsg("%4d STOREV v:%s", current,
2288 get_vim_var_name(iptr->isn_arg.number));
2289 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002291 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2292 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002293 case ISN_STOREB:
2294 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2295 break;
2296 case ISN_STOREW:
2297 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2298 break;
2299 case ISN_STORET:
2300 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2301 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002302 case ISN_STORES:
2303 {
2304 scriptitem_T *si = SCRIPT_ITEM(
2305 iptr->isn_arg.loadstore.ls_sid);
2306
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002307 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002308 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002309 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 break;
2311 case ISN_STORESCRIPT:
2312 {
2313 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002314 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2316 + iptr->isn_arg.script.script_idx;
2317
2318 smsg("%4d STORESCRIPT %s in %s", current,
2319 sv->sv_name, si->sn_name);
2320 }
2321 break;
2322 case ISN_STOREOPT:
2323 smsg("%4d STOREOPT &%s", current,
2324 iptr->isn_arg.storeopt.so_name);
2325 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002326 case ISN_STOREENV:
2327 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2328 break;
2329 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002330 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002331 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 case ISN_STORENR:
2333 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002334 iptr->isn_arg.storenr.stnr_val,
2335 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 break;
2337
2338 // constants
2339 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002340 smsg("%4d PUSHNR %lld", current,
2341 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 break;
2343 case ISN_PUSHBOOL:
2344 case ISN_PUSHSPEC:
2345 smsg("%4d PUSH %s", current,
2346 get_var_special_name(iptr->isn_arg.number));
2347 break;
2348 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002349#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002351#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 break;
2353 case ISN_PUSHS:
2354 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2355 break;
2356 case ISN_PUSHBLOB:
2357 {
2358 char_u *r;
2359 char_u numbuf[NUMBUFLEN];
2360 char_u *tofree;
2361
2362 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002363 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 vim_free(tofree);
2365 }
2366 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002367 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002368 {
2369 char *name = (char *)iptr->isn_arg.string;
2370
2371 smsg("%4d PUSHFUNC \"%s\"", current,
2372 name == NULL ? "[none]" : name);
2373 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002374 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002375 case ISN_PUSHCHANNEL:
2376#ifdef FEAT_JOB_CHANNEL
2377 {
2378 channel_T *channel = iptr->isn_arg.channel;
2379
2380 smsg("%4d PUSHCHANNEL %d", current,
2381 channel == NULL ? 0 : channel->ch_id);
2382 }
2383#endif
2384 break;
2385 case ISN_PUSHJOB:
2386#ifdef FEAT_JOB_CHANNEL
2387 {
2388 typval_T tv;
2389 char_u *name;
2390
2391 tv.v_type = VAR_JOB;
2392 tv.vval.v_job = iptr->isn_arg.job;
2393 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002394 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002395 }
2396#endif
2397 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 case ISN_PUSHEXC:
2399 smsg("%4d PUSH v:exception", current);
2400 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002401 case ISN_UNLET:
2402 smsg("%4d UNLET%s %s", current,
2403 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2404 iptr->isn_arg.unlet.ul_name);
2405 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002406 case ISN_UNLETENV:
2407 smsg("%4d UNLETENV%s $%s", current,
2408 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2409 iptr->isn_arg.unlet.ul_name);
2410 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002412 smsg("%4d NEWLIST size %lld", current,
2413 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 break;
2415 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002416 smsg("%4d NEWDICT size %lld", current,
2417 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 break;
2419
2420 // function call
2421 case ISN_BCALL:
2422 {
2423 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2424
2425 smsg("%4d BCALL %s(argc %d)", current,
2426 internal_func_name(cbfunc->cbf_idx),
2427 cbfunc->cbf_argcount);
2428 }
2429 break;
2430 case ISN_DCALL:
2431 {
2432 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2433 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2434 + cdfunc->cdf_idx;
2435
2436 smsg("%4d DCALL %s(argc %d)", current,
2437 df->df_ufunc->uf_name_exp != NULL
2438 ? df->df_ufunc->uf_name_exp
2439 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2440 }
2441 break;
2442 case ISN_UCALL:
2443 {
2444 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2445
2446 smsg("%4d UCALL %s(argc %d)", current,
2447 cufunc->cuf_name, cufunc->cuf_argcount);
2448 }
2449 break;
2450 case ISN_PCALL:
2451 {
2452 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2453
2454 smsg("%4d PCALL%s (argc %d)", current,
2455 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2456 }
2457 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002458 case ISN_PCALL_END:
2459 smsg("%4d PCALL end", current);
2460 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 case ISN_RETURN:
2462 smsg("%4d RETURN", current);
2463 break;
2464 case ISN_FUNCREF:
2465 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002466 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002468 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002469
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002470 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002471 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 }
2473 break;
2474
2475 case ISN_JUMP:
2476 {
2477 char *when = "?";
2478
2479 switch (iptr->isn_arg.jump.jump_when)
2480 {
2481 case JUMP_ALWAYS:
2482 when = "JUMP";
2483 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 case JUMP_AND_KEEP_IF_TRUE:
2485 when = "JUMP_AND_KEEP_IF_TRUE";
2486 break;
2487 case JUMP_IF_FALSE:
2488 when = "JUMP_IF_FALSE";
2489 break;
2490 case JUMP_AND_KEEP_IF_FALSE:
2491 when = "JUMP_AND_KEEP_IF_FALSE";
2492 break;
2493 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002494 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 iptr->isn_arg.jump.jump_where);
2496 }
2497 break;
2498
2499 case ISN_FOR:
2500 {
2501 forloop_T *forloop = &iptr->isn_arg.forloop;
2502
2503 smsg("%4d FOR $%d -> %d", current,
2504 forloop->for_idx, forloop->for_end);
2505 }
2506 break;
2507
2508 case ISN_TRY:
2509 {
2510 try_T *try = &iptr->isn_arg.try;
2511
2512 smsg("%4d TRY catch -> %d, finally -> %d", current,
2513 try->try_catch, try->try_finally);
2514 }
2515 break;
2516 case ISN_CATCH:
2517 // TODO
2518 smsg("%4d CATCH", current);
2519 break;
2520 case ISN_ENDTRY:
2521 smsg("%4d ENDTRY", current);
2522 break;
2523 case ISN_THROW:
2524 smsg("%4d THROW", current);
2525 break;
2526
2527 // expression operations on number
2528 case ISN_OPNR:
2529 case ISN_OPFLOAT:
2530 case ISN_OPANY:
2531 {
2532 char *what;
2533 char *ins;
2534
2535 switch (iptr->isn_arg.op.op_type)
2536 {
2537 case EXPR_MULT: what = "*"; break;
2538 case EXPR_DIV: what = "/"; break;
2539 case EXPR_REM: what = "%"; break;
2540 case EXPR_SUB: what = "-"; break;
2541 case EXPR_ADD: what = "+"; break;
2542 default: what = "???"; break;
2543 }
2544 switch (iptr->isn_type)
2545 {
2546 case ISN_OPNR: ins = "OPNR"; break;
2547 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2548 case ISN_OPANY: ins = "OPANY"; break;
2549 default: ins = "???"; break;
2550 }
2551 smsg("%4d %s %s", current, ins, what);
2552 }
2553 break;
2554
2555 case ISN_COMPAREBOOL:
2556 case ISN_COMPARESPECIAL:
2557 case ISN_COMPARENR:
2558 case ISN_COMPAREFLOAT:
2559 case ISN_COMPARESTRING:
2560 case ISN_COMPAREBLOB:
2561 case ISN_COMPARELIST:
2562 case ISN_COMPAREDICT:
2563 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 case ISN_COMPAREANY:
2565 {
2566 char *p;
2567 char buf[10];
2568 char *type;
2569
2570 switch (iptr->isn_arg.op.op_type)
2571 {
2572 case EXPR_EQUAL: p = "=="; break;
2573 case EXPR_NEQUAL: p = "!="; break;
2574 case EXPR_GREATER: p = ">"; break;
2575 case EXPR_GEQUAL: p = ">="; break;
2576 case EXPR_SMALLER: p = "<"; break;
2577 case EXPR_SEQUAL: p = "<="; break;
2578 case EXPR_MATCH: p = "=~"; break;
2579 case EXPR_IS: p = "is"; break;
2580 case EXPR_ISNOT: p = "isnot"; break;
2581 case EXPR_NOMATCH: p = "!~"; break;
2582 default: p = "???"; break;
2583 }
2584 STRCPY(buf, p);
2585 if (iptr->isn_arg.op.op_ic == TRUE)
2586 strcat(buf, "?");
2587 switch(iptr->isn_type)
2588 {
2589 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2590 case ISN_COMPARESPECIAL:
2591 type = "COMPARESPECIAL"; break;
2592 case ISN_COMPARENR: type = "COMPARENR"; break;
2593 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2594 case ISN_COMPARESTRING:
2595 type = "COMPARESTRING"; break;
2596 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2597 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2598 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2599 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2601 default: type = "???"; break;
2602 }
2603
2604 smsg("%4d %s %s", current, type, buf);
2605 }
2606 break;
2607
2608 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2609 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2610
2611 // expression operations
2612 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2613 case ISN_INDEX: smsg("%4d INDEX", current); break;
2614 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2615 iptr->isn_arg.string); break;
2616 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2617
2618 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2619 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2620 vartype_name(iptr->isn_arg.type.ct_type),
2621 iptr->isn_arg.type.ct_off);
2622 break;
2623 case ISN_2BOOL: if (iptr->isn_arg.number)
2624 smsg("%4d INVERT (!val)", current);
2625 else
2626 smsg("%4d 2BOOL (!!val)", current);
2627 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002628 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2629 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 break;
2631
2632 case ISN_DROP: smsg("%4d DROP", current); break;
2633 }
2634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635}
2636
2637/*
2638 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2639 * list, etc. Mostly like what JavaScript does, except that empty list and
2640 * empty dictionary are FALSE.
2641 */
2642 int
2643tv2bool(typval_T *tv)
2644{
2645 switch (tv->v_type)
2646 {
2647 case VAR_NUMBER:
2648 return tv->vval.v_number != 0;
2649 case VAR_FLOAT:
2650#ifdef FEAT_FLOAT
2651 return tv->vval.v_float != 0.0;
2652#else
2653 break;
2654#endif
2655 case VAR_PARTIAL:
2656 return tv->vval.v_partial != NULL;
2657 case VAR_FUNC:
2658 case VAR_STRING:
2659 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2660 case VAR_LIST:
2661 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2662 case VAR_DICT:
2663 return tv->vval.v_dict != NULL
2664 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2665 case VAR_BOOL:
2666 case VAR_SPECIAL:
2667 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2668 case VAR_JOB:
2669#ifdef FEAT_JOB_CHANNEL
2670 return tv->vval.v_job != NULL;
2671#else
2672 break;
2673#endif
2674 case VAR_CHANNEL:
2675#ifdef FEAT_JOB_CHANNEL
2676 return tv->vval.v_channel != NULL;
2677#else
2678 break;
2679#endif
2680 case VAR_BLOB:
2681 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2682 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002683 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 case VAR_VOID:
2685 break;
2686 }
2687 return FALSE;
2688}
2689
2690/*
2691 * If "tv" is a string give an error and return FAIL.
2692 */
2693 int
2694check_not_string(typval_T *tv)
2695{
2696 if (tv->v_type == VAR_STRING)
2697 {
2698 emsg(_("E1030: Using a String as a Number"));
2699 clear_tv(tv);
2700 return FAIL;
2701 }
2702 return OK;
2703}
2704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705
2706#endif // FEAT_EVAL