blob: 7172718c29621d90148639d894efb1862df88d8e [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 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200463 dictitem_T *v;
464
465 v = find_var(name, NULL, FALSE);
466 if (v == NULL)
467 {
468 semsg(_(e_unknownfunc), name);
469 return FAIL;
470 }
471 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
472 {
473 semsg(_(e_unknownfunc), name);
474 return FAIL;
475 }
476 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 }
478 return OK;
479}
480
481/*
482 * Call a "def" function from old Vim script.
483 * Return OK or FAIL.
484 */
485 int
486call_def_function(
487 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200488 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489 typval_T *argv, // arguments
490 typval_T *rettv) // return value
491{
492 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200493 int argc = argc_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 int initial_frame_ptr;
495 typval_T *tv;
496 int idx;
497 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100498 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200499 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500
501// Get pointer to item in the stack.
502#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
503
504// Get pointer to item at the bottom of the stack, -1 is the bottom.
505#undef STACK_TV_BOT
506#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
507
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200508// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
510
Bram Moolenaara80faa82020-04-12 19:37:17 +0200511 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
513 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100514 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
516
517 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
518
519 // Put arguments on the stack.
520 for (idx = 0; idx < argc; ++idx)
521 {
522 copy_tv(&argv[idx], STACK_TV_BOT(0));
523 ++ectx.ec_stack.ga_len;
524 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200525
526 // Turn varargs into a list. Empty list if no args.
527 if (ufunc->uf_va_name != NULL)
528 {
529 int vararg_count = argc - ufunc->uf_args.ga_len;
530
531 if (vararg_count < 0)
532 vararg_count = 0;
533 else
534 argc -= vararg_count;
535 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200536 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200537 if (defcount > 0)
538 // Move varargs list to below missing default arguments.
539 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
540 --ectx.ec_stack.ga_len;
541 }
542
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100543 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200544 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100545 if (defcount > 0)
546 for (idx = 0; idx < defcount; ++idx)
547 {
548 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
549 ++ectx.ec_stack.ga_len;
550 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200551 if (ufunc->uf_va_name != NULL)
552 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100553
554 // Frame pointer points to just after arguments.
555 ectx.ec_frame = ectx.ec_stack.ga_len;
556 initial_frame_ptr = ectx.ec_frame;
557
558 // dummy frame entries
559 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
560 {
561 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
562 ++ectx.ec_stack.ga_len;
563 }
564
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200565 {
566 // Reserve space for local variables.
567 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
568 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200570 for (idx = 0; idx < dfunc->df_varcount; ++idx)
571 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
572 ectx.ec_stack.ga_len += dfunc->df_varcount;
573
574 ectx.ec_instr = dfunc->df_instr;
575 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100576
Bram Moolenaara26b9702020-04-18 19:53:28 +0200577 // Commands behave like vim9script.
578 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
579
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580 // Decide where to start execution, handles optional arguments.
581 init_instr_idx(ufunc, argc, &ectx);
582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 for (;;)
584 {
585 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100586
587 veryfast_breakcheck();
588 if (got_int)
589 {
590 // Turn CTRL-C into an exception.
591 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100592 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100593 goto failed;
594 did_throw = TRUE;
595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596
Bram Moolenaara26b9702020-04-18 19:53:28 +0200597 if (did_emsg && msg_list != NULL && *msg_list != NULL)
598 {
599 // Turn an error message into an exception.
600 did_emsg = FALSE;
601 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
602 goto failed;
603 did_throw = TRUE;
604 *msg_list = NULL;
605 }
606
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 if (did_throw && !ectx.ec_in_catch)
608 {
609 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100610 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
612 // An exception jumps to the first catch, finally, or returns from
613 // the current function.
614 if (trystack->ga_len > 0)
615 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
616 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
617 {
618 // jump to ":catch" or ":finally"
619 ectx.ec_in_catch = TRUE;
620 ectx.ec_iidx = trycmd->tcd_catch_idx;
621 }
622 else
623 {
624 // not inside try or need to return from current functions.
625 if (ectx.ec_frame == initial_frame_ptr)
626 {
627 // At the toplevel we are done. Push a dummy return value.
628 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
629 goto failed;
630 tv = STACK_TV_BOT(0);
631 tv->v_type = VAR_NUMBER;
632 tv->vval.v_number = 0;
633 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100634 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 goto done;
636 }
637
638 func_return(&ectx);
639 }
640 continue;
641 }
642
643 iptr = &ectx.ec_instr[ectx.ec_iidx++];
644 switch (iptr->isn_type)
645 {
646 // execute Ex command line
647 case ISN_EXEC:
648 do_cmdline_cmd(iptr->isn_arg.string);
649 break;
650
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200651 // execute Ex command from pieces on the stack
652 case ISN_EXECCONCAT:
653 {
654 int count = iptr->isn_arg.number;
655 int len = 0;
656 int pass;
657 int i;
658 char_u *cmd = NULL;
659 char_u *str;
660
661 for (pass = 1; pass <= 2; ++pass)
662 {
663 for (i = 0; i < count; ++i)
664 {
665 tv = STACK_TV_BOT(i - count);
666 str = tv->vval.v_string;
667 if (str != NULL && *str != NUL)
668 {
669 if (pass == 2)
670 STRCPY(cmd + len, str);
671 len += STRLEN(str);
672 }
673 if (pass == 2)
674 clear_tv(tv);
675 }
676 if (pass == 1)
677 {
678 cmd = alloc(len + 1);
679 if (cmd == NULL)
680 goto failed;
681 len = 0;
682 }
683 }
684
685 do_cmdline_cmd(cmd);
686 vim_free(cmd);
687 }
688 break;
689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 // execute :echo {string} ...
691 case ISN_ECHO:
692 {
693 int count = iptr->isn_arg.echo.echo_count;
694 int atstart = TRUE;
695 int needclr = TRUE;
696
697 for (idx = 0; idx < count; ++idx)
698 {
699 tv = STACK_TV_BOT(idx - count);
700 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
701 &atstart, &needclr);
702 clear_tv(tv);
703 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100704 if (needclr)
705 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 ectx.ec_stack.ga_len -= count;
707 }
708 break;
709
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200710 // :execute {string} ...
711 // :echomsg {string} ...
712 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100713 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200714 case ISN_ECHOMSG:
715 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100716 {
717 int count = iptr->isn_arg.number;
718 garray_T ga;
719 char_u buf[NUMBUFLEN];
720 char_u *p;
721 int len;
722 int failed = FALSE;
723
724 ga_init2(&ga, 1, 80);
725 for (idx = 0; idx < count; ++idx)
726 {
727 tv = STACK_TV_BOT(idx - count);
728 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
729 {
730 emsg(_(e_inval_string));
731 break;
732 }
733 else
734 p = tv_get_string_buf(tv, buf);
735
736 len = (int)STRLEN(p);
737 if (ga_grow(&ga, len + 2) == FAIL)
738 failed = TRUE;
739 else
740 {
741 if (ga.ga_len > 0)
742 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
743 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
744 ga.ga_len += len;
745 }
746 clear_tv(tv);
747 }
748 ectx.ec_stack.ga_len -= count;
749
750 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200751 {
752 if (iptr->isn_type == ISN_EXECUTE)
753 do_cmdline_cmd((char_u *)ga.ga_data);
754 else
755 {
756 msg_sb_eol();
757 if (iptr->isn_type == ISN_ECHOMSG)
758 {
759 msg_attr(ga.ga_data, echo_attr);
760 out_flush();
761 }
762 else
763 {
764 int save_did_emsg = did_emsg;
765
766 SOURCING_LNUM = iptr->isn_lnum;
767 emsg(ga.ga_data);
768 if (!force_abort)
769 // We don't want to abort following
770 // commands, restore did_emsg.
771 did_emsg = save_did_emsg;
772 }
773 }
774 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100775 ga_clear(&ga);
776 }
777 break;
778
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 // load local variable or argument
780 case ISN_LOAD:
781 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
782 goto failed;
783 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
784 ++ectx.ec_stack.ga_len;
785 break;
786
787 // load v: variable
788 case ISN_LOADV:
789 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
790 goto failed;
791 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
792 ++ectx.ec_stack.ga_len;
793 break;
794
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100795 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796 case ISN_LOADSCRIPT:
797 {
798 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100799 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 svar_T *sv;
801
802 sv = ((svar_T *)si->sn_var_vals.ga_data)
803 + iptr->isn_arg.script.script_idx;
804 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
805 goto failed;
806 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
807 ++ectx.ec_stack.ga_len;
808 }
809 break;
810
811 // load s: variable in old script
812 case ISN_LOADS:
813 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100814 hashtab_T *ht = &SCRIPT_VARS(
815 iptr->isn_arg.loadstore.ls_sid);
816 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100818
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 if (di == NULL)
820 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100821 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 goto failed;
823 }
824 else
825 {
826 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
827 goto failed;
828 copy_tv(&di->di_tv, STACK_TV_BOT(0));
829 ++ectx.ec_stack.ga_len;
830 }
831 }
832 break;
833
Bram Moolenaard3aac292020-04-19 14:32:17 +0200834 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200836 case ISN_LOADB:
837 case ISN_LOADW:
838 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200840 dictitem_T *di = NULL;
841 hashtab_T *ht = NULL;
842 char namespace;
843 switch (iptr->isn_type)
844 {
845 case ISN_LOADG:
846 ht = get_globvar_ht();
847 namespace = 'g';
848 break;
849 case ISN_LOADB:
850 ht = &curbuf->b_vars->dv_hashtab;
851 namespace = 'b';
852 break;
853 case ISN_LOADW:
854 ht = &curwin->w_vars->dv_hashtab;
855 namespace = 'w';
856 break;
857 case ISN_LOADT:
858 ht = &curtab->tp_vars->dv_hashtab;
859 namespace = 't';
860 break;
861 default: // Cannot reach here
862 goto failed;
863 }
864 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100865
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100866 if (di == NULL)
867 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200868 semsg(_("E121: Undefined variable: %c:%s"),
869 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 goto failed;
871 }
872 else
873 {
874 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
875 goto failed;
876 copy_tv(&di->di_tv, STACK_TV_BOT(0));
877 ++ectx.ec_stack.ga_len;
878 }
879 }
880 break;
881
882 // load &option
883 case ISN_LOADOPT:
884 {
885 typval_T optval;
886 char_u *name = iptr->isn_arg.string;
887
Bram Moolenaara8c17702020-04-01 21:17:24 +0200888 // This is not expected to fail, name is checked during
889 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
891 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100892 if (get_option_tv(&name, &optval, TRUE) == FAIL)
893 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 *STACK_TV_BOT(0) = optval;
895 ++ectx.ec_stack.ga_len;
896 }
897 break;
898
899 // load $ENV
900 case ISN_LOADENV:
901 {
902 typval_T optval;
903 char_u *name = iptr->isn_arg.string;
904
905 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
906 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100907 // name is always valid, checked when compiling
908 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 *STACK_TV_BOT(0) = optval;
910 ++ectx.ec_stack.ga_len;
911 }
912 break;
913
914 // load @register
915 case ISN_LOADREG:
916 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
917 goto failed;
918 tv = STACK_TV_BOT(0);
919 tv->v_type = VAR_STRING;
920 tv->vval.v_string = get_reg_contents(
921 iptr->isn_arg.number, GREG_EXPR_SRC);
922 ++ectx.ec_stack.ga_len;
923 break;
924
925 // store local variable
926 case ISN_STORE:
927 --ectx.ec_stack.ga_len;
928 tv = STACK_TV_VAR(iptr->isn_arg.number);
929 clear_tv(tv);
930 *tv = *STACK_TV_BOT(0);
931 break;
932
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100933 // store s: variable in old script
934 case ISN_STORES:
935 {
936 hashtab_T *ht = &SCRIPT_VARS(
937 iptr->isn_arg.loadstore.ls_sid);
938 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100939 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100940
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100941 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100942 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200943 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100944 else
945 {
946 clear_tv(&di->di_tv);
947 di->di_tv = *STACK_TV_BOT(0);
948 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100949 }
950 break;
951
952 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100953 case ISN_STORESCRIPT:
954 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100955 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 iptr->isn_arg.script.script_sid);
957 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
958 + iptr->isn_arg.script.script_idx;
959
960 --ectx.ec_stack.ga_len;
961 clear_tv(sv->sv_tv);
962 *sv->sv_tv = *STACK_TV_BOT(0);
963 }
964 break;
965
966 // store option
967 case ISN_STOREOPT:
968 {
969 long n = 0;
970 char_u *s = NULL;
971 char *msg;
972
973 --ectx.ec_stack.ga_len;
974 tv = STACK_TV_BOT(0);
975 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100976 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100978 if (s == NULL)
979 s = (char_u *)"";
980 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981 else if (tv->v_type == VAR_NUMBER)
982 n = tv->vval.v_number;
983 else
984 {
985 emsg(_("E1051: Expected string or number"));
986 goto failed;
987 }
988 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
989 n, s, iptr->isn_arg.storeopt.so_flags);
990 if (msg != NULL)
991 {
992 emsg(_(msg));
993 goto failed;
994 }
995 clear_tv(tv);
996 }
997 break;
998
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100999 // store $ENV
1000 case ISN_STOREENV:
1001 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001002 tv = STACK_TV_BOT(0);
1003 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1004 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001005 break;
1006
1007 // store @r
1008 case ISN_STOREREG:
1009 {
1010 int reg = iptr->isn_arg.number;
1011
1012 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001013 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001014 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001015 tv_get_string(tv), -1, FALSE);
1016 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001017 }
1018 break;
1019
1020 // store v: variable
1021 case ISN_STOREV:
1022 --ectx.ec_stack.ga_len;
1023 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1024 == FAIL)
1025 goto failed;
1026 break;
1027
Bram Moolenaard3aac292020-04-19 14:32:17 +02001028 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001029 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001030 case ISN_STOREB:
1031 case ISN_STOREW:
1032 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 {
1034 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001035 hashtab_T *ht;
1036 switch (iptr->isn_type)
1037 {
1038 case ISN_STOREG:
1039 ht = get_globvar_ht();
1040 break;
1041 case ISN_STOREB:
1042 ht = &curbuf->b_vars->dv_hashtab;
1043 break;
1044 case ISN_STOREW:
1045 ht = &curwin->w_vars->dv_hashtab;
1046 break;
1047 case ISN_STORET:
1048 ht = &curtab->tp_vars->dv_hashtab;
1049 break;
1050 default: // Cannot reach here
1051 goto failed;
1052 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053
1054 --ectx.ec_stack.ga_len;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001055 di = find_var_in_ht(ht, 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001056 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001057 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001058 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 else
1060 {
1061 clear_tv(&di->di_tv);
1062 di->di_tv = *STACK_TV_BOT(0);
1063 }
1064 }
1065 break;
1066
1067 // store number in local variable
1068 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001069 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 clear_tv(tv);
1071 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001072 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 break;
1074
1075 // push constant
1076 case ISN_PUSHNR:
1077 case ISN_PUSHBOOL:
1078 case ISN_PUSHSPEC:
1079 case ISN_PUSHF:
1080 case ISN_PUSHS:
1081 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001082 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001083 case ISN_PUSHCHANNEL:
1084 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001085 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1086 goto failed;
1087 tv = STACK_TV_BOT(0);
1088 ++ectx.ec_stack.ga_len;
1089 switch (iptr->isn_type)
1090 {
1091 case ISN_PUSHNR:
1092 tv->v_type = VAR_NUMBER;
1093 tv->vval.v_number = iptr->isn_arg.number;
1094 break;
1095 case ISN_PUSHBOOL:
1096 tv->v_type = VAR_BOOL;
1097 tv->vval.v_number = iptr->isn_arg.number;
1098 break;
1099 case ISN_PUSHSPEC:
1100 tv->v_type = VAR_SPECIAL;
1101 tv->vval.v_number = iptr->isn_arg.number;
1102 break;
1103#ifdef FEAT_FLOAT
1104 case ISN_PUSHF:
1105 tv->v_type = VAR_FLOAT;
1106 tv->vval.v_float = iptr->isn_arg.fnumber;
1107 break;
1108#endif
1109 case ISN_PUSHBLOB:
1110 blob_copy(iptr->isn_arg.blob, tv);
1111 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001112 case ISN_PUSHFUNC:
1113 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001114 if (iptr->isn_arg.string == NULL)
1115 tv->vval.v_string = NULL;
1116 else
1117 tv->vval.v_string =
1118 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001119 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001120 case ISN_PUSHCHANNEL:
1121#ifdef FEAT_JOB_CHANNEL
1122 tv->v_type = VAR_CHANNEL;
1123 tv->vval.v_channel = iptr->isn_arg.channel;
1124 if (tv->vval.v_channel != NULL)
1125 ++tv->vval.v_channel->ch_refcount;
1126#endif
1127 break;
1128 case ISN_PUSHJOB:
1129#ifdef FEAT_JOB_CHANNEL
1130 tv->v_type = VAR_JOB;
1131 tv->vval.v_job = iptr->isn_arg.job;
1132 if (tv->vval.v_job != NULL)
1133 ++tv->vval.v_job->jv_refcount;
1134#endif
1135 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001136 default:
1137 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001138 tv->vval.v_string = vim_strsave(
1139 iptr->isn_arg.string == NULL
1140 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 }
1142 break;
1143
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001144 case ISN_UNLET:
1145 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1146 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1147 goto failed;
1148 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001149 case ISN_UNLETENV:
1150 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1151 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001152
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001153 // create a list from items on the stack; uses a single allocation
1154 // for the list header and the items
1155 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001156 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1157 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 break;
1159
1160 // create a dict from items on the stack
1161 case ISN_NEWDICT:
1162 {
1163 int count = iptr->isn_arg.number;
1164 dict_T *dict = dict_alloc();
1165 dictitem_T *item;
1166
1167 if (dict == NULL)
1168 goto failed;
1169 for (idx = 0; idx < count; ++idx)
1170 {
1171 // check key type is VAR_STRING
1172 tv = STACK_TV_BOT(2 * (idx - count));
1173 item = dictitem_alloc(tv->vval.v_string);
1174 clear_tv(tv);
1175 if (item == NULL)
1176 goto failed;
1177 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1178 item->di_tv.v_lock = 0;
1179 if (dict_add(dict, item) == FAIL)
1180 goto failed;
1181 }
1182
1183 if (count > 0)
1184 ectx.ec_stack.ga_len -= 2 * count - 1;
1185 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1186 goto failed;
1187 else
1188 ++ectx.ec_stack.ga_len;
1189 tv = STACK_TV_BOT(-1);
1190 tv->v_type = VAR_DICT;
1191 tv->vval.v_dict = dict;
1192 ++dict->dv_refcount;
1193 }
1194 break;
1195
1196 // call a :def function
1197 case ISN_DCALL:
1198 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1199 iptr->isn_arg.dfunc.cdf_argcount,
1200 &ectx) == FAIL)
1201 goto failed;
1202 break;
1203
1204 // call a builtin function
1205 case ISN_BCALL:
1206 SOURCING_LNUM = iptr->isn_lnum;
1207 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1208 iptr->isn_arg.bfunc.cbf_argcount,
1209 &ectx) == FAIL)
1210 goto failed;
1211 break;
1212
1213 // call a funcref or partial
1214 case ISN_PCALL:
1215 {
1216 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1217 int r;
1218 typval_T partial;
1219
1220 SOURCING_LNUM = iptr->isn_lnum;
1221 if (pfunc->cpf_top)
1222 {
1223 // funcref is above the arguments
1224 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1225 }
1226 else
1227 {
1228 // Get the funcref from the stack.
1229 --ectx.ec_stack.ga_len;
1230 partial = *STACK_TV_BOT(0);
1231 tv = &partial;
1232 }
1233 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1234 if (tv == &partial)
1235 clear_tv(&partial);
1236 if (r == FAIL)
1237 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001238 }
1239 break;
1240
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001241 case ISN_PCALL_END:
1242 // PCALL finished, arguments have been consumed and replaced by
1243 // the return value. Now clear the funcref from the stack,
1244 // and move the return value in its place.
1245 --ectx.ec_stack.ga_len;
1246 clear_tv(STACK_TV_BOT(-1));
1247 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1248 break;
1249
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 // call a user defined function or funcref/partial
1251 case ISN_UCALL:
1252 {
1253 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1254
1255 SOURCING_LNUM = iptr->isn_lnum;
1256 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001257 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001258 goto failed;
1259 }
1260 break;
1261
1262 // return from a :def function call
1263 case ISN_RETURN:
1264 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001265 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001266 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001267
1268 if (trystack->ga_len > 0)
1269 trycmd = ((trycmd_T *)trystack->ga_data)
1270 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
1272 && trycmd->tcd_finally_idx != 0)
1273 {
1274 // jump to ":finally"
1275 ectx.ec_iidx = trycmd->tcd_finally_idx;
1276 trycmd->tcd_return = TRUE;
1277 }
1278 else
1279 {
1280 // Restore previous function. If the frame pointer
1281 // is zero then there is none and we are done.
1282 if (ectx.ec_frame == initial_frame_ptr)
1283 goto done;
1284
1285 func_return(&ectx);
1286 }
1287 }
1288 break;
1289
1290 // push a function reference to a compiled function
1291 case ISN_FUNCREF:
1292 {
1293 partial_T *pt = NULL;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001294 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295
1296 pt = ALLOC_CLEAR_ONE(partial_T);
1297 if (pt == NULL)
1298 goto failed;
1299 dfunc = ((dfunc_T *)def_functions.ga_data)
1300 + iptr->isn_arg.number;
1301 pt->pt_func = dfunc->df_ufunc;
1302 pt->pt_refcount = 1;
1303 ++dfunc->df_ufunc->uf_refcount;
1304
1305 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1306 goto failed;
1307 tv = STACK_TV_BOT(0);
1308 ++ectx.ec_stack.ga_len;
1309 tv->vval.v_partial = pt;
1310 tv->v_type = VAR_PARTIAL;
1311 }
1312 break;
1313
1314 // jump if a condition is met
1315 case ISN_JUMP:
1316 {
1317 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1318 int jump = TRUE;
1319
1320 if (when != JUMP_ALWAYS)
1321 {
1322 tv = STACK_TV_BOT(-1);
1323 jump = tv2bool(tv);
1324 if (when == JUMP_IF_FALSE
1325 || when == JUMP_AND_KEEP_IF_FALSE)
1326 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001327 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001328 {
1329 // drop the value from the stack
1330 clear_tv(tv);
1331 --ectx.ec_stack.ga_len;
1332 }
1333 }
1334 if (jump)
1335 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1336 }
1337 break;
1338
1339 // top of a for loop
1340 case ISN_FOR:
1341 {
1342 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1343 typval_T *idxtv =
1344 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1345
1346 // push the next item from the list
1347 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1348 goto failed;
1349 if (++idxtv->vval.v_number >= list->lv_len)
1350 // past the end of the list, jump to "endfor"
1351 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1352 else if (list->lv_first == &range_list_item)
1353 {
1354 // non-materialized range() list
1355 tv = STACK_TV_BOT(0);
1356 tv->v_type = VAR_NUMBER;
1357 tv->vval.v_number = list_find_nr(
1358 list, idxtv->vval.v_number, NULL);
1359 ++ectx.ec_stack.ga_len;
1360 }
1361 else
1362 {
1363 listitem_T *li = list_find(list, idxtv->vval.v_number);
1364
1365 if (li == NULL)
1366 goto failed;
1367 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1368 ++ectx.ec_stack.ga_len;
1369 }
1370 }
1371 break;
1372
1373 // start of ":try" block
1374 case ISN_TRY:
1375 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001376 trycmd_T *trycmd = NULL;
1377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1379 goto failed;
1380 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1381 + ectx.ec_trystack.ga_len;
1382 ++ectx.ec_trystack.ga_len;
1383 ++trylevel;
1384 trycmd->tcd_frame = ectx.ec_frame;
1385 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1386 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001387 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 }
1389 break;
1390
1391 case ISN_PUSHEXC:
1392 if (current_exception == NULL)
1393 {
1394 iemsg("Evaluating catch while current_exception is NULL");
1395 goto failed;
1396 }
1397 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1398 goto failed;
1399 tv = STACK_TV_BOT(0);
1400 ++ectx.ec_stack.ga_len;
1401 tv->v_type = VAR_STRING;
1402 tv->vval.v_string = vim_strsave(
1403 (char_u *)current_exception->value);
1404 break;
1405
1406 case ISN_CATCH:
1407 {
1408 garray_T *trystack = &ectx.ec_trystack;
1409
1410 if (trystack->ga_len > 0)
1411 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001412 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 + trystack->ga_len - 1;
1414 trycmd->tcd_caught = TRUE;
1415 }
1416 did_emsg = got_int = did_throw = FALSE;
1417 catch_exception(current_exception);
1418 }
1419 break;
1420
1421 // end of ":try" block
1422 case ISN_ENDTRY:
1423 {
1424 garray_T *trystack = &ectx.ec_trystack;
1425
1426 if (trystack->ga_len > 0)
1427 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001428 trycmd_T *trycmd = NULL;
1429
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001430 --trystack->ga_len;
1431 --trylevel;
1432 trycmd = ((trycmd_T *)trystack->ga_data)
1433 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001434 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 {
1436 // discard the exception
1437 if (caught_stack == current_exception)
1438 caught_stack = caught_stack->caught;
1439 discard_current_exception();
1440 }
1441
1442 if (trycmd->tcd_return)
1443 {
1444 // Restore previous function. If the frame pointer
1445 // is zero then there is none and we are done.
1446 if (ectx.ec_frame == initial_frame_ptr)
1447 goto done;
1448
1449 func_return(&ectx);
1450 }
1451 }
1452 }
1453 break;
1454
1455 case ISN_THROW:
1456 --ectx.ec_stack.ga_len;
1457 tv = STACK_TV_BOT(0);
1458 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1459 {
1460 vim_free(tv->vval.v_string);
1461 goto failed;
1462 }
1463 did_throw = TRUE;
1464 break;
1465
1466 // compare with special values
1467 case ISN_COMPAREBOOL:
1468 case ISN_COMPARESPECIAL:
1469 {
1470 typval_T *tv1 = STACK_TV_BOT(-2);
1471 typval_T *tv2 = STACK_TV_BOT(-1);
1472 varnumber_T arg1 = tv1->vval.v_number;
1473 varnumber_T arg2 = tv2->vval.v_number;
1474 int res;
1475
1476 switch (iptr->isn_arg.op.op_type)
1477 {
1478 case EXPR_EQUAL: res = arg1 == arg2; break;
1479 case EXPR_NEQUAL: res = arg1 != arg2; break;
1480 default: res = 0; break;
1481 }
1482
1483 --ectx.ec_stack.ga_len;
1484 tv1->v_type = VAR_BOOL;
1485 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1486 }
1487 break;
1488
1489 // Operation with two number arguments
1490 case ISN_OPNR:
1491 case ISN_COMPARENR:
1492 {
1493 typval_T *tv1 = STACK_TV_BOT(-2);
1494 typval_T *tv2 = STACK_TV_BOT(-1);
1495 varnumber_T arg1 = tv1->vval.v_number;
1496 varnumber_T arg2 = tv2->vval.v_number;
1497 varnumber_T res;
1498
1499 switch (iptr->isn_arg.op.op_type)
1500 {
1501 case EXPR_MULT: res = arg1 * arg2; break;
1502 case EXPR_DIV: res = arg1 / arg2; break;
1503 case EXPR_REM: res = arg1 % arg2; break;
1504 case EXPR_SUB: res = arg1 - arg2; break;
1505 case EXPR_ADD: res = arg1 + arg2; break;
1506
1507 case EXPR_EQUAL: res = arg1 == arg2; break;
1508 case EXPR_NEQUAL: res = arg1 != arg2; break;
1509 case EXPR_GREATER: res = arg1 > arg2; break;
1510 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1511 case EXPR_SMALLER: res = arg1 < arg2; break;
1512 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1513 default: res = 0; break;
1514 }
1515
1516 --ectx.ec_stack.ga_len;
1517 if (iptr->isn_type == ISN_COMPARENR)
1518 {
1519 tv1->v_type = VAR_BOOL;
1520 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1521 }
1522 else
1523 tv1->vval.v_number = res;
1524 }
1525 break;
1526
1527 // Computation with two float arguments
1528 case ISN_OPFLOAT:
1529 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001530#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001531 {
1532 typval_T *tv1 = STACK_TV_BOT(-2);
1533 typval_T *tv2 = STACK_TV_BOT(-1);
1534 float_T arg1 = tv1->vval.v_float;
1535 float_T arg2 = tv2->vval.v_float;
1536 float_T res = 0;
1537 int cmp = FALSE;
1538
1539 switch (iptr->isn_arg.op.op_type)
1540 {
1541 case EXPR_MULT: res = arg1 * arg2; break;
1542 case EXPR_DIV: res = arg1 / arg2; break;
1543 case EXPR_SUB: res = arg1 - arg2; break;
1544 case EXPR_ADD: res = arg1 + arg2; break;
1545
1546 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1547 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1548 case EXPR_GREATER: cmp = arg1 > arg2; break;
1549 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1550 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1551 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1552 default: cmp = 0; break;
1553 }
1554 --ectx.ec_stack.ga_len;
1555 if (iptr->isn_type == ISN_COMPAREFLOAT)
1556 {
1557 tv1->v_type = VAR_BOOL;
1558 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1559 }
1560 else
1561 tv1->vval.v_float = res;
1562 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001563#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 break;
1565
1566 case ISN_COMPARELIST:
1567 {
1568 typval_T *tv1 = STACK_TV_BOT(-2);
1569 typval_T *tv2 = STACK_TV_BOT(-1);
1570 list_T *arg1 = tv1->vval.v_list;
1571 list_T *arg2 = tv2->vval.v_list;
1572 int cmp = FALSE;
1573 int ic = iptr->isn_arg.op.op_ic;
1574
1575 switch (iptr->isn_arg.op.op_type)
1576 {
1577 case EXPR_EQUAL: cmp =
1578 list_equal(arg1, arg2, ic, FALSE); break;
1579 case EXPR_NEQUAL: cmp =
1580 !list_equal(arg1, arg2, ic, FALSE); break;
1581 case EXPR_IS: cmp = arg1 == arg2; break;
1582 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1583 default: cmp = 0; break;
1584 }
1585 --ectx.ec_stack.ga_len;
1586 clear_tv(tv1);
1587 clear_tv(tv2);
1588 tv1->v_type = VAR_BOOL;
1589 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1590 }
1591 break;
1592
1593 case ISN_COMPAREBLOB:
1594 {
1595 typval_T *tv1 = STACK_TV_BOT(-2);
1596 typval_T *tv2 = STACK_TV_BOT(-1);
1597 blob_T *arg1 = tv1->vval.v_blob;
1598 blob_T *arg2 = tv2->vval.v_blob;
1599 int cmp = FALSE;
1600
1601 switch (iptr->isn_arg.op.op_type)
1602 {
1603 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1604 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1605 case EXPR_IS: cmp = arg1 == arg2; break;
1606 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1607 default: cmp = 0; break;
1608 }
1609 --ectx.ec_stack.ga_len;
1610 clear_tv(tv1);
1611 clear_tv(tv2);
1612 tv1->v_type = VAR_BOOL;
1613 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1614 }
1615 break;
1616
1617 // TODO: handle separately
1618 case ISN_COMPARESTRING:
1619 case ISN_COMPAREDICT:
1620 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001621 case ISN_COMPAREANY:
1622 {
1623 typval_T *tv1 = STACK_TV_BOT(-2);
1624 typval_T *tv2 = STACK_TV_BOT(-1);
1625 exptype_T exptype = iptr->isn_arg.op.op_type;
1626 int ic = iptr->isn_arg.op.op_ic;
1627
1628 typval_compare(tv1, tv2, exptype, ic);
1629 clear_tv(tv2);
1630 tv1->v_type = VAR_BOOL;
1631 tv1->vval.v_number = tv1->vval.v_number
1632 ? VVAL_TRUE : VVAL_FALSE;
1633 --ectx.ec_stack.ga_len;
1634 }
1635 break;
1636
1637 case ISN_ADDLIST:
1638 case ISN_ADDBLOB:
1639 {
1640 typval_T *tv1 = STACK_TV_BOT(-2);
1641 typval_T *tv2 = STACK_TV_BOT(-1);
1642
1643 if (iptr->isn_type == ISN_ADDLIST)
1644 eval_addlist(tv1, tv2);
1645 else
1646 eval_addblob(tv1, tv2);
1647 clear_tv(tv2);
1648 --ectx.ec_stack.ga_len;
1649 }
1650 break;
1651
1652 // Computation with two arguments of unknown type
1653 case ISN_OPANY:
1654 {
1655 typval_T *tv1 = STACK_TV_BOT(-2);
1656 typval_T *tv2 = STACK_TV_BOT(-1);
1657 varnumber_T n1, n2;
1658#ifdef FEAT_FLOAT
1659 float_T f1 = 0, f2 = 0;
1660#endif
1661 int error = FALSE;
1662
1663 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1664 {
1665 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1666 {
1667 eval_addlist(tv1, tv2);
1668 clear_tv(tv2);
1669 --ectx.ec_stack.ga_len;
1670 break;
1671 }
1672 else if (tv1->v_type == VAR_BLOB
1673 && tv2->v_type == VAR_BLOB)
1674 {
1675 eval_addblob(tv1, tv2);
1676 clear_tv(tv2);
1677 --ectx.ec_stack.ga_len;
1678 break;
1679 }
1680 }
1681#ifdef FEAT_FLOAT
1682 if (tv1->v_type == VAR_FLOAT)
1683 {
1684 f1 = tv1->vval.v_float;
1685 n1 = 0;
1686 }
1687 else
1688#endif
1689 {
1690 n1 = tv_get_number_chk(tv1, &error);
1691 if (error)
1692 goto failed;
1693#ifdef FEAT_FLOAT
1694 if (tv2->v_type == VAR_FLOAT)
1695 f1 = n1;
1696#endif
1697 }
1698#ifdef FEAT_FLOAT
1699 if (tv2->v_type == VAR_FLOAT)
1700 {
1701 f2 = tv2->vval.v_float;
1702 n2 = 0;
1703 }
1704 else
1705#endif
1706 {
1707 n2 = tv_get_number_chk(tv2, &error);
1708 if (error)
1709 goto failed;
1710#ifdef FEAT_FLOAT
1711 if (tv1->v_type == VAR_FLOAT)
1712 f2 = n2;
1713#endif
1714 }
1715#ifdef FEAT_FLOAT
1716 // if there is a float on either side the result is a float
1717 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1718 {
1719 switch (iptr->isn_arg.op.op_type)
1720 {
1721 case EXPR_MULT: f1 = f1 * f2; break;
1722 case EXPR_DIV: f1 = f1 / f2; break;
1723 case EXPR_SUB: f1 = f1 - f2; break;
1724 case EXPR_ADD: f1 = f1 + f2; break;
1725 default: emsg(_(e_modulus)); goto failed;
1726 }
1727 clear_tv(tv1);
1728 clear_tv(tv2);
1729 tv1->v_type = VAR_FLOAT;
1730 tv1->vval.v_float = f1;
1731 --ectx.ec_stack.ga_len;
1732 }
1733 else
1734#endif
1735 {
1736 switch (iptr->isn_arg.op.op_type)
1737 {
1738 case EXPR_MULT: n1 = n1 * n2; break;
1739 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1740 case EXPR_SUB: n1 = n1 - n2; break;
1741 case EXPR_ADD: n1 = n1 + n2; break;
1742 default: n1 = num_modulus(n1, n2); break;
1743 }
1744 clear_tv(tv1);
1745 clear_tv(tv2);
1746 tv1->v_type = VAR_NUMBER;
1747 tv1->vval.v_number = n1;
1748 --ectx.ec_stack.ga_len;
1749 }
1750 }
1751 break;
1752
1753 case ISN_CONCAT:
1754 {
1755 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1756 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1757 char_u *res;
1758
1759 res = concat_str(str1, str2);
1760 clear_tv(STACK_TV_BOT(-2));
1761 clear_tv(STACK_TV_BOT(-1));
1762 --ectx.ec_stack.ga_len;
1763 STACK_TV_BOT(-1)->vval.v_string = res;
1764 }
1765 break;
1766
1767 case ISN_INDEX:
1768 {
1769 list_T *list;
1770 varnumber_T n;
1771 listitem_T *li;
1772
1773 // list index: list is at stack-2, index at stack-1
1774 tv = STACK_TV_BOT(-2);
1775 if (tv->v_type != VAR_LIST)
1776 {
1777 emsg(_(e_listreq));
1778 goto failed;
1779 }
1780 list = tv->vval.v_list;
1781
1782 tv = STACK_TV_BOT(-1);
1783 if (tv->v_type != VAR_NUMBER)
1784 {
1785 emsg(_(e_number_exp));
1786 goto failed;
1787 }
1788 n = tv->vval.v_number;
1789 clear_tv(tv);
1790 if ((li = list_find(list, n)) == NULL)
1791 {
1792 semsg(_(e_listidx), n);
1793 goto failed;
1794 }
1795 --ectx.ec_stack.ga_len;
1796 clear_tv(STACK_TV_BOT(-1));
1797 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1798 }
1799 break;
1800
1801 // dict member with string key
1802 case ISN_MEMBER:
1803 {
1804 dict_T *dict;
1805 dictitem_T *di;
1806
1807 tv = STACK_TV_BOT(-1);
1808 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1809 {
1810 emsg(_(e_dictreq));
1811 goto failed;
1812 }
1813 dict = tv->vval.v_dict;
1814
1815 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1816 == NULL)
1817 {
1818 semsg(_(e_dictkey), iptr->isn_arg.string);
1819 goto failed;
1820 }
1821 clear_tv(tv);
1822 copy_tv(&di->di_tv, tv);
1823 }
1824 break;
1825
1826 case ISN_NEGATENR:
1827 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001828 if (tv->v_type != VAR_NUMBER
1829#ifdef FEAT_FLOAT
1830 && tv->v_type != VAR_FLOAT
1831#endif
1832 )
1833 {
1834 emsg(_(e_number_exp));
1835 goto failed;
1836 }
1837#ifdef FEAT_FLOAT
1838 if (tv->v_type == VAR_FLOAT)
1839 tv->vval.v_float = -tv->vval.v_float;
1840 else
1841#endif
1842 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001843 break;
1844
1845 case ISN_CHECKNR:
1846 {
1847 int error = FALSE;
1848
1849 tv = STACK_TV_BOT(-1);
1850 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001851 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 (void)tv_get_number_chk(tv, &error);
1853 if (error)
1854 goto failed;
1855 }
1856 break;
1857
1858 case ISN_CHECKTYPE:
1859 {
1860 checktype_T *ct = &iptr->isn_arg.type;
1861
1862 tv = STACK_TV_BOT(ct->ct_off);
1863 if (tv->v_type != ct->ct_type)
1864 {
1865 semsg(_("E1029: Expected %s but got %s"),
1866 vartype_name(ct->ct_type),
1867 vartype_name(tv->v_type));
1868 goto failed;
1869 }
1870 }
1871 break;
1872
1873 case ISN_2BOOL:
1874 {
1875 int n;
1876
1877 tv = STACK_TV_BOT(-1);
1878 n = tv2bool(tv);
1879 if (iptr->isn_arg.number) // invert
1880 n = !n;
1881 clear_tv(tv);
1882 tv->v_type = VAR_BOOL;
1883 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1884 }
1885 break;
1886
1887 case ISN_2STRING:
1888 {
1889 char_u *str;
1890
1891 tv = STACK_TV_BOT(iptr->isn_arg.number);
1892 if (tv->v_type != VAR_STRING)
1893 {
1894 str = typval_tostring(tv);
1895 clear_tv(tv);
1896 tv->v_type = VAR_STRING;
1897 tv->vval.v_string = str;
1898 }
1899 }
1900 break;
1901
1902 case ISN_DROP:
1903 --ectx.ec_stack.ga_len;
1904 clear_tv(STACK_TV_BOT(0));
1905 break;
1906 }
1907 }
1908
1909done:
1910 // function finished, get result from the stack.
1911 tv = STACK_TV_BOT(-1);
1912 *rettv = *tv;
1913 tv->v_type = VAR_UNKNOWN;
1914 ret = OK;
1915
1916failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001917 // When failed need to unwind the call stack.
1918 while (ectx.ec_frame != initial_frame_ptr)
1919 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001920failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02001921 current_sctx.sc_version = save_sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1923 clear_tv(STACK_TV(idx));
1924 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001925 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 return ret;
1927}
1928
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001929/*
1930 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001931 * We don't really need this at runtime, but we do have tests that require it,
1932 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 */
1934 void
1935ex_disassemble(exarg_T *eap)
1936{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001937 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001938 char_u *fname;
1939 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001940 dfunc_T *dfunc;
1941 isn_T *instr;
1942 int current;
1943 int line_idx = 0;
1944 int prev_current = 0;
1945
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001946 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001947 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001948 if (fname == NULL)
1949 {
1950 semsg(_(e_invarg2), eap->arg);
1951 return;
1952 }
1953
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001954 ufunc = find_func(fname, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001955 if (ufunc == NULL)
1956 {
1957 char_u *p = untrans_function_name(fname);
1958
1959 if (p != NULL)
1960 // Try again without making it script-local.
1961 ufunc = find_func(p, NULL);
1962 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001963 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001964 if (ufunc == NULL)
1965 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001966 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 return;
1968 }
1969 if (ufunc->uf_dfunc_idx < 0)
1970 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001971 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 return;
1973 }
1974 if (ufunc->uf_name_exp != NULL)
1975 msg((char *)ufunc->uf_name_exp);
1976 else
1977 msg((char *)ufunc->uf_name);
1978
1979 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1980 instr = dfunc->df_instr;
1981 for (current = 0; current < dfunc->df_instr_count; ++current)
1982 {
1983 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001984 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001985
1986 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1987 {
1988 if (current > prev_current)
1989 {
1990 msg_puts("\n\n");
1991 prev_current = current;
1992 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001993 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1994 if (line != NULL)
1995 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 }
1997
1998 switch (iptr->isn_type)
1999 {
2000 case ISN_EXEC:
2001 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2002 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002003 case ISN_EXECCONCAT:
2004 smsg("%4d EXECCONCAT %lld", current,
2005 (long long)iptr->isn_arg.number);
2006 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 case ISN_ECHO:
2008 {
2009 echo_T *echo = &iptr->isn_arg.echo;
2010
2011 smsg("%4d %s %d", current,
2012 echo->echo_with_white ? "ECHO" : "ECHON",
2013 echo->echo_count);
2014 }
2015 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002016 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002017 smsg("%4d EXECUTE %lld", current,
2018 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002019 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002020 case ISN_ECHOMSG:
2021 smsg("%4d ECHOMSG %lld", current,
2022 (long long)(iptr->isn_arg.number));
2023 break;
2024 case ISN_ECHOERR:
2025 smsg("%4d ECHOERR %lld", current,
2026 (long long)(iptr->isn_arg.number));
2027 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028 case ISN_LOAD:
2029 if (iptr->isn_arg.number < 0)
2030 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002031 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002032 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002033 smsg("%4d LOAD $%lld", current,
2034 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 break;
2036 case ISN_LOADV:
2037 smsg("%4d LOADV v:%s", current,
2038 get_vim_var_name(iptr->isn_arg.number));
2039 break;
2040 case ISN_LOADSCRIPT:
2041 {
2042 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002043 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2045 + iptr->isn_arg.script.script_idx;
2046
2047 smsg("%4d LOADSCRIPT %s from %s", current,
2048 sv->sv_name, si->sn_name);
2049 }
2050 break;
2051 case ISN_LOADS:
2052 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002053 scriptitem_T *si = SCRIPT_ITEM(
2054 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002055
2056 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002057 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002058 }
2059 break;
2060 case ISN_LOADG:
2061 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2062 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002063 case ISN_LOADB:
2064 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2065 break;
2066 case ISN_LOADW:
2067 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2068 break;
2069 case ISN_LOADT:
2070 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2071 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002072 case ISN_LOADOPT:
2073 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2074 break;
2075 case ISN_LOADENV:
2076 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2077 break;
2078 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002079 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002080 break;
2081
2082 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002083 if (iptr->isn_arg.number < 0)
2084 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002085 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002086 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002087 smsg("%4d STORE $%lld", current,
2088 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002090 case ISN_STOREV:
2091 smsg("%4d STOREV v:%s", current,
2092 get_vim_var_name(iptr->isn_arg.number));
2093 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002095 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2096 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002097 case ISN_STOREB:
2098 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2099 break;
2100 case ISN_STOREW:
2101 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2102 break;
2103 case ISN_STORET:
2104 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2105 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002106 case ISN_STORES:
2107 {
2108 scriptitem_T *si = SCRIPT_ITEM(
2109 iptr->isn_arg.loadstore.ls_sid);
2110
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002111 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002112 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002113 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 break;
2115 case ISN_STORESCRIPT:
2116 {
2117 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002118 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2120 + iptr->isn_arg.script.script_idx;
2121
2122 smsg("%4d STORESCRIPT %s in %s", current,
2123 sv->sv_name, si->sn_name);
2124 }
2125 break;
2126 case ISN_STOREOPT:
2127 smsg("%4d STOREOPT &%s", current,
2128 iptr->isn_arg.storeopt.so_name);
2129 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002130 case ISN_STOREENV:
2131 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2132 break;
2133 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002134 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002135 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136 case ISN_STORENR:
2137 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002138 iptr->isn_arg.storenr.stnr_val,
2139 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 break;
2141
2142 // constants
2143 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002144 smsg("%4d PUSHNR %lld", current,
2145 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146 break;
2147 case ISN_PUSHBOOL:
2148 case ISN_PUSHSPEC:
2149 smsg("%4d PUSH %s", current,
2150 get_var_special_name(iptr->isn_arg.number));
2151 break;
2152 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002153#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002154 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002155#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156 break;
2157 case ISN_PUSHS:
2158 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2159 break;
2160 case ISN_PUSHBLOB:
2161 {
2162 char_u *r;
2163 char_u numbuf[NUMBUFLEN];
2164 char_u *tofree;
2165
2166 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002167 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 vim_free(tofree);
2169 }
2170 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002171 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002172 {
2173 char *name = (char *)iptr->isn_arg.string;
2174
2175 smsg("%4d PUSHFUNC \"%s\"", current,
2176 name == NULL ? "[none]" : name);
2177 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002178 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002179 case ISN_PUSHCHANNEL:
2180#ifdef FEAT_JOB_CHANNEL
2181 {
2182 channel_T *channel = iptr->isn_arg.channel;
2183
2184 smsg("%4d PUSHCHANNEL %d", current,
2185 channel == NULL ? 0 : channel->ch_id);
2186 }
2187#endif
2188 break;
2189 case ISN_PUSHJOB:
2190#ifdef FEAT_JOB_CHANNEL
2191 {
2192 typval_T tv;
2193 char_u *name;
2194
2195 tv.v_type = VAR_JOB;
2196 tv.vval.v_job = iptr->isn_arg.job;
2197 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002198 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002199 }
2200#endif
2201 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 case ISN_PUSHEXC:
2203 smsg("%4d PUSH v:exception", current);
2204 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002205 case ISN_UNLET:
2206 smsg("%4d UNLET%s %s", current,
2207 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2208 iptr->isn_arg.unlet.ul_name);
2209 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002210 case ISN_UNLETENV:
2211 smsg("%4d UNLETENV%s $%s", current,
2212 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2213 iptr->isn_arg.unlet.ul_name);
2214 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002215 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002216 smsg("%4d NEWLIST size %lld", current,
2217 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002218 break;
2219 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002220 smsg("%4d NEWDICT size %lld", current,
2221 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 break;
2223
2224 // function call
2225 case ISN_BCALL:
2226 {
2227 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2228
2229 smsg("%4d BCALL %s(argc %d)", current,
2230 internal_func_name(cbfunc->cbf_idx),
2231 cbfunc->cbf_argcount);
2232 }
2233 break;
2234 case ISN_DCALL:
2235 {
2236 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2237 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2238 + cdfunc->cdf_idx;
2239
2240 smsg("%4d DCALL %s(argc %d)", current,
2241 df->df_ufunc->uf_name_exp != NULL
2242 ? df->df_ufunc->uf_name_exp
2243 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2244 }
2245 break;
2246 case ISN_UCALL:
2247 {
2248 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2249
2250 smsg("%4d UCALL %s(argc %d)", current,
2251 cufunc->cuf_name, cufunc->cuf_argcount);
2252 }
2253 break;
2254 case ISN_PCALL:
2255 {
2256 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2257
2258 smsg("%4d PCALL%s (argc %d)", current,
2259 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2260 }
2261 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002262 case ISN_PCALL_END:
2263 smsg("%4d PCALL end", current);
2264 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 case ISN_RETURN:
2266 smsg("%4d RETURN", current);
2267 break;
2268 case ISN_FUNCREF:
2269 {
2270 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2271 + iptr->isn_arg.number;
2272
2273 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
2274 }
2275 break;
2276
2277 case ISN_JUMP:
2278 {
2279 char *when = "?";
2280
2281 switch (iptr->isn_arg.jump.jump_when)
2282 {
2283 case JUMP_ALWAYS:
2284 when = "JUMP";
2285 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 case JUMP_AND_KEEP_IF_TRUE:
2287 when = "JUMP_AND_KEEP_IF_TRUE";
2288 break;
2289 case JUMP_IF_FALSE:
2290 when = "JUMP_IF_FALSE";
2291 break;
2292 case JUMP_AND_KEEP_IF_FALSE:
2293 when = "JUMP_AND_KEEP_IF_FALSE";
2294 break;
2295 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002296 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 iptr->isn_arg.jump.jump_where);
2298 }
2299 break;
2300
2301 case ISN_FOR:
2302 {
2303 forloop_T *forloop = &iptr->isn_arg.forloop;
2304
2305 smsg("%4d FOR $%d -> %d", current,
2306 forloop->for_idx, forloop->for_end);
2307 }
2308 break;
2309
2310 case ISN_TRY:
2311 {
2312 try_T *try = &iptr->isn_arg.try;
2313
2314 smsg("%4d TRY catch -> %d, finally -> %d", current,
2315 try->try_catch, try->try_finally);
2316 }
2317 break;
2318 case ISN_CATCH:
2319 // TODO
2320 smsg("%4d CATCH", current);
2321 break;
2322 case ISN_ENDTRY:
2323 smsg("%4d ENDTRY", current);
2324 break;
2325 case ISN_THROW:
2326 smsg("%4d THROW", current);
2327 break;
2328
2329 // expression operations on number
2330 case ISN_OPNR:
2331 case ISN_OPFLOAT:
2332 case ISN_OPANY:
2333 {
2334 char *what;
2335 char *ins;
2336
2337 switch (iptr->isn_arg.op.op_type)
2338 {
2339 case EXPR_MULT: what = "*"; break;
2340 case EXPR_DIV: what = "/"; break;
2341 case EXPR_REM: what = "%"; break;
2342 case EXPR_SUB: what = "-"; break;
2343 case EXPR_ADD: what = "+"; break;
2344 default: what = "???"; break;
2345 }
2346 switch (iptr->isn_type)
2347 {
2348 case ISN_OPNR: ins = "OPNR"; break;
2349 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2350 case ISN_OPANY: ins = "OPANY"; break;
2351 default: ins = "???"; break;
2352 }
2353 smsg("%4d %s %s", current, ins, what);
2354 }
2355 break;
2356
2357 case ISN_COMPAREBOOL:
2358 case ISN_COMPARESPECIAL:
2359 case ISN_COMPARENR:
2360 case ISN_COMPAREFLOAT:
2361 case ISN_COMPARESTRING:
2362 case ISN_COMPAREBLOB:
2363 case ISN_COMPARELIST:
2364 case ISN_COMPAREDICT:
2365 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 case ISN_COMPAREANY:
2367 {
2368 char *p;
2369 char buf[10];
2370 char *type;
2371
2372 switch (iptr->isn_arg.op.op_type)
2373 {
2374 case EXPR_EQUAL: p = "=="; break;
2375 case EXPR_NEQUAL: p = "!="; break;
2376 case EXPR_GREATER: p = ">"; break;
2377 case EXPR_GEQUAL: p = ">="; break;
2378 case EXPR_SMALLER: p = "<"; break;
2379 case EXPR_SEQUAL: p = "<="; break;
2380 case EXPR_MATCH: p = "=~"; break;
2381 case EXPR_IS: p = "is"; break;
2382 case EXPR_ISNOT: p = "isnot"; break;
2383 case EXPR_NOMATCH: p = "!~"; break;
2384 default: p = "???"; break;
2385 }
2386 STRCPY(buf, p);
2387 if (iptr->isn_arg.op.op_ic == TRUE)
2388 strcat(buf, "?");
2389 switch(iptr->isn_type)
2390 {
2391 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2392 case ISN_COMPARESPECIAL:
2393 type = "COMPARESPECIAL"; break;
2394 case ISN_COMPARENR: type = "COMPARENR"; break;
2395 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2396 case ISN_COMPARESTRING:
2397 type = "COMPARESTRING"; break;
2398 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2399 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2400 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2401 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2403 default: type = "???"; break;
2404 }
2405
2406 smsg("%4d %s %s", current, type, buf);
2407 }
2408 break;
2409
2410 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2411 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2412
2413 // expression operations
2414 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2415 case ISN_INDEX: smsg("%4d INDEX", current); break;
2416 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2417 iptr->isn_arg.string); break;
2418 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2419
2420 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2421 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2422 vartype_name(iptr->isn_arg.type.ct_type),
2423 iptr->isn_arg.type.ct_off);
2424 break;
2425 case ISN_2BOOL: if (iptr->isn_arg.number)
2426 smsg("%4d INVERT (!val)", current);
2427 else
2428 smsg("%4d 2BOOL (!!val)", current);
2429 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002430 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2431 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432 break;
2433
2434 case ISN_DROP: smsg("%4d DROP", current); break;
2435 }
2436 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437}
2438
2439/*
2440 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2441 * list, etc. Mostly like what JavaScript does, except that empty list and
2442 * empty dictionary are FALSE.
2443 */
2444 int
2445tv2bool(typval_T *tv)
2446{
2447 switch (tv->v_type)
2448 {
2449 case VAR_NUMBER:
2450 return tv->vval.v_number != 0;
2451 case VAR_FLOAT:
2452#ifdef FEAT_FLOAT
2453 return tv->vval.v_float != 0.0;
2454#else
2455 break;
2456#endif
2457 case VAR_PARTIAL:
2458 return tv->vval.v_partial != NULL;
2459 case VAR_FUNC:
2460 case VAR_STRING:
2461 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2462 case VAR_LIST:
2463 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2464 case VAR_DICT:
2465 return tv->vval.v_dict != NULL
2466 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2467 case VAR_BOOL:
2468 case VAR_SPECIAL:
2469 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2470 case VAR_JOB:
2471#ifdef FEAT_JOB_CHANNEL
2472 return tv->vval.v_job != NULL;
2473#else
2474 break;
2475#endif
2476 case VAR_CHANNEL:
2477#ifdef FEAT_JOB_CHANNEL
2478 return tv->vval.v_channel != NULL;
2479#else
2480 break;
2481#endif
2482 case VAR_BLOB:
2483 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2484 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002485 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 case VAR_VOID:
2487 break;
2488 }
2489 return FALSE;
2490}
2491
2492/*
2493 * If "tv" is a string give an error and return FAIL.
2494 */
2495 int
2496check_not_string(typval_T *tv)
2497{
2498 if (tv->v_type == VAR_STRING)
2499 {
2500 emsg(_("E1030: Using a String as a Number"));
2501 clear_tv(tv);
2502 return FAIL;
2503 }
2504 return OK;
2505}
2506
2507
2508#endif // FEAT_EVAL