blob: c27bcf0ab26ba5ca5daf60dda64b4853dd4df78b [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
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200235 // used for closures
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200236 ectx->ec_outer_stack = dfunc->df_ectx_stack;
237 ectx->ec_outer_frame = dfunc->df_ectx_frame;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200238
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100239 // Decide where to start execution, handles optional arguments.
240 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
242 return OK;
243}
244
245// Get pointer to item in the stack.
246#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
247
248/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200249 * Used when returning from a function: Check if any closure is still
250 * referenced. If so then move the arguments and variables to a separate piece
251 * of stack to be used when the closure is called.
252 * When "free_arguments" is TRUE the arguments are to be freed.
253 * Returns FAIL when out of memory.
254 */
255 static int
256handle_closure_in_use(ectx_T *ectx, int free_arguments)
257{
258 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
259 + ectx->ec_dfunc_idx;
260 int argcount = ufunc_argcount(dfunc->df_ufunc);
261 int top = ectx->ec_frame_idx - argcount;
262 int idx;
263 typval_T *tv;
264 int closure_in_use = FALSE;
265
266 // Check if any created closure is still in use.
267 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
268 {
269 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
270 + dfunc->df_varcount + idx);
271 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial->pt_refcount > 1)
272 closure_in_use = TRUE;
273 }
274
275 if (closure_in_use)
276 {
277 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
278 typval_T *stack;
279
280 // A closure is using the arguments and/or local variables.
281 // Move them to the called function.
282 if (funcstack == NULL)
283 return FAIL;
284 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
285 + dfunc->df_varcount;
286 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
287 funcstack->fs_ga.ga_data = stack;
288 if (stack == NULL)
289 {
290 vim_free(funcstack);
291 return FAIL;
292 }
293
294 // Move or copy the arguments.
295 for (idx = 0; idx < argcount; ++idx)
296 {
297 tv = STACK_TV(top + idx);
298 if (free_arguments)
299 {
300 *(stack + idx) = *tv;
301 tv->v_type = VAR_UNKNOWN;
302 }
303 else
304 copy_tv(tv, stack + idx);
305 }
306 // Move the local variables.
307 for (idx = 0; idx < dfunc->df_varcount; ++idx)
308 {
309 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
310 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
311 tv->v_type = VAR_UNKNOWN;
312 }
313
314 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
315 {
316 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
317 + dfunc->df_varcount + idx);
318 if (tv->v_type == VAR_PARTIAL
319 && tv->vval.v_partial->pt_refcount > 1)
320 {
321 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
322 + tv->vval.v_partial->pt_func->uf_dfunc_idx;
323 ++funcstack->fs_refcount;
324 pt_dfunc->df_funcstack = funcstack;
325 pt_dfunc->df_ectx_stack = &funcstack->fs_ga;
326 pt_dfunc->df_ectx_frame = ectx->ec_frame_idx - top;
327 }
328 }
329 }
330
331 return OK;
332}
333
334/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100335 * Return from the current function.
336 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200337 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100338func_return(ectx_T *ectx)
339{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200341 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
342 + ectx->ec_dfunc_idx;
343 int argcount = ufunc_argcount(dfunc->df_ufunc);
344 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100345
346 // execution context goes one level up
347 estack_pop();
348
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200349 if (handle_closure_in_use(ectx, TRUE) == FAIL)
350 return FAIL;
351
352 // Clear the arguments.
353 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
354 clear_tv(STACK_TV(idx));
355
356 // Clear local variables and temp values, but not the return value.
357 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100358 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100359 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100360
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100361 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200362 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
363 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
364 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
366 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100367
368 // Reset the stack to the position before the call, move the return value
369 // to the top of the stack.
370 idx = ectx->ec_stack.ga_len - 1;
371 ectx->ec_stack.ga_len = top + 1;
372 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200373
374 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100375}
376
377#undef STACK_TV
378
379/*
380 * Prepare arguments and rettv for calling a builtin or user function.
381 */
382 static int
383call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
384{
385 int idx;
386 typval_T *tv;
387
388 // Move arguments from bottom of the stack to argvars[] and add terminator.
389 for (idx = 0; idx < argcount; ++idx)
390 argvars[idx] = *STACK_TV_BOT(idx - argcount);
391 argvars[argcount].v_type = VAR_UNKNOWN;
392
393 // Result replaces the arguments on the stack.
394 if (argcount > 0)
395 ectx->ec_stack.ga_len -= argcount - 1;
396 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
397 return FAIL;
398 else
399 ++ectx->ec_stack.ga_len;
400
401 // Default return value is zero.
402 tv = STACK_TV_BOT(-1);
403 tv->v_type = VAR_NUMBER;
404 tv->vval.v_number = 0;
405
406 return OK;
407}
408
409/*
410 * Call a builtin function by index.
411 */
412 static int
413call_bfunc(int func_idx, int argcount, ectx_T *ectx)
414{
415 typval_T argvars[MAX_FUNC_ARGS];
416 int idx;
417
418 if (call_prepare(argcount, argvars, ectx) == FAIL)
419 return FAIL;
420
421 // Call the builtin function.
422 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
423
424 // Clear the arguments.
425 for (idx = 0; idx < argcount; ++idx)
426 clear_tv(&argvars[idx]);
427 return OK;
428}
429
430/*
431 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100432 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100433 */
434 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100435call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436{
437 typval_T argvars[MAX_FUNC_ARGS];
438 funcexe_T funcexe;
439 int error;
440 int idx;
441
442 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100443 {
444 // The function has been compiled, can call it quickly. For a function
445 // that was defined later: we can call it directly next time.
446 if (iptr != NULL)
447 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100448 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100449 iptr->isn_type = ISN_DCALL;
450 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
451 iptr->isn_arg.dfunc.cdf_argcount = argcount;
452 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100453 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100454 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100455
456 if (call_prepare(argcount, argvars, ectx) == FAIL)
457 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200458 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100459 funcexe.evaluate = TRUE;
460
461 // Call the user function. Result goes in last position on the stack.
462 // TODO: add selfdict if there is one
463 error = call_user_func_check(ufunc, argcount, argvars,
464 STACK_TV_BOT(-1), &funcexe, NULL);
465
466 // Clear the arguments.
467 for (idx = 0; idx < argcount; ++idx)
468 clear_tv(&argvars[idx]);
469
470 if (error != FCERR_NONE)
471 {
472 user_func_error(error, ufunc->uf_name);
473 return FAIL;
474 }
475 return OK;
476}
477
478/*
479 * Execute a function by "name".
480 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100481 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 * Returns FAIL if not found without an error message.
483 */
484 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100485call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100486{
487 ufunc_T *ufunc;
488
489 if (builtin_function(name, -1))
490 {
491 int func_idx = find_internal_func(name);
492
493 if (func_idx < 0)
494 return FAIL;
495 if (check_internal_func(func_idx, argcount) == FAIL)
496 return FAIL;
497 return call_bfunc(func_idx, argcount, ectx);
498 }
499
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200500 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503
504 return FAIL;
505}
506
507 static int
508call_partial(typval_T *tv, int argcount, ectx_T *ectx)
509{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200510 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 int called_emsg_before = called_emsg;
512
513 if (tv->v_type == VAR_PARTIAL)
514 {
515 partial_T *pt = tv->vval.v_partial;
516
517 if (pt->pt_func != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100518 return call_ufunc(pt->pt_func, argcount, ectx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519 name = pt->pt_name;
520 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200521 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200523 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 {
525 if (called_emsg == called_emsg_before)
526 semsg(_(e_unknownfunc), name);
527 return FAIL;
528 }
529 return OK;
530}
531
532/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100533 * Store "tv" in variable "name".
534 * This is for s: and g: variables.
535 */
536 static void
537store_var(char_u *name, typval_T *tv)
538{
539 funccal_entry_T entry;
540
541 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200542 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100543 restore_funccal();
544}
545
Bram Moolenaard3aac292020-04-19 14:32:17 +0200546
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100547/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100548 * Execute a function by "name".
549 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100550 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100551 */
552 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100553call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554{
555 int called_emsg_before = called_emsg;
556
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100557 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 && called_emsg == called_emsg_before)
559 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200560 dictitem_T *v;
561
562 v = find_var(name, NULL, FALSE);
563 if (v == NULL)
564 {
565 semsg(_(e_unknownfunc), name);
566 return FAIL;
567 }
568 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
569 {
570 semsg(_(e_unknownfunc), name);
571 return FAIL;
572 }
573 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 }
575 return OK;
576}
577
578/*
579 * Call a "def" function from old Vim script.
580 * Return OK or FAIL.
581 */
582 int
583call_def_function(
584 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200585 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100586 typval_T *argv, // arguments
587 typval_T *rettv) // return value
588{
589 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200590 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200591 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 typval_T *tv;
593 int idx;
594 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100595 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200596 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597
598// Get pointer to item in the stack.
599#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
600
601// Get pointer to item at the bottom of the stack, -1 is the bottom.
602#undef STACK_TV_BOT
603#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
604
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200605// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200606#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 +0100607
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200608// Like STACK_TV_VAR but use the outer scope
609#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
610
Bram Moolenaara80faa82020-04-12 19:37:17 +0200611 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100612 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
613 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100614 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100615 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
616
617 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
618
619 // Put arguments on the stack.
620 for (idx = 0; idx < argc; ++idx)
621 {
622 copy_tv(&argv[idx], STACK_TV_BOT(0));
623 ++ectx.ec_stack.ga_len;
624 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200625
626 // Turn varargs into a list. Empty list if no args.
627 if (ufunc->uf_va_name != NULL)
628 {
629 int vararg_count = argc - ufunc->uf_args.ga_len;
630
631 if (vararg_count < 0)
632 vararg_count = 0;
633 else
634 argc -= vararg_count;
635 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200636 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200637 if (defcount > 0)
638 // Move varargs list to below missing default arguments.
639 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
640 --ectx.ec_stack.ga_len;
641 }
642
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100643 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200644 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100645 if (defcount > 0)
646 for (idx = 0; idx < defcount; ++idx)
647 {
648 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
649 ++ectx.ec_stack.ga_len;
650 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200651 if (ufunc->uf_va_name != NULL)
652 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653
654 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200655 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
656 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100657
658 // dummy frame entries
659 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
660 {
661 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
662 ++ectx.ec_stack.ga_len;
663 }
664
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200665 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200666 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200667 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
668 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200669 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200671 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200672 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200673 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200674
675 ectx.ec_instr = dfunc->df_instr;
676 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100677
Bram Moolenaara26b9702020-04-18 19:53:28 +0200678 // Commands behave like vim9script.
679 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
680
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100681 // Decide where to start execution, handles optional arguments.
682 init_instr_idx(ufunc, argc, &ectx);
683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 for (;;)
685 {
686 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100687
688 veryfast_breakcheck();
689 if (got_int)
690 {
691 // Turn CTRL-C into an exception.
692 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100693 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100694 goto failed;
695 did_throw = TRUE;
696 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697
Bram Moolenaara26b9702020-04-18 19:53:28 +0200698 if (did_emsg && msg_list != NULL && *msg_list != NULL)
699 {
700 // Turn an error message into an exception.
701 did_emsg = FALSE;
702 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
703 goto failed;
704 did_throw = TRUE;
705 *msg_list = NULL;
706 }
707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 if (did_throw && !ectx.ec_in_catch)
709 {
710 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100711 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712
713 // An exception jumps to the first catch, finally, or returns from
714 // the current function.
715 if (trystack->ga_len > 0)
716 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200717 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 {
719 // jump to ":catch" or ":finally"
720 ectx.ec_in_catch = TRUE;
721 ectx.ec_iidx = trycmd->tcd_catch_idx;
722 }
723 else
724 {
725 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 {
728 // At the toplevel we are done. Push a dummy return value.
729 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
730 goto failed;
731 tv = STACK_TV_BOT(0);
732 tv->v_type = VAR_NUMBER;
733 tv->vval.v_number = 0;
734 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100735 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200736 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
737 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 goto done;
739 }
740
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200741 if (func_return(&ectx) == FAIL)
742 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743 }
744 continue;
745 }
746
747 iptr = &ectx.ec_instr[ectx.ec_iidx++];
748 switch (iptr->isn_type)
749 {
750 // execute Ex command line
751 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200752 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753 do_cmdline_cmd(iptr->isn_arg.string);
754 break;
755
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200756 // execute Ex command from pieces on the stack
757 case ISN_EXECCONCAT:
758 {
759 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200760 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200761 int pass;
762 int i;
763 char_u *cmd = NULL;
764 char_u *str;
765
766 for (pass = 1; pass <= 2; ++pass)
767 {
768 for (i = 0; i < count; ++i)
769 {
770 tv = STACK_TV_BOT(i - count);
771 str = tv->vval.v_string;
772 if (str != NULL && *str != NUL)
773 {
774 if (pass == 2)
775 STRCPY(cmd + len, str);
776 len += STRLEN(str);
777 }
778 if (pass == 2)
779 clear_tv(tv);
780 }
781 if (pass == 1)
782 {
783 cmd = alloc(len + 1);
784 if (cmd == NULL)
785 goto failed;
786 len = 0;
787 }
788 }
789
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200790 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200791 do_cmdline_cmd(cmd);
792 vim_free(cmd);
793 }
794 break;
795
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 // execute :echo {string} ...
797 case ISN_ECHO:
798 {
799 int count = iptr->isn_arg.echo.echo_count;
800 int atstart = TRUE;
801 int needclr = TRUE;
802
803 for (idx = 0; idx < count; ++idx)
804 {
805 tv = STACK_TV_BOT(idx - count);
806 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
807 &atstart, &needclr);
808 clear_tv(tv);
809 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100810 if (needclr)
811 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812 ectx.ec_stack.ga_len -= count;
813 }
814 break;
815
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200816 // :execute {string} ...
817 // :echomsg {string} ...
818 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100819 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200820 case ISN_ECHOMSG:
821 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100822 {
823 int count = iptr->isn_arg.number;
824 garray_T ga;
825 char_u buf[NUMBUFLEN];
826 char_u *p;
827 int len;
828 int failed = FALSE;
829
830 ga_init2(&ga, 1, 80);
831 for (idx = 0; idx < count; ++idx)
832 {
833 tv = STACK_TV_BOT(idx - count);
834 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
835 {
836 emsg(_(e_inval_string));
837 break;
838 }
839 else
840 p = tv_get_string_buf(tv, buf);
841
842 len = (int)STRLEN(p);
843 if (ga_grow(&ga, len + 2) == FAIL)
844 failed = TRUE;
845 else
846 {
847 if (ga.ga_len > 0)
848 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
849 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
850 ga.ga_len += len;
851 }
852 clear_tv(tv);
853 }
854 ectx.ec_stack.ga_len -= count;
855
856 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200857 {
858 if (iptr->isn_type == ISN_EXECUTE)
859 do_cmdline_cmd((char_u *)ga.ga_data);
860 else
861 {
862 msg_sb_eol();
863 if (iptr->isn_type == ISN_ECHOMSG)
864 {
865 msg_attr(ga.ga_data, echo_attr);
866 out_flush();
867 }
868 else
869 {
870 int save_did_emsg = did_emsg;
871
872 SOURCING_LNUM = iptr->isn_lnum;
873 emsg(ga.ga_data);
874 if (!force_abort)
875 // We don't want to abort following
876 // commands, restore did_emsg.
877 did_emsg = save_did_emsg;
878 }
879 }
880 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100881 ga_clear(&ga);
882 }
883 break;
884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 // load local variable or argument
886 case ISN_LOAD:
887 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
888 goto failed;
889 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
890 ++ectx.ec_stack.ga_len;
891 break;
892
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200893 // load variable or argument from outer scope
894 case ISN_LOADOUTER:
895 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
896 goto failed;
897 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
898 STACK_TV_BOT(0));
899 ++ectx.ec_stack.ga_len;
900 break;
901
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 // load v: variable
903 case ISN_LOADV:
904 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
905 goto failed;
906 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
907 ++ectx.ec_stack.ga_len;
908 break;
909
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100910 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 case ISN_LOADSCRIPT:
912 {
913 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100914 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 svar_T *sv;
916
917 sv = ((svar_T *)si->sn_var_vals.ga_data)
918 + iptr->isn_arg.script.script_idx;
919 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
920 goto failed;
921 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
922 ++ectx.ec_stack.ga_len;
923 }
924 break;
925
926 // load s: variable in old script
927 case ISN_LOADS:
928 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100929 hashtab_T *ht = &SCRIPT_VARS(
930 iptr->isn_arg.loadstore.ls_sid);
931 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100933
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100934 if (di == NULL)
935 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100936 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 goto failed;
938 }
939 else
940 {
941 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
942 goto failed;
943 copy_tv(&di->di_tv, STACK_TV_BOT(0));
944 ++ectx.ec_stack.ga_len;
945 }
946 }
947 break;
948
Bram Moolenaard3aac292020-04-19 14:32:17 +0200949 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100950 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200951 case ISN_LOADB:
952 case ISN_LOADW:
953 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100954 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200955 dictitem_T *di = NULL;
956 hashtab_T *ht = NULL;
957 char namespace;
958 switch (iptr->isn_type)
959 {
960 case ISN_LOADG:
961 ht = get_globvar_ht();
962 namespace = 'g';
963 break;
964 case ISN_LOADB:
965 ht = &curbuf->b_vars->dv_hashtab;
966 namespace = 'b';
967 break;
968 case ISN_LOADW:
969 ht = &curwin->w_vars->dv_hashtab;
970 namespace = 'w';
971 break;
972 case ISN_LOADT:
973 ht = &curtab->tp_vars->dv_hashtab;
974 namespace = 't';
975 break;
976 default: // Cannot reach here
977 goto failed;
978 }
979 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100980
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 if (di == NULL)
982 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200983 semsg(_("E121: Undefined variable: %c:%s"),
984 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100985 goto failed;
986 }
987 else
988 {
989 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
990 goto failed;
991 copy_tv(&di->di_tv, STACK_TV_BOT(0));
992 ++ectx.ec_stack.ga_len;
993 }
994 }
995 break;
996
997 // load &option
998 case ISN_LOADOPT:
999 {
1000 typval_T optval;
1001 char_u *name = iptr->isn_arg.string;
1002
Bram Moolenaara8c17702020-04-01 21:17:24 +02001003 // This is not expected to fail, name is checked during
1004 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1006 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001007 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1008 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001009 *STACK_TV_BOT(0) = optval;
1010 ++ectx.ec_stack.ga_len;
1011 }
1012 break;
1013
1014 // load $ENV
1015 case ISN_LOADENV:
1016 {
1017 typval_T optval;
1018 char_u *name = iptr->isn_arg.string;
1019
1020 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1021 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001022 // name is always valid, checked when compiling
1023 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 *STACK_TV_BOT(0) = optval;
1025 ++ectx.ec_stack.ga_len;
1026 }
1027 break;
1028
1029 // load @register
1030 case ISN_LOADREG:
1031 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1032 goto failed;
1033 tv = STACK_TV_BOT(0);
1034 tv->v_type = VAR_STRING;
1035 tv->vval.v_string = get_reg_contents(
1036 iptr->isn_arg.number, GREG_EXPR_SRC);
1037 ++ectx.ec_stack.ga_len;
1038 break;
1039
1040 // store local variable
1041 case ISN_STORE:
1042 --ectx.ec_stack.ga_len;
1043 tv = STACK_TV_VAR(iptr->isn_arg.number);
1044 clear_tv(tv);
1045 *tv = *STACK_TV_BOT(0);
1046 break;
1047
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001048 // store s: variable in old script
1049 case ISN_STORES:
1050 {
1051 hashtab_T *ht = &SCRIPT_VARS(
1052 iptr->isn_arg.loadstore.ls_sid);
1053 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001054 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001055
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001056 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001057 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001058 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001059 else
1060 {
1061 clear_tv(&di->di_tv);
1062 di->di_tv = *STACK_TV_BOT(0);
1063 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001064 }
1065 break;
1066
1067 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 case ISN_STORESCRIPT:
1069 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001070 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001071 iptr->isn_arg.script.script_sid);
1072 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1073 + iptr->isn_arg.script.script_idx;
1074
1075 --ectx.ec_stack.ga_len;
1076 clear_tv(sv->sv_tv);
1077 *sv->sv_tv = *STACK_TV_BOT(0);
1078 }
1079 break;
1080
1081 // store option
1082 case ISN_STOREOPT:
1083 {
1084 long n = 0;
1085 char_u *s = NULL;
1086 char *msg;
1087
1088 --ectx.ec_stack.ga_len;
1089 tv = STACK_TV_BOT(0);
1090 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001091 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001093 if (s == NULL)
1094 s = (char_u *)"";
1095 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 else if (tv->v_type == VAR_NUMBER)
1097 n = tv->vval.v_number;
1098 else
1099 {
1100 emsg(_("E1051: Expected string or number"));
1101 goto failed;
1102 }
1103 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1104 n, s, iptr->isn_arg.storeopt.so_flags);
1105 if (msg != NULL)
1106 {
1107 emsg(_(msg));
1108 goto failed;
1109 }
1110 clear_tv(tv);
1111 }
1112 break;
1113
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001114 // store $ENV
1115 case ISN_STOREENV:
1116 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001117 tv = STACK_TV_BOT(0);
1118 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1119 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001120 break;
1121
1122 // store @r
1123 case ISN_STOREREG:
1124 {
1125 int reg = iptr->isn_arg.number;
1126
1127 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001128 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001129 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001130 tv_get_string(tv), -1, FALSE);
1131 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001132 }
1133 break;
1134
1135 // store v: variable
1136 case ISN_STOREV:
1137 --ectx.ec_stack.ga_len;
1138 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1139 == FAIL)
1140 goto failed;
1141 break;
1142
Bram Moolenaard3aac292020-04-19 14:32:17 +02001143 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001145 case ISN_STOREB:
1146 case ISN_STOREW:
1147 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001148 {
1149 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001150 hashtab_T *ht;
1151 switch (iptr->isn_type)
1152 {
1153 case ISN_STOREG:
1154 ht = get_globvar_ht();
1155 break;
1156 case ISN_STOREB:
1157 ht = &curbuf->b_vars->dv_hashtab;
1158 break;
1159 case ISN_STOREW:
1160 ht = &curwin->w_vars->dv_hashtab;
1161 break;
1162 case ISN_STORET:
1163 ht = &curtab->tp_vars->dv_hashtab;
1164 break;
1165 default: // Cannot reach here
1166 goto failed;
1167 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168
1169 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001170 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001172 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173 else
1174 {
1175 clear_tv(&di->di_tv);
1176 di->di_tv = *STACK_TV_BOT(0);
1177 }
1178 }
1179 break;
1180
1181 // store number in local variable
1182 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001183 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 clear_tv(tv);
1185 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001186 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001187 break;
1188
1189 // push constant
1190 case ISN_PUSHNR:
1191 case ISN_PUSHBOOL:
1192 case ISN_PUSHSPEC:
1193 case ISN_PUSHF:
1194 case ISN_PUSHS:
1195 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001196 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001197 case ISN_PUSHCHANNEL:
1198 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1200 goto failed;
1201 tv = STACK_TV_BOT(0);
1202 ++ectx.ec_stack.ga_len;
1203 switch (iptr->isn_type)
1204 {
1205 case ISN_PUSHNR:
1206 tv->v_type = VAR_NUMBER;
1207 tv->vval.v_number = iptr->isn_arg.number;
1208 break;
1209 case ISN_PUSHBOOL:
1210 tv->v_type = VAR_BOOL;
1211 tv->vval.v_number = iptr->isn_arg.number;
1212 break;
1213 case ISN_PUSHSPEC:
1214 tv->v_type = VAR_SPECIAL;
1215 tv->vval.v_number = iptr->isn_arg.number;
1216 break;
1217#ifdef FEAT_FLOAT
1218 case ISN_PUSHF:
1219 tv->v_type = VAR_FLOAT;
1220 tv->vval.v_float = iptr->isn_arg.fnumber;
1221 break;
1222#endif
1223 case ISN_PUSHBLOB:
1224 blob_copy(iptr->isn_arg.blob, tv);
1225 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001226 case ISN_PUSHFUNC:
1227 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001228 if (iptr->isn_arg.string == NULL)
1229 tv->vval.v_string = NULL;
1230 else
1231 tv->vval.v_string =
1232 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001233 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001234 case ISN_PUSHCHANNEL:
1235#ifdef FEAT_JOB_CHANNEL
1236 tv->v_type = VAR_CHANNEL;
1237 tv->vval.v_channel = iptr->isn_arg.channel;
1238 if (tv->vval.v_channel != NULL)
1239 ++tv->vval.v_channel->ch_refcount;
1240#endif
1241 break;
1242 case ISN_PUSHJOB:
1243#ifdef FEAT_JOB_CHANNEL
1244 tv->v_type = VAR_JOB;
1245 tv->vval.v_job = iptr->isn_arg.job;
1246 if (tv->vval.v_job != NULL)
1247 ++tv->vval.v_job->jv_refcount;
1248#endif
1249 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 default:
1251 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001252 tv->vval.v_string = vim_strsave(
1253 iptr->isn_arg.string == NULL
1254 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 }
1256 break;
1257
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001258 case ISN_UNLET:
1259 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1260 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1261 goto failed;
1262 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001263 case ISN_UNLETENV:
1264 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1265 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001266
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001267 // create a list from items on the stack; uses a single allocation
1268 // for the list header and the items
1269 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001270 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1271 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001272 break;
1273
1274 // create a dict from items on the stack
1275 case ISN_NEWDICT:
1276 {
1277 int count = iptr->isn_arg.number;
1278 dict_T *dict = dict_alloc();
1279 dictitem_T *item;
1280
1281 if (dict == NULL)
1282 goto failed;
1283 for (idx = 0; idx < count; ++idx)
1284 {
1285 // check key type is VAR_STRING
1286 tv = STACK_TV_BOT(2 * (idx - count));
1287 item = dictitem_alloc(tv->vval.v_string);
1288 clear_tv(tv);
1289 if (item == NULL)
1290 goto failed;
1291 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1292 item->di_tv.v_lock = 0;
1293 if (dict_add(dict, item) == FAIL)
1294 goto failed;
1295 }
1296
1297 if (count > 0)
1298 ectx.ec_stack.ga_len -= 2 * count - 1;
1299 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1300 goto failed;
1301 else
1302 ++ectx.ec_stack.ga_len;
1303 tv = STACK_TV_BOT(-1);
1304 tv->v_type = VAR_DICT;
1305 tv->vval.v_dict = dict;
1306 ++dict->dv_refcount;
1307 }
1308 break;
1309
1310 // call a :def function
1311 case ISN_DCALL:
1312 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1313 iptr->isn_arg.dfunc.cdf_argcount,
1314 &ectx) == FAIL)
1315 goto failed;
1316 break;
1317
1318 // call a builtin function
1319 case ISN_BCALL:
1320 SOURCING_LNUM = iptr->isn_lnum;
1321 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1322 iptr->isn_arg.bfunc.cbf_argcount,
1323 &ectx) == FAIL)
1324 goto failed;
1325 break;
1326
1327 // call a funcref or partial
1328 case ISN_PCALL:
1329 {
1330 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1331 int r;
1332 typval_T partial;
1333
1334 SOURCING_LNUM = iptr->isn_lnum;
1335 if (pfunc->cpf_top)
1336 {
1337 // funcref is above the arguments
1338 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1339 }
1340 else
1341 {
1342 // Get the funcref from the stack.
1343 --ectx.ec_stack.ga_len;
1344 partial = *STACK_TV_BOT(0);
1345 tv = &partial;
1346 }
1347 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1348 if (tv == &partial)
1349 clear_tv(&partial);
1350 if (r == FAIL)
1351 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 }
1353 break;
1354
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001355 case ISN_PCALL_END:
1356 // PCALL finished, arguments have been consumed and replaced by
1357 // the return value. Now clear the funcref from the stack,
1358 // and move the return value in its place.
1359 --ectx.ec_stack.ga_len;
1360 clear_tv(STACK_TV_BOT(-1));
1361 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1362 break;
1363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001364 // call a user defined function or funcref/partial
1365 case ISN_UCALL:
1366 {
1367 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1368
1369 SOURCING_LNUM = iptr->isn_lnum;
1370 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001371 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 goto failed;
1373 }
1374 break;
1375
1376 // return from a :def function call
1377 case ISN_RETURN:
1378 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001379 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001380 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001381
1382 if (trystack->ga_len > 0)
1383 trycmd = ((trycmd_T *)trystack->ga_data)
1384 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001385 if (trycmd != NULL
1386 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 && trycmd->tcd_finally_idx != 0)
1388 {
1389 // jump to ":finally"
1390 ectx.ec_iidx = trycmd->tcd_finally_idx;
1391 trycmd->tcd_return = TRUE;
1392 }
1393 else
1394 {
1395 // Restore previous function. If the frame pointer
1396 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001397 if (ectx.ec_frame_idx == initial_frame_idx)
1398 {
1399 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1400 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001402 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001404 if (func_return(&ectx) == FAIL)
1405 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 }
1407 }
1408 break;
1409
1410 // push a function reference to a compiled function
1411 case ISN_FUNCREF:
1412 {
1413 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001414 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415
1416 pt = ALLOC_CLEAR_ONE(partial_T);
1417 if (pt == NULL)
1418 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001419 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001420 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001421 vim_free(pt);
1422 goto failed;
1423 }
1424 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1425 + iptr->isn_arg.funcref.fr_func;
1426 pt->pt_func = pt_dfunc->df_ufunc;
1427 pt->pt_refcount = 1;
1428 ++pt_dfunc->df_ufunc->uf_refcount;
1429
1430 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1431 {
1432 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1433 + ectx.ec_dfunc_idx;
1434
1435 // The closure needs to find arguments and local
1436 // variables in the current stack.
1437 pt_dfunc->df_ectx_stack = &ectx.ec_stack;
1438 pt_dfunc->df_ectx_frame = ectx.ec_frame_idx;
1439
1440 // If this function returns and the closure is still
1441 // used, we need to make a copy of the context
1442 // (arguments and local variables). Store a reference
1443 // to the partial so we can handle that.
1444 ++pt->pt_refcount;
1445 tv = STACK_TV_VAR(dfunc->df_varcount
1446 + iptr->isn_arg.funcref.fr_var_idx);
1447 if (tv->v_type == VAR_PARTIAL)
1448 {
1449 // TODO: use a garray_T on ectx.
1450 emsg("Multiple closures not supported yet");
1451 goto failed;
1452 }
1453 tv->v_type = VAR_PARTIAL;
1454 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001455 }
1456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 tv = STACK_TV_BOT(0);
1458 ++ectx.ec_stack.ga_len;
1459 tv->vval.v_partial = pt;
1460 tv->v_type = VAR_PARTIAL;
1461 }
1462 break;
1463
1464 // jump if a condition is met
1465 case ISN_JUMP:
1466 {
1467 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1468 int jump = TRUE;
1469
1470 if (when != JUMP_ALWAYS)
1471 {
1472 tv = STACK_TV_BOT(-1);
1473 jump = tv2bool(tv);
1474 if (when == JUMP_IF_FALSE
1475 || when == JUMP_AND_KEEP_IF_FALSE)
1476 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001477 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001478 {
1479 // drop the value from the stack
1480 clear_tv(tv);
1481 --ectx.ec_stack.ga_len;
1482 }
1483 }
1484 if (jump)
1485 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1486 }
1487 break;
1488
1489 // top of a for loop
1490 case ISN_FOR:
1491 {
1492 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1493 typval_T *idxtv =
1494 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1495
1496 // push the next item from the list
1497 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1498 goto failed;
1499 if (++idxtv->vval.v_number >= list->lv_len)
1500 // past the end of the list, jump to "endfor"
1501 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1502 else if (list->lv_first == &range_list_item)
1503 {
1504 // non-materialized range() list
1505 tv = STACK_TV_BOT(0);
1506 tv->v_type = VAR_NUMBER;
1507 tv->vval.v_number = list_find_nr(
1508 list, idxtv->vval.v_number, NULL);
1509 ++ectx.ec_stack.ga_len;
1510 }
1511 else
1512 {
1513 listitem_T *li = list_find(list, idxtv->vval.v_number);
1514
1515 if (li == NULL)
1516 goto failed;
1517 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1518 ++ectx.ec_stack.ga_len;
1519 }
1520 }
1521 break;
1522
1523 // start of ":try" block
1524 case ISN_TRY:
1525 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001526 trycmd_T *trycmd = NULL;
1527
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001528 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1529 goto failed;
1530 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1531 + ectx.ec_trystack.ga_len;
1532 ++ectx.ec_trystack.ga_len;
1533 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001534 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1536 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001537 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 }
1539 break;
1540
1541 case ISN_PUSHEXC:
1542 if (current_exception == NULL)
1543 {
1544 iemsg("Evaluating catch while current_exception is NULL");
1545 goto failed;
1546 }
1547 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1548 goto failed;
1549 tv = STACK_TV_BOT(0);
1550 ++ectx.ec_stack.ga_len;
1551 tv->v_type = VAR_STRING;
1552 tv->vval.v_string = vim_strsave(
1553 (char_u *)current_exception->value);
1554 break;
1555
1556 case ISN_CATCH:
1557 {
1558 garray_T *trystack = &ectx.ec_trystack;
1559
1560 if (trystack->ga_len > 0)
1561 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001562 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 + trystack->ga_len - 1;
1564 trycmd->tcd_caught = TRUE;
1565 }
1566 did_emsg = got_int = did_throw = FALSE;
1567 catch_exception(current_exception);
1568 }
1569 break;
1570
1571 // end of ":try" block
1572 case ISN_ENDTRY:
1573 {
1574 garray_T *trystack = &ectx.ec_trystack;
1575
1576 if (trystack->ga_len > 0)
1577 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001578 trycmd_T *trycmd = NULL;
1579
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 --trystack->ga_len;
1581 --trylevel;
1582 trycmd = ((trycmd_T *)trystack->ga_data)
1583 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001584 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 {
1586 // discard the exception
1587 if (caught_stack == current_exception)
1588 caught_stack = caught_stack->caught;
1589 discard_current_exception();
1590 }
1591
1592 if (trycmd->tcd_return)
1593 {
1594 // Restore previous function. If the frame pointer
1595 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001596 if (ectx.ec_frame_idx == initial_frame_idx)
1597 {
1598 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1599 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001601 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001602
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001603 if (func_return(&ectx) == FAIL)
1604 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 }
1606 }
1607 }
1608 break;
1609
1610 case ISN_THROW:
1611 --ectx.ec_stack.ga_len;
1612 tv = STACK_TV_BOT(0);
1613 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1614 {
1615 vim_free(tv->vval.v_string);
1616 goto failed;
1617 }
1618 did_throw = TRUE;
1619 break;
1620
1621 // compare with special values
1622 case ISN_COMPAREBOOL:
1623 case ISN_COMPARESPECIAL:
1624 {
1625 typval_T *tv1 = STACK_TV_BOT(-2);
1626 typval_T *tv2 = STACK_TV_BOT(-1);
1627 varnumber_T arg1 = tv1->vval.v_number;
1628 varnumber_T arg2 = tv2->vval.v_number;
1629 int res;
1630
1631 switch (iptr->isn_arg.op.op_type)
1632 {
1633 case EXPR_EQUAL: res = arg1 == arg2; break;
1634 case EXPR_NEQUAL: res = arg1 != arg2; break;
1635 default: res = 0; break;
1636 }
1637
1638 --ectx.ec_stack.ga_len;
1639 tv1->v_type = VAR_BOOL;
1640 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1641 }
1642 break;
1643
1644 // Operation with two number arguments
1645 case ISN_OPNR:
1646 case ISN_COMPARENR:
1647 {
1648 typval_T *tv1 = STACK_TV_BOT(-2);
1649 typval_T *tv2 = STACK_TV_BOT(-1);
1650 varnumber_T arg1 = tv1->vval.v_number;
1651 varnumber_T arg2 = tv2->vval.v_number;
1652 varnumber_T res;
1653
1654 switch (iptr->isn_arg.op.op_type)
1655 {
1656 case EXPR_MULT: res = arg1 * arg2; break;
1657 case EXPR_DIV: res = arg1 / arg2; break;
1658 case EXPR_REM: res = arg1 % arg2; break;
1659 case EXPR_SUB: res = arg1 - arg2; break;
1660 case EXPR_ADD: res = arg1 + arg2; break;
1661
1662 case EXPR_EQUAL: res = arg1 == arg2; break;
1663 case EXPR_NEQUAL: res = arg1 != arg2; break;
1664 case EXPR_GREATER: res = arg1 > arg2; break;
1665 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1666 case EXPR_SMALLER: res = arg1 < arg2; break;
1667 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1668 default: res = 0; break;
1669 }
1670
1671 --ectx.ec_stack.ga_len;
1672 if (iptr->isn_type == ISN_COMPARENR)
1673 {
1674 tv1->v_type = VAR_BOOL;
1675 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1676 }
1677 else
1678 tv1->vval.v_number = res;
1679 }
1680 break;
1681
1682 // Computation with two float arguments
1683 case ISN_OPFLOAT:
1684 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001685#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 {
1687 typval_T *tv1 = STACK_TV_BOT(-2);
1688 typval_T *tv2 = STACK_TV_BOT(-1);
1689 float_T arg1 = tv1->vval.v_float;
1690 float_T arg2 = tv2->vval.v_float;
1691 float_T res = 0;
1692 int cmp = FALSE;
1693
1694 switch (iptr->isn_arg.op.op_type)
1695 {
1696 case EXPR_MULT: res = arg1 * arg2; break;
1697 case EXPR_DIV: res = arg1 / arg2; break;
1698 case EXPR_SUB: res = arg1 - arg2; break;
1699 case EXPR_ADD: res = arg1 + arg2; break;
1700
1701 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1702 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1703 case EXPR_GREATER: cmp = arg1 > arg2; break;
1704 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1705 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1706 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1707 default: cmp = 0; break;
1708 }
1709 --ectx.ec_stack.ga_len;
1710 if (iptr->isn_type == ISN_COMPAREFLOAT)
1711 {
1712 tv1->v_type = VAR_BOOL;
1713 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1714 }
1715 else
1716 tv1->vval.v_float = res;
1717 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001718#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 break;
1720
1721 case ISN_COMPARELIST:
1722 {
1723 typval_T *tv1 = STACK_TV_BOT(-2);
1724 typval_T *tv2 = STACK_TV_BOT(-1);
1725 list_T *arg1 = tv1->vval.v_list;
1726 list_T *arg2 = tv2->vval.v_list;
1727 int cmp = FALSE;
1728 int ic = iptr->isn_arg.op.op_ic;
1729
1730 switch (iptr->isn_arg.op.op_type)
1731 {
1732 case EXPR_EQUAL: cmp =
1733 list_equal(arg1, arg2, ic, FALSE); break;
1734 case EXPR_NEQUAL: cmp =
1735 !list_equal(arg1, arg2, ic, FALSE); break;
1736 case EXPR_IS: cmp = arg1 == arg2; break;
1737 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1738 default: cmp = 0; break;
1739 }
1740 --ectx.ec_stack.ga_len;
1741 clear_tv(tv1);
1742 clear_tv(tv2);
1743 tv1->v_type = VAR_BOOL;
1744 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1745 }
1746 break;
1747
1748 case ISN_COMPAREBLOB:
1749 {
1750 typval_T *tv1 = STACK_TV_BOT(-2);
1751 typval_T *tv2 = STACK_TV_BOT(-1);
1752 blob_T *arg1 = tv1->vval.v_blob;
1753 blob_T *arg2 = tv2->vval.v_blob;
1754 int cmp = FALSE;
1755
1756 switch (iptr->isn_arg.op.op_type)
1757 {
1758 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1759 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1760 case EXPR_IS: cmp = arg1 == arg2; break;
1761 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1762 default: cmp = 0; break;
1763 }
1764 --ectx.ec_stack.ga_len;
1765 clear_tv(tv1);
1766 clear_tv(tv2);
1767 tv1->v_type = VAR_BOOL;
1768 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1769 }
1770 break;
1771
1772 // TODO: handle separately
1773 case ISN_COMPARESTRING:
1774 case ISN_COMPAREDICT:
1775 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 case ISN_COMPAREANY:
1777 {
1778 typval_T *tv1 = STACK_TV_BOT(-2);
1779 typval_T *tv2 = STACK_TV_BOT(-1);
1780 exptype_T exptype = iptr->isn_arg.op.op_type;
1781 int ic = iptr->isn_arg.op.op_ic;
1782
1783 typval_compare(tv1, tv2, exptype, ic);
1784 clear_tv(tv2);
1785 tv1->v_type = VAR_BOOL;
1786 tv1->vval.v_number = tv1->vval.v_number
1787 ? VVAL_TRUE : VVAL_FALSE;
1788 --ectx.ec_stack.ga_len;
1789 }
1790 break;
1791
1792 case ISN_ADDLIST:
1793 case ISN_ADDBLOB:
1794 {
1795 typval_T *tv1 = STACK_TV_BOT(-2);
1796 typval_T *tv2 = STACK_TV_BOT(-1);
1797
1798 if (iptr->isn_type == ISN_ADDLIST)
1799 eval_addlist(tv1, tv2);
1800 else
1801 eval_addblob(tv1, tv2);
1802 clear_tv(tv2);
1803 --ectx.ec_stack.ga_len;
1804 }
1805 break;
1806
1807 // Computation with two arguments of unknown type
1808 case ISN_OPANY:
1809 {
1810 typval_T *tv1 = STACK_TV_BOT(-2);
1811 typval_T *tv2 = STACK_TV_BOT(-1);
1812 varnumber_T n1, n2;
1813#ifdef FEAT_FLOAT
1814 float_T f1 = 0, f2 = 0;
1815#endif
1816 int error = FALSE;
1817
1818 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1819 {
1820 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1821 {
1822 eval_addlist(tv1, tv2);
1823 clear_tv(tv2);
1824 --ectx.ec_stack.ga_len;
1825 break;
1826 }
1827 else if (tv1->v_type == VAR_BLOB
1828 && tv2->v_type == VAR_BLOB)
1829 {
1830 eval_addblob(tv1, tv2);
1831 clear_tv(tv2);
1832 --ectx.ec_stack.ga_len;
1833 break;
1834 }
1835 }
1836#ifdef FEAT_FLOAT
1837 if (tv1->v_type == VAR_FLOAT)
1838 {
1839 f1 = tv1->vval.v_float;
1840 n1 = 0;
1841 }
1842 else
1843#endif
1844 {
1845 n1 = tv_get_number_chk(tv1, &error);
1846 if (error)
1847 goto failed;
1848#ifdef FEAT_FLOAT
1849 if (tv2->v_type == VAR_FLOAT)
1850 f1 = n1;
1851#endif
1852 }
1853#ifdef FEAT_FLOAT
1854 if (tv2->v_type == VAR_FLOAT)
1855 {
1856 f2 = tv2->vval.v_float;
1857 n2 = 0;
1858 }
1859 else
1860#endif
1861 {
1862 n2 = tv_get_number_chk(tv2, &error);
1863 if (error)
1864 goto failed;
1865#ifdef FEAT_FLOAT
1866 if (tv1->v_type == VAR_FLOAT)
1867 f2 = n2;
1868#endif
1869 }
1870#ifdef FEAT_FLOAT
1871 // if there is a float on either side the result is a float
1872 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1873 {
1874 switch (iptr->isn_arg.op.op_type)
1875 {
1876 case EXPR_MULT: f1 = f1 * f2; break;
1877 case EXPR_DIV: f1 = f1 / f2; break;
1878 case EXPR_SUB: f1 = f1 - f2; break;
1879 case EXPR_ADD: f1 = f1 + f2; break;
1880 default: emsg(_(e_modulus)); goto failed;
1881 }
1882 clear_tv(tv1);
1883 clear_tv(tv2);
1884 tv1->v_type = VAR_FLOAT;
1885 tv1->vval.v_float = f1;
1886 --ectx.ec_stack.ga_len;
1887 }
1888 else
1889#endif
1890 {
1891 switch (iptr->isn_arg.op.op_type)
1892 {
1893 case EXPR_MULT: n1 = n1 * n2; break;
1894 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1895 case EXPR_SUB: n1 = n1 - n2; break;
1896 case EXPR_ADD: n1 = n1 + n2; break;
1897 default: n1 = num_modulus(n1, n2); break;
1898 }
1899 clear_tv(tv1);
1900 clear_tv(tv2);
1901 tv1->v_type = VAR_NUMBER;
1902 tv1->vval.v_number = n1;
1903 --ectx.ec_stack.ga_len;
1904 }
1905 }
1906 break;
1907
1908 case ISN_CONCAT:
1909 {
1910 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1911 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1912 char_u *res;
1913
1914 res = concat_str(str1, str2);
1915 clear_tv(STACK_TV_BOT(-2));
1916 clear_tv(STACK_TV_BOT(-1));
1917 --ectx.ec_stack.ga_len;
1918 STACK_TV_BOT(-1)->vval.v_string = res;
1919 }
1920 break;
1921
1922 case ISN_INDEX:
1923 {
1924 list_T *list;
1925 varnumber_T n;
1926 listitem_T *li;
1927
1928 // list index: list is at stack-2, index at stack-1
1929 tv = STACK_TV_BOT(-2);
1930 if (tv->v_type != VAR_LIST)
1931 {
1932 emsg(_(e_listreq));
1933 goto failed;
1934 }
1935 list = tv->vval.v_list;
1936
1937 tv = STACK_TV_BOT(-1);
1938 if (tv->v_type != VAR_NUMBER)
1939 {
1940 emsg(_(e_number_exp));
1941 goto failed;
1942 }
1943 n = tv->vval.v_number;
1944 clear_tv(tv);
1945 if ((li = list_find(list, n)) == NULL)
1946 {
1947 semsg(_(e_listidx), n);
1948 goto failed;
1949 }
1950 --ectx.ec_stack.ga_len;
1951 clear_tv(STACK_TV_BOT(-1));
1952 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1953 }
1954 break;
1955
1956 // dict member with string key
1957 case ISN_MEMBER:
1958 {
1959 dict_T *dict;
1960 dictitem_T *di;
1961
1962 tv = STACK_TV_BOT(-1);
1963 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1964 {
1965 emsg(_(e_dictreq));
1966 goto failed;
1967 }
1968 dict = tv->vval.v_dict;
1969
1970 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1971 == NULL)
1972 {
1973 semsg(_(e_dictkey), iptr->isn_arg.string);
1974 goto failed;
1975 }
1976 clear_tv(tv);
1977 copy_tv(&di->di_tv, tv);
1978 }
1979 break;
1980
1981 case ISN_NEGATENR:
1982 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001983 if (tv->v_type != VAR_NUMBER
1984#ifdef FEAT_FLOAT
1985 && tv->v_type != VAR_FLOAT
1986#endif
1987 )
1988 {
1989 emsg(_(e_number_exp));
1990 goto failed;
1991 }
1992#ifdef FEAT_FLOAT
1993 if (tv->v_type == VAR_FLOAT)
1994 tv->vval.v_float = -tv->vval.v_float;
1995 else
1996#endif
1997 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 break;
1999
2000 case ISN_CHECKNR:
2001 {
2002 int error = FALSE;
2003
2004 tv = STACK_TV_BOT(-1);
2005 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 (void)tv_get_number_chk(tv, &error);
2008 if (error)
2009 goto failed;
2010 }
2011 break;
2012
2013 case ISN_CHECKTYPE:
2014 {
2015 checktype_T *ct = &iptr->isn_arg.type;
2016
2017 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002018 // TODO: better type comparison
2019 if (tv->v_type != ct->ct_type
2020 && !((tv->v_type == VAR_PARTIAL
2021 && ct->ct_type == VAR_FUNC)
2022 || (tv->v_type == VAR_FUNC
2023 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 {
2025 semsg(_("E1029: Expected %s but got %s"),
2026 vartype_name(ct->ct_type),
2027 vartype_name(tv->v_type));
2028 goto failed;
2029 }
2030 }
2031 break;
2032
2033 case ISN_2BOOL:
2034 {
2035 int n;
2036
2037 tv = STACK_TV_BOT(-1);
2038 n = tv2bool(tv);
2039 if (iptr->isn_arg.number) // invert
2040 n = !n;
2041 clear_tv(tv);
2042 tv->v_type = VAR_BOOL;
2043 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2044 }
2045 break;
2046
2047 case ISN_2STRING:
2048 {
2049 char_u *str;
2050
2051 tv = STACK_TV_BOT(iptr->isn_arg.number);
2052 if (tv->v_type != VAR_STRING)
2053 {
2054 str = typval_tostring(tv);
2055 clear_tv(tv);
2056 tv->v_type = VAR_STRING;
2057 tv->vval.v_string = str;
2058 }
2059 }
2060 break;
2061
2062 case ISN_DROP:
2063 --ectx.ec_stack.ga_len;
2064 clear_tv(STACK_TV_BOT(0));
2065 break;
2066 }
2067 }
2068
2069done:
2070 // function finished, get result from the stack.
2071 tv = STACK_TV_BOT(-1);
2072 *rettv = *tv;
2073 tv->v_type = VAR_UNKNOWN;
2074 ret = OK;
2075
2076failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002077 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002078 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002079 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002080failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002081 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002082
2083 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2085 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002086
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002087 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002088 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 return ret;
2090}
2091
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002092/*
2093 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002094 * We don't really need this at runtime, but we do have tests that require it,
2095 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 */
2097 void
2098ex_disassemble(exarg_T *eap)
2099{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002100 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002101 char_u *fname;
2102 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002103 dfunc_T *dfunc;
2104 isn_T *instr;
2105 int current;
2106 int line_idx = 0;
2107 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002108 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002110 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002111 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002112 if (fname == NULL)
2113 {
2114 semsg(_(e_invarg2), eap->arg);
2115 return;
2116 }
2117
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002118 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002119 if (ufunc == NULL)
2120 {
2121 char_u *p = untrans_function_name(fname);
2122
2123 if (p != NULL)
2124 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002125 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002126 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002127 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002128 if (ufunc == NULL)
2129 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002130 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002131 return;
2132 }
2133 if (ufunc->uf_dfunc_idx < 0)
2134 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002135 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 return;
2137 }
2138 if (ufunc->uf_name_exp != NULL)
2139 msg((char *)ufunc->uf_name_exp);
2140 else
2141 msg((char *)ufunc->uf_name);
2142
2143 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2144 instr = dfunc->df_instr;
2145 for (current = 0; current < dfunc->df_instr_count; ++current)
2146 {
2147 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002148 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002149
2150 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2151 {
2152 if (current > prev_current)
2153 {
2154 msg_puts("\n\n");
2155 prev_current = current;
2156 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002157 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2158 if (line != NULL)
2159 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 }
2161
2162 switch (iptr->isn_type)
2163 {
2164 case ISN_EXEC:
2165 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2166 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002167 case ISN_EXECCONCAT:
2168 smsg("%4d EXECCONCAT %lld", current,
2169 (long long)iptr->isn_arg.number);
2170 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 case ISN_ECHO:
2172 {
2173 echo_T *echo = &iptr->isn_arg.echo;
2174
2175 smsg("%4d %s %d", current,
2176 echo->echo_with_white ? "ECHO" : "ECHON",
2177 echo->echo_count);
2178 }
2179 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002180 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002181 smsg("%4d EXECUTE %lld", current,
2182 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002183 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002184 case ISN_ECHOMSG:
2185 smsg("%4d ECHOMSG %lld", current,
2186 (long long)(iptr->isn_arg.number));
2187 break;
2188 case ISN_ECHOERR:
2189 smsg("%4d ECHOERR %lld", current,
2190 (long long)(iptr->isn_arg.number));
2191 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002193 case ISN_LOADOUTER:
2194 {
2195 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2196
2197 if (iptr->isn_arg.number < 0)
2198 smsg("%4d LOAD%s arg[%lld]", current, add,
2199 (long long)(iptr->isn_arg.number
2200 + STACK_FRAME_SIZE));
2201 else
2202 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002203 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002204 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 break;
2206 case ISN_LOADV:
2207 smsg("%4d LOADV v:%s", current,
2208 get_vim_var_name(iptr->isn_arg.number));
2209 break;
2210 case ISN_LOADSCRIPT:
2211 {
2212 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002213 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2215 + iptr->isn_arg.script.script_idx;
2216
2217 smsg("%4d LOADSCRIPT %s from %s", current,
2218 sv->sv_name, si->sn_name);
2219 }
2220 break;
2221 case ISN_LOADS:
2222 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002223 scriptitem_T *si = SCRIPT_ITEM(
2224 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225
2226 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002227 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 }
2229 break;
2230 case ISN_LOADG:
2231 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2232 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002233 case ISN_LOADB:
2234 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2235 break;
2236 case ISN_LOADW:
2237 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2238 break;
2239 case ISN_LOADT:
2240 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2241 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 case ISN_LOADOPT:
2243 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2244 break;
2245 case ISN_LOADENV:
2246 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2247 break;
2248 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002249 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 break;
2251
2252 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002253 if (iptr->isn_arg.number < 0)
2254 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002255 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002256 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002257 smsg("%4d STORE $%lld", current,
2258 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002260 case ISN_STOREV:
2261 smsg("%4d STOREV v:%s", current,
2262 get_vim_var_name(iptr->isn_arg.number));
2263 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002265 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2266 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002267 case ISN_STOREB:
2268 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2269 break;
2270 case ISN_STOREW:
2271 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2272 break;
2273 case ISN_STORET:
2274 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2275 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002276 case ISN_STORES:
2277 {
2278 scriptitem_T *si = SCRIPT_ITEM(
2279 iptr->isn_arg.loadstore.ls_sid);
2280
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002281 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002282 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002283 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 break;
2285 case ISN_STORESCRIPT:
2286 {
2287 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002288 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2290 + iptr->isn_arg.script.script_idx;
2291
2292 smsg("%4d STORESCRIPT %s in %s", current,
2293 sv->sv_name, si->sn_name);
2294 }
2295 break;
2296 case ISN_STOREOPT:
2297 smsg("%4d STOREOPT &%s", current,
2298 iptr->isn_arg.storeopt.so_name);
2299 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002300 case ISN_STOREENV:
2301 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2302 break;
2303 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002304 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002305 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 case ISN_STORENR:
2307 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002308 iptr->isn_arg.storenr.stnr_val,
2309 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002310 break;
2311
2312 // constants
2313 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002314 smsg("%4d PUSHNR %lld", current,
2315 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 break;
2317 case ISN_PUSHBOOL:
2318 case ISN_PUSHSPEC:
2319 smsg("%4d PUSH %s", current,
2320 get_var_special_name(iptr->isn_arg.number));
2321 break;
2322 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002323#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002325#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 break;
2327 case ISN_PUSHS:
2328 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2329 break;
2330 case ISN_PUSHBLOB:
2331 {
2332 char_u *r;
2333 char_u numbuf[NUMBUFLEN];
2334 char_u *tofree;
2335
2336 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002337 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 vim_free(tofree);
2339 }
2340 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002341 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002342 {
2343 char *name = (char *)iptr->isn_arg.string;
2344
2345 smsg("%4d PUSHFUNC \"%s\"", current,
2346 name == NULL ? "[none]" : name);
2347 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002348 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002349 case ISN_PUSHCHANNEL:
2350#ifdef FEAT_JOB_CHANNEL
2351 {
2352 channel_T *channel = iptr->isn_arg.channel;
2353
2354 smsg("%4d PUSHCHANNEL %d", current,
2355 channel == NULL ? 0 : channel->ch_id);
2356 }
2357#endif
2358 break;
2359 case ISN_PUSHJOB:
2360#ifdef FEAT_JOB_CHANNEL
2361 {
2362 typval_T tv;
2363 char_u *name;
2364
2365 tv.v_type = VAR_JOB;
2366 tv.vval.v_job = iptr->isn_arg.job;
2367 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002368 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002369 }
2370#endif
2371 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 case ISN_PUSHEXC:
2373 smsg("%4d PUSH v:exception", current);
2374 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002375 case ISN_UNLET:
2376 smsg("%4d UNLET%s %s", current,
2377 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2378 iptr->isn_arg.unlet.ul_name);
2379 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002380 case ISN_UNLETENV:
2381 smsg("%4d UNLETENV%s $%s", current,
2382 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2383 iptr->isn_arg.unlet.ul_name);
2384 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002386 smsg("%4d NEWLIST size %lld", current,
2387 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 break;
2389 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002390 smsg("%4d NEWDICT size %lld", current,
2391 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002392 break;
2393
2394 // function call
2395 case ISN_BCALL:
2396 {
2397 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2398
2399 smsg("%4d BCALL %s(argc %d)", current,
2400 internal_func_name(cbfunc->cbf_idx),
2401 cbfunc->cbf_argcount);
2402 }
2403 break;
2404 case ISN_DCALL:
2405 {
2406 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2407 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2408 + cdfunc->cdf_idx;
2409
2410 smsg("%4d DCALL %s(argc %d)", current,
2411 df->df_ufunc->uf_name_exp != NULL
2412 ? df->df_ufunc->uf_name_exp
2413 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2414 }
2415 break;
2416 case ISN_UCALL:
2417 {
2418 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2419
2420 smsg("%4d UCALL %s(argc %d)", current,
2421 cufunc->cuf_name, cufunc->cuf_argcount);
2422 }
2423 break;
2424 case ISN_PCALL:
2425 {
2426 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2427
2428 smsg("%4d PCALL%s (argc %d)", current,
2429 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2430 }
2431 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002432 case ISN_PCALL_END:
2433 smsg("%4d PCALL end", current);
2434 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 case ISN_RETURN:
2436 smsg("%4d RETURN", current);
2437 break;
2438 case ISN_FUNCREF:
2439 {
2440 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002441 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002443 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
2444 iptr->isn_arg.funcref.fr_var_idx + df->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 }
2446 break;
2447
2448 case ISN_JUMP:
2449 {
2450 char *when = "?";
2451
2452 switch (iptr->isn_arg.jump.jump_when)
2453 {
2454 case JUMP_ALWAYS:
2455 when = "JUMP";
2456 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457 case JUMP_AND_KEEP_IF_TRUE:
2458 when = "JUMP_AND_KEEP_IF_TRUE";
2459 break;
2460 case JUMP_IF_FALSE:
2461 when = "JUMP_IF_FALSE";
2462 break;
2463 case JUMP_AND_KEEP_IF_FALSE:
2464 when = "JUMP_AND_KEEP_IF_FALSE";
2465 break;
2466 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002467 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 iptr->isn_arg.jump.jump_where);
2469 }
2470 break;
2471
2472 case ISN_FOR:
2473 {
2474 forloop_T *forloop = &iptr->isn_arg.forloop;
2475
2476 smsg("%4d FOR $%d -> %d", current,
2477 forloop->for_idx, forloop->for_end);
2478 }
2479 break;
2480
2481 case ISN_TRY:
2482 {
2483 try_T *try = &iptr->isn_arg.try;
2484
2485 smsg("%4d TRY catch -> %d, finally -> %d", current,
2486 try->try_catch, try->try_finally);
2487 }
2488 break;
2489 case ISN_CATCH:
2490 // TODO
2491 smsg("%4d CATCH", current);
2492 break;
2493 case ISN_ENDTRY:
2494 smsg("%4d ENDTRY", current);
2495 break;
2496 case ISN_THROW:
2497 smsg("%4d THROW", current);
2498 break;
2499
2500 // expression operations on number
2501 case ISN_OPNR:
2502 case ISN_OPFLOAT:
2503 case ISN_OPANY:
2504 {
2505 char *what;
2506 char *ins;
2507
2508 switch (iptr->isn_arg.op.op_type)
2509 {
2510 case EXPR_MULT: what = "*"; break;
2511 case EXPR_DIV: what = "/"; break;
2512 case EXPR_REM: what = "%"; break;
2513 case EXPR_SUB: what = "-"; break;
2514 case EXPR_ADD: what = "+"; break;
2515 default: what = "???"; break;
2516 }
2517 switch (iptr->isn_type)
2518 {
2519 case ISN_OPNR: ins = "OPNR"; break;
2520 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2521 case ISN_OPANY: ins = "OPANY"; break;
2522 default: ins = "???"; break;
2523 }
2524 smsg("%4d %s %s", current, ins, what);
2525 }
2526 break;
2527
2528 case ISN_COMPAREBOOL:
2529 case ISN_COMPARESPECIAL:
2530 case ISN_COMPARENR:
2531 case ISN_COMPAREFLOAT:
2532 case ISN_COMPARESTRING:
2533 case ISN_COMPAREBLOB:
2534 case ISN_COMPARELIST:
2535 case ISN_COMPAREDICT:
2536 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 case ISN_COMPAREANY:
2538 {
2539 char *p;
2540 char buf[10];
2541 char *type;
2542
2543 switch (iptr->isn_arg.op.op_type)
2544 {
2545 case EXPR_EQUAL: p = "=="; break;
2546 case EXPR_NEQUAL: p = "!="; break;
2547 case EXPR_GREATER: p = ">"; break;
2548 case EXPR_GEQUAL: p = ">="; break;
2549 case EXPR_SMALLER: p = "<"; break;
2550 case EXPR_SEQUAL: p = "<="; break;
2551 case EXPR_MATCH: p = "=~"; break;
2552 case EXPR_IS: p = "is"; break;
2553 case EXPR_ISNOT: p = "isnot"; break;
2554 case EXPR_NOMATCH: p = "!~"; break;
2555 default: p = "???"; break;
2556 }
2557 STRCPY(buf, p);
2558 if (iptr->isn_arg.op.op_ic == TRUE)
2559 strcat(buf, "?");
2560 switch(iptr->isn_type)
2561 {
2562 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2563 case ISN_COMPARESPECIAL:
2564 type = "COMPARESPECIAL"; break;
2565 case ISN_COMPARENR: type = "COMPARENR"; break;
2566 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2567 case ISN_COMPARESTRING:
2568 type = "COMPARESTRING"; break;
2569 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2570 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2571 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2572 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2574 default: type = "???"; break;
2575 }
2576
2577 smsg("%4d %s %s", current, type, buf);
2578 }
2579 break;
2580
2581 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2582 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2583
2584 // expression operations
2585 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2586 case ISN_INDEX: smsg("%4d INDEX", current); break;
2587 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2588 iptr->isn_arg.string); break;
2589 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2590
2591 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2592 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2593 vartype_name(iptr->isn_arg.type.ct_type),
2594 iptr->isn_arg.type.ct_off);
2595 break;
2596 case ISN_2BOOL: if (iptr->isn_arg.number)
2597 smsg("%4d INVERT (!val)", current);
2598 else
2599 smsg("%4d 2BOOL (!!val)", current);
2600 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002601 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2602 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 break;
2604
2605 case ISN_DROP: smsg("%4d DROP", current); break;
2606 }
2607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002608}
2609
2610/*
2611 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2612 * list, etc. Mostly like what JavaScript does, except that empty list and
2613 * empty dictionary are FALSE.
2614 */
2615 int
2616tv2bool(typval_T *tv)
2617{
2618 switch (tv->v_type)
2619 {
2620 case VAR_NUMBER:
2621 return tv->vval.v_number != 0;
2622 case VAR_FLOAT:
2623#ifdef FEAT_FLOAT
2624 return tv->vval.v_float != 0.0;
2625#else
2626 break;
2627#endif
2628 case VAR_PARTIAL:
2629 return tv->vval.v_partial != NULL;
2630 case VAR_FUNC:
2631 case VAR_STRING:
2632 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2633 case VAR_LIST:
2634 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2635 case VAR_DICT:
2636 return tv->vval.v_dict != NULL
2637 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2638 case VAR_BOOL:
2639 case VAR_SPECIAL:
2640 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2641 case VAR_JOB:
2642#ifdef FEAT_JOB_CHANNEL
2643 return tv->vval.v_job != NULL;
2644#else
2645 break;
2646#endif
2647 case VAR_CHANNEL:
2648#ifdef FEAT_JOB_CHANNEL
2649 return tv->vval.v_channel != NULL;
2650#else
2651 break;
2652#endif
2653 case VAR_BLOB:
2654 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2655 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002656 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 case VAR_VOID:
2658 break;
2659 }
2660 return FALSE;
2661}
2662
2663/*
2664 * If "tv" is a string give an error and return FAIL.
2665 */
2666 int
2667check_not_string(typval_T *tv)
2668{
2669 if (tv->v_type == VAR_STRING)
2670 {
2671 emsg(_("E1030: Using a String as a Number"));
2672 clear_tv(tv);
2673 return FAIL;
2674 }
2675 return OK;
2676}
2677
2678
2679#endif // FEAT_EVAL