blob: b0e35b6f36d3147d2f0dab4a79b71ae390006f1d [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 {
27 int tcd_frame; // ec_frame when ISN_TRY was encountered
28 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
59 int ec_frame; // index in ec_stack: context of ec_dfunc_idx
60
61 garray_T ec_trystack; // stack of trycmd_T values
62 int ec_in_catch; // when TRUE in catch or finally block
63
64 int ec_dfunc_idx; // current function index
65 isn_T *ec_instr; // array with instructions
66 int ec_iidx; // index in ec_instr: instruction to execute
67} ectx_T;
68
69// Get pointer to item relative to the bottom of the stack, -1 is the last one.
70#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
71
72/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010073 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010074 */
75 static int
76ufunc_argcount(ufunc_T *ufunc)
77{
78 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
79}
80
81/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Set the instruction index, depending on omitted arguments, where the default
83 * values are to be computed. If all optional arguments are present, start
84 * with the function body.
85 * The expression evaluation is at the start of the instructions:
86 * 0 -> EVAL default1
87 * STORE arg[-2]
88 * 1 -> EVAL default2
89 * STORE arg[-1]
90 * 2 -> function body
91 */
92 static void
93init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
94{
95 if (ufunc->uf_def_args.ga_len == 0)
96 ectx->ec_iidx = 0;
97 else
98 {
99 int defcount = ufunc->uf_args.ga_len - argcount;
100
101 // If there is a varargs argument defcount can be negative, no defaults
102 // to evaluate then.
103 if (defcount < 0)
104 defcount = 0;
105 ectx->ec_iidx = ufunc->uf_def_arg_idx[
106 ufunc->uf_def_args.ga_len - defcount];
107 }
108}
109
110/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200111 * Create a new list from "count" items at the bottom of the stack.
112 * When "count" is zero an empty list is added to the stack.
113 */
114 static int
115exe_newlist(int count, ectx_T *ectx)
116{
117 list_T *list = list_alloc_with_items(count);
118 int idx;
119 typval_T *tv;
120
121 if (list == NULL)
122 return FAIL;
123 for (idx = 0; idx < count; ++idx)
124 list_set_item(list, idx, STACK_TV_BOT(idx - count));
125
126 if (count > 0)
127 ectx->ec_stack.ga_len -= count - 1;
128 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
129 return FAIL;
130 else
131 ++ectx->ec_stack.ga_len;
132 tv = STACK_TV_BOT(-1);
133 tv->v_type = VAR_LIST;
134 tv->vval.v_list = list;
135 ++list->lv_refcount;
136 return OK;
137}
138
139/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100140 * Call compiled function "cdf_idx" from compiled code.
141 *
142 * Stack has:
143 * - current arguments (already there)
144 * - omitted optional argument (default values) added here
145 * - stack frame:
146 * - pointer to calling function
147 * - Index of next instruction in calling function
148 * - previous frame pointer
149 * - reserved space for local variables
150 */
151 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200152call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100153{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200154 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
156 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int arg_to_add;
158 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100159 int idx;
160
161 if (dfunc->df_deleted)
162 {
163 emsg_funcname(e_func_deleted, ufunc->uf_name);
164 return FAIL;
165 }
166
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200167 if (ufunc->uf_va_name != NULL)
168 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200169 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 // Stack at time of call with 2 varargs:
171 // normal_arg
172 // optional_arg
173 // vararg_1
174 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200175 // After creating the list:
176 // normal_arg
177 // optional_arg
178 // vararg-list
179 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200181 // After creating the list
182 // normal_arg
183 // (space for optional_arg)
184 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200185 vararg_count = argcount - ufunc->uf_args.ga_len;
186 if (vararg_count < 0)
187 vararg_count = 0;
188 else
189 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200191 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192
193 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 }
195
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 if (arg_to_add < 0)
198 {
199 iemsg("Argument count wrong?");
200 return FAIL;
201 }
202 if (ga_grow(&ectx->ec_stack, arg_to_add + 3 + dfunc->df_varcount) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100203 return FAIL;
204
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205 // Move the vararg-list to below the missing optional arguments.
206 if (vararg_count > 0 && arg_to_add > 0)
207 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100208
209 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200211 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200212 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213
214 // Store current execution state in stack frame for ISN_RETURN.
215 // TODO: If the actual number of arguments doesn't match what the called
216 // function expects things go bad.
217 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
218 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
219 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame;
220 ectx->ec_frame = ectx->ec_stack.ga_len;
221
222 // Initialize local variables
223 for (idx = 0; idx < dfunc->df_varcount; ++idx)
224 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200225 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + dfunc->df_varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226
227 // Set execution state to the start of the called function.
228 ectx->ec_dfunc_idx = cdf_idx;
229 ectx->ec_instr = dfunc->df_instr;
230 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100231
232 // Decide where to start execution, handles optional arguments.
233 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100234
235 return OK;
236}
237
238// Get pointer to item in the stack.
239#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
240
241/*
242 * Return from the current function.
243 */
244 static void
245func_return(ectx_T *ectx)
246{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100247 int idx;
248 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100249 int top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250
251 // execution context goes one level up
252 estack_pop();
253
254 // Clear the local variables and temporary values, but not
255 // the return value.
256 for (idx = ectx->ec_frame + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100257 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100258 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100259
260 // Clear the arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100261 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100262 top = ectx->ec_frame - ufunc_argcount(dfunc->df_ufunc);
263 for (idx = top; idx < ectx->ec_frame; ++idx)
264 clear_tv(STACK_TV(idx));
265
266 // Restore the previous frame.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame)->vval.v_number;
268 ectx->ec_iidx = STACK_TV(ectx->ec_frame + 1)->vval.v_number;
269 ectx->ec_frame = STACK_TV(ectx->ec_frame + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100270 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
271 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100272
273 // Reset the stack to the position before the call, move the return value
274 // to the top of the stack.
275 idx = ectx->ec_stack.ga_len - 1;
276 ectx->ec_stack.ga_len = top + 1;
277 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278}
279
280#undef STACK_TV
281
282/*
283 * Prepare arguments and rettv for calling a builtin or user function.
284 */
285 static int
286call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
287{
288 int idx;
289 typval_T *tv;
290
291 // Move arguments from bottom of the stack to argvars[] and add terminator.
292 for (idx = 0; idx < argcount; ++idx)
293 argvars[idx] = *STACK_TV_BOT(idx - argcount);
294 argvars[argcount].v_type = VAR_UNKNOWN;
295
296 // Result replaces the arguments on the stack.
297 if (argcount > 0)
298 ectx->ec_stack.ga_len -= argcount - 1;
299 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
300 return FAIL;
301 else
302 ++ectx->ec_stack.ga_len;
303
304 // Default return value is zero.
305 tv = STACK_TV_BOT(-1);
306 tv->v_type = VAR_NUMBER;
307 tv->vval.v_number = 0;
308
309 return OK;
310}
311
312/*
313 * Call a builtin function by index.
314 */
315 static int
316call_bfunc(int func_idx, int argcount, ectx_T *ectx)
317{
318 typval_T argvars[MAX_FUNC_ARGS];
319 int idx;
320
321 if (call_prepare(argcount, argvars, ectx) == FAIL)
322 return FAIL;
323
324 // Call the builtin function.
325 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
326
327 // Clear the arguments.
328 for (idx = 0; idx < argcount; ++idx)
329 clear_tv(&argvars[idx]);
330 return OK;
331}
332
333/*
334 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100335 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100336 */
337 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100338call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100339{
340 typval_T argvars[MAX_FUNC_ARGS];
341 funcexe_T funcexe;
342 int error;
343 int idx;
344
345 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100346 {
347 // The function has been compiled, can call it quickly. For a function
348 // that was defined later: we can call it directly next time.
349 if (iptr != NULL)
350 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100351 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100352 iptr->isn_type = ISN_DCALL;
353 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
354 iptr->isn_arg.dfunc.cdf_argcount = argcount;
355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100356 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100357 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100358
359 if (call_prepare(argcount, argvars, ectx) == FAIL)
360 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200361 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 funcexe.evaluate = TRUE;
363
364 // Call the user function. Result goes in last position on the stack.
365 // TODO: add selfdict if there is one
366 error = call_user_func_check(ufunc, argcount, argvars,
367 STACK_TV_BOT(-1), &funcexe, NULL);
368
369 // Clear the arguments.
370 for (idx = 0; idx < argcount; ++idx)
371 clear_tv(&argvars[idx]);
372
373 if (error != FCERR_NONE)
374 {
375 user_func_error(error, ufunc->uf_name);
376 return FAIL;
377 }
378 return OK;
379}
380
381/*
382 * Execute a function by "name".
383 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100384 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 * Returns FAIL if not found without an error message.
386 */
387 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100388call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389{
390 ufunc_T *ufunc;
391
392 if (builtin_function(name, -1))
393 {
394 int func_idx = find_internal_func(name);
395
396 if (func_idx < 0)
397 return FAIL;
398 if (check_internal_func(func_idx, argcount) == FAIL)
399 return FAIL;
400 return call_bfunc(func_idx, argcount, ectx);
401 }
402
403 ufunc = find_func(name, NULL);
404 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100405 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100406
407 return FAIL;
408}
409
410 static int
411call_partial(typval_T *tv, int argcount, ectx_T *ectx)
412{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200413 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 int called_emsg_before = called_emsg;
415
416 if (tv->v_type == VAR_PARTIAL)
417 {
418 partial_T *pt = tv->vval.v_partial;
419
420 if (pt->pt_func != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100421 return call_ufunc(pt->pt_func, argcount, ectx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100422 name = pt->pt_name;
423 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200424 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200426 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100427 {
428 if (called_emsg == called_emsg_before)
429 semsg(_(e_unknownfunc), name);
430 return FAIL;
431 }
432 return OK;
433}
434
435/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100436 * Store "tv" in variable "name".
437 * This is for s: and g: variables.
438 */
439 static void
440store_var(char_u *name, typval_T *tv)
441{
442 funccal_entry_T entry;
443
444 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200445 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100446 restore_funccal();
447}
448
Bram Moolenaard3aac292020-04-19 14:32:17 +0200449
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100450/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100451 * Execute a function by "name".
452 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100453 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100454 */
455 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100456call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457{
458 int called_emsg_before = called_emsg;
459
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100460 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 && called_emsg == called_emsg_before)
462 {
463 // "name" may be a variable that is a funcref or partial
464 // if find variable
465 // call_partial()
466 // else
467 // semsg(_(e_unknownfunc), name);
468 emsg("call_eval_func(partial) not implemented yet");
469 return FAIL;
470 }
471 return OK;
472}
473
474/*
475 * Call a "def" function from old Vim script.
476 * Return OK or FAIL.
477 */
478 int
479call_def_function(
480 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200481 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100482 typval_T *argv, // arguments
483 typval_T *rettv) // return value
484{
485 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200486 int argc = argc_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487 int initial_frame_ptr;
488 typval_T *tv;
489 int idx;
490 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100491 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200492 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493
494// Get pointer to item in the stack.
495#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
496
497// Get pointer to item at the bottom of the stack, -1 is the bottom.
498#undef STACK_TV_BOT
499#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
500
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200501// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
503
Bram Moolenaara80faa82020-04-12 19:37:17 +0200504 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100505 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
506 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100507 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
509
510 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
511
512 // Put arguments on the stack.
513 for (idx = 0; idx < argc; ++idx)
514 {
515 copy_tv(&argv[idx], STACK_TV_BOT(0));
516 ++ectx.ec_stack.ga_len;
517 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200518
519 // Turn varargs into a list. Empty list if no args.
520 if (ufunc->uf_va_name != NULL)
521 {
522 int vararg_count = argc - ufunc->uf_args.ga_len;
523
524 if (vararg_count < 0)
525 vararg_count = 0;
526 else
527 argc -= vararg_count;
528 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200529 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200530 if (defcount > 0)
531 // Move varargs list to below missing default arguments.
532 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
533 --ectx.ec_stack.ga_len;
534 }
535
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100536 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200537 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100538 if (defcount > 0)
539 for (idx = 0; idx < defcount; ++idx)
540 {
541 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
542 ++ectx.ec_stack.ga_len;
543 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200544 if (ufunc->uf_va_name != NULL)
545 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546
547 // Frame pointer points to just after arguments.
548 ectx.ec_frame = ectx.ec_stack.ga_len;
549 initial_frame_ptr = ectx.ec_frame;
550
551 // dummy frame entries
552 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
553 {
554 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
555 ++ectx.ec_stack.ga_len;
556 }
557
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200558 {
559 // Reserve space for local variables.
560 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
561 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200563 for (idx = 0; idx < dfunc->df_varcount; ++idx)
564 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
565 ectx.ec_stack.ga_len += dfunc->df_varcount;
566
567 ectx.ec_instr = dfunc->df_instr;
568 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100569
Bram Moolenaara26b9702020-04-18 19:53:28 +0200570 // Commands behave like vim9script.
571 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
572
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100573 // Decide where to start execution, handles optional arguments.
574 init_instr_idx(ufunc, argc, &ectx);
575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100576 for (;;)
577 {
578 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100579
580 veryfast_breakcheck();
581 if (got_int)
582 {
583 // Turn CTRL-C into an exception.
584 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100585 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100586 goto failed;
587 did_throw = TRUE;
588 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
Bram Moolenaara26b9702020-04-18 19:53:28 +0200590 if (did_emsg && msg_list != NULL && *msg_list != NULL)
591 {
592 // Turn an error message into an exception.
593 did_emsg = FALSE;
594 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
595 goto failed;
596 did_throw = TRUE;
597 *msg_list = NULL;
598 }
599
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600 if (did_throw && !ectx.ec_in_catch)
601 {
602 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100603 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 // An exception jumps to the first catch, finally, or returns from
606 // the current function.
607 if (trystack->ga_len > 0)
608 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
609 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
610 {
611 // jump to ":catch" or ":finally"
612 ectx.ec_in_catch = TRUE;
613 ectx.ec_iidx = trycmd->tcd_catch_idx;
614 }
615 else
616 {
617 // not inside try or need to return from current functions.
618 if (ectx.ec_frame == initial_frame_ptr)
619 {
620 // At the toplevel we are done. Push a dummy return value.
621 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
622 goto failed;
623 tv = STACK_TV_BOT(0);
624 tv->v_type = VAR_NUMBER;
625 tv->vval.v_number = 0;
626 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100627 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628 goto done;
629 }
630
631 func_return(&ectx);
632 }
633 continue;
634 }
635
636 iptr = &ectx.ec_instr[ectx.ec_iidx++];
637 switch (iptr->isn_type)
638 {
639 // execute Ex command line
640 case ISN_EXEC:
641 do_cmdline_cmd(iptr->isn_arg.string);
642 break;
643
644 // execute :echo {string} ...
645 case ISN_ECHO:
646 {
647 int count = iptr->isn_arg.echo.echo_count;
648 int atstart = TRUE;
649 int needclr = TRUE;
650
651 for (idx = 0; idx < count; ++idx)
652 {
653 tv = STACK_TV_BOT(idx - count);
654 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
655 &atstart, &needclr);
656 clear_tv(tv);
657 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100658 if (needclr)
659 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660 ectx.ec_stack.ga_len -= count;
661 }
662 break;
663
Bram Moolenaarad39c092020-02-26 18:23:43 +0100664 // execute :execute {string} ...
665 case ISN_EXECUTE:
666 {
667 int count = iptr->isn_arg.number;
668 garray_T ga;
669 char_u buf[NUMBUFLEN];
670 char_u *p;
671 int len;
672 int failed = FALSE;
673
674 ga_init2(&ga, 1, 80);
675 for (idx = 0; idx < count; ++idx)
676 {
677 tv = STACK_TV_BOT(idx - count);
678 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
679 {
680 emsg(_(e_inval_string));
681 break;
682 }
683 else
684 p = tv_get_string_buf(tv, buf);
685
686 len = (int)STRLEN(p);
687 if (ga_grow(&ga, len + 2) == FAIL)
688 failed = TRUE;
689 else
690 {
691 if (ga.ga_len > 0)
692 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
693 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
694 ga.ga_len += len;
695 }
696 clear_tv(tv);
697 }
698 ectx.ec_stack.ga_len -= count;
699
700 if (!failed && ga.ga_data != NULL)
701 do_cmdline_cmd((char_u *)ga.ga_data);
702 ga_clear(&ga);
703 }
704 break;
705
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 // load local variable or argument
707 case ISN_LOAD:
708 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
709 goto failed;
710 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
711 ++ectx.ec_stack.ga_len;
712 break;
713
714 // load v: variable
715 case ISN_LOADV:
716 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
717 goto failed;
718 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
719 ++ectx.ec_stack.ga_len;
720 break;
721
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100722 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723 case ISN_LOADSCRIPT:
724 {
725 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100726 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 svar_T *sv;
728
729 sv = ((svar_T *)si->sn_var_vals.ga_data)
730 + iptr->isn_arg.script.script_idx;
731 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
732 goto failed;
733 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
734 ++ectx.ec_stack.ga_len;
735 }
736 break;
737
738 // load s: variable in old script
739 case ISN_LOADS:
740 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100741 hashtab_T *ht = &SCRIPT_VARS(
742 iptr->isn_arg.loadstore.ls_sid);
743 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 if (di == NULL)
747 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100748 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 goto failed;
750 }
751 else
752 {
753 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
754 goto failed;
755 copy_tv(&di->di_tv, STACK_TV_BOT(0));
756 ++ectx.ec_stack.ga_len;
757 }
758 }
759 break;
760
Bram Moolenaard3aac292020-04-19 14:32:17 +0200761 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200763 case ISN_LOADB:
764 case ISN_LOADW:
765 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200767 dictitem_T *di = NULL;
768 hashtab_T *ht = NULL;
769 char namespace;
770 switch (iptr->isn_type)
771 {
772 case ISN_LOADG:
773 ht = get_globvar_ht();
774 namespace = 'g';
775 break;
776 case ISN_LOADB:
777 ht = &curbuf->b_vars->dv_hashtab;
778 namespace = 'b';
779 break;
780 case ISN_LOADW:
781 ht = &curwin->w_vars->dv_hashtab;
782 namespace = 'w';
783 break;
784 case ISN_LOADT:
785 ht = &curtab->tp_vars->dv_hashtab;
786 namespace = 't';
787 break;
788 default: // Cannot reach here
789 goto failed;
790 }
791 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100793 if (di == NULL)
794 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200795 semsg(_("E121: Undefined variable: %c:%s"),
796 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100797 goto failed;
798 }
799 else
800 {
801 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
802 goto failed;
803 copy_tv(&di->di_tv, STACK_TV_BOT(0));
804 ++ectx.ec_stack.ga_len;
805 }
806 }
807 break;
808
809 // load &option
810 case ISN_LOADOPT:
811 {
812 typval_T optval;
813 char_u *name = iptr->isn_arg.string;
814
Bram Moolenaara8c17702020-04-01 21:17:24 +0200815 // This is not expected to fail, name is checked during
816 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
818 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100819 if (get_option_tv(&name, &optval, TRUE) == FAIL)
820 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 *STACK_TV_BOT(0) = optval;
822 ++ectx.ec_stack.ga_len;
823 }
824 break;
825
826 // load $ENV
827 case ISN_LOADENV:
828 {
829 typval_T optval;
830 char_u *name = iptr->isn_arg.string;
831
832 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
833 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100834 // name is always valid, checked when compiling
835 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 *STACK_TV_BOT(0) = optval;
837 ++ectx.ec_stack.ga_len;
838 }
839 break;
840
841 // load @register
842 case ISN_LOADREG:
843 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
844 goto failed;
845 tv = STACK_TV_BOT(0);
846 tv->v_type = VAR_STRING;
847 tv->vval.v_string = get_reg_contents(
848 iptr->isn_arg.number, GREG_EXPR_SRC);
849 ++ectx.ec_stack.ga_len;
850 break;
851
852 // store local variable
853 case ISN_STORE:
854 --ectx.ec_stack.ga_len;
855 tv = STACK_TV_VAR(iptr->isn_arg.number);
856 clear_tv(tv);
857 *tv = *STACK_TV_BOT(0);
858 break;
859
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100860 // store s: variable in old script
861 case ISN_STORES:
862 {
863 hashtab_T *ht = &SCRIPT_VARS(
864 iptr->isn_arg.loadstore.ls_sid);
865 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100866 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100867
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100868 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100869 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200870 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100871 else
872 {
873 clear_tv(&di->di_tv);
874 di->di_tv = *STACK_TV_BOT(0);
875 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100876 }
877 break;
878
879 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 case ISN_STORESCRIPT:
881 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100882 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100883 iptr->isn_arg.script.script_sid);
884 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
885 + iptr->isn_arg.script.script_idx;
886
887 --ectx.ec_stack.ga_len;
888 clear_tv(sv->sv_tv);
889 *sv->sv_tv = *STACK_TV_BOT(0);
890 }
891 break;
892
893 // store option
894 case ISN_STOREOPT:
895 {
896 long n = 0;
897 char_u *s = NULL;
898 char *msg;
899
900 --ectx.ec_stack.ga_len;
901 tv = STACK_TV_BOT(0);
902 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100903 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100904 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100905 if (s == NULL)
906 s = (char_u *)"";
907 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 else if (tv->v_type == VAR_NUMBER)
909 n = tv->vval.v_number;
910 else
911 {
912 emsg(_("E1051: Expected string or number"));
913 goto failed;
914 }
915 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
916 n, s, iptr->isn_arg.storeopt.so_flags);
917 if (msg != NULL)
918 {
919 emsg(_(msg));
920 goto failed;
921 }
922 clear_tv(tv);
923 }
924 break;
925
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100926 // store $ENV
927 case ISN_STOREENV:
928 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100929 tv = STACK_TV_BOT(0);
930 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
931 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100932 break;
933
934 // store @r
935 case ISN_STOREREG:
936 {
937 int reg = iptr->isn_arg.number;
938
939 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100940 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100941 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100942 tv_get_string(tv), -1, FALSE);
943 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100944 }
945 break;
946
947 // store v: variable
948 case ISN_STOREV:
949 --ectx.ec_stack.ga_len;
950 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
951 == FAIL)
952 goto failed;
953 break;
954
Bram Moolenaard3aac292020-04-19 14:32:17 +0200955 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200957 case ISN_STOREB:
958 case ISN_STOREW:
959 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100960 {
961 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +0200962 hashtab_T *ht;
963 switch (iptr->isn_type)
964 {
965 case ISN_STOREG:
966 ht = get_globvar_ht();
967 break;
968 case ISN_STOREB:
969 ht = &curbuf->b_vars->dv_hashtab;
970 break;
971 case ISN_STOREW:
972 ht = &curwin->w_vars->dv_hashtab;
973 break;
974 case ISN_STORET:
975 ht = &curtab->tp_vars->dv_hashtab;
976 break;
977 default: // Cannot reach here
978 goto failed;
979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980
981 --ectx.ec_stack.ga_len;
Bram Moolenaard3aac292020-04-19 14:32:17 +0200982 di = find_var_in_ht(ht, 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100983 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100984 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100985 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100986 else
987 {
988 clear_tv(&di->di_tv);
989 di->di_tv = *STACK_TV_BOT(0);
990 }
991 }
992 break;
993
994 // store number in local variable
995 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +0100996 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997 clear_tv(tv);
998 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100999 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 break;
1001
1002 // push constant
1003 case ISN_PUSHNR:
1004 case ISN_PUSHBOOL:
1005 case ISN_PUSHSPEC:
1006 case ISN_PUSHF:
1007 case ISN_PUSHS:
1008 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001009 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001010 case ISN_PUSHCHANNEL:
1011 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1013 goto failed;
1014 tv = STACK_TV_BOT(0);
1015 ++ectx.ec_stack.ga_len;
1016 switch (iptr->isn_type)
1017 {
1018 case ISN_PUSHNR:
1019 tv->v_type = VAR_NUMBER;
1020 tv->vval.v_number = iptr->isn_arg.number;
1021 break;
1022 case ISN_PUSHBOOL:
1023 tv->v_type = VAR_BOOL;
1024 tv->vval.v_number = iptr->isn_arg.number;
1025 break;
1026 case ISN_PUSHSPEC:
1027 tv->v_type = VAR_SPECIAL;
1028 tv->vval.v_number = iptr->isn_arg.number;
1029 break;
1030#ifdef FEAT_FLOAT
1031 case ISN_PUSHF:
1032 tv->v_type = VAR_FLOAT;
1033 tv->vval.v_float = iptr->isn_arg.fnumber;
1034 break;
1035#endif
1036 case ISN_PUSHBLOB:
1037 blob_copy(iptr->isn_arg.blob, tv);
1038 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001039 case ISN_PUSHFUNC:
1040 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001041 if (iptr->isn_arg.string == NULL)
1042 tv->vval.v_string = NULL;
1043 else
1044 tv->vval.v_string =
1045 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001046 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001047 case ISN_PUSHCHANNEL:
1048#ifdef FEAT_JOB_CHANNEL
1049 tv->v_type = VAR_CHANNEL;
1050 tv->vval.v_channel = iptr->isn_arg.channel;
1051 if (tv->vval.v_channel != NULL)
1052 ++tv->vval.v_channel->ch_refcount;
1053#endif
1054 break;
1055 case ISN_PUSHJOB:
1056#ifdef FEAT_JOB_CHANNEL
1057 tv->v_type = VAR_JOB;
1058 tv->vval.v_job = iptr->isn_arg.job;
1059 if (tv->vval.v_job != NULL)
1060 ++tv->vval.v_job->jv_refcount;
1061#endif
1062 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 default:
1064 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001065 tv->vval.v_string = vim_strsave(
1066 iptr->isn_arg.string == NULL
1067 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 }
1069 break;
1070
1071 // create a list from items on the stack; uses a single allocation
1072 // for the list header and the items
1073 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001074 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1075 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 break;
1077
1078 // create a dict from items on the stack
1079 case ISN_NEWDICT:
1080 {
1081 int count = iptr->isn_arg.number;
1082 dict_T *dict = dict_alloc();
1083 dictitem_T *item;
1084
1085 if (dict == NULL)
1086 goto failed;
1087 for (idx = 0; idx < count; ++idx)
1088 {
1089 // check key type is VAR_STRING
1090 tv = STACK_TV_BOT(2 * (idx - count));
1091 item = dictitem_alloc(tv->vval.v_string);
1092 clear_tv(tv);
1093 if (item == NULL)
1094 goto failed;
1095 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1096 item->di_tv.v_lock = 0;
1097 if (dict_add(dict, item) == FAIL)
1098 goto failed;
1099 }
1100
1101 if (count > 0)
1102 ectx.ec_stack.ga_len -= 2 * count - 1;
1103 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1104 goto failed;
1105 else
1106 ++ectx.ec_stack.ga_len;
1107 tv = STACK_TV_BOT(-1);
1108 tv->v_type = VAR_DICT;
1109 tv->vval.v_dict = dict;
1110 ++dict->dv_refcount;
1111 }
1112 break;
1113
1114 // call a :def function
1115 case ISN_DCALL:
1116 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1117 iptr->isn_arg.dfunc.cdf_argcount,
1118 &ectx) == FAIL)
1119 goto failed;
1120 break;
1121
1122 // call a builtin function
1123 case ISN_BCALL:
1124 SOURCING_LNUM = iptr->isn_lnum;
1125 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1126 iptr->isn_arg.bfunc.cbf_argcount,
1127 &ectx) == FAIL)
1128 goto failed;
1129 break;
1130
1131 // call a funcref or partial
1132 case ISN_PCALL:
1133 {
1134 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1135 int r;
1136 typval_T partial;
1137
1138 SOURCING_LNUM = iptr->isn_lnum;
1139 if (pfunc->cpf_top)
1140 {
1141 // funcref is above the arguments
1142 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1143 }
1144 else
1145 {
1146 // Get the funcref from the stack.
1147 --ectx.ec_stack.ga_len;
1148 partial = *STACK_TV_BOT(0);
1149 tv = &partial;
1150 }
1151 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1152 if (tv == &partial)
1153 clear_tv(&partial);
1154 if (r == FAIL)
1155 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 }
1157 break;
1158
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001159 case ISN_PCALL_END:
1160 // PCALL finished, arguments have been consumed and replaced by
1161 // the return value. Now clear the funcref from the stack,
1162 // and move the return value in its place.
1163 --ectx.ec_stack.ga_len;
1164 clear_tv(STACK_TV_BOT(-1));
1165 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1166 break;
1167
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 // call a user defined function or funcref/partial
1169 case ISN_UCALL:
1170 {
1171 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1172
1173 SOURCING_LNUM = iptr->isn_lnum;
1174 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001175 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 goto failed;
1177 }
1178 break;
1179
1180 // return from a :def function call
1181 case ISN_RETURN:
1182 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001183 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001184 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001185
1186 if (trystack->ga_len > 0)
1187 trycmd = ((trycmd_T *)trystack->ga_data)
1188 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
1190 && trycmd->tcd_finally_idx != 0)
1191 {
1192 // jump to ":finally"
1193 ectx.ec_iidx = trycmd->tcd_finally_idx;
1194 trycmd->tcd_return = TRUE;
1195 }
1196 else
1197 {
1198 // Restore previous function. If the frame pointer
1199 // is zero then there is none and we are done.
1200 if (ectx.ec_frame == initial_frame_ptr)
1201 goto done;
1202
1203 func_return(&ectx);
1204 }
1205 }
1206 break;
1207
1208 // push a function reference to a compiled function
1209 case ISN_FUNCREF:
1210 {
1211 partial_T *pt = NULL;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001212 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213
1214 pt = ALLOC_CLEAR_ONE(partial_T);
1215 if (pt == NULL)
1216 goto failed;
1217 dfunc = ((dfunc_T *)def_functions.ga_data)
1218 + iptr->isn_arg.number;
1219 pt->pt_func = dfunc->df_ufunc;
1220 pt->pt_refcount = 1;
1221 ++dfunc->df_ufunc->uf_refcount;
1222
1223 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1224 goto failed;
1225 tv = STACK_TV_BOT(0);
1226 ++ectx.ec_stack.ga_len;
1227 tv->vval.v_partial = pt;
1228 tv->v_type = VAR_PARTIAL;
1229 }
1230 break;
1231
1232 // jump if a condition is met
1233 case ISN_JUMP:
1234 {
1235 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1236 int jump = TRUE;
1237
1238 if (when != JUMP_ALWAYS)
1239 {
1240 tv = STACK_TV_BOT(-1);
1241 jump = tv2bool(tv);
1242 if (when == JUMP_IF_FALSE
1243 || when == JUMP_AND_KEEP_IF_FALSE)
1244 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001245 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001246 {
1247 // drop the value from the stack
1248 clear_tv(tv);
1249 --ectx.ec_stack.ga_len;
1250 }
1251 }
1252 if (jump)
1253 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1254 }
1255 break;
1256
1257 // top of a for loop
1258 case ISN_FOR:
1259 {
1260 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1261 typval_T *idxtv =
1262 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1263
1264 // push the next item from the list
1265 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1266 goto failed;
1267 if (++idxtv->vval.v_number >= list->lv_len)
1268 // past the end of the list, jump to "endfor"
1269 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1270 else if (list->lv_first == &range_list_item)
1271 {
1272 // non-materialized range() list
1273 tv = STACK_TV_BOT(0);
1274 tv->v_type = VAR_NUMBER;
1275 tv->vval.v_number = list_find_nr(
1276 list, idxtv->vval.v_number, NULL);
1277 ++ectx.ec_stack.ga_len;
1278 }
1279 else
1280 {
1281 listitem_T *li = list_find(list, idxtv->vval.v_number);
1282
1283 if (li == NULL)
1284 goto failed;
1285 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1286 ++ectx.ec_stack.ga_len;
1287 }
1288 }
1289 break;
1290
1291 // start of ":try" block
1292 case ISN_TRY:
1293 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001294 trycmd_T *trycmd = NULL;
1295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1297 goto failed;
1298 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1299 + ectx.ec_trystack.ga_len;
1300 ++ectx.ec_trystack.ga_len;
1301 ++trylevel;
1302 trycmd->tcd_frame = ectx.ec_frame;
1303 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1304 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001305 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001306 }
1307 break;
1308
1309 case ISN_PUSHEXC:
1310 if (current_exception == NULL)
1311 {
1312 iemsg("Evaluating catch while current_exception is NULL");
1313 goto failed;
1314 }
1315 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1316 goto failed;
1317 tv = STACK_TV_BOT(0);
1318 ++ectx.ec_stack.ga_len;
1319 tv->v_type = VAR_STRING;
1320 tv->vval.v_string = vim_strsave(
1321 (char_u *)current_exception->value);
1322 break;
1323
1324 case ISN_CATCH:
1325 {
1326 garray_T *trystack = &ectx.ec_trystack;
1327
1328 if (trystack->ga_len > 0)
1329 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001330 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 + trystack->ga_len - 1;
1332 trycmd->tcd_caught = TRUE;
1333 }
1334 did_emsg = got_int = did_throw = FALSE;
1335 catch_exception(current_exception);
1336 }
1337 break;
1338
1339 // end of ":try" block
1340 case ISN_ENDTRY:
1341 {
1342 garray_T *trystack = &ectx.ec_trystack;
1343
1344 if (trystack->ga_len > 0)
1345 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001346 trycmd_T *trycmd = NULL;
1347
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001348 --trystack->ga_len;
1349 --trylevel;
1350 trycmd = ((trycmd_T *)trystack->ga_data)
1351 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001352 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 {
1354 // discard the exception
1355 if (caught_stack == current_exception)
1356 caught_stack = caught_stack->caught;
1357 discard_current_exception();
1358 }
1359
1360 if (trycmd->tcd_return)
1361 {
1362 // Restore previous function. If the frame pointer
1363 // is zero then there is none and we are done.
1364 if (ectx.ec_frame == initial_frame_ptr)
1365 goto done;
1366
1367 func_return(&ectx);
1368 }
1369 }
1370 }
1371 break;
1372
1373 case ISN_THROW:
1374 --ectx.ec_stack.ga_len;
1375 tv = STACK_TV_BOT(0);
1376 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1377 {
1378 vim_free(tv->vval.v_string);
1379 goto failed;
1380 }
1381 did_throw = TRUE;
1382 break;
1383
1384 // compare with special values
1385 case ISN_COMPAREBOOL:
1386 case ISN_COMPARESPECIAL:
1387 {
1388 typval_T *tv1 = STACK_TV_BOT(-2);
1389 typval_T *tv2 = STACK_TV_BOT(-1);
1390 varnumber_T arg1 = tv1->vval.v_number;
1391 varnumber_T arg2 = tv2->vval.v_number;
1392 int res;
1393
1394 switch (iptr->isn_arg.op.op_type)
1395 {
1396 case EXPR_EQUAL: res = arg1 == arg2; break;
1397 case EXPR_NEQUAL: res = arg1 != arg2; break;
1398 default: res = 0; break;
1399 }
1400
1401 --ectx.ec_stack.ga_len;
1402 tv1->v_type = VAR_BOOL;
1403 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1404 }
1405 break;
1406
1407 // Operation with two number arguments
1408 case ISN_OPNR:
1409 case ISN_COMPARENR:
1410 {
1411 typval_T *tv1 = STACK_TV_BOT(-2);
1412 typval_T *tv2 = STACK_TV_BOT(-1);
1413 varnumber_T arg1 = tv1->vval.v_number;
1414 varnumber_T arg2 = tv2->vval.v_number;
1415 varnumber_T res;
1416
1417 switch (iptr->isn_arg.op.op_type)
1418 {
1419 case EXPR_MULT: res = arg1 * arg2; break;
1420 case EXPR_DIV: res = arg1 / arg2; break;
1421 case EXPR_REM: res = arg1 % arg2; break;
1422 case EXPR_SUB: res = arg1 - arg2; break;
1423 case EXPR_ADD: res = arg1 + arg2; break;
1424
1425 case EXPR_EQUAL: res = arg1 == arg2; break;
1426 case EXPR_NEQUAL: res = arg1 != arg2; break;
1427 case EXPR_GREATER: res = arg1 > arg2; break;
1428 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1429 case EXPR_SMALLER: res = arg1 < arg2; break;
1430 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1431 default: res = 0; break;
1432 }
1433
1434 --ectx.ec_stack.ga_len;
1435 if (iptr->isn_type == ISN_COMPARENR)
1436 {
1437 tv1->v_type = VAR_BOOL;
1438 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1439 }
1440 else
1441 tv1->vval.v_number = res;
1442 }
1443 break;
1444
1445 // Computation with two float arguments
1446 case ISN_OPFLOAT:
1447 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001448#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 {
1450 typval_T *tv1 = STACK_TV_BOT(-2);
1451 typval_T *tv2 = STACK_TV_BOT(-1);
1452 float_T arg1 = tv1->vval.v_float;
1453 float_T arg2 = tv2->vval.v_float;
1454 float_T res = 0;
1455 int cmp = FALSE;
1456
1457 switch (iptr->isn_arg.op.op_type)
1458 {
1459 case EXPR_MULT: res = arg1 * arg2; break;
1460 case EXPR_DIV: res = arg1 / arg2; break;
1461 case EXPR_SUB: res = arg1 - arg2; break;
1462 case EXPR_ADD: res = arg1 + arg2; break;
1463
1464 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1465 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1466 case EXPR_GREATER: cmp = arg1 > arg2; break;
1467 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1468 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1469 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1470 default: cmp = 0; break;
1471 }
1472 --ectx.ec_stack.ga_len;
1473 if (iptr->isn_type == ISN_COMPAREFLOAT)
1474 {
1475 tv1->v_type = VAR_BOOL;
1476 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1477 }
1478 else
1479 tv1->vval.v_float = res;
1480 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001481#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001482 break;
1483
1484 case ISN_COMPARELIST:
1485 {
1486 typval_T *tv1 = STACK_TV_BOT(-2);
1487 typval_T *tv2 = STACK_TV_BOT(-1);
1488 list_T *arg1 = tv1->vval.v_list;
1489 list_T *arg2 = tv2->vval.v_list;
1490 int cmp = FALSE;
1491 int ic = iptr->isn_arg.op.op_ic;
1492
1493 switch (iptr->isn_arg.op.op_type)
1494 {
1495 case EXPR_EQUAL: cmp =
1496 list_equal(arg1, arg2, ic, FALSE); break;
1497 case EXPR_NEQUAL: cmp =
1498 !list_equal(arg1, arg2, ic, FALSE); break;
1499 case EXPR_IS: cmp = arg1 == arg2; break;
1500 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1501 default: cmp = 0; break;
1502 }
1503 --ectx.ec_stack.ga_len;
1504 clear_tv(tv1);
1505 clear_tv(tv2);
1506 tv1->v_type = VAR_BOOL;
1507 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1508 }
1509 break;
1510
1511 case ISN_COMPAREBLOB:
1512 {
1513 typval_T *tv1 = STACK_TV_BOT(-2);
1514 typval_T *tv2 = STACK_TV_BOT(-1);
1515 blob_T *arg1 = tv1->vval.v_blob;
1516 blob_T *arg2 = tv2->vval.v_blob;
1517 int cmp = FALSE;
1518
1519 switch (iptr->isn_arg.op.op_type)
1520 {
1521 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1522 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1523 case EXPR_IS: cmp = arg1 == arg2; break;
1524 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1525 default: cmp = 0; break;
1526 }
1527 --ectx.ec_stack.ga_len;
1528 clear_tv(tv1);
1529 clear_tv(tv2);
1530 tv1->v_type = VAR_BOOL;
1531 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1532 }
1533 break;
1534
1535 // TODO: handle separately
1536 case ISN_COMPARESTRING:
1537 case ISN_COMPAREDICT:
1538 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001539 case ISN_COMPAREANY:
1540 {
1541 typval_T *tv1 = STACK_TV_BOT(-2);
1542 typval_T *tv2 = STACK_TV_BOT(-1);
1543 exptype_T exptype = iptr->isn_arg.op.op_type;
1544 int ic = iptr->isn_arg.op.op_ic;
1545
1546 typval_compare(tv1, tv2, exptype, ic);
1547 clear_tv(tv2);
1548 tv1->v_type = VAR_BOOL;
1549 tv1->vval.v_number = tv1->vval.v_number
1550 ? VVAL_TRUE : VVAL_FALSE;
1551 --ectx.ec_stack.ga_len;
1552 }
1553 break;
1554
1555 case ISN_ADDLIST:
1556 case ISN_ADDBLOB:
1557 {
1558 typval_T *tv1 = STACK_TV_BOT(-2);
1559 typval_T *tv2 = STACK_TV_BOT(-1);
1560
1561 if (iptr->isn_type == ISN_ADDLIST)
1562 eval_addlist(tv1, tv2);
1563 else
1564 eval_addblob(tv1, tv2);
1565 clear_tv(tv2);
1566 --ectx.ec_stack.ga_len;
1567 }
1568 break;
1569
1570 // Computation with two arguments of unknown type
1571 case ISN_OPANY:
1572 {
1573 typval_T *tv1 = STACK_TV_BOT(-2);
1574 typval_T *tv2 = STACK_TV_BOT(-1);
1575 varnumber_T n1, n2;
1576#ifdef FEAT_FLOAT
1577 float_T f1 = 0, f2 = 0;
1578#endif
1579 int error = FALSE;
1580
1581 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1582 {
1583 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1584 {
1585 eval_addlist(tv1, tv2);
1586 clear_tv(tv2);
1587 --ectx.ec_stack.ga_len;
1588 break;
1589 }
1590 else if (tv1->v_type == VAR_BLOB
1591 && tv2->v_type == VAR_BLOB)
1592 {
1593 eval_addblob(tv1, tv2);
1594 clear_tv(tv2);
1595 --ectx.ec_stack.ga_len;
1596 break;
1597 }
1598 }
1599#ifdef FEAT_FLOAT
1600 if (tv1->v_type == VAR_FLOAT)
1601 {
1602 f1 = tv1->vval.v_float;
1603 n1 = 0;
1604 }
1605 else
1606#endif
1607 {
1608 n1 = tv_get_number_chk(tv1, &error);
1609 if (error)
1610 goto failed;
1611#ifdef FEAT_FLOAT
1612 if (tv2->v_type == VAR_FLOAT)
1613 f1 = n1;
1614#endif
1615 }
1616#ifdef FEAT_FLOAT
1617 if (tv2->v_type == VAR_FLOAT)
1618 {
1619 f2 = tv2->vval.v_float;
1620 n2 = 0;
1621 }
1622 else
1623#endif
1624 {
1625 n2 = tv_get_number_chk(tv2, &error);
1626 if (error)
1627 goto failed;
1628#ifdef FEAT_FLOAT
1629 if (tv1->v_type == VAR_FLOAT)
1630 f2 = n2;
1631#endif
1632 }
1633#ifdef FEAT_FLOAT
1634 // if there is a float on either side the result is a float
1635 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1636 {
1637 switch (iptr->isn_arg.op.op_type)
1638 {
1639 case EXPR_MULT: f1 = f1 * f2; break;
1640 case EXPR_DIV: f1 = f1 / f2; break;
1641 case EXPR_SUB: f1 = f1 - f2; break;
1642 case EXPR_ADD: f1 = f1 + f2; break;
1643 default: emsg(_(e_modulus)); goto failed;
1644 }
1645 clear_tv(tv1);
1646 clear_tv(tv2);
1647 tv1->v_type = VAR_FLOAT;
1648 tv1->vval.v_float = f1;
1649 --ectx.ec_stack.ga_len;
1650 }
1651 else
1652#endif
1653 {
1654 switch (iptr->isn_arg.op.op_type)
1655 {
1656 case EXPR_MULT: n1 = n1 * n2; break;
1657 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1658 case EXPR_SUB: n1 = n1 - n2; break;
1659 case EXPR_ADD: n1 = n1 + n2; break;
1660 default: n1 = num_modulus(n1, n2); break;
1661 }
1662 clear_tv(tv1);
1663 clear_tv(tv2);
1664 tv1->v_type = VAR_NUMBER;
1665 tv1->vval.v_number = n1;
1666 --ectx.ec_stack.ga_len;
1667 }
1668 }
1669 break;
1670
1671 case ISN_CONCAT:
1672 {
1673 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1674 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1675 char_u *res;
1676
1677 res = concat_str(str1, str2);
1678 clear_tv(STACK_TV_BOT(-2));
1679 clear_tv(STACK_TV_BOT(-1));
1680 --ectx.ec_stack.ga_len;
1681 STACK_TV_BOT(-1)->vval.v_string = res;
1682 }
1683 break;
1684
1685 case ISN_INDEX:
1686 {
1687 list_T *list;
1688 varnumber_T n;
1689 listitem_T *li;
1690
1691 // list index: list is at stack-2, index at stack-1
1692 tv = STACK_TV_BOT(-2);
1693 if (tv->v_type != VAR_LIST)
1694 {
1695 emsg(_(e_listreq));
1696 goto failed;
1697 }
1698 list = tv->vval.v_list;
1699
1700 tv = STACK_TV_BOT(-1);
1701 if (tv->v_type != VAR_NUMBER)
1702 {
1703 emsg(_(e_number_exp));
1704 goto failed;
1705 }
1706 n = tv->vval.v_number;
1707 clear_tv(tv);
1708 if ((li = list_find(list, n)) == NULL)
1709 {
1710 semsg(_(e_listidx), n);
1711 goto failed;
1712 }
1713 --ectx.ec_stack.ga_len;
1714 clear_tv(STACK_TV_BOT(-1));
1715 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1716 }
1717 break;
1718
1719 // dict member with string key
1720 case ISN_MEMBER:
1721 {
1722 dict_T *dict;
1723 dictitem_T *di;
1724
1725 tv = STACK_TV_BOT(-1);
1726 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1727 {
1728 emsg(_(e_dictreq));
1729 goto failed;
1730 }
1731 dict = tv->vval.v_dict;
1732
1733 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1734 == NULL)
1735 {
1736 semsg(_(e_dictkey), iptr->isn_arg.string);
1737 goto failed;
1738 }
1739 clear_tv(tv);
1740 copy_tv(&di->di_tv, tv);
1741 }
1742 break;
1743
1744 case ISN_NEGATENR:
1745 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001746 if (tv->v_type != VAR_NUMBER
1747#ifdef FEAT_FLOAT
1748 && tv->v_type != VAR_FLOAT
1749#endif
1750 )
1751 {
1752 emsg(_(e_number_exp));
1753 goto failed;
1754 }
1755#ifdef FEAT_FLOAT
1756 if (tv->v_type == VAR_FLOAT)
1757 tv->vval.v_float = -tv->vval.v_float;
1758 else
1759#endif
1760 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 break;
1762
1763 case ISN_CHECKNR:
1764 {
1765 int error = FALSE;
1766
1767 tv = STACK_TV_BOT(-1);
1768 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 (void)tv_get_number_chk(tv, &error);
1771 if (error)
1772 goto failed;
1773 }
1774 break;
1775
1776 case ISN_CHECKTYPE:
1777 {
1778 checktype_T *ct = &iptr->isn_arg.type;
1779
1780 tv = STACK_TV_BOT(ct->ct_off);
1781 if (tv->v_type != ct->ct_type)
1782 {
1783 semsg(_("E1029: Expected %s but got %s"),
1784 vartype_name(ct->ct_type),
1785 vartype_name(tv->v_type));
1786 goto failed;
1787 }
1788 }
1789 break;
1790
1791 case ISN_2BOOL:
1792 {
1793 int n;
1794
1795 tv = STACK_TV_BOT(-1);
1796 n = tv2bool(tv);
1797 if (iptr->isn_arg.number) // invert
1798 n = !n;
1799 clear_tv(tv);
1800 tv->v_type = VAR_BOOL;
1801 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1802 }
1803 break;
1804
1805 case ISN_2STRING:
1806 {
1807 char_u *str;
1808
1809 tv = STACK_TV_BOT(iptr->isn_arg.number);
1810 if (tv->v_type != VAR_STRING)
1811 {
1812 str = typval_tostring(tv);
1813 clear_tv(tv);
1814 tv->v_type = VAR_STRING;
1815 tv->vval.v_string = str;
1816 }
1817 }
1818 break;
1819
1820 case ISN_DROP:
1821 --ectx.ec_stack.ga_len;
1822 clear_tv(STACK_TV_BOT(0));
1823 break;
1824 }
1825 }
1826
1827done:
1828 // function finished, get result from the stack.
1829 tv = STACK_TV_BOT(-1);
1830 *rettv = *tv;
1831 tv->v_type = VAR_UNKNOWN;
1832 ret = OK;
1833
1834failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001835 // When failed need to unwind the call stack.
1836 while (ectx.ec_frame != initial_frame_ptr)
1837 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001838failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02001839 current_sctx.sc_version = save_sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1841 clear_tv(STACK_TV(idx));
1842 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001843 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 return ret;
1845}
1846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847/*
1848 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001849 * We don't really need this at runtime, but we do have tests that require it,
1850 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 */
1852 void
1853ex_disassemble(exarg_T *eap)
1854{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001855 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001856 char_u *fname;
1857 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 dfunc_T *dfunc;
1859 isn_T *instr;
1860 int current;
1861 int line_idx = 0;
1862 int prev_current = 0;
1863
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001864 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001865 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001866 if (fname == NULL)
1867 {
1868 semsg(_(e_invarg2), eap->arg);
1869 return;
1870 }
1871
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001872 ufunc = find_func(fname, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001873 if (ufunc == NULL)
1874 {
1875 char_u *p = untrans_function_name(fname);
1876
1877 if (p != NULL)
1878 // Try again without making it script-local.
1879 ufunc = find_func(p, NULL);
1880 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001881 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882 if (ufunc == NULL)
1883 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001884 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 return;
1886 }
1887 if (ufunc->uf_dfunc_idx < 0)
1888 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001889 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 return;
1891 }
1892 if (ufunc->uf_name_exp != NULL)
1893 msg((char *)ufunc->uf_name_exp);
1894 else
1895 msg((char *)ufunc->uf_name);
1896
1897 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1898 instr = dfunc->df_instr;
1899 for (current = 0; current < dfunc->df_instr_count; ++current)
1900 {
1901 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001902 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903
1904 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1905 {
1906 if (current > prev_current)
1907 {
1908 msg_puts("\n\n");
1909 prev_current = current;
1910 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001911 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1912 if (line != NULL)
1913 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 }
1915
1916 switch (iptr->isn_type)
1917 {
1918 case ISN_EXEC:
1919 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1920 break;
1921 case ISN_ECHO:
1922 {
1923 echo_T *echo = &iptr->isn_arg.echo;
1924
1925 smsg("%4d %s %d", current,
1926 echo->echo_with_white ? "ECHO" : "ECHON",
1927 echo->echo_count);
1928 }
1929 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001930 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01001931 smsg("%4d EXECUTE %lld", current,
1932 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01001933 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 case ISN_LOAD:
1935 if (iptr->isn_arg.number < 0)
1936 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01001937 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001938 else
Bram Moolenaar10827722020-03-23 22:53:22 +01001939 smsg("%4d LOAD $%lld", current,
1940 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 break;
1942 case ISN_LOADV:
1943 smsg("%4d LOADV v:%s", current,
1944 get_vim_var_name(iptr->isn_arg.number));
1945 break;
1946 case ISN_LOADSCRIPT:
1947 {
1948 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001949 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001950 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1951 + iptr->isn_arg.script.script_idx;
1952
1953 smsg("%4d LOADSCRIPT %s from %s", current,
1954 sv->sv_name, si->sn_name);
1955 }
1956 break;
1957 case ISN_LOADS:
1958 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001959 scriptitem_T *si = SCRIPT_ITEM(
1960 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961
1962 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001963 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 }
1965 break;
1966 case ISN_LOADG:
1967 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1968 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001969 case ISN_LOADB:
1970 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
1971 break;
1972 case ISN_LOADW:
1973 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
1974 break;
1975 case ISN_LOADT:
1976 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
1977 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 case ISN_LOADOPT:
1979 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1980 break;
1981 case ISN_LOADENV:
1982 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1983 break;
1984 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01001985 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 break;
1987
1988 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001989 if (iptr->isn_arg.number < 0)
1990 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01001991 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001992 else
Bram Moolenaar10827722020-03-23 22:53:22 +01001993 smsg("%4d STORE $%lld", current,
1994 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001995 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001996 case ISN_STOREV:
1997 smsg("%4d STOREV v:%s", current,
1998 get_vim_var_name(iptr->isn_arg.number));
1999 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002000 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002001 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2002 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002003 case ISN_STOREB:
2004 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2005 break;
2006 case ISN_STOREW:
2007 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2008 break;
2009 case ISN_STORET:
2010 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2011 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002012 case ISN_STORES:
2013 {
2014 scriptitem_T *si = SCRIPT_ITEM(
2015 iptr->isn_arg.loadstore.ls_sid);
2016
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002017 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002018 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 break;
2021 case ISN_STORESCRIPT:
2022 {
2023 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002024 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2026 + iptr->isn_arg.script.script_idx;
2027
2028 smsg("%4d STORESCRIPT %s in %s", current,
2029 sv->sv_name, si->sn_name);
2030 }
2031 break;
2032 case ISN_STOREOPT:
2033 smsg("%4d STOREOPT &%s", current,
2034 iptr->isn_arg.storeopt.so_name);
2035 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002036 case ISN_STOREENV:
2037 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2038 break;
2039 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002040 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002041 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 case ISN_STORENR:
2043 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002044 iptr->isn_arg.storenr.stnr_val,
2045 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 break;
2047
2048 // constants
2049 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002050 smsg("%4d PUSHNR %lld", current,
2051 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002052 break;
2053 case ISN_PUSHBOOL:
2054 case ISN_PUSHSPEC:
2055 smsg("%4d PUSH %s", current,
2056 get_var_special_name(iptr->isn_arg.number));
2057 break;
2058 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002059#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002060 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002061#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002062 break;
2063 case ISN_PUSHS:
2064 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2065 break;
2066 case ISN_PUSHBLOB:
2067 {
2068 char_u *r;
2069 char_u numbuf[NUMBUFLEN];
2070 char_u *tofree;
2071
2072 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002073 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 vim_free(tofree);
2075 }
2076 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002077 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002078 {
2079 char *name = (char *)iptr->isn_arg.string;
2080
2081 smsg("%4d PUSHFUNC \"%s\"", current,
2082 name == NULL ? "[none]" : name);
2083 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002084 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002085 case ISN_PUSHCHANNEL:
2086#ifdef FEAT_JOB_CHANNEL
2087 {
2088 channel_T *channel = iptr->isn_arg.channel;
2089
2090 smsg("%4d PUSHCHANNEL %d", current,
2091 channel == NULL ? 0 : channel->ch_id);
2092 }
2093#endif
2094 break;
2095 case ISN_PUSHJOB:
2096#ifdef FEAT_JOB_CHANNEL
2097 {
2098 typval_T tv;
2099 char_u *name;
2100
2101 tv.v_type = VAR_JOB;
2102 tv.vval.v_job = iptr->isn_arg.job;
2103 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002104 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002105 }
2106#endif
2107 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002108 case ISN_PUSHEXC:
2109 smsg("%4d PUSH v:exception", current);
2110 break;
2111 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002112 smsg("%4d NEWLIST size %lld", current,
2113 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 break;
2115 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002116 smsg("%4d NEWDICT size %lld", current,
2117 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 break;
2119
2120 // function call
2121 case ISN_BCALL:
2122 {
2123 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2124
2125 smsg("%4d BCALL %s(argc %d)", current,
2126 internal_func_name(cbfunc->cbf_idx),
2127 cbfunc->cbf_argcount);
2128 }
2129 break;
2130 case ISN_DCALL:
2131 {
2132 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2133 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2134 + cdfunc->cdf_idx;
2135
2136 smsg("%4d DCALL %s(argc %d)", current,
2137 df->df_ufunc->uf_name_exp != NULL
2138 ? df->df_ufunc->uf_name_exp
2139 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2140 }
2141 break;
2142 case ISN_UCALL:
2143 {
2144 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2145
2146 smsg("%4d UCALL %s(argc %d)", current,
2147 cufunc->cuf_name, cufunc->cuf_argcount);
2148 }
2149 break;
2150 case ISN_PCALL:
2151 {
2152 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2153
2154 smsg("%4d PCALL%s (argc %d)", current,
2155 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2156 }
2157 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002158 case ISN_PCALL_END:
2159 smsg("%4d PCALL end", current);
2160 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002161 case ISN_RETURN:
2162 smsg("%4d RETURN", current);
2163 break;
2164 case ISN_FUNCREF:
2165 {
2166 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2167 + iptr->isn_arg.number;
2168
2169 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
2170 }
2171 break;
2172
2173 case ISN_JUMP:
2174 {
2175 char *when = "?";
2176
2177 switch (iptr->isn_arg.jump.jump_when)
2178 {
2179 case JUMP_ALWAYS:
2180 when = "JUMP";
2181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002182 case JUMP_AND_KEEP_IF_TRUE:
2183 when = "JUMP_AND_KEEP_IF_TRUE";
2184 break;
2185 case JUMP_IF_FALSE:
2186 when = "JUMP_IF_FALSE";
2187 break;
2188 case JUMP_AND_KEEP_IF_FALSE:
2189 when = "JUMP_AND_KEEP_IF_FALSE";
2190 break;
2191 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002192 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002193 iptr->isn_arg.jump.jump_where);
2194 }
2195 break;
2196
2197 case ISN_FOR:
2198 {
2199 forloop_T *forloop = &iptr->isn_arg.forloop;
2200
2201 smsg("%4d FOR $%d -> %d", current,
2202 forloop->for_idx, forloop->for_end);
2203 }
2204 break;
2205
2206 case ISN_TRY:
2207 {
2208 try_T *try = &iptr->isn_arg.try;
2209
2210 smsg("%4d TRY catch -> %d, finally -> %d", current,
2211 try->try_catch, try->try_finally);
2212 }
2213 break;
2214 case ISN_CATCH:
2215 // TODO
2216 smsg("%4d CATCH", current);
2217 break;
2218 case ISN_ENDTRY:
2219 smsg("%4d ENDTRY", current);
2220 break;
2221 case ISN_THROW:
2222 smsg("%4d THROW", current);
2223 break;
2224
2225 // expression operations on number
2226 case ISN_OPNR:
2227 case ISN_OPFLOAT:
2228 case ISN_OPANY:
2229 {
2230 char *what;
2231 char *ins;
2232
2233 switch (iptr->isn_arg.op.op_type)
2234 {
2235 case EXPR_MULT: what = "*"; break;
2236 case EXPR_DIV: what = "/"; break;
2237 case EXPR_REM: what = "%"; break;
2238 case EXPR_SUB: what = "-"; break;
2239 case EXPR_ADD: what = "+"; break;
2240 default: what = "???"; break;
2241 }
2242 switch (iptr->isn_type)
2243 {
2244 case ISN_OPNR: ins = "OPNR"; break;
2245 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2246 case ISN_OPANY: ins = "OPANY"; break;
2247 default: ins = "???"; break;
2248 }
2249 smsg("%4d %s %s", current, ins, what);
2250 }
2251 break;
2252
2253 case ISN_COMPAREBOOL:
2254 case ISN_COMPARESPECIAL:
2255 case ISN_COMPARENR:
2256 case ISN_COMPAREFLOAT:
2257 case ISN_COMPARESTRING:
2258 case ISN_COMPAREBLOB:
2259 case ISN_COMPARELIST:
2260 case ISN_COMPAREDICT:
2261 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 case ISN_COMPAREANY:
2263 {
2264 char *p;
2265 char buf[10];
2266 char *type;
2267
2268 switch (iptr->isn_arg.op.op_type)
2269 {
2270 case EXPR_EQUAL: p = "=="; break;
2271 case EXPR_NEQUAL: p = "!="; break;
2272 case EXPR_GREATER: p = ">"; break;
2273 case EXPR_GEQUAL: p = ">="; break;
2274 case EXPR_SMALLER: p = "<"; break;
2275 case EXPR_SEQUAL: p = "<="; break;
2276 case EXPR_MATCH: p = "=~"; break;
2277 case EXPR_IS: p = "is"; break;
2278 case EXPR_ISNOT: p = "isnot"; break;
2279 case EXPR_NOMATCH: p = "!~"; break;
2280 default: p = "???"; break;
2281 }
2282 STRCPY(buf, p);
2283 if (iptr->isn_arg.op.op_ic == TRUE)
2284 strcat(buf, "?");
2285 switch(iptr->isn_type)
2286 {
2287 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2288 case ISN_COMPARESPECIAL:
2289 type = "COMPARESPECIAL"; break;
2290 case ISN_COMPARENR: type = "COMPARENR"; break;
2291 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2292 case ISN_COMPARESTRING:
2293 type = "COMPARESTRING"; break;
2294 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2295 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2296 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2297 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2299 default: type = "???"; break;
2300 }
2301
2302 smsg("%4d %s %s", current, type, buf);
2303 }
2304 break;
2305
2306 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2307 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2308
2309 // expression operations
2310 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2311 case ISN_INDEX: smsg("%4d INDEX", current); break;
2312 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2313 iptr->isn_arg.string); break;
2314 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2315
2316 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2317 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2318 vartype_name(iptr->isn_arg.type.ct_type),
2319 iptr->isn_arg.type.ct_off);
2320 break;
2321 case ISN_2BOOL: if (iptr->isn_arg.number)
2322 smsg("%4d INVERT (!val)", current);
2323 else
2324 smsg("%4d 2BOOL (!!val)", current);
2325 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002326 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2327 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 break;
2329
2330 case ISN_DROP: smsg("%4d DROP", current); break;
2331 }
2332 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333}
2334
2335/*
2336 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2337 * list, etc. Mostly like what JavaScript does, except that empty list and
2338 * empty dictionary are FALSE.
2339 */
2340 int
2341tv2bool(typval_T *tv)
2342{
2343 switch (tv->v_type)
2344 {
2345 case VAR_NUMBER:
2346 return tv->vval.v_number != 0;
2347 case VAR_FLOAT:
2348#ifdef FEAT_FLOAT
2349 return tv->vval.v_float != 0.0;
2350#else
2351 break;
2352#endif
2353 case VAR_PARTIAL:
2354 return tv->vval.v_partial != NULL;
2355 case VAR_FUNC:
2356 case VAR_STRING:
2357 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2358 case VAR_LIST:
2359 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2360 case VAR_DICT:
2361 return tv->vval.v_dict != NULL
2362 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2363 case VAR_BOOL:
2364 case VAR_SPECIAL:
2365 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2366 case VAR_JOB:
2367#ifdef FEAT_JOB_CHANNEL
2368 return tv->vval.v_job != NULL;
2369#else
2370 break;
2371#endif
2372 case VAR_CHANNEL:
2373#ifdef FEAT_JOB_CHANNEL
2374 return tv->vval.v_channel != NULL;
2375#else
2376 break;
2377#endif
2378 case VAR_BLOB:
2379 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2380 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002381 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 case VAR_VOID:
2383 break;
2384 }
2385 return FALSE;
2386}
2387
2388/*
2389 * If "tv" is a string give an error and return FAIL.
2390 */
2391 int
2392check_not_string(typval_T *tv)
2393{
2394 if (tv->v_type == VAR_STRING)
2395 {
2396 emsg(_("E1030: Using a String as a Number"));
2397 clear_tv(tv);
2398 return FAIL;
2399 }
2400 return OK;
2401}
2402
2403
2404#endif // FEAT_EVAL