blob: c74240ff2037f217b3b12da791959dcbea602b23 [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);
267 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200268 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200269 closure_in_use = TRUE;
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200270 break;
271 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200272 }
273
274 if (closure_in_use)
275 {
276 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
277 typval_T *stack;
278
279 // A closure is using the arguments and/or local variables.
280 // Move them to the called function.
281 if (funcstack == NULL)
282 return FAIL;
283 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
284 + dfunc->df_varcount;
285 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
286 funcstack->fs_ga.ga_data = stack;
287 if (stack == NULL)
288 {
289 vim_free(funcstack);
290 return FAIL;
291 }
292
293 // Move or copy the arguments.
294 for (idx = 0; idx < argcount; ++idx)
295 {
296 tv = STACK_TV(top + idx);
297 if (free_arguments)
298 {
299 *(stack + idx) = *tv;
300 tv->v_type = VAR_UNKNOWN;
301 }
302 else
303 copy_tv(tv, stack + idx);
304 }
305 // Move the local variables.
306 for (idx = 0; idx < dfunc->df_varcount; ++idx)
307 {
308 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
309 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
310 tv->v_type = VAR_UNKNOWN;
311 }
312
313 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
314 {
315 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
316 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200317 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200318 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200319 partial_T *partial = tv->vval.v_partial;
320
321 if (partial->pt_refcount > 1)
322 {
323 ++funcstack->fs_refcount;
324 partial->pt_funcstack = funcstack;
325 partial->pt_ectx_stack = &funcstack->fs_ga;
326 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
327 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 }
329 }
330 }
331
332 return OK;
333}
334
335/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 * Return from the current function.
337 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200338 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339func_return(ectx_T *ectx)
340{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200342 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
343 + ectx->ec_dfunc_idx;
344 int argcount = ufunc_argcount(dfunc->df_ufunc);
345 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346
347 // execution context goes one level up
348 estack_pop();
349
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200350 if (handle_closure_in_use(ectx, TRUE) == FAIL)
351 return FAIL;
352
353 // Clear the arguments.
354 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
355 clear_tv(STACK_TV(idx));
356
357 // Clear local variables and temp values, but not the return value.
358 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100359 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100361
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100362 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200363 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
364 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
365 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100366 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
367 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100368
369 // Reset the stack to the position before the call, move the return value
370 // to the top of the stack.
371 idx = ectx->ec_stack.ga_len - 1;
372 ectx->ec_stack.ga_len = top + 1;
373 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200374
375 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100376}
377
378#undef STACK_TV
379
380/*
381 * Prepare arguments and rettv for calling a builtin or user function.
382 */
383 static int
384call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
385{
386 int idx;
387 typval_T *tv;
388
389 // Move arguments from bottom of the stack to argvars[] and add terminator.
390 for (idx = 0; idx < argcount; ++idx)
391 argvars[idx] = *STACK_TV_BOT(idx - argcount);
392 argvars[argcount].v_type = VAR_UNKNOWN;
393
394 // Result replaces the arguments on the stack.
395 if (argcount > 0)
396 ectx->ec_stack.ga_len -= argcount - 1;
397 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
398 return FAIL;
399 else
400 ++ectx->ec_stack.ga_len;
401
402 // Default return value is zero.
403 tv = STACK_TV_BOT(-1);
404 tv->v_type = VAR_NUMBER;
405 tv->vval.v_number = 0;
406
407 return OK;
408}
409
410/*
411 * Call a builtin function by index.
412 */
413 static int
414call_bfunc(int func_idx, int argcount, ectx_T *ectx)
415{
416 typval_T argvars[MAX_FUNC_ARGS];
417 int idx;
418
419 if (call_prepare(argcount, argvars, ectx) == FAIL)
420 return FAIL;
421
422 // Call the builtin function.
423 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
424
425 // Clear the arguments.
426 for (idx = 0; idx < argcount; ++idx)
427 clear_tv(&argvars[idx]);
428 return OK;
429}
430
431/*
432 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100433 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 */
435 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100436call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100437{
438 typval_T argvars[MAX_FUNC_ARGS];
439 funcexe_T funcexe;
440 int error;
441 int idx;
442
443 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100444 {
445 // The function has been compiled, can call it quickly. For a function
446 // that was defined later: we can call it directly next time.
447 if (iptr != NULL)
448 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100449 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100450 iptr->isn_type = ISN_DCALL;
451 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
452 iptr->isn_arg.dfunc.cdf_argcount = argcount;
453 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100455 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100456
457 if (call_prepare(argcount, argvars, ectx) == FAIL)
458 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200459 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100460 funcexe.evaluate = TRUE;
461
462 // Call the user function. Result goes in last position on the stack.
463 // TODO: add selfdict if there is one
464 error = call_user_func_check(ufunc, argcount, argvars,
465 STACK_TV_BOT(-1), &funcexe, NULL);
466
467 // Clear the arguments.
468 for (idx = 0; idx < argcount; ++idx)
469 clear_tv(&argvars[idx]);
470
471 if (error != FCERR_NONE)
472 {
473 user_func_error(error, ufunc->uf_name);
474 return FAIL;
475 }
476 return OK;
477}
478
479/*
480 * Execute a function by "name".
481 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100482 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100483 * Returns FAIL if not found without an error message.
484 */
485 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100486call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487{
488 ufunc_T *ufunc;
489
490 if (builtin_function(name, -1))
491 {
492 int func_idx = find_internal_func(name);
493
494 if (func_idx < 0)
495 return FAIL;
496 if (check_internal_func(func_idx, argcount) == FAIL)
497 return FAIL;
498 return call_bfunc(func_idx, argcount, ectx);
499 }
500
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200501 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100503 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504
505 return FAIL;
506}
507
508 static int
509call_partial(typval_T *tv, int argcount, ectx_T *ectx)
510{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200511 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 int called_emsg_before = called_emsg;
513
514 if (tv->v_type == VAR_PARTIAL)
515 {
516 partial_T *pt = tv->vval.v_partial;
517
518 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200519 {
520 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
521
522 // closure may need the function context where it was defined
523 ectx->ec_outer_stack = pt->pt_ectx_stack;
524 ectx->ec_outer_frame = pt->pt_ectx_frame;
525
526 return ret;
527 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 name = pt->pt_name;
529 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200530 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200532 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 {
534 if (called_emsg == called_emsg_before)
535 semsg(_(e_unknownfunc), name);
536 return FAIL;
537 }
538 return OK;
539}
540
541/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100542 * Store "tv" in variable "name".
543 * This is for s: and g: variables.
544 */
545 static void
546store_var(char_u *name, typval_T *tv)
547{
548 funccal_entry_T entry;
549
550 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200551 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100552 restore_funccal();
553}
554
Bram Moolenaard3aac292020-04-19 14:32:17 +0200555
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100556/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100557 * Execute a function by "name".
558 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100559 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100560 */
561 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100562call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563{
564 int called_emsg_before = called_emsg;
565
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100566 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100567 && called_emsg == called_emsg_before)
568 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200569 dictitem_T *v;
570
571 v = find_var(name, NULL, FALSE);
572 if (v == NULL)
573 {
574 semsg(_(e_unknownfunc), name);
575 return FAIL;
576 }
577 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
578 {
579 semsg(_(e_unknownfunc), name);
580 return FAIL;
581 }
582 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 }
584 return OK;
585}
586
587/*
588 * Call a "def" function from old Vim script.
589 * Return OK or FAIL.
590 */
591 int
592call_def_function(
593 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200594 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100595 typval_T *argv, // arguments
596 typval_T *rettv) // return value
597{
598 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200599 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200600 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100601 typval_T *tv;
602 int idx;
603 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100604 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200605 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606
607// Get pointer to item in the stack.
608#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
609
610// Get pointer to item at the bottom of the stack, -1 is the bottom.
611#undef STACK_TV_BOT
612#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
613
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200614// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200615#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 +0100616
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200617// Like STACK_TV_VAR but use the outer scope
618#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
619
Bram Moolenaara80faa82020-04-12 19:37:17 +0200620 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
622 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100623 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
625
626 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
627
628 // Put arguments on the stack.
629 for (idx = 0; idx < argc; ++idx)
630 {
631 copy_tv(&argv[idx], STACK_TV_BOT(0));
632 ++ectx.ec_stack.ga_len;
633 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200634
635 // Turn varargs into a list. Empty list if no args.
636 if (ufunc->uf_va_name != NULL)
637 {
638 int vararg_count = argc - ufunc->uf_args.ga_len;
639
640 if (vararg_count < 0)
641 vararg_count = 0;
642 else
643 argc -= vararg_count;
644 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200645 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200646 if (defcount > 0)
647 // Move varargs list to below missing default arguments.
648 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
649 --ectx.ec_stack.ga_len;
650 }
651
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100652 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200653 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100654 if (defcount > 0)
655 for (idx = 0; idx < defcount; ++idx)
656 {
657 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
658 ++ectx.ec_stack.ga_len;
659 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200660 if (ufunc->uf_va_name != NULL)
661 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662
663 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200664 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
665 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666
667 // dummy frame entries
668 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
669 {
670 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
671 ++ectx.ec_stack.ga_len;
672 }
673
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200674 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200675 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200676 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
677 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200678 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200680 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200681 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200682 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200683
684 ectx.ec_instr = dfunc->df_instr;
685 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100686
Bram Moolenaara26b9702020-04-18 19:53:28 +0200687 // Commands behave like vim9script.
688 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
689
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100690 // Decide where to start execution, handles optional arguments.
691 init_instr_idx(ufunc, argc, &ectx);
692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 for (;;)
694 {
695 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100696
697 veryfast_breakcheck();
698 if (got_int)
699 {
700 // Turn CTRL-C into an exception.
701 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100702 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100703 goto failed;
704 did_throw = TRUE;
705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706
Bram Moolenaara26b9702020-04-18 19:53:28 +0200707 if (did_emsg && msg_list != NULL && *msg_list != NULL)
708 {
709 // Turn an error message into an exception.
710 did_emsg = FALSE;
711 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
712 goto failed;
713 did_throw = TRUE;
714 *msg_list = NULL;
715 }
716
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 if (did_throw && !ectx.ec_in_catch)
718 {
719 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100720 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100721
722 // An exception jumps to the first catch, finally, or returns from
723 // the current function.
724 if (trystack->ga_len > 0)
725 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200726 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 {
728 // jump to ":catch" or ":finally"
729 ectx.ec_in_catch = TRUE;
730 ectx.ec_iidx = trycmd->tcd_catch_idx;
731 }
732 else
733 {
734 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200735 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100736 {
737 // At the toplevel we are done. Push a dummy return value.
738 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
739 goto failed;
740 tv = STACK_TV_BOT(0);
741 tv->v_type = VAR_NUMBER;
742 tv->vval.v_number = 0;
743 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100744 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200745 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
746 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747 goto done;
748 }
749
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200750 if (func_return(&ectx) == FAIL)
751 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 }
753 continue;
754 }
755
756 iptr = &ectx.ec_instr[ectx.ec_iidx++];
757 switch (iptr->isn_type)
758 {
759 // execute Ex command line
760 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200761 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 do_cmdline_cmd(iptr->isn_arg.string);
763 break;
764
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200765 // execute Ex command from pieces on the stack
766 case ISN_EXECCONCAT:
767 {
768 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200769 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200770 int pass;
771 int i;
772 char_u *cmd = NULL;
773 char_u *str;
774
775 for (pass = 1; pass <= 2; ++pass)
776 {
777 for (i = 0; i < count; ++i)
778 {
779 tv = STACK_TV_BOT(i - count);
780 str = tv->vval.v_string;
781 if (str != NULL && *str != NUL)
782 {
783 if (pass == 2)
784 STRCPY(cmd + len, str);
785 len += STRLEN(str);
786 }
787 if (pass == 2)
788 clear_tv(tv);
789 }
790 if (pass == 1)
791 {
792 cmd = alloc(len + 1);
793 if (cmd == NULL)
794 goto failed;
795 len = 0;
796 }
797 }
798
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200799 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200800 do_cmdline_cmd(cmd);
801 vim_free(cmd);
802 }
803 break;
804
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100805 // execute :echo {string} ...
806 case ISN_ECHO:
807 {
808 int count = iptr->isn_arg.echo.echo_count;
809 int atstart = TRUE;
810 int needclr = TRUE;
811
812 for (idx = 0; idx < count; ++idx)
813 {
814 tv = STACK_TV_BOT(idx - count);
815 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
816 &atstart, &needclr);
817 clear_tv(tv);
818 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100819 if (needclr)
820 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 ectx.ec_stack.ga_len -= count;
822 }
823 break;
824
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200825 // :execute {string} ...
826 // :echomsg {string} ...
827 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100828 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200829 case ISN_ECHOMSG:
830 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100831 {
832 int count = iptr->isn_arg.number;
833 garray_T ga;
834 char_u buf[NUMBUFLEN];
835 char_u *p;
836 int len;
837 int failed = FALSE;
838
839 ga_init2(&ga, 1, 80);
840 for (idx = 0; idx < count; ++idx)
841 {
842 tv = STACK_TV_BOT(idx - count);
843 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
844 {
845 emsg(_(e_inval_string));
846 break;
847 }
848 else
849 p = tv_get_string_buf(tv, buf);
850
851 len = (int)STRLEN(p);
852 if (ga_grow(&ga, len + 2) == FAIL)
853 failed = TRUE;
854 else
855 {
856 if (ga.ga_len > 0)
857 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
858 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
859 ga.ga_len += len;
860 }
861 clear_tv(tv);
862 }
863 ectx.ec_stack.ga_len -= count;
864
865 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200866 {
867 if (iptr->isn_type == ISN_EXECUTE)
868 do_cmdline_cmd((char_u *)ga.ga_data);
869 else
870 {
871 msg_sb_eol();
872 if (iptr->isn_type == ISN_ECHOMSG)
873 {
874 msg_attr(ga.ga_data, echo_attr);
875 out_flush();
876 }
877 else
878 {
879 int save_did_emsg = did_emsg;
880
881 SOURCING_LNUM = iptr->isn_lnum;
882 emsg(ga.ga_data);
883 if (!force_abort)
884 // We don't want to abort following
885 // commands, restore did_emsg.
886 did_emsg = save_did_emsg;
887 }
888 }
889 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100890 ga_clear(&ga);
891 }
892 break;
893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 // load local variable or argument
895 case ISN_LOAD:
896 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
897 goto failed;
898 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
899 ++ectx.ec_stack.ga_len;
900 break;
901
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200902 // load variable or argument from outer scope
903 case ISN_LOADOUTER:
904 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
905 goto failed;
906 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
907 STACK_TV_BOT(0));
908 ++ectx.ec_stack.ga_len;
909 break;
910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 // load v: variable
912 case ISN_LOADV:
913 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
914 goto failed;
915 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
916 ++ectx.ec_stack.ga_len;
917 break;
918
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100919 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 case ISN_LOADSCRIPT:
921 {
922 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100923 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100924 svar_T *sv;
925
926 sv = ((svar_T *)si->sn_var_vals.ga_data)
927 + iptr->isn_arg.script.script_idx;
928 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
929 goto failed;
930 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
931 ++ectx.ec_stack.ga_len;
932 }
933 break;
934
935 // load s: variable in old script
936 case ISN_LOADS:
937 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100938 hashtab_T *ht = &SCRIPT_VARS(
939 iptr->isn_arg.loadstore.ls_sid);
940 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100941 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100942
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100943 if (di == NULL)
944 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100945 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 goto failed;
947 }
948 else
949 {
950 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
951 goto failed;
952 copy_tv(&di->di_tv, STACK_TV_BOT(0));
953 ++ectx.ec_stack.ga_len;
954 }
955 }
956 break;
957
Bram Moolenaard3aac292020-04-19 14:32:17 +0200958 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100959 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200960 case ISN_LOADB:
961 case ISN_LOADW:
962 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200964 dictitem_T *di = NULL;
965 hashtab_T *ht = NULL;
966 char namespace;
967 switch (iptr->isn_type)
968 {
969 case ISN_LOADG:
970 ht = get_globvar_ht();
971 namespace = 'g';
972 break;
973 case ISN_LOADB:
974 ht = &curbuf->b_vars->dv_hashtab;
975 namespace = 'b';
976 break;
977 case ISN_LOADW:
978 ht = &curwin->w_vars->dv_hashtab;
979 namespace = 'w';
980 break;
981 case ISN_LOADT:
982 ht = &curtab->tp_vars->dv_hashtab;
983 namespace = 't';
984 break;
985 default: // Cannot reach here
986 goto failed;
987 }
988 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 if (di == NULL)
991 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200992 semsg(_("E121: Undefined variable: %c:%s"),
993 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 goto failed;
995 }
996 else
997 {
998 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
999 goto failed;
1000 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1001 ++ectx.ec_stack.ga_len;
1002 }
1003 }
1004 break;
1005
1006 // load &option
1007 case ISN_LOADOPT:
1008 {
1009 typval_T optval;
1010 char_u *name = iptr->isn_arg.string;
1011
Bram Moolenaara8c17702020-04-01 21:17:24 +02001012 // This is not expected to fail, name is checked during
1013 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001014 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1015 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001016 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1017 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001018 *STACK_TV_BOT(0) = optval;
1019 ++ectx.ec_stack.ga_len;
1020 }
1021 break;
1022
1023 // load $ENV
1024 case ISN_LOADENV:
1025 {
1026 typval_T optval;
1027 char_u *name = iptr->isn_arg.string;
1028
1029 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1030 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001031 // name is always valid, checked when compiling
1032 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 *STACK_TV_BOT(0) = optval;
1034 ++ectx.ec_stack.ga_len;
1035 }
1036 break;
1037
1038 // load @register
1039 case ISN_LOADREG:
1040 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1041 goto failed;
1042 tv = STACK_TV_BOT(0);
1043 tv->v_type = VAR_STRING;
1044 tv->vval.v_string = get_reg_contents(
1045 iptr->isn_arg.number, GREG_EXPR_SRC);
1046 ++ectx.ec_stack.ga_len;
1047 break;
1048
1049 // store local variable
1050 case ISN_STORE:
1051 --ectx.ec_stack.ga_len;
1052 tv = STACK_TV_VAR(iptr->isn_arg.number);
1053 clear_tv(tv);
1054 *tv = *STACK_TV_BOT(0);
1055 break;
1056
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001057 // store s: variable in old script
1058 case ISN_STORES:
1059 {
1060 hashtab_T *ht = &SCRIPT_VARS(
1061 iptr->isn_arg.loadstore.ls_sid);
1062 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001063 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001064
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001065 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001066 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001067 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001068 else
1069 {
1070 clear_tv(&di->di_tv);
1071 di->di_tv = *STACK_TV_BOT(0);
1072 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001073 }
1074 break;
1075
1076 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 case ISN_STORESCRIPT:
1078 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001079 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 iptr->isn_arg.script.script_sid);
1081 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1082 + iptr->isn_arg.script.script_idx;
1083
1084 --ectx.ec_stack.ga_len;
1085 clear_tv(sv->sv_tv);
1086 *sv->sv_tv = *STACK_TV_BOT(0);
1087 }
1088 break;
1089
1090 // store option
1091 case ISN_STOREOPT:
1092 {
1093 long n = 0;
1094 char_u *s = NULL;
1095 char *msg;
1096
1097 --ectx.ec_stack.ga_len;
1098 tv = STACK_TV_BOT(0);
1099 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001100 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001102 if (s == NULL)
1103 s = (char_u *)"";
1104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 else if (tv->v_type == VAR_NUMBER)
1106 n = tv->vval.v_number;
1107 else
1108 {
1109 emsg(_("E1051: Expected string or number"));
1110 goto failed;
1111 }
1112 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1113 n, s, iptr->isn_arg.storeopt.so_flags);
1114 if (msg != NULL)
1115 {
1116 emsg(_(msg));
1117 goto failed;
1118 }
1119 clear_tv(tv);
1120 }
1121 break;
1122
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 // store $ENV
1124 case ISN_STOREENV:
1125 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001126 tv = STACK_TV_BOT(0);
1127 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1128 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001129 break;
1130
1131 // store @r
1132 case ISN_STOREREG:
1133 {
1134 int reg = iptr->isn_arg.number;
1135
1136 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001137 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001138 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001139 tv_get_string(tv), -1, FALSE);
1140 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141 }
1142 break;
1143
1144 // store v: variable
1145 case ISN_STOREV:
1146 --ectx.ec_stack.ga_len;
1147 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1148 == FAIL)
1149 goto failed;
1150 break;
1151
Bram Moolenaard3aac292020-04-19 14:32:17 +02001152 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001154 case ISN_STOREB:
1155 case ISN_STOREW:
1156 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 {
1158 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001159 hashtab_T *ht;
1160 switch (iptr->isn_type)
1161 {
1162 case ISN_STOREG:
1163 ht = get_globvar_ht();
1164 break;
1165 case ISN_STOREB:
1166 ht = &curbuf->b_vars->dv_hashtab;
1167 break;
1168 case ISN_STOREW:
1169 ht = &curwin->w_vars->dv_hashtab;
1170 break;
1171 case ISN_STORET:
1172 ht = &curtab->tp_vars->dv_hashtab;
1173 break;
1174 default: // Cannot reach here
1175 goto failed;
1176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177
1178 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001179 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001181 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 else
1183 {
1184 clear_tv(&di->di_tv);
1185 di->di_tv = *STACK_TV_BOT(0);
1186 }
1187 }
1188 break;
1189
1190 // store number in local variable
1191 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001192 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 clear_tv(tv);
1194 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001195 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 break;
1197
1198 // push constant
1199 case ISN_PUSHNR:
1200 case ISN_PUSHBOOL:
1201 case ISN_PUSHSPEC:
1202 case ISN_PUSHF:
1203 case ISN_PUSHS:
1204 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001205 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001206 case ISN_PUSHCHANNEL:
1207 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1209 goto failed;
1210 tv = STACK_TV_BOT(0);
1211 ++ectx.ec_stack.ga_len;
1212 switch (iptr->isn_type)
1213 {
1214 case ISN_PUSHNR:
1215 tv->v_type = VAR_NUMBER;
1216 tv->vval.v_number = iptr->isn_arg.number;
1217 break;
1218 case ISN_PUSHBOOL:
1219 tv->v_type = VAR_BOOL;
1220 tv->vval.v_number = iptr->isn_arg.number;
1221 break;
1222 case ISN_PUSHSPEC:
1223 tv->v_type = VAR_SPECIAL;
1224 tv->vval.v_number = iptr->isn_arg.number;
1225 break;
1226#ifdef FEAT_FLOAT
1227 case ISN_PUSHF:
1228 tv->v_type = VAR_FLOAT;
1229 tv->vval.v_float = iptr->isn_arg.fnumber;
1230 break;
1231#endif
1232 case ISN_PUSHBLOB:
1233 blob_copy(iptr->isn_arg.blob, tv);
1234 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001235 case ISN_PUSHFUNC:
1236 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001237 if (iptr->isn_arg.string == NULL)
1238 tv->vval.v_string = NULL;
1239 else
1240 tv->vval.v_string =
1241 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001242 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001243 case ISN_PUSHCHANNEL:
1244#ifdef FEAT_JOB_CHANNEL
1245 tv->v_type = VAR_CHANNEL;
1246 tv->vval.v_channel = iptr->isn_arg.channel;
1247 if (tv->vval.v_channel != NULL)
1248 ++tv->vval.v_channel->ch_refcount;
1249#endif
1250 break;
1251 case ISN_PUSHJOB:
1252#ifdef FEAT_JOB_CHANNEL
1253 tv->v_type = VAR_JOB;
1254 tv->vval.v_job = iptr->isn_arg.job;
1255 if (tv->vval.v_job != NULL)
1256 ++tv->vval.v_job->jv_refcount;
1257#endif
1258 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 default:
1260 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001261 tv->vval.v_string = vim_strsave(
1262 iptr->isn_arg.string == NULL
1263 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264 }
1265 break;
1266
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001267 case ISN_UNLET:
1268 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1269 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1270 goto failed;
1271 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001272 case ISN_UNLETENV:
1273 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1274 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001275
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 // create a list from items on the stack; uses a single allocation
1277 // for the list header and the items
1278 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001279 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1280 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 break;
1282
1283 // create a dict from items on the stack
1284 case ISN_NEWDICT:
1285 {
1286 int count = iptr->isn_arg.number;
1287 dict_T *dict = dict_alloc();
1288 dictitem_T *item;
1289
1290 if (dict == NULL)
1291 goto failed;
1292 for (idx = 0; idx < count; ++idx)
1293 {
1294 // check key type is VAR_STRING
1295 tv = STACK_TV_BOT(2 * (idx - count));
1296 item = dictitem_alloc(tv->vval.v_string);
1297 clear_tv(tv);
1298 if (item == NULL)
1299 goto failed;
1300 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1301 item->di_tv.v_lock = 0;
1302 if (dict_add(dict, item) == FAIL)
1303 goto failed;
1304 }
1305
1306 if (count > 0)
1307 ectx.ec_stack.ga_len -= 2 * count - 1;
1308 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1309 goto failed;
1310 else
1311 ++ectx.ec_stack.ga_len;
1312 tv = STACK_TV_BOT(-1);
1313 tv->v_type = VAR_DICT;
1314 tv->vval.v_dict = dict;
1315 ++dict->dv_refcount;
1316 }
1317 break;
1318
1319 // call a :def function
1320 case ISN_DCALL:
1321 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1322 iptr->isn_arg.dfunc.cdf_argcount,
1323 &ectx) == FAIL)
1324 goto failed;
1325 break;
1326
1327 // call a builtin function
1328 case ISN_BCALL:
1329 SOURCING_LNUM = iptr->isn_lnum;
1330 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1331 iptr->isn_arg.bfunc.cbf_argcount,
1332 &ectx) == FAIL)
1333 goto failed;
1334 break;
1335
1336 // call a funcref or partial
1337 case ISN_PCALL:
1338 {
1339 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1340 int r;
1341 typval_T partial;
1342
1343 SOURCING_LNUM = iptr->isn_lnum;
1344 if (pfunc->cpf_top)
1345 {
1346 // funcref is above the arguments
1347 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1348 }
1349 else
1350 {
1351 // Get the funcref from the stack.
1352 --ectx.ec_stack.ga_len;
1353 partial = *STACK_TV_BOT(0);
1354 tv = &partial;
1355 }
1356 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1357 if (tv == &partial)
1358 clear_tv(&partial);
1359 if (r == FAIL)
1360 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 }
1362 break;
1363
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001364 case ISN_PCALL_END:
1365 // PCALL finished, arguments have been consumed and replaced by
1366 // the return value. Now clear the funcref from the stack,
1367 // and move the return value in its place.
1368 --ectx.ec_stack.ga_len;
1369 clear_tv(STACK_TV_BOT(-1));
1370 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1371 break;
1372
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 // call a user defined function or funcref/partial
1374 case ISN_UCALL:
1375 {
1376 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1377
1378 SOURCING_LNUM = iptr->isn_lnum;
1379 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001380 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 goto failed;
1382 }
1383 break;
1384
1385 // return from a :def function call
1386 case ISN_RETURN:
1387 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001388 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001389 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001390
1391 if (trystack->ga_len > 0)
1392 trycmd = ((trycmd_T *)trystack->ga_data)
1393 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001394 if (trycmd != NULL
1395 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 && trycmd->tcd_finally_idx != 0)
1397 {
1398 // jump to ":finally"
1399 ectx.ec_iidx = trycmd->tcd_finally_idx;
1400 trycmd->tcd_return = TRUE;
1401 }
1402 else
1403 {
1404 // Restore previous function. If the frame pointer
1405 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001406 if (ectx.ec_frame_idx == initial_frame_idx)
1407 {
1408 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1409 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001411 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001413 if (func_return(&ectx) == FAIL)
1414 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001415 }
1416 }
1417 break;
1418
1419 // push a function reference to a compiled function
1420 case ISN_FUNCREF:
1421 {
1422 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001423 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424
1425 pt = ALLOC_CLEAR_ONE(partial_T);
1426 if (pt == NULL)
1427 goto failed;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001428 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001429 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001430 vim_free(pt);
1431 goto failed;
1432 }
1433 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1434 + iptr->isn_arg.funcref.fr_func;
1435 pt->pt_func = pt_dfunc->df_ufunc;
1436 pt->pt_refcount = 1;
1437 ++pt_dfunc->df_ufunc->uf_refcount;
1438
1439 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1440 {
1441 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1442 + ectx.ec_dfunc_idx;
1443
1444 // The closure needs to find arguments and local
1445 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001446 pt->pt_ectx_stack = &ectx.ec_stack;
1447 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001448
1449 // If this function returns and the closure is still
1450 // used, we need to make a copy of the context
1451 // (arguments and local variables). Store a reference
1452 // to the partial so we can handle that.
1453 ++pt->pt_refcount;
1454 tv = STACK_TV_VAR(dfunc->df_varcount
1455 + iptr->isn_arg.funcref.fr_var_idx);
1456 if (tv->v_type == VAR_PARTIAL)
1457 {
1458 // TODO: use a garray_T on ectx.
1459 emsg("Multiple closures not supported yet");
1460 goto failed;
1461 }
1462 tv->v_type = VAR_PARTIAL;
1463 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001464 }
1465
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001466 tv = STACK_TV_BOT(0);
1467 ++ectx.ec_stack.ga_len;
1468 tv->vval.v_partial = pt;
1469 tv->v_type = VAR_PARTIAL;
1470 }
1471 break;
1472
1473 // jump if a condition is met
1474 case ISN_JUMP:
1475 {
1476 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1477 int jump = TRUE;
1478
1479 if (when != JUMP_ALWAYS)
1480 {
1481 tv = STACK_TV_BOT(-1);
1482 jump = tv2bool(tv);
1483 if (when == JUMP_IF_FALSE
1484 || when == JUMP_AND_KEEP_IF_FALSE)
1485 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001486 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487 {
1488 // drop the value from the stack
1489 clear_tv(tv);
1490 --ectx.ec_stack.ga_len;
1491 }
1492 }
1493 if (jump)
1494 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1495 }
1496 break;
1497
1498 // top of a for loop
1499 case ISN_FOR:
1500 {
1501 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1502 typval_T *idxtv =
1503 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1504
1505 // push the next item from the list
1506 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1507 goto failed;
1508 if (++idxtv->vval.v_number >= list->lv_len)
1509 // past the end of the list, jump to "endfor"
1510 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1511 else if (list->lv_first == &range_list_item)
1512 {
1513 // non-materialized range() list
1514 tv = STACK_TV_BOT(0);
1515 tv->v_type = VAR_NUMBER;
1516 tv->vval.v_number = list_find_nr(
1517 list, idxtv->vval.v_number, NULL);
1518 ++ectx.ec_stack.ga_len;
1519 }
1520 else
1521 {
1522 listitem_T *li = list_find(list, idxtv->vval.v_number);
1523
1524 if (li == NULL)
1525 goto failed;
1526 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1527 ++ectx.ec_stack.ga_len;
1528 }
1529 }
1530 break;
1531
1532 // start of ":try" block
1533 case ISN_TRY:
1534 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001535 trycmd_T *trycmd = NULL;
1536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001537 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1538 goto failed;
1539 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1540 + ectx.ec_trystack.ga_len;
1541 ++ectx.ec_trystack.ga_len;
1542 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001543 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001544 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1545 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001546 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 }
1548 break;
1549
1550 case ISN_PUSHEXC:
1551 if (current_exception == NULL)
1552 {
1553 iemsg("Evaluating catch while current_exception is NULL");
1554 goto failed;
1555 }
1556 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1557 goto failed;
1558 tv = STACK_TV_BOT(0);
1559 ++ectx.ec_stack.ga_len;
1560 tv->v_type = VAR_STRING;
1561 tv->vval.v_string = vim_strsave(
1562 (char_u *)current_exception->value);
1563 break;
1564
1565 case ISN_CATCH:
1566 {
1567 garray_T *trystack = &ectx.ec_trystack;
1568
1569 if (trystack->ga_len > 0)
1570 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001571 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 + trystack->ga_len - 1;
1573 trycmd->tcd_caught = TRUE;
1574 }
1575 did_emsg = got_int = did_throw = FALSE;
1576 catch_exception(current_exception);
1577 }
1578 break;
1579
1580 // end of ":try" block
1581 case ISN_ENDTRY:
1582 {
1583 garray_T *trystack = &ectx.ec_trystack;
1584
1585 if (trystack->ga_len > 0)
1586 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001587 trycmd_T *trycmd = NULL;
1588
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 --trystack->ga_len;
1590 --trylevel;
1591 trycmd = ((trycmd_T *)trystack->ga_data)
1592 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001593 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 {
1595 // discard the exception
1596 if (caught_stack == current_exception)
1597 caught_stack = caught_stack->caught;
1598 discard_current_exception();
1599 }
1600
1601 if (trycmd->tcd_return)
1602 {
1603 // Restore previous function. If the frame pointer
1604 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001605 if (ectx.ec_frame_idx == initial_frame_idx)
1606 {
1607 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1608 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001609 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001610 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001612 if (func_return(&ectx) == FAIL)
1613 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 }
1615 }
1616 }
1617 break;
1618
1619 case ISN_THROW:
1620 --ectx.ec_stack.ga_len;
1621 tv = STACK_TV_BOT(0);
1622 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1623 {
1624 vim_free(tv->vval.v_string);
1625 goto failed;
1626 }
1627 did_throw = TRUE;
1628 break;
1629
1630 // compare with special values
1631 case ISN_COMPAREBOOL:
1632 case ISN_COMPARESPECIAL:
1633 {
1634 typval_T *tv1 = STACK_TV_BOT(-2);
1635 typval_T *tv2 = STACK_TV_BOT(-1);
1636 varnumber_T arg1 = tv1->vval.v_number;
1637 varnumber_T arg2 = tv2->vval.v_number;
1638 int res;
1639
1640 switch (iptr->isn_arg.op.op_type)
1641 {
1642 case EXPR_EQUAL: res = arg1 == arg2; break;
1643 case EXPR_NEQUAL: res = arg1 != arg2; break;
1644 default: res = 0; break;
1645 }
1646
1647 --ectx.ec_stack.ga_len;
1648 tv1->v_type = VAR_BOOL;
1649 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1650 }
1651 break;
1652
1653 // Operation with two number arguments
1654 case ISN_OPNR:
1655 case ISN_COMPARENR:
1656 {
1657 typval_T *tv1 = STACK_TV_BOT(-2);
1658 typval_T *tv2 = STACK_TV_BOT(-1);
1659 varnumber_T arg1 = tv1->vval.v_number;
1660 varnumber_T arg2 = tv2->vval.v_number;
1661 varnumber_T res;
1662
1663 switch (iptr->isn_arg.op.op_type)
1664 {
1665 case EXPR_MULT: res = arg1 * arg2; break;
1666 case EXPR_DIV: res = arg1 / arg2; break;
1667 case EXPR_REM: res = arg1 % arg2; break;
1668 case EXPR_SUB: res = arg1 - arg2; break;
1669 case EXPR_ADD: res = arg1 + arg2; break;
1670
1671 case EXPR_EQUAL: res = arg1 == arg2; break;
1672 case EXPR_NEQUAL: res = arg1 != arg2; break;
1673 case EXPR_GREATER: res = arg1 > arg2; break;
1674 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1675 case EXPR_SMALLER: res = arg1 < arg2; break;
1676 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1677 default: res = 0; break;
1678 }
1679
1680 --ectx.ec_stack.ga_len;
1681 if (iptr->isn_type == ISN_COMPARENR)
1682 {
1683 tv1->v_type = VAR_BOOL;
1684 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1685 }
1686 else
1687 tv1->vval.v_number = res;
1688 }
1689 break;
1690
1691 // Computation with two float arguments
1692 case ISN_OPFLOAT:
1693 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001694#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 {
1696 typval_T *tv1 = STACK_TV_BOT(-2);
1697 typval_T *tv2 = STACK_TV_BOT(-1);
1698 float_T arg1 = tv1->vval.v_float;
1699 float_T arg2 = tv2->vval.v_float;
1700 float_T res = 0;
1701 int cmp = FALSE;
1702
1703 switch (iptr->isn_arg.op.op_type)
1704 {
1705 case EXPR_MULT: res = arg1 * arg2; break;
1706 case EXPR_DIV: res = arg1 / arg2; break;
1707 case EXPR_SUB: res = arg1 - arg2; break;
1708 case EXPR_ADD: res = arg1 + arg2; break;
1709
1710 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1711 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1712 case EXPR_GREATER: cmp = arg1 > arg2; break;
1713 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1714 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1715 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1716 default: cmp = 0; break;
1717 }
1718 --ectx.ec_stack.ga_len;
1719 if (iptr->isn_type == ISN_COMPAREFLOAT)
1720 {
1721 tv1->v_type = VAR_BOOL;
1722 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1723 }
1724 else
1725 tv1->vval.v_float = res;
1726 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001727#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001728 break;
1729
1730 case ISN_COMPARELIST:
1731 {
1732 typval_T *tv1 = STACK_TV_BOT(-2);
1733 typval_T *tv2 = STACK_TV_BOT(-1);
1734 list_T *arg1 = tv1->vval.v_list;
1735 list_T *arg2 = tv2->vval.v_list;
1736 int cmp = FALSE;
1737 int ic = iptr->isn_arg.op.op_ic;
1738
1739 switch (iptr->isn_arg.op.op_type)
1740 {
1741 case EXPR_EQUAL: cmp =
1742 list_equal(arg1, arg2, ic, FALSE); break;
1743 case EXPR_NEQUAL: cmp =
1744 !list_equal(arg1, arg2, ic, FALSE); break;
1745 case EXPR_IS: cmp = arg1 == arg2; break;
1746 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1747 default: cmp = 0; break;
1748 }
1749 --ectx.ec_stack.ga_len;
1750 clear_tv(tv1);
1751 clear_tv(tv2);
1752 tv1->v_type = VAR_BOOL;
1753 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1754 }
1755 break;
1756
1757 case ISN_COMPAREBLOB:
1758 {
1759 typval_T *tv1 = STACK_TV_BOT(-2);
1760 typval_T *tv2 = STACK_TV_BOT(-1);
1761 blob_T *arg1 = tv1->vval.v_blob;
1762 blob_T *arg2 = tv2->vval.v_blob;
1763 int cmp = FALSE;
1764
1765 switch (iptr->isn_arg.op.op_type)
1766 {
1767 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1768 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1769 case EXPR_IS: cmp = arg1 == arg2; break;
1770 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1771 default: cmp = 0; break;
1772 }
1773 --ectx.ec_stack.ga_len;
1774 clear_tv(tv1);
1775 clear_tv(tv2);
1776 tv1->v_type = VAR_BOOL;
1777 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1778 }
1779 break;
1780
1781 // TODO: handle separately
1782 case ISN_COMPARESTRING:
1783 case ISN_COMPAREDICT:
1784 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001785 case ISN_COMPAREANY:
1786 {
1787 typval_T *tv1 = STACK_TV_BOT(-2);
1788 typval_T *tv2 = STACK_TV_BOT(-1);
1789 exptype_T exptype = iptr->isn_arg.op.op_type;
1790 int ic = iptr->isn_arg.op.op_ic;
1791
1792 typval_compare(tv1, tv2, exptype, ic);
1793 clear_tv(tv2);
1794 tv1->v_type = VAR_BOOL;
1795 tv1->vval.v_number = tv1->vval.v_number
1796 ? VVAL_TRUE : VVAL_FALSE;
1797 --ectx.ec_stack.ga_len;
1798 }
1799 break;
1800
1801 case ISN_ADDLIST:
1802 case ISN_ADDBLOB:
1803 {
1804 typval_T *tv1 = STACK_TV_BOT(-2);
1805 typval_T *tv2 = STACK_TV_BOT(-1);
1806
1807 if (iptr->isn_type == ISN_ADDLIST)
1808 eval_addlist(tv1, tv2);
1809 else
1810 eval_addblob(tv1, tv2);
1811 clear_tv(tv2);
1812 --ectx.ec_stack.ga_len;
1813 }
1814 break;
1815
1816 // Computation with two arguments of unknown type
1817 case ISN_OPANY:
1818 {
1819 typval_T *tv1 = STACK_TV_BOT(-2);
1820 typval_T *tv2 = STACK_TV_BOT(-1);
1821 varnumber_T n1, n2;
1822#ifdef FEAT_FLOAT
1823 float_T f1 = 0, f2 = 0;
1824#endif
1825 int error = FALSE;
1826
1827 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1828 {
1829 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1830 {
1831 eval_addlist(tv1, tv2);
1832 clear_tv(tv2);
1833 --ectx.ec_stack.ga_len;
1834 break;
1835 }
1836 else if (tv1->v_type == VAR_BLOB
1837 && tv2->v_type == VAR_BLOB)
1838 {
1839 eval_addblob(tv1, tv2);
1840 clear_tv(tv2);
1841 --ectx.ec_stack.ga_len;
1842 break;
1843 }
1844 }
1845#ifdef FEAT_FLOAT
1846 if (tv1->v_type == VAR_FLOAT)
1847 {
1848 f1 = tv1->vval.v_float;
1849 n1 = 0;
1850 }
1851 else
1852#endif
1853 {
1854 n1 = tv_get_number_chk(tv1, &error);
1855 if (error)
1856 goto failed;
1857#ifdef FEAT_FLOAT
1858 if (tv2->v_type == VAR_FLOAT)
1859 f1 = n1;
1860#endif
1861 }
1862#ifdef FEAT_FLOAT
1863 if (tv2->v_type == VAR_FLOAT)
1864 {
1865 f2 = tv2->vval.v_float;
1866 n2 = 0;
1867 }
1868 else
1869#endif
1870 {
1871 n2 = tv_get_number_chk(tv2, &error);
1872 if (error)
1873 goto failed;
1874#ifdef FEAT_FLOAT
1875 if (tv1->v_type == VAR_FLOAT)
1876 f2 = n2;
1877#endif
1878 }
1879#ifdef FEAT_FLOAT
1880 // if there is a float on either side the result is a float
1881 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1882 {
1883 switch (iptr->isn_arg.op.op_type)
1884 {
1885 case EXPR_MULT: f1 = f1 * f2; break;
1886 case EXPR_DIV: f1 = f1 / f2; break;
1887 case EXPR_SUB: f1 = f1 - f2; break;
1888 case EXPR_ADD: f1 = f1 + f2; break;
1889 default: emsg(_(e_modulus)); goto failed;
1890 }
1891 clear_tv(tv1);
1892 clear_tv(tv2);
1893 tv1->v_type = VAR_FLOAT;
1894 tv1->vval.v_float = f1;
1895 --ectx.ec_stack.ga_len;
1896 }
1897 else
1898#endif
1899 {
1900 switch (iptr->isn_arg.op.op_type)
1901 {
1902 case EXPR_MULT: n1 = n1 * n2; break;
1903 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1904 case EXPR_SUB: n1 = n1 - n2; break;
1905 case EXPR_ADD: n1 = n1 + n2; break;
1906 default: n1 = num_modulus(n1, n2); break;
1907 }
1908 clear_tv(tv1);
1909 clear_tv(tv2);
1910 tv1->v_type = VAR_NUMBER;
1911 tv1->vval.v_number = n1;
1912 --ectx.ec_stack.ga_len;
1913 }
1914 }
1915 break;
1916
1917 case ISN_CONCAT:
1918 {
1919 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1920 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1921 char_u *res;
1922
1923 res = concat_str(str1, str2);
1924 clear_tv(STACK_TV_BOT(-2));
1925 clear_tv(STACK_TV_BOT(-1));
1926 --ectx.ec_stack.ga_len;
1927 STACK_TV_BOT(-1)->vval.v_string = res;
1928 }
1929 break;
1930
1931 case ISN_INDEX:
1932 {
1933 list_T *list;
1934 varnumber_T n;
1935 listitem_T *li;
1936
1937 // list index: list is at stack-2, index at stack-1
1938 tv = STACK_TV_BOT(-2);
1939 if (tv->v_type != VAR_LIST)
1940 {
1941 emsg(_(e_listreq));
1942 goto failed;
1943 }
1944 list = tv->vval.v_list;
1945
1946 tv = STACK_TV_BOT(-1);
1947 if (tv->v_type != VAR_NUMBER)
1948 {
1949 emsg(_(e_number_exp));
1950 goto failed;
1951 }
1952 n = tv->vval.v_number;
1953 clear_tv(tv);
1954 if ((li = list_find(list, n)) == NULL)
1955 {
1956 semsg(_(e_listidx), n);
1957 goto failed;
1958 }
1959 --ectx.ec_stack.ga_len;
1960 clear_tv(STACK_TV_BOT(-1));
1961 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1962 }
1963 break;
1964
1965 // dict member with string key
1966 case ISN_MEMBER:
1967 {
1968 dict_T *dict;
1969 dictitem_T *di;
1970
1971 tv = STACK_TV_BOT(-1);
1972 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1973 {
1974 emsg(_(e_dictreq));
1975 goto failed;
1976 }
1977 dict = tv->vval.v_dict;
1978
1979 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1980 == NULL)
1981 {
1982 semsg(_(e_dictkey), iptr->isn_arg.string);
1983 goto failed;
1984 }
1985 clear_tv(tv);
1986 copy_tv(&di->di_tv, tv);
1987 }
1988 break;
1989
1990 case ISN_NEGATENR:
1991 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001992 if (tv->v_type != VAR_NUMBER
1993#ifdef FEAT_FLOAT
1994 && tv->v_type != VAR_FLOAT
1995#endif
1996 )
1997 {
1998 emsg(_(e_number_exp));
1999 goto failed;
2000 }
2001#ifdef FEAT_FLOAT
2002 if (tv->v_type == VAR_FLOAT)
2003 tv->vval.v_float = -tv->vval.v_float;
2004 else
2005#endif
2006 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 break;
2008
2009 case ISN_CHECKNR:
2010 {
2011 int error = FALSE;
2012
2013 tv = STACK_TV_BOT(-1);
2014 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 (void)tv_get_number_chk(tv, &error);
2017 if (error)
2018 goto failed;
2019 }
2020 break;
2021
2022 case ISN_CHECKTYPE:
2023 {
2024 checktype_T *ct = &iptr->isn_arg.type;
2025
2026 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002027 // TODO: better type comparison
2028 if (tv->v_type != ct->ct_type
2029 && !((tv->v_type == VAR_PARTIAL
2030 && ct->ct_type == VAR_FUNC)
2031 || (tv->v_type == VAR_FUNC
2032 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033 {
2034 semsg(_("E1029: Expected %s but got %s"),
2035 vartype_name(ct->ct_type),
2036 vartype_name(tv->v_type));
2037 goto failed;
2038 }
2039 }
2040 break;
2041
2042 case ISN_2BOOL:
2043 {
2044 int n;
2045
2046 tv = STACK_TV_BOT(-1);
2047 n = tv2bool(tv);
2048 if (iptr->isn_arg.number) // invert
2049 n = !n;
2050 clear_tv(tv);
2051 tv->v_type = VAR_BOOL;
2052 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2053 }
2054 break;
2055
2056 case ISN_2STRING:
2057 {
2058 char_u *str;
2059
2060 tv = STACK_TV_BOT(iptr->isn_arg.number);
2061 if (tv->v_type != VAR_STRING)
2062 {
2063 str = typval_tostring(tv);
2064 clear_tv(tv);
2065 tv->v_type = VAR_STRING;
2066 tv->vval.v_string = str;
2067 }
2068 }
2069 break;
2070
2071 case ISN_DROP:
2072 --ectx.ec_stack.ga_len;
2073 clear_tv(STACK_TV_BOT(0));
2074 break;
2075 }
2076 }
2077
2078done:
2079 // function finished, get result from the stack.
2080 tv = STACK_TV_BOT(-1);
2081 *rettv = *tv;
2082 tv->v_type = VAR_UNKNOWN;
2083 ret = OK;
2084
2085failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002086 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002087 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002088 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002089failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002090 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002091
2092 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2094 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002097 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 return ret;
2099}
2100
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002101/*
2102 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002103 * We don't really need this at runtime, but we do have tests that require it,
2104 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 */
2106 void
2107ex_disassemble(exarg_T *eap)
2108{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002109 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002110 char_u *fname;
2111 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002112 dfunc_T *dfunc;
2113 isn_T *instr;
2114 int current;
2115 int line_idx = 0;
2116 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002117 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002119 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002120 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002121 if (fname == NULL)
2122 {
2123 semsg(_(e_invarg2), eap->arg);
2124 return;
2125 }
2126
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002127 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002128 if (ufunc == NULL)
2129 {
2130 char_u *p = untrans_function_name(fname);
2131
2132 if (p != NULL)
2133 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002134 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002135 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002136 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 if (ufunc == NULL)
2138 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002139 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 return;
2141 }
2142 if (ufunc->uf_dfunc_idx < 0)
2143 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002144 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145 return;
2146 }
2147 if (ufunc->uf_name_exp != NULL)
2148 msg((char *)ufunc->uf_name_exp);
2149 else
2150 msg((char *)ufunc->uf_name);
2151
2152 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2153 instr = dfunc->df_instr;
2154 for (current = 0; current < dfunc->df_instr_count; ++current)
2155 {
2156 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002157 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158
2159 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2160 {
2161 if (current > prev_current)
2162 {
2163 msg_puts("\n\n");
2164 prev_current = current;
2165 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002166 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2167 if (line != NULL)
2168 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 }
2170
2171 switch (iptr->isn_type)
2172 {
2173 case ISN_EXEC:
2174 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2175 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002176 case ISN_EXECCONCAT:
2177 smsg("%4d EXECCONCAT %lld", current,
2178 (long long)iptr->isn_arg.number);
2179 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 case ISN_ECHO:
2181 {
2182 echo_T *echo = &iptr->isn_arg.echo;
2183
2184 smsg("%4d %s %d", current,
2185 echo->echo_with_white ? "ECHO" : "ECHON",
2186 echo->echo_count);
2187 }
2188 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002189 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002190 smsg("%4d EXECUTE %lld", current,
2191 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002192 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002193 case ISN_ECHOMSG:
2194 smsg("%4d ECHOMSG %lld", current,
2195 (long long)(iptr->isn_arg.number));
2196 break;
2197 case ISN_ECHOERR:
2198 smsg("%4d ECHOERR %lld", current,
2199 (long long)(iptr->isn_arg.number));
2200 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002201 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002202 case ISN_LOADOUTER:
2203 {
2204 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2205
2206 if (iptr->isn_arg.number < 0)
2207 smsg("%4d LOAD%s arg[%lld]", current, add,
2208 (long long)(iptr->isn_arg.number
2209 + STACK_FRAME_SIZE));
2210 else
2211 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002212 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002213 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 break;
2215 case ISN_LOADV:
2216 smsg("%4d LOADV v:%s", current,
2217 get_vim_var_name(iptr->isn_arg.number));
2218 break;
2219 case ISN_LOADSCRIPT:
2220 {
2221 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002222 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002223 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2224 + iptr->isn_arg.script.script_idx;
2225
2226 smsg("%4d LOADSCRIPT %s from %s", current,
2227 sv->sv_name, si->sn_name);
2228 }
2229 break;
2230 case ISN_LOADS:
2231 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002232 scriptitem_T *si = SCRIPT_ITEM(
2233 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002234
2235 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002236 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 }
2238 break;
2239 case ISN_LOADG:
2240 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2241 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002242 case ISN_LOADB:
2243 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2244 break;
2245 case ISN_LOADW:
2246 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2247 break;
2248 case ISN_LOADT:
2249 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2250 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 case ISN_LOADOPT:
2252 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2253 break;
2254 case ISN_LOADENV:
2255 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2256 break;
2257 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002258 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 break;
2260
2261 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002262 if (iptr->isn_arg.number < 0)
2263 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002264 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002265 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002266 smsg("%4d STORE $%lld", current,
2267 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002269 case ISN_STOREV:
2270 smsg("%4d STOREV v:%s", current,
2271 get_vim_var_name(iptr->isn_arg.number));
2272 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002274 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2275 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002276 case ISN_STOREB:
2277 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2278 break;
2279 case ISN_STOREW:
2280 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2281 break;
2282 case ISN_STORET:
2283 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2284 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002285 case ISN_STORES:
2286 {
2287 scriptitem_T *si = SCRIPT_ITEM(
2288 iptr->isn_arg.loadstore.ls_sid);
2289
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002290 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002291 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 break;
2294 case ISN_STORESCRIPT:
2295 {
2296 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002297 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2299 + iptr->isn_arg.script.script_idx;
2300
2301 smsg("%4d STORESCRIPT %s in %s", current,
2302 sv->sv_name, si->sn_name);
2303 }
2304 break;
2305 case ISN_STOREOPT:
2306 smsg("%4d STOREOPT &%s", current,
2307 iptr->isn_arg.storeopt.so_name);
2308 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002309 case ISN_STOREENV:
2310 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2311 break;
2312 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002313 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002314 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 case ISN_STORENR:
2316 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002317 iptr->isn_arg.storenr.stnr_val,
2318 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 break;
2320
2321 // constants
2322 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002323 smsg("%4d PUSHNR %lld", current,
2324 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 break;
2326 case ISN_PUSHBOOL:
2327 case ISN_PUSHSPEC:
2328 smsg("%4d PUSH %s", current,
2329 get_var_special_name(iptr->isn_arg.number));
2330 break;
2331 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002332#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002334#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 break;
2336 case ISN_PUSHS:
2337 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2338 break;
2339 case ISN_PUSHBLOB:
2340 {
2341 char_u *r;
2342 char_u numbuf[NUMBUFLEN];
2343 char_u *tofree;
2344
2345 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002346 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 vim_free(tofree);
2348 }
2349 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002350 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002351 {
2352 char *name = (char *)iptr->isn_arg.string;
2353
2354 smsg("%4d PUSHFUNC \"%s\"", current,
2355 name == NULL ? "[none]" : name);
2356 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002357 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002358 case ISN_PUSHCHANNEL:
2359#ifdef FEAT_JOB_CHANNEL
2360 {
2361 channel_T *channel = iptr->isn_arg.channel;
2362
2363 smsg("%4d PUSHCHANNEL %d", current,
2364 channel == NULL ? 0 : channel->ch_id);
2365 }
2366#endif
2367 break;
2368 case ISN_PUSHJOB:
2369#ifdef FEAT_JOB_CHANNEL
2370 {
2371 typval_T tv;
2372 char_u *name;
2373
2374 tv.v_type = VAR_JOB;
2375 tv.vval.v_job = iptr->isn_arg.job;
2376 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002377 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002378 }
2379#endif
2380 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002381 case ISN_PUSHEXC:
2382 smsg("%4d PUSH v:exception", current);
2383 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002384 case ISN_UNLET:
2385 smsg("%4d UNLET%s %s", current,
2386 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2387 iptr->isn_arg.unlet.ul_name);
2388 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002389 case ISN_UNLETENV:
2390 smsg("%4d UNLETENV%s $%s", current,
2391 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2392 iptr->isn_arg.unlet.ul_name);
2393 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002395 smsg("%4d NEWLIST size %lld", current,
2396 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 break;
2398 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002399 smsg("%4d NEWDICT size %lld", current,
2400 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 break;
2402
2403 // function call
2404 case ISN_BCALL:
2405 {
2406 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2407
2408 smsg("%4d BCALL %s(argc %d)", current,
2409 internal_func_name(cbfunc->cbf_idx),
2410 cbfunc->cbf_argcount);
2411 }
2412 break;
2413 case ISN_DCALL:
2414 {
2415 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2416 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2417 + cdfunc->cdf_idx;
2418
2419 smsg("%4d DCALL %s(argc %d)", current,
2420 df->df_ufunc->uf_name_exp != NULL
2421 ? df->df_ufunc->uf_name_exp
2422 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2423 }
2424 break;
2425 case ISN_UCALL:
2426 {
2427 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2428
2429 smsg("%4d UCALL %s(argc %d)", current,
2430 cufunc->cuf_name, cufunc->cuf_argcount);
2431 }
2432 break;
2433 case ISN_PCALL:
2434 {
2435 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2436
2437 smsg("%4d PCALL%s (argc %d)", current,
2438 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2439 }
2440 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002441 case ISN_PCALL_END:
2442 smsg("%4d PCALL end", current);
2443 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 case ISN_RETURN:
2445 smsg("%4d RETURN", current);
2446 break;
2447 case ISN_FUNCREF:
2448 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002449 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002450 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002451 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002452
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002453 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002454 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002455 }
2456 break;
2457
2458 case ISN_JUMP:
2459 {
2460 char *when = "?";
2461
2462 switch (iptr->isn_arg.jump.jump_when)
2463 {
2464 case JUMP_ALWAYS:
2465 when = "JUMP";
2466 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002467 case JUMP_AND_KEEP_IF_TRUE:
2468 when = "JUMP_AND_KEEP_IF_TRUE";
2469 break;
2470 case JUMP_IF_FALSE:
2471 when = "JUMP_IF_FALSE";
2472 break;
2473 case JUMP_AND_KEEP_IF_FALSE:
2474 when = "JUMP_AND_KEEP_IF_FALSE";
2475 break;
2476 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002477 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 iptr->isn_arg.jump.jump_where);
2479 }
2480 break;
2481
2482 case ISN_FOR:
2483 {
2484 forloop_T *forloop = &iptr->isn_arg.forloop;
2485
2486 smsg("%4d FOR $%d -> %d", current,
2487 forloop->for_idx, forloop->for_end);
2488 }
2489 break;
2490
2491 case ISN_TRY:
2492 {
2493 try_T *try = &iptr->isn_arg.try;
2494
2495 smsg("%4d TRY catch -> %d, finally -> %d", current,
2496 try->try_catch, try->try_finally);
2497 }
2498 break;
2499 case ISN_CATCH:
2500 // TODO
2501 smsg("%4d CATCH", current);
2502 break;
2503 case ISN_ENDTRY:
2504 smsg("%4d ENDTRY", current);
2505 break;
2506 case ISN_THROW:
2507 smsg("%4d THROW", current);
2508 break;
2509
2510 // expression operations on number
2511 case ISN_OPNR:
2512 case ISN_OPFLOAT:
2513 case ISN_OPANY:
2514 {
2515 char *what;
2516 char *ins;
2517
2518 switch (iptr->isn_arg.op.op_type)
2519 {
2520 case EXPR_MULT: what = "*"; break;
2521 case EXPR_DIV: what = "/"; break;
2522 case EXPR_REM: what = "%"; break;
2523 case EXPR_SUB: what = "-"; break;
2524 case EXPR_ADD: what = "+"; break;
2525 default: what = "???"; break;
2526 }
2527 switch (iptr->isn_type)
2528 {
2529 case ISN_OPNR: ins = "OPNR"; break;
2530 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2531 case ISN_OPANY: ins = "OPANY"; break;
2532 default: ins = "???"; break;
2533 }
2534 smsg("%4d %s %s", current, ins, what);
2535 }
2536 break;
2537
2538 case ISN_COMPAREBOOL:
2539 case ISN_COMPARESPECIAL:
2540 case ISN_COMPARENR:
2541 case ISN_COMPAREFLOAT:
2542 case ISN_COMPARESTRING:
2543 case ISN_COMPAREBLOB:
2544 case ISN_COMPARELIST:
2545 case ISN_COMPAREDICT:
2546 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 case ISN_COMPAREANY:
2548 {
2549 char *p;
2550 char buf[10];
2551 char *type;
2552
2553 switch (iptr->isn_arg.op.op_type)
2554 {
2555 case EXPR_EQUAL: p = "=="; break;
2556 case EXPR_NEQUAL: p = "!="; break;
2557 case EXPR_GREATER: p = ">"; break;
2558 case EXPR_GEQUAL: p = ">="; break;
2559 case EXPR_SMALLER: p = "<"; break;
2560 case EXPR_SEQUAL: p = "<="; break;
2561 case EXPR_MATCH: p = "=~"; break;
2562 case EXPR_IS: p = "is"; break;
2563 case EXPR_ISNOT: p = "isnot"; break;
2564 case EXPR_NOMATCH: p = "!~"; break;
2565 default: p = "???"; break;
2566 }
2567 STRCPY(buf, p);
2568 if (iptr->isn_arg.op.op_ic == TRUE)
2569 strcat(buf, "?");
2570 switch(iptr->isn_type)
2571 {
2572 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2573 case ISN_COMPARESPECIAL:
2574 type = "COMPARESPECIAL"; break;
2575 case ISN_COMPARENR: type = "COMPARENR"; break;
2576 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2577 case ISN_COMPARESTRING:
2578 type = "COMPARESTRING"; break;
2579 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2580 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2581 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2582 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2584 default: type = "???"; break;
2585 }
2586
2587 smsg("%4d %s %s", current, type, buf);
2588 }
2589 break;
2590
2591 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2592 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2593
2594 // expression operations
2595 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2596 case ISN_INDEX: smsg("%4d INDEX", current); break;
2597 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2598 iptr->isn_arg.string); break;
2599 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2600
2601 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2602 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2603 vartype_name(iptr->isn_arg.type.ct_type),
2604 iptr->isn_arg.type.ct_off);
2605 break;
2606 case ISN_2BOOL: if (iptr->isn_arg.number)
2607 smsg("%4d INVERT (!val)", current);
2608 else
2609 smsg("%4d 2BOOL (!!val)", current);
2610 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002611 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2612 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002613 break;
2614
2615 case ISN_DROP: smsg("%4d DROP", current); break;
2616 }
2617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002618}
2619
2620/*
2621 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2622 * list, etc. Mostly like what JavaScript does, except that empty list and
2623 * empty dictionary are FALSE.
2624 */
2625 int
2626tv2bool(typval_T *tv)
2627{
2628 switch (tv->v_type)
2629 {
2630 case VAR_NUMBER:
2631 return tv->vval.v_number != 0;
2632 case VAR_FLOAT:
2633#ifdef FEAT_FLOAT
2634 return tv->vval.v_float != 0.0;
2635#else
2636 break;
2637#endif
2638 case VAR_PARTIAL:
2639 return tv->vval.v_partial != NULL;
2640 case VAR_FUNC:
2641 case VAR_STRING:
2642 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2643 case VAR_LIST:
2644 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2645 case VAR_DICT:
2646 return tv->vval.v_dict != NULL
2647 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2648 case VAR_BOOL:
2649 case VAR_SPECIAL:
2650 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2651 case VAR_JOB:
2652#ifdef FEAT_JOB_CHANNEL
2653 return tv->vval.v_job != NULL;
2654#else
2655 break;
2656#endif
2657 case VAR_CHANNEL:
2658#ifdef FEAT_JOB_CHANNEL
2659 return tv->vval.v_channel != NULL;
2660#else
2661 break;
2662#endif
2663 case VAR_BLOB:
2664 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2665 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002666 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 case VAR_VOID:
2668 break;
2669 }
2670 return FALSE;
2671}
2672
2673/*
2674 * If "tv" is a string give an error and return FAIL.
2675 */
2676 int
2677check_not_string(typval_T *tv)
2678{
2679 if (tv->v_type == VAR_STRING)
2680 {
2681 emsg(_("E1030: Using a String as a Number"));
2682 clear_tv(tv);
2683 return FAIL;
2684 }
2685 return OK;
2686}
2687
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688
2689#endif // FEAT_EVAL