blob: 237b024eff298857c672478b6f5387492bf7f2bf [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100111 * Call compiled function "cdf_idx" from compiled code.
112 *
113 * Stack has:
114 * - current arguments (already there)
115 * - omitted optional argument (default values) added here
116 * - stack frame:
117 * - pointer to calling function
118 * - Index of next instruction in calling function
119 * - previous frame pointer
120 * - reserved space for local variables
121 */
122 static int
123call_dfunc(int cdf_idx, int argcount, ectx_T *ectx)
124{
125 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
126 ufunc_T *ufunc = dfunc->df_ufunc;
127 int optcount = ufunc_argcount(ufunc) - argcount;
128 int idx;
129
130 if (dfunc->df_deleted)
131 {
132 emsg_funcname(e_func_deleted, ufunc->uf_name);
133 return FAIL;
134 }
135
136 if (ga_grow(&ectx->ec_stack, optcount + 3 + dfunc->df_varcount) == FAIL)
137 return FAIL;
138
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100139 if (optcount < 0)
140 {
141 emsg("argument count wrong?");
142 return FAIL;
143 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100144
145 // Reserve space for omitted optional arguments, filled in soon.
146 // Also any empty varargs argument.
147 ectx->ec_stack.ga_len += optcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100148
149 // Store current execution state in stack frame for ISN_RETURN.
150 // TODO: If the actual number of arguments doesn't match what the called
151 // function expects things go bad.
152 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
153 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
154 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame;
155 ectx->ec_frame = ectx->ec_stack.ga_len;
156
157 // Initialize local variables
158 for (idx = 0; idx < dfunc->df_varcount; ++idx)
159 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
160 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + dfunc->df_varcount;
161
162 // Set execution state to the start of the called function.
163 ectx->ec_dfunc_idx = cdf_idx;
164 ectx->ec_instr = dfunc->df_instr;
165 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100166
167 // Decide where to start execution, handles optional arguments.
168 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169
170 return OK;
171}
172
173// Get pointer to item in the stack.
174#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
175
176/*
177 * Return from the current function.
178 */
179 static void
180func_return(ectx_T *ectx)
181{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 int idx;
183 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100184 int top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100185
186 // execution context goes one level up
187 estack_pop();
188
189 // Clear the local variables and temporary values, but not
190 // the return value.
191 for (idx = ectx->ec_frame + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100192 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100193 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100194
195 // Clear the arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100196 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100197 top = ectx->ec_frame - ufunc_argcount(dfunc->df_ufunc);
198 for (idx = top; idx < ectx->ec_frame; ++idx)
199 clear_tv(STACK_TV(idx));
200
201 // Restore the previous frame.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100202 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame)->vval.v_number;
203 ectx->ec_iidx = STACK_TV(ectx->ec_frame + 1)->vval.v_number;
204 ectx->ec_frame = STACK_TV(ectx->ec_frame + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100205 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
206 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100207
208 // Reset the stack to the position before the call, move the return value
209 // to the top of the stack.
210 idx = ectx->ec_stack.ga_len - 1;
211 ectx->ec_stack.ga_len = top + 1;
212 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100213}
214
215#undef STACK_TV
216
217/*
218 * Prepare arguments and rettv for calling a builtin or user function.
219 */
220 static int
221call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
222{
223 int idx;
224 typval_T *tv;
225
226 // Move arguments from bottom of the stack to argvars[] and add terminator.
227 for (idx = 0; idx < argcount; ++idx)
228 argvars[idx] = *STACK_TV_BOT(idx - argcount);
229 argvars[argcount].v_type = VAR_UNKNOWN;
230
231 // Result replaces the arguments on the stack.
232 if (argcount > 0)
233 ectx->ec_stack.ga_len -= argcount - 1;
234 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
235 return FAIL;
236 else
237 ++ectx->ec_stack.ga_len;
238
239 // Default return value is zero.
240 tv = STACK_TV_BOT(-1);
241 tv->v_type = VAR_NUMBER;
242 tv->vval.v_number = 0;
243
244 return OK;
245}
246
247/*
248 * Call a builtin function by index.
249 */
250 static int
251call_bfunc(int func_idx, int argcount, ectx_T *ectx)
252{
253 typval_T argvars[MAX_FUNC_ARGS];
254 int idx;
255
256 if (call_prepare(argcount, argvars, ectx) == FAIL)
257 return FAIL;
258
259 // Call the builtin function.
260 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
261
262 // Clear the arguments.
263 for (idx = 0; idx < argcount; ++idx)
264 clear_tv(&argvars[idx]);
265 return OK;
266}
267
268/*
269 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100270 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 */
272 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100273call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274{
275 typval_T argvars[MAX_FUNC_ARGS];
276 funcexe_T funcexe;
277 int error;
278 int idx;
279
280 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100281 {
282 // The function has been compiled, can call it quickly. For a function
283 // that was defined later: we can call it directly next time.
284 if (iptr != NULL)
285 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100286 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100287 iptr->isn_type = ISN_DCALL;
288 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
289 iptr->isn_arg.dfunc.cdf_argcount = argcount;
290 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100291 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100292 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293
294 if (call_prepare(argcount, argvars, ectx) == FAIL)
295 return FAIL;
296 vim_memset(&funcexe, 0, sizeof(funcexe));
297 funcexe.evaluate = TRUE;
298
299 // Call the user function. Result goes in last position on the stack.
300 // TODO: add selfdict if there is one
301 error = call_user_func_check(ufunc, argcount, argvars,
302 STACK_TV_BOT(-1), &funcexe, NULL);
303
304 // Clear the arguments.
305 for (idx = 0; idx < argcount; ++idx)
306 clear_tv(&argvars[idx]);
307
308 if (error != FCERR_NONE)
309 {
310 user_func_error(error, ufunc->uf_name);
311 return FAIL;
312 }
313 return OK;
314}
315
316/*
317 * Execute a function by "name".
318 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100319 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100320 * Returns FAIL if not found without an error message.
321 */
322 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100323call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100324{
325 ufunc_T *ufunc;
326
327 if (builtin_function(name, -1))
328 {
329 int func_idx = find_internal_func(name);
330
331 if (func_idx < 0)
332 return FAIL;
333 if (check_internal_func(func_idx, argcount) == FAIL)
334 return FAIL;
335 return call_bfunc(func_idx, argcount, ectx);
336 }
337
338 ufunc = find_func(name, NULL);
339 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100340 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100341
342 return FAIL;
343}
344
345 static int
346call_partial(typval_T *tv, int argcount, ectx_T *ectx)
347{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200348 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100349 int called_emsg_before = called_emsg;
350
351 if (tv->v_type == VAR_PARTIAL)
352 {
353 partial_T *pt = tv->vval.v_partial;
354
355 if (pt->pt_func != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100356 return call_ufunc(pt->pt_func, argcount, ectx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 name = pt->pt_name;
358 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200359 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100360 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200361 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100362 {
363 if (called_emsg == called_emsg_before)
364 semsg(_(e_unknownfunc), name);
365 return FAIL;
366 }
367 return OK;
368}
369
370/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100371 * Store "tv" in variable "name".
372 * This is for s: and g: variables.
373 */
374 static void
375store_var(char_u *name, typval_T *tv)
376{
377 funccal_entry_T entry;
378
379 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200380 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100381 restore_funccal();
382}
383
384/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100385 * Execute a function by "name".
386 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100387 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100388 */
389 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100390call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100391{
392 int called_emsg_before = called_emsg;
393
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100394 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100395 && called_emsg == called_emsg_before)
396 {
397 // "name" may be a variable that is a funcref or partial
398 // if find variable
399 // call_partial()
400 // else
401 // semsg(_(e_unknownfunc), name);
402 emsg("call_eval_func(partial) not implemented yet");
403 return FAIL;
404 }
405 return OK;
406}
407
408/*
409 * Call a "def" function from old Vim script.
410 * Return OK or FAIL.
411 */
412 int
413call_def_function(
414 ufunc_T *ufunc,
415 int argc, // nr of arguments
416 typval_T *argv, // arguments
417 typval_T *rettv) // return value
418{
419 ectx_T ectx; // execution context
420 int initial_frame_ptr;
421 typval_T *tv;
422 int idx;
423 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100424 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100425
426// Get pointer to item in the stack.
427#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
428
429// Get pointer to item at the bottom of the stack, -1 is the bottom.
430#undef STACK_TV_BOT
431#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
432
433// Get pointer to local variable on the stack.
434#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
435
436 vim_memset(&ectx, 0, sizeof(ectx));
437 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
438 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100439 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100440 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
441
442 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
443
444 // Put arguments on the stack.
445 for (idx = 0; idx < argc; ++idx)
446 {
447 copy_tv(&argv[idx], STACK_TV_BOT(0));
448 ++ectx.ec_stack.ga_len;
449 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100450 // Make space for omitted arguments, will store default value below.
451 if (defcount > 0)
452 for (idx = 0; idx < defcount; ++idx)
453 {
454 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
455 ++ectx.ec_stack.ga_len;
456 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100457
458 // Frame pointer points to just after arguments.
459 ectx.ec_frame = ectx.ec_stack.ga_len;
460 initial_frame_ptr = ectx.ec_frame;
461
462 // dummy frame entries
463 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
464 {
465 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
466 ++ectx.ec_stack.ga_len;
467 }
468
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200469 {
470 // Reserve space for local variables.
471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
472 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100473
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200474 for (idx = 0; idx < dfunc->df_varcount; ++idx)
475 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
476 ectx.ec_stack.ga_len += dfunc->df_varcount;
477
478 ectx.ec_instr = dfunc->df_instr;
479 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100480
481 // Decide where to start execution, handles optional arguments.
482 init_instr_idx(ufunc, argc, &ectx);
483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 for (;;)
485 {
486 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100487
488 veryfast_breakcheck();
489 if (got_int)
490 {
491 // Turn CTRL-C into an exception.
492 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100493 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100494 goto failed;
495 did_throw = TRUE;
496 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100497
498 if (did_throw && !ectx.ec_in_catch)
499 {
500 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100501 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100502
503 // An exception jumps to the first catch, finally, or returns from
504 // the current function.
505 if (trystack->ga_len > 0)
506 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
507 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
508 {
509 // jump to ":catch" or ":finally"
510 ectx.ec_in_catch = TRUE;
511 ectx.ec_iidx = trycmd->tcd_catch_idx;
512 }
513 else
514 {
515 // not inside try or need to return from current functions.
516 if (ectx.ec_frame == initial_frame_ptr)
517 {
518 // At the toplevel we are done. Push a dummy return value.
519 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
520 goto failed;
521 tv = STACK_TV_BOT(0);
522 tv->v_type = VAR_NUMBER;
523 tv->vval.v_number = 0;
524 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100525 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526 goto done;
527 }
528
529 func_return(&ectx);
530 }
531 continue;
532 }
533
534 iptr = &ectx.ec_instr[ectx.ec_iidx++];
535 switch (iptr->isn_type)
536 {
537 // execute Ex command line
538 case ISN_EXEC:
539 do_cmdline_cmd(iptr->isn_arg.string);
540 break;
541
542 // execute :echo {string} ...
543 case ISN_ECHO:
544 {
545 int count = iptr->isn_arg.echo.echo_count;
546 int atstart = TRUE;
547 int needclr = TRUE;
548
549 for (idx = 0; idx < count; ++idx)
550 {
551 tv = STACK_TV_BOT(idx - count);
552 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
553 &atstart, &needclr);
554 clear_tv(tv);
555 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100556 if (needclr)
557 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 ectx.ec_stack.ga_len -= count;
559 }
560 break;
561
Bram Moolenaarad39c092020-02-26 18:23:43 +0100562 // execute :execute {string} ...
563 case ISN_EXECUTE:
564 {
565 int count = iptr->isn_arg.number;
566 garray_T ga;
567 char_u buf[NUMBUFLEN];
568 char_u *p;
569 int len;
570 int failed = FALSE;
571
572 ga_init2(&ga, 1, 80);
573 for (idx = 0; idx < count; ++idx)
574 {
575 tv = STACK_TV_BOT(idx - count);
576 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
577 {
578 emsg(_(e_inval_string));
579 break;
580 }
581 else
582 p = tv_get_string_buf(tv, buf);
583
584 len = (int)STRLEN(p);
585 if (ga_grow(&ga, len + 2) == FAIL)
586 failed = TRUE;
587 else
588 {
589 if (ga.ga_len > 0)
590 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
591 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
592 ga.ga_len += len;
593 }
594 clear_tv(tv);
595 }
596 ectx.ec_stack.ga_len -= count;
597
598 if (!failed && ga.ga_data != NULL)
599 do_cmdline_cmd((char_u *)ga.ga_data);
600 ga_clear(&ga);
601 }
602 break;
603
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604 // load local variable or argument
605 case ISN_LOAD:
606 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
607 goto failed;
608 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
609 ++ectx.ec_stack.ga_len;
610 break;
611
612 // load v: variable
613 case ISN_LOADV:
614 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
615 goto failed;
616 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
617 ++ectx.ec_stack.ga_len;
618 break;
619
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100620 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621 case ISN_LOADSCRIPT:
622 {
623 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100624 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 svar_T *sv;
626
627 sv = ((svar_T *)si->sn_var_vals.ga_data)
628 + iptr->isn_arg.script.script_idx;
629 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
630 goto failed;
631 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
632 ++ectx.ec_stack.ga_len;
633 }
634 break;
635
636 // load s: variable in old script
637 case ISN_LOADS:
638 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100639 hashtab_T *ht = &SCRIPT_VARS(
640 iptr->isn_arg.loadstore.ls_sid);
641 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100642 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100643
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 if (di == NULL)
645 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100646 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647 goto failed;
648 }
649 else
650 {
651 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
652 goto failed;
653 copy_tv(&di->di_tv, STACK_TV_BOT(0));
654 ++ectx.ec_stack.ga_len;
655 }
656 }
657 break;
658
659 // load g: variable
660 case ISN_LOADG:
661 {
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100662 dictitem_T *di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100664
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665 if (di == NULL)
666 {
667 semsg(_("E121: Undefined variable: g:%s"),
668 iptr->isn_arg.string);
669 goto failed;
670 }
671 else
672 {
673 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
674 goto failed;
675 copy_tv(&di->di_tv, STACK_TV_BOT(0));
676 ++ectx.ec_stack.ga_len;
677 }
678 }
679 break;
680
681 // load &option
682 case ISN_LOADOPT:
683 {
684 typval_T optval;
685 char_u *name = iptr->isn_arg.string;
686
Bram Moolenaara8c17702020-04-01 21:17:24 +0200687 // This is not expected to fail, name is checked during
688 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100689 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
690 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100691 if (get_option_tv(&name, &optval, TRUE) == FAIL)
692 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100693 *STACK_TV_BOT(0) = optval;
694 ++ectx.ec_stack.ga_len;
695 }
696 break;
697
698 // load $ENV
699 case ISN_LOADENV:
700 {
701 typval_T optval;
702 char_u *name = iptr->isn_arg.string;
703
704 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
705 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100706 // name is always valid, checked when compiling
707 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708 *STACK_TV_BOT(0) = optval;
709 ++ectx.ec_stack.ga_len;
710 }
711 break;
712
713 // load @register
714 case ISN_LOADREG:
715 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
716 goto failed;
717 tv = STACK_TV_BOT(0);
718 tv->v_type = VAR_STRING;
719 tv->vval.v_string = get_reg_contents(
720 iptr->isn_arg.number, GREG_EXPR_SRC);
721 ++ectx.ec_stack.ga_len;
722 break;
723
724 // store local variable
725 case ISN_STORE:
726 --ectx.ec_stack.ga_len;
727 tv = STACK_TV_VAR(iptr->isn_arg.number);
728 clear_tv(tv);
729 *tv = *STACK_TV_BOT(0);
730 break;
731
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100732 // store s: variable in old script
733 case ISN_STORES:
734 {
735 hashtab_T *ht = &SCRIPT_VARS(
736 iptr->isn_arg.loadstore.ls_sid);
737 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100738 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100739
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100740 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100741 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200742 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100743 else
744 {
745 clear_tv(&di->di_tv);
746 di->di_tv = *STACK_TV_BOT(0);
747 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100748 }
749 break;
750
751 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 case ISN_STORESCRIPT:
753 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100754 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100755 iptr->isn_arg.script.script_sid);
756 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
757 + iptr->isn_arg.script.script_idx;
758
759 --ectx.ec_stack.ga_len;
760 clear_tv(sv->sv_tv);
761 *sv->sv_tv = *STACK_TV_BOT(0);
762 }
763 break;
764
765 // store option
766 case ISN_STOREOPT:
767 {
768 long n = 0;
769 char_u *s = NULL;
770 char *msg;
771
772 --ectx.ec_stack.ga_len;
773 tv = STACK_TV_BOT(0);
774 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100775 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100776 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100777 if (s == NULL)
778 s = (char_u *)"";
779 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100780 else if (tv->v_type == VAR_NUMBER)
781 n = tv->vval.v_number;
782 else
783 {
784 emsg(_("E1051: Expected string or number"));
785 goto failed;
786 }
787 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
788 n, s, iptr->isn_arg.storeopt.so_flags);
789 if (msg != NULL)
790 {
791 emsg(_(msg));
792 goto failed;
793 }
794 clear_tv(tv);
795 }
796 break;
797
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100798 // store $ENV
799 case ISN_STOREENV:
800 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100801 tv = STACK_TV_BOT(0);
802 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
803 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100804 break;
805
806 // store @r
807 case ISN_STOREREG:
808 {
809 int reg = iptr->isn_arg.number;
810
811 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100812 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100813 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100814 tv_get_string(tv), -1, FALSE);
815 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100816 }
817 break;
818
819 // store v: variable
820 case ISN_STOREV:
821 --ectx.ec_stack.ga_len;
822 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
823 == FAIL)
824 goto failed;
825 break;
826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 // store g: variable
828 case ISN_STOREG:
829 {
830 dictitem_T *di;
831
832 --ectx.ec_stack.ga_len;
833 di = find_var_in_ht(get_globvar_ht(), 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +0100834 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100836 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 else
838 {
839 clear_tv(&di->di_tv);
840 di->di_tv = *STACK_TV_BOT(0);
841 }
842 }
843 break;
844
845 // store number in local variable
846 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +0100847 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848 clear_tv(tv);
849 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +0100850 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100851 break;
852
853 // push constant
854 case ISN_PUSHNR:
855 case ISN_PUSHBOOL:
856 case ISN_PUSHSPEC:
857 case ISN_PUSHF:
858 case ISN_PUSHS:
859 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100860 case ISN_PUSHFUNC:
861 case ISN_PUSHPARTIAL:
862 case ISN_PUSHCHANNEL:
863 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
865 goto failed;
866 tv = STACK_TV_BOT(0);
867 ++ectx.ec_stack.ga_len;
868 switch (iptr->isn_type)
869 {
870 case ISN_PUSHNR:
871 tv->v_type = VAR_NUMBER;
872 tv->vval.v_number = iptr->isn_arg.number;
873 break;
874 case ISN_PUSHBOOL:
875 tv->v_type = VAR_BOOL;
876 tv->vval.v_number = iptr->isn_arg.number;
877 break;
878 case ISN_PUSHSPEC:
879 tv->v_type = VAR_SPECIAL;
880 tv->vval.v_number = iptr->isn_arg.number;
881 break;
882#ifdef FEAT_FLOAT
883 case ISN_PUSHF:
884 tv->v_type = VAR_FLOAT;
885 tv->vval.v_float = iptr->isn_arg.fnumber;
886 break;
887#endif
888 case ISN_PUSHBLOB:
889 blob_copy(iptr->isn_arg.blob, tv);
890 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100891 case ISN_PUSHFUNC:
892 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100893 if (iptr->isn_arg.string == NULL)
894 tv->vval.v_string = NULL;
895 else
896 tv->vval.v_string =
897 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100898 break;
899 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +0100900 tv->v_type = VAR_PARTIAL;
901 tv->vval.v_partial = iptr->isn_arg.partial;
902 if (tv->vval.v_partial != NULL)
903 ++tv->vval.v_partial->pt_refcount;
Bram Moolenaar42a480b2020-02-29 23:23:47 +0100904 break;
905 case ISN_PUSHCHANNEL:
906#ifdef FEAT_JOB_CHANNEL
907 tv->v_type = VAR_CHANNEL;
908 tv->vval.v_channel = iptr->isn_arg.channel;
909 if (tv->vval.v_channel != NULL)
910 ++tv->vval.v_channel->ch_refcount;
911#endif
912 break;
913 case ISN_PUSHJOB:
914#ifdef FEAT_JOB_CHANNEL
915 tv->v_type = VAR_JOB;
916 tv->vval.v_job = iptr->isn_arg.job;
917 if (tv->vval.v_job != NULL)
918 ++tv->vval.v_job->jv_refcount;
919#endif
920 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921 default:
922 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +0200923 tv->vval.v_string = vim_strsave(
924 iptr->isn_arg.string == NULL
925 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100926 }
927 break;
928
929 // create a list from items on the stack; uses a single allocation
930 // for the list header and the items
931 case ISN_NEWLIST:
932 {
933 int count = iptr->isn_arg.number;
934 list_T *list = list_alloc_with_items(count);
935
936 if (list == NULL)
937 goto failed;
938 for (idx = 0; idx < count; ++idx)
939 list_set_item(list, idx, STACK_TV_BOT(idx - count));
940
941 if (count > 0)
942 ectx.ec_stack.ga_len -= count - 1;
943 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
944 goto failed;
945 else
946 ++ectx.ec_stack.ga_len;
947 tv = STACK_TV_BOT(-1);
948 tv->v_type = VAR_LIST;
949 tv->vval.v_list = list;
950 ++list->lv_refcount;
951 }
952 break;
953
954 // create a dict from items on the stack
955 case ISN_NEWDICT:
956 {
957 int count = iptr->isn_arg.number;
958 dict_T *dict = dict_alloc();
959 dictitem_T *item;
960
961 if (dict == NULL)
962 goto failed;
963 for (idx = 0; idx < count; ++idx)
964 {
965 // check key type is VAR_STRING
966 tv = STACK_TV_BOT(2 * (idx - count));
967 item = dictitem_alloc(tv->vval.v_string);
968 clear_tv(tv);
969 if (item == NULL)
970 goto failed;
971 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
972 item->di_tv.v_lock = 0;
973 if (dict_add(dict, item) == FAIL)
974 goto failed;
975 }
976
977 if (count > 0)
978 ectx.ec_stack.ga_len -= 2 * count - 1;
979 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
980 goto failed;
981 else
982 ++ectx.ec_stack.ga_len;
983 tv = STACK_TV_BOT(-1);
984 tv->v_type = VAR_DICT;
985 tv->vval.v_dict = dict;
986 ++dict->dv_refcount;
987 }
988 break;
989
990 // call a :def function
991 case ISN_DCALL:
992 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
993 iptr->isn_arg.dfunc.cdf_argcount,
994 &ectx) == FAIL)
995 goto failed;
996 break;
997
998 // call a builtin function
999 case ISN_BCALL:
1000 SOURCING_LNUM = iptr->isn_lnum;
1001 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1002 iptr->isn_arg.bfunc.cbf_argcount,
1003 &ectx) == FAIL)
1004 goto failed;
1005 break;
1006
1007 // call a funcref or partial
1008 case ISN_PCALL:
1009 {
1010 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1011 int r;
1012 typval_T partial;
1013
1014 SOURCING_LNUM = iptr->isn_lnum;
1015 if (pfunc->cpf_top)
1016 {
1017 // funcref is above the arguments
1018 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1019 }
1020 else
1021 {
1022 // Get the funcref from the stack.
1023 --ectx.ec_stack.ga_len;
1024 partial = *STACK_TV_BOT(0);
1025 tv = &partial;
1026 }
1027 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1028 if (tv == &partial)
1029 clear_tv(&partial);
1030 if (r == FAIL)
1031 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 }
1033 break;
1034
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001035 case ISN_PCALL_END:
1036 // PCALL finished, arguments have been consumed and replaced by
1037 // the return value. Now clear the funcref from the stack,
1038 // and move the return value in its place.
1039 --ectx.ec_stack.ga_len;
1040 clear_tv(STACK_TV_BOT(-1));
1041 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1042 break;
1043
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 // call a user defined function or funcref/partial
1045 case ISN_UCALL:
1046 {
1047 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1048
1049 SOURCING_LNUM = iptr->isn_lnum;
1050 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001051 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 goto failed;
1053 }
1054 break;
1055
1056 // return from a :def function call
1057 case ISN_RETURN:
1058 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001059 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001060 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001061
1062 if (trystack->ga_len > 0)
1063 trycmd = ((trycmd_T *)trystack->ga_data)
1064 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
1066 && trycmd->tcd_finally_idx != 0)
1067 {
1068 // jump to ":finally"
1069 ectx.ec_iidx = trycmd->tcd_finally_idx;
1070 trycmd->tcd_return = TRUE;
1071 }
1072 else
1073 {
1074 // Restore previous function. If the frame pointer
1075 // is zero then there is none and we are done.
1076 if (ectx.ec_frame == initial_frame_ptr)
1077 goto done;
1078
1079 func_return(&ectx);
1080 }
1081 }
1082 break;
1083
1084 // push a function reference to a compiled function
1085 case ISN_FUNCREF:
1086 {
1087 partial_T *pt = NULL;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001088 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089
1090 pt = ALLOC_CLEAR_ONE(partial_T);
1091 if (pt == NULL)
1092 goto failed;
1093 dfunc = ((dfunc_T *)def_functions.ga_data)
1094 + iptr->isn_arg.number;
1095 pt->pt_func = dfunc->df_ufunc;
1096 pt->pt_refcount = 1;
1097 ++dfunc->df_ufunc->uf_refcount;
1098
1099 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1100 goto failed;
1101 tv = STACK_TV_BOT(0);
1102 ++ectx.ec_stack.ga_len;
1103 tv->vval.v_partial = pt;
1104 tv->v_type = VAR_PARTIAL;
1105 }
1106 break;
1107
1108 // jump if a condition is met
1109 case ISN_JUMP:
1110 {
1111 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1112 int jump = TRUE;
1113
1114 if (when != JUMP_ALWAYS)
1115 {
1116 tv = STACK_TV_BOT(-1);
1117 jump = tv2bool(tv);
1118 if (when == JUMP_IF_FALSE
1119 || when == JUMP_AND_KEEP_IF_FALSE)
1120 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001121 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 {
1123 // drop the value from the stack
1124 clear_tv(tv);
1125 --ectx.ec_stack.ga_len;
1126 }
1127 }
1128 if (jump)
1129 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1130 }
1131 break;
1132
1133 // top of a for loop
1134 case ISN_FOR:
1135 {
1136 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1137 typval_T *idxtv =
1138 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1139
1140 // push the next item from the list
1141 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1142 goto failed;
1143 if (++idxtv->vval.v_number >= list->lv_len)
1144 // past the end of the list, jump to "endfor"
1145 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1146 else if (list->lv_first == &range_list_item)
1147 {
1148 // non-materialized range() list
1149 tv = STACK_TV_BOT(0);
1150 tv->v_type = VAR_NUMBER;
1151 tv->vval.v_number = list_find_nr(
1152 list, idxtv->vval.v_number, NULL);
1153 ++ectx.ec_stack.ga_len;
1154 }
1155 else
1156 {
1157 listitem_T *li = list_find(list, idxtv->vval.v_number);
1158
1159 if (li == NULL)
1160 goto failed;
1161 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1162 ++ectx.ec_stack.ga_len;
1163 }
1164 }
1165 break;
1166
1167 // start of ":try" block
1168 case ISN_TRY:
1169 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001170 trycmd_T *trycmd = NULL;
1171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1173 goto failed;
1174 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1175 + ectx.ec_trystack.ga_len;
1176 ++ectx.ec_trystack.ga_len;
1177 ++trylevel;
1178 trycmd->tcd_frame = ectx.ec_frame;
1179 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1180 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001181 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 }
1183 break;
1184
1185 case ISN_PUSHEXC:
1186 if (current_exception == NULL)
1187 {
1188 iemsg("Evaluating catch while current_exception is NULL");
1189 goto failed;
1190 }
1191 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1192 goto failed;
1193 tv = STACK_TV_BOT(0);
1194 ++ectx.ec_stack.ga_len;
1195 tv->v_type = VAR_STRING;
1196 tv->vval.v_string = vim_strsave(
1197 (char_u *)current_exception->value);
1198 break;
1199
1200 case ISN_CATCH:
1201 {
1202 garray_T *trystack = &ectx.ec_trystack;
1203
1204 if (trystack->ga_len > 0)
1205 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001206 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001207 + trystack->ga_len - 1;
1208 trycmd->tcd_caught = TRUE;
1209 }
1210 did_emsg = got_int = did_throw = FALSE;
1211 catch_exception(current_exception);
1212 }
1213 break;
1214
1215 // end of ":try" block
1216 case ISN_ENDTRY:
1217 {
1218 garray_T *trystack = &ectx.ec_trystack;
1219
1220 if (trystack->ga_len > 0)
1221 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001222 trycmd_T *trycmd = NULL;
1223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 --trystack->ga_len;
1225 --trylevel;
1226 trycmd = ((trycmd_T *)trystack->ga_data)
1227 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001228 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001229 {
1230 // discard the exception
1231 if (caught_stack == current_exception)
1232 caught_stack = caught_stack->caught;
1233 discard_current_exception();
1234 }
1235
1236 if (trycmd->tcd_return)
1237 {
1238 // Restore previous function. If the frame pointer
1239 // is zero then there is none and we are done.
1240 if (ectx.ec_frame == initial_frame_ptr)
1241 goto done;
1242
1243 func_return(&ectx);
1244 }
1245 }
1246 }
1247 break;
1248
1249 case ISN_THROW:
1250 --ectx.ec_stack.ga_len;
1251 tv = STACK_TV_BOT(0);
1252 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1253 {
1254 vim_free(tv->vval.v_string);
1255 goto failed;
1256 }
1257 did_throw = TRUE;
1258 break;
1259
1260 // compare with special values
1261 case ISN_COMPAREBOOL:
1262 case ISN_COMPARESPECIAL:
1263 {
1264 typval_T *tv1 = STACK_TV_BOT(-2);
1265 typval_T *tv2 = STACK_TV_BOT(-1);
1266 varnumber_T arg1 = tv1->vval.v_number;
1267 varnumber_T arg2 = tv2->vval.v_number;
1268 int res;
1269
1270 switch (iptr->isn_arg.op.op_type)
1271 {
1272 case EXPR_EQUAL: res = arg1 == arg2; break;
1273 case EXPR_NEQUAL: res = arg1 != arg2; break;
1274 default: res = 0; break;
1275 }
1276
1277 --ectx.ec_stack.ga_len;
1278 tv1->v_type = VAR_BOOL;
1279 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1280 }
1281 break;
1282
1283 // Operation with two number arguments
1284 case ISN_OPNR:
1285 case ISN_COMPARENR:
1286 {
1287 typval_T *tv1 = STACK_TV_BOT(-2);
1288 typval_T *tv2 = STACK_TV_BOT(-1);
1289 varnumber_T arg1 = tv1->vval.v_number;
1290 varnumber_T arg2 = tv2->vval.v_number;
1291 varnumber_T res;
1292
1293 switch (iptr->isn_arg.op.op_type)
1294 {
1295 case EXPR_MULT: res = arg1 * arg2; break;
1296 case EXPR_DIV: res = arg1 / arg2; break;
1297 case EXPR_REM: res = arg1 % arg2; break;
1298 case EXPR_SUB: res = arg1 - arg2; break;
1299 case EXPR_ADD: res = arg1 + arg2; break;
1300
1301 case EXPR_EQUAL: res = arg1 == arg2; break;
1302 case EXPR_NEQUAL: res = arg1 != arg2; break;
1303 case EXPR_GREATER: res = arg1 > arg2; break;
1304 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1305 case EXPR_SMALLER: res = arg1 < arg2; break;
1306 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1307 default: res = 0; break;
1308 }
1309
1310 --ectx.ec_stack.ga_len;
1311 if (iptr->isn_type == ISN_COMPARENR)
1312 {
1313 tv1->v_type = VAR_BOOL;
1314 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1315 }
1316 else
1317 tv1->vval.v_number = res;
1318 }
1319 break;
1320
1321 // Computation with two float arguments
1322 case ISN_OPFLOAT:
1323 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001324#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 {
1326 typval_T *tv1 = STACK_TV_BOT(-2);
1327 typval_T *tv2 = STACK_TV_BOT(-1);
1328 float_T arg1 = tv1->vval.v_float;
1329 float_T arg2 = tv2->vval.v_float;
1330 float_T res = 0;
1331 int cmp = FALSE;
1332
1333 switch (iptr->isn_arg.op.op_type)
1334 {
1335 case EXPR_MULT: res = arg1 * arg2; break;
1336 case EXPR_DIV: res = arg1 / arg2; break;
1337 case EXPR_SUB: res = arg1 - arg2; break;
1338 case EXPR_ADD: res = arg1 + arg2; break;
1339
1340 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1341 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1342 case EXPR_GREATER: cmp = arg1 > arg2; break;
1343 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1344 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1345 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1346 default: cmp = 0; break;
1347 }
1348 --ectx.ec_stack.ga_len;
1349 if (iptr->isn_type == ISN_COMPAREFLOAT)
1350 {
1351 tv1->v_type = VAR_BOOL;
1352 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1353 }
1354 else
1355 tv1->vval.v_float = res;
1356 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001357#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 break;
1359
1360 case ISN_COMPARELIST:
1361 {
1362 typval_T *tv1 = STACK_TV_BOT(-2);
1363 typval_T *tv2 = STACK_TV_BOT(-1);
1364 list_T *arg1 = tv1->vval.v_list;
1365 list_T *arg2 = tv2->vval.v_list;
1366 int cmp = FALSE;
1367 int ic = iptr->isn_arg.op.op_ic;
1368
1369 switch (iptr->isn_arg.op.op_type)
1370 {
1371 case EXPR_EQUAL: cmp =
1372 list_equal(arg1, arg2, ic, FALSE); break;
1373 case EXPR_NEQUAL: cmp =
1374 !list_equal(arg1, arg2, ic, FALSE); break;
1375 case EXPR_IS: cmp = arg1 == arg2; break;
1376 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1377 default: cmp = 0; break;
1378 }
1379 --ectx.ec_stack.ga_len;
1380 clear_tv(tv1);
1381 clear_tv(tv2);
1382 tv1->v_type = VAR_BOOL;
1383 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1384 }
1385 break;
1386
1387 case ISN_COMPAREBLOB:
1388 {
1389 typval_T *tv1 = STACK_TV_BOT(-2);
1390 typval_T *tv2 = STACK_TV_BOT(-1);
1391 blob_T *arg1 = tv1->vval.v_blob;
1392 blob_T *arg2 = tv2->vval.v_blob;
1393 int cmp = FALSE;
1394
1395 switch (iptr->isn_arg.op.op_type)
1396 {
1397 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1398 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1399 case EXPR_IS: cmp = arg1 == arg2; break;
1400 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1401 default: cmp = 0; break;
1402 }
1403 --ectx.ec_stack.ga_len;
1404 clear_tv(tv1);
1405 clear_tv(tv2);
1406 tv1->v_type = VAR_BOOL;
1407 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1408 }
1409 break;
1410
1411 // TODO: handle separately
1412 case ISN_COMPARESTRING:
1413 case ISN_COMPAREDICT:
1414 case ISN_COMPAREFUNC:
1415 case ISN_COMPAREPARTIAL:
1416 case ISN_COMPAREANY:
1417 {
1418 typval_T *tv1 = STACK_TV_BOT(-2);
1419 typval_T *tv2 = STACK_TV_BOT(-1);
1420 exptype_T exptype = iptr->isn_arg.op.op_type;
1421 int ic = iptr->isn_arg.op.op_ic;
1422
1423 typval_compare(tv1, tv2, exptype, ic);
1424 clear_tv(tv2);
1425 tv1->v_type = VAR_BOOL;
1426 tv1->vval.v_number = tv1->vval.v_number
1427 ? VVAL_TRUE : VVAL_FALSE;
1428 --ectx.ec_stack.ga_len;
1429 }
1430 break;
1431
1432 case ISN_ADDLIST:
1433 case ISN_ADDBLOB:
1434 {
1435 typval_T *tv1 = STACK_TV_BOT(-2);
1436 typval_T *tv2 = STACK_TV_BOT(-1);
1437
1438 if (iptr->isn_type == ISN_ADDLIST)
1439 eval_addlist(tv1, tv2);
1440 else
1441 eval_addblob(tv1, tv2);
1442 clear_tv(tv2);
1443 --ectx.ec_stack.ga_len;
1444 }
1445 break;
1446
1447 // Computation with two arguments of unknown type
1448 case ISN_OPANY:
1449 {
1450 typval_T *tv1 = STACK_TV_BOT(-2);
1451 typval_T *tv2 = STACK_TV_BOT(-1);
1452 varnumber_T n1, n2;
1453#ifdef FEAT_FLOAT
1454 float_T f1 = 0, f2 = 0;
1455#endif
1456 int error = FALSE;
1457
1458 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1459 {
1460 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1461 {
1462 eval_addlist(tv1, tv2);
1463 clear_tv(tv2);
1464 --ectx.ec_stack.ga_len;
1465 break;
1466 }
1467 else if (tv1->v_type == VAR_BLOB
1468 && tv2->v_type == VAR_BLOB)
1469 {
1470 eval_addblob(tv1, tv2);
1471 clear_tv(tv2);
1472 --ectx.ec_stack.ga_len;
1473 break;
1474 }
1475 }
1476#ifdef FEAT_FLOAT
1477 if (tv1->v_type == VAR_FLOAT)
1478 {
1479 f1 = tv1->vval.v_float;
1480 n1 = 0;
1481 }
1482 else
1483#endif
1484 {
1485 n1 = tv_get_number_chk(tv1, &error);
1486 if (error)
1487 goto failed;
1488#ifdef FEAT_FLOAT
1489 if (tv2->v_type == VAR_FLOAT)
1490 f1 = n1;
1491#endif
1492 }
1493#ifdef FEAT_FLOAT
1494 if (tv2->v_type == VAR_FLOAT)
1495 {
1496 f2 = tv2->vval.v_float;
1497 n2 = 0;
1498 }
1499 else
1500#endif
1501 {
1502 n2 = tv_get_number_chk(tv2, &error);
1503 if (error)
1504 goto failed;
1505#ifdef FEAT_FLOAT
1506 if (tv1->v_type == VAR_FLOAT)
1507 f2 = n2;
1508#endif
1509 }
1510#ifdef FEAT_FLOAT
1511 // if there is a float on either side the result is a float
1512 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1513 {
1514 switch (iptr->isn_arg.op.op_type)
1515 {
1516 case EXPR_MULT: f1 = f1 * f2; break;
1517 case EXPR_DIV: f1 = f1 / f2; break;
1518 case EXPR_SUB: f1 = f1 - f2; break;
1519 case EXPR_ADD: f1 = f1 + f2; break;
1520 default: emsg(_(e_modulus)); goto failed;
1521 }
1522 clear_tv(tv1);
1523 clear_tv(tv2);
1524 tv1->v_type = VAR_FLOAT;
1525 tv1->vval.v_float = f1;
1526 --ectx.ec_stack.ga_len;
1527 }
1528 else
1529#endif
1530 {
1531 switch (iptr->isn_arg.op.op_type)
1532 {
1533 case EXPR_MULT: n1 = n1 * n2; break;
1534 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1535 case EXPR_SUB: n1 = n1 - n2; break;
1536 case EXPR_ADD: n1 = n1 + n2; break;
1537 default: n1 = num_modulus(n1, n2); break;
1538 }
1539 clear_tv(tv1);
1540 clear_tv(tv2);
1541 tv1->v_type = VAR_NUMBER;
1542 tv1->vval.v_number = n1;
1543 --ectx.ec_stack.ga_len;
1544 }
1545 }
1546 break;
1547
1548 case ISN_CONCAT:
1549 {
1550 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1551 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1552 char_u *res;
1553
1554 res = concat_str(str1, str2);
1555 clear_tv(STACK_TV_BOT(-2));
1556 clear_tv(STACK_TV_BOT(-1));
1557 --ectx.ec_stack.ga_len;
1558 STACK_TV_BOT(-1)->vval.v_string = res;
1559 }
1560 break;
1561
1562 case ISN_INDEX:
1563 {
1564 list_T *list;
1565 varnumber_T n;
1566 listitem_T *li;
1567
1568 // list index: list is at stack-2, index at stack-1
1569 tv = STACK_TV_BOT(-2);
1570 if (tv->v_type != VAR_LIST)
1571 {
1572 emsg(_(e_listreq));
1573 goto failed;
1574 }
1575 list = tv->vval.v_list;
1576
1577 tv = STACK_TV_BOT(-1);
1578 if (tv->v_type != VAR_NUMBER)
1579 {
1580 emsg(_(e_number_exp));
1581 goto failed;
1582 }
1583 n = tv->vval.v_number;
1584 clear_tv(tv);
1585 if ((li = list_find(list, n)) == NULL)
1586 {
1587 semsg(_(e_listidx), n);
1588 goto failed;
1589 }
1590 --ectx.ec_stack.ga_len;
1591 clear_tv(STACK_TV_BOT(-1));
1592 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1593 }
1594 break;
1595
1596 // dict member with string key
1597 case ISN_MEMBER:
1598 {
1599 dict_T *dict;
1600 dictitem_T *di;
1601
1602 tv = STACK_TV_BOT(-1);
1603 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1604 {
1605 emsg(_(e_dictreq));
1606 goto failed;
1607 }
1608 dict = tv->vval.v_dict;
1609
1610 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1611 == NULL)
1612 {
1613 semsg(_(e_dictkey), iptr->isn_arg.string);
1614 goto failed;
1615 }
1616 clear_tv(tv);
1617 copy_tv(&di->di_tv, tv);
1618 }
1619 break;
1620
1621 case ISN_NEGATENR:
1622 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001623 if (tv->v_type != VAR_NUMBER
1624#ifdef FEAT_FLOAT
1625 && tv->v_type != VAR_FLOAT
1626#endif
1627 )
1628 {
1629 emsg(_(e_number_exp));
1630 goto failed;
1631 }
1632#ifdef FEAT_FLOAT
1633 if (tv->v_type == VAR_FLOAT)
1634 tv->vval.v_float = -tv->vval.v_float;
1635 else
1636#endif
1637 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 break;
1639
1640 case ISN_CHECKNR:
1641 {
1642 int error = FALSE;
1643
1644 tv = STACK_TV_BOT(-1);
1645 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 (void)tv_get_number_chk(tv, &error);
1648 if (error)
1649 goto failed;
1650 }
1651 break;
1652
1653 case ISN_CHECKTYPE:
1654 {
1655 checktype_T *ct = &iptr->isn_arg.type;
1656
1657 tv = STACK_TV_BOT(ct->ct_off);
1658 if (tv->v_type != ct->ct_type)
1659 {
1660 semsg(_("E1029: Expected %s but got %s"),
1661 vartype_name(ct->ct_type),
1662 vartype_name(tv->v_type));
1663 goto failed;
1664 }
1665 }
1666 break;
1667
1668 case ISN_2BOOL:
1669 {
1670 int n;
1671
1672 tv = STACK_TV_BOT(-1);
1673 n = tv2bool(tv);
1674 if (iptr->isn_arg.number) // invert
1675 n = !n;
1676 clear_tv(tv);
1677 tv->v_type = VAR_BOOL;
1678 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1679 }
1680 break;
1681
1682 case ISN_2STRING:
1683 {
1684 char_u *str;
1685
1686 tv = STACK_TV_BOT(iptr->isn_arg.number);
1687 if (tv->v_type != VAR_STRING)
1688 {
1689 str = typval_tostring(tv);
1690 clear_tv(tv);
1691 tv->v_type = VAR_STRING;
1692 tv->vval.v_string = str;
1693 }
1694 }
1695 break;
1696
1697 case ISN_DROP:
1698 --ectx.ec_stack.ga_len;
1699 clear_tv(STACK_TV_BOT(0));
1700 break;
1701 }
1702 }
1703
1704done:
1705 // function finished, get result from the stack.
1706 tv = STACK_TV_BOT(-1);
1707 *rettv = *tv;
1708 tv->v_type = VAR_UNKNOWN;
1709 ret = OK;
1710
1711failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001712 // When failed need to unwind the call stack.
1713 while (ectx.ec_frame != initial_frame_ptr)
1714 func_return(&ectx);
1715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1717 clear_tv(STACK_TV(idx));
1718 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001719 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 return ret;
1721}
1722
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001723/*
1724 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001725 * We don't really need this at runtime, but we do have tests that require it,
1726 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 */
1728 void
1729ex_disassemble(exarg_T *eap)
1730{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001731 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001732 char_u *fname;
1733 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 dfunc_T *dfunc;
1735 isn_T *instr;
1736 int current;
1737 int line_idx = 0;
1738 int prev_current = 0;
1739
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001740 fname = trans_function_name(&arg, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001741 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001742 if (fname == NULL)
1743 {
1744 semsg(_(e_invarg2), eap->arg);
1745 return;
1746 }
1747
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001748 ufunc = find_func(fname, NULL);
1749 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 if (ufunc == NULL)
1751 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001752 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 return;
1754 }
1755 if (ufunc->uf_dfunc_idx < 0)
1756 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01001757 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 return;
1759 }
1760 if (ufunc->uf_name_exp != NULL)
1761 msg((char *)ufunc->uf_name_exp);
1762 else
1763 msg((char *)ufunc->uf_name);
1764
1765 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
1766 instr = dfunc->df_instr;
1767 for (current = 0; current < dfunc->df_instr_count; ++current)
1768 {
1769 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001770 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001771
1772 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
1773 {
1774 if (current > prev_current)
1775 {
1776 msg_puts("\n\n");
1777 prev_current = current;
1778 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01001779 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
1780 if (line != NULL)
1781 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 }
1783
1784 switch (iptr->isn_type)
1785 {
1786 case ISN_EXEC:
1787 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
1788 break;
1789 case ISN_ECHO:
1790 {
1791 echo_T *echo = &iptr->isn_arg.echo;
1792
1793 smsg("%4d %s %d", current,
1794 echo->echo_with_white ? "ECHO" : "ECHON",
1795 echo->echo_count);
1796 }
1797 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001798 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01001799 smsg("%4d EXECUTE %lld", current,
1800 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01001801 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 case ISN_LOAD:
1803 if (iptr->isn_arg.number < 0)
1804 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01001805 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 else
Bram Moolenaar10827722020-03-23 22:53:22 +01001807 smsg("%4d LOAD $%lld", current,
1808 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 break;
1810 case ISN_LOADV:
1811 smsg("%4d LOADV v:%s", current,
1812 get_vim_var_name(iptr->isn_arg.number));
1813 break;
1814 case ISN_LOADSCRIPT:
1815 {
1816 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001817 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1819 + iptr->isn_arg.script.script_idx;
1820
1821 smsg("%4d LOADSCRIPT %s from %s", current,
1822 sv->sv_name, si->sn_name);
1823 }
1824 break;
1825 case ISN_LOADS:
1826 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001827 scriptitem_T *si = SCRIPT_ITEM(
1828 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829
1830 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001831 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 }
1833 break;
1834 case ISN_LOADG:
1835 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
1836 break;
1837 case ISN_LOADOPT:
1838 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
1839 break;
1840 case ISN_LOADENV:
1841 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
1842 break;
1843 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01001844 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 break;
1846
1847 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001848 if (iptr->isn_arg.number < 0)
1849 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01001850 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001851 else
Bram Moolenaar10827722020-03-23 22:53:22 +01001852 smsg("%4d STORE $%lld", current,
1853 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001855 case ISN_STOREV:
1856 smsg("%4d STOREV v:%s", current,
1857 get_vim_var_name(iptr->isn_arg.number));
1858 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001860 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
1861 break;
1862 case ISN_STORES:
1863 {
1864 scriptitem_T *si = SCRIPT_ITEM(
1865 iptr->isn_arg.loadstore.ls_sid);
1866
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001867 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 break;
1871 case ISN_STORESCRIPT:
1872 {
1873 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001874 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1876 + iptr->isn_arg.script.script_idx;
1877
1878 smsg("%4d STORESCRIPT %s in %s", current,
1879 sv->sv_name, si->sn_name);
1880 }
1881 break;
1882 case ISN_STOREOPT:
1883 smsg("%4d STOREOPT &%s", current,
1884 iptr->isn_arg.storeopt.so_name);
1885 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001886 case ISN_STOREENV:
1887 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
1888 break;
1889 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01001890 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001891 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 case ISN_STORENR:
1893 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01001894 iptr->isn_arg.storenr.stnr_val,
1895 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 break;
1897
1898 // constants
1899 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01001900 smsg("%4d PUSHNR %lld", current,
1901 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 break;
1903 case ISN_PUSHBOOL:
1904 case ISN_PUSHSPEC:
1905 smsg("%4d PUSH %s", current,
1906 get_var_special_name(iptr->isn_arg.number));
1907 break;
1908 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001909#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01001911#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 break;
1913 case ISN_PUSHS:
1914 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
1915 break;
1916 case ISN_PUSHBLOB:
1917 {
1918 char_u *r;
1919 char_u numbuf[NUMBUFLEN];
1920 char_u *tofree;
1921
1922 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01001923 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 vim_free(tofree);
1925 }
1926 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001927 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001928 {
1929 char *name = (char *)iptr->isn_arg.string;
1930
1931 smsg("%4d PUSHFUNC \"%s\"", current,
1932 name == NULL ? "[none]" : name);
1933 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001934 break;
1935 case ISN_PUSHPARTIAL:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001936 {
1937 partial_T *part = iptr->isn_arg.partial;
1938
1939 smsg("%4d PUSHPARTIAL \"%s\"", current,
1940 part == NULL ? "[none]" : (char *)partial_name(part));
1941 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001942 break;
1943 case ISN_PUSHCHANNEL:
1944#ifdef FEAT_JOB_CHANNEL
1945 {
1946 channel_T *channel = iptr->isn_arg.channel;
1947
1948 smsg("%4d PUSHCHANNEL %d", current,
1949 channel == NULL ? 0 : channel->ch_id);
1950 }
1951#endif
1952 break;
1953 case ISN_PUSHJOB:
1954#ifdef FEAT_JOB_CHANNEL
1955 {
1956 typval_T tv;
1957 char_u *name;
1958
1959 tv.v_type = VAR_JOB;
1960 tv.vval.v_job = iptr->isn_arg.job;
1961 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01001962 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001963 }
1964#endif
1965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001966 case ISN_PUSHEXC:
1967 smsg("%4d PUSH v:exception", current);
1968 break;
1969 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01001970 smsg("%4d NEWLIST size %lld", current,
1971 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001972 break;
1973 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01001974 smsg("%4d NEWDICT size %lld", current,
1975 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 break;
1977
1978 // function call
1979 case ISN_BCALL:
1980 {
1981 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
1982
1983 smsg("%4d BCALL %s(argc %d)", current,
1984 internal_func_name(cbfunc->cbf_idx),
1985 cbfunc->cbf_argcount);
1986 }
1987 break;
1988 case ISN_DCALL:
1989 {
1990 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
1991 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
1992 + cdfunc->cdf_idx;
1993
1994 smsg("%4d DCALL %s(argc %d)", current,
1995 df->df_ufunc->uf_name_exp != NULL
1996 ? df->df_ufunc->uf_name_exp
1997 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
1998 }
1999 break;
2000 case ISN_UCALL:
2001 {
2002 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2003
2004 smsg("%4d UCALL %s(argc %d)", current,
2005 cufunc->cuf_name, cufunc->cuf_argcount);
2006 }
2007 break;
2008 case ISN_PCALL:
2009 {
2010 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2011
2012 smsg("%4d PCALL%s (argc %d)", current,
2013 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2014 }
2015 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002016 case ISN_PCALL_END:
2017 smsg("%4d PCALL end", current);
2018 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 case ISN_RETURN:
2020 smsg("%4d RETURN", current);
2021 break;
2022 case ISN_FUNCREF:
2023 {
2024 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2025 + iptr->isn_arg.number;
2026
2027 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
2028 }
2029 break;
2030
2031 case ISN_JUMP:
2032 {
2033 char *when = "?";
2034
2035 switch (iptr->isn_arg.jump.jump_when)
2036 {
2037 case JUMP_ALWAYS:
2038 when = "JUMP";
2039 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 case JUMP_AND_KEEP_IF_TRUE:
2041 when = "JUMP_AND_KEEP_IF_TRUE";
2042 break;
2043 case JUMP_IF_FALSE:
2044 when = "JUMP_IF_FALSE";
2045 break;
2046 case JUMP_AND_KEEP_IF_FALSE:
2047 when = "JUMP_AND_KEEP_IF_FALSE";
2048 break;
2049 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002050 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 iptr->isn_arg.jump.jump_where);
2052 }
2053 break;
2054
2055 case ISN_FOR:
2056 {
2057 forloop_T *forloop = &iptr->isn_arg.forloop;
2058
2059 smsg("%4d FOR $%d -> %d", current,
2060 forloop->for_idx, forloop->for_end);
2061 }
2062 break;
2063
2064 case ISN_TRY:
2065 {
2066 try_T *try = &iptr->isn_arg.try;
2067
2068 smsg("%4d TRY catch -> %d, finally -> %d", current,
2069 try->try_catch, try->try_finally);
2070 }
2071 break;
2072 case ISN_CATCH:
2073 // TODO
2074 smsg("%4d CATCH", current);
2075 break;
2076 case ISN_ENDTRY:
2077 smsg("%4d ENDTRY", current);
2078 break;
2079 case ISN_THROW:
2080 smsg("%4d THROW", current);
2081 break;
2082
2083 // expression operations on number
2084 case ISN_OPNR:
2085 case ISN_OPFLOAT:
2086 case ISN_OPANY:
2087 {
2088 char *what;
2089 char *ins;
2090
2091 switch (iptr->isn_arg.op.op_type)
2092 {
2093 case EXPR_MULT: what = "*"; break;
2094 case EXPR_DIV: what = "/"; break;
2095 case EXPR_REM: what = "%"; break;
2096 case EXPR_SUB: what = "-"; break;
2097 case EXPR_ADD: what = "+"; break;
2098 default: what = "???"; break;
2099 }
2100 switch (iptr->isn_type)
2101 {
2102 case ISN_OPNR: ins = "OPNR"; break;
2103 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2104 case ISN_OPANY: ins = "OPANY"; break;
2105 default: ins = "???"; break;
2106 }
2107 smsg("%4d %s %s", current, ins, what);
2108 }
2109 break;
2110
2111 case ISN_COMPAREBOOL:
2112 case ISN_COMPARESPECIAL:
2113 case ISN_COMPARENR:
2114 case ISN_COMPAREFLOAT:
2115 case ISN_COMPARESTRING:
2116 case ISN_COMPAREBLOB:
2117 case ISN_COMPARELIST:
2118 case ISN_COMPAREDICT:
2119 case ISN_COMPAREFUNC:
2120 case ISN_COMPAREPARTIAL:
2121 case ISN_COMPAREANY:
2122 {
2123 char *p;
2124 char buf[10];
2125 char *type;
2126
2127 switch (iptr->isn_arg.op.op_type)
2128 {
2129 case EXPR_EQUAL: p = "=="; break;
2130 case EXPR_NEQUAL: p = "!="; break;
2131 case EXPR_GREATER: p = ">"; break;
2132 case EXPR_GEQUAL: p = ">="; break;
2133 case EXPR_SMALLER: p = "<"; break;
2134 case EXPR_SEQUAL: p = "<="; break;
2135 case EXPR_MATCH: p = "=~"; break;
2136 case EXPR_IS: p = "is"; break;
2137 case EXPR_ISNOT: p = "isnot"; break;
2138 case EXPR_NOMATCH: p = "!~"; break;
2139 default: p = "???"; break;
2140 }
2141 STRCPY(buf, p);
2142 if (iptr->isn_arg.op.op_ic == TRUE)
2143 strcat(buf, "?");
2144 switch(iptr->isn_type)
2145 {
2146 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2147 case ISN_COMPARESPECIAL:
2148 type = "COMPARESPECIAL"; break;
2149 case ISN_COMPARENR: type = "COMPARENR"; break;
2150 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2151 case ISN_COMPARESTRING:
2152 type = "COMPARESTRING"; break;
2153 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2154 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2155 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2156 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
2157 case ISN_COMPAREPARTIAL:
2158 type = "COMPAREPARTIAL"; break;
2159 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2160 default: type = "???"; break;
2161 }
2162
2163 smsg("%4d %s %s", current, type, buf);
2164 }
2165 break;
2166
2167 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2168 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2169
2170 // expression operations
2171 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2172 case ISN_INDEX: smsg("%4d INDEX", current); break;
2173 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2174 iptr->isn_arg.string); break;
2175 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2176
2177 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2178 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2179 vartype_name(iptr->isn_arg.type.ct_type),
2180 iptr->isn_arg.type.ct_off);
2181 break;
2182 case ISN_2BOOL: if (iptr->isn_arg.number)
2183 smsg("%4d INVERT (!val)", current);
2184 else
2185 smsg("%4d 2BOOL (!!val)", current);
2186 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002187 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2188 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002189 break;
2190
2191 case ISN_DROP: smsg("%4d DROP", current); break;
2192 }
2193 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002194}
2195
2196/*
2197 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2198 * list, etc. Mostly like what JavaScript does, except that empty list and
2199 * empty dictionary are FALSE.
2200 */
2201 int
2202tv2bool(typval_T *tv)
2203{
2204 switch (tv->v_type)
2205 {
2206 case VAR_NUMBER:
2207 return tv->vval.v_number != 0;
2208 case VAR_FLOAT:
2209#ifdef FEAT_FLOAT
2210 return tv->vval.v_float != 0.0;
2211#else
2212 break;
2213#endif
2214 case VAR_PARTIAL:
2215 return tv->vval.v_partial != NULL;
2216 case VAR_FUNC:
2217 case VAR_STRING:
2218 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2219 case VAR_LIST:
2220 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2221 case VAR_DICT:
2222 return tv->vval.v_dict != NULL
2223 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2224 case VAR_BOOL:
2225 case VAR_SPECIAL:
2226 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2227 case VAR_JOB:
2228#ifdef FEAT_JOB_CHANNEL
2229 return tv->vval.v_job != NULL;
2230#else
2231 break;
2232#endif
2233 case VAR_CHANNEL:
2234#ifdef FEAT_JOB_CHANNEL
2235 return tv->vval.v_channel != NULL;
2236#else
2237 break;
2238#endif
2239 case VAR_BLOB:
2240 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2241 case VAR_UNKNOWN:
2242 case VAR_VOID:
2243 break;
2244 }
2245 return FALSE;
2246}
2247
2248/*
2249 * If "tv" is a string give an error and return FAIL.
2250 */
2251 int
2252check_not_string(typval_T *tv)
2253{
2254 if (tv->v_type == VAR_STRING)
2255 {
2256 emsg(_("E1030: Using a String as a Number"));
2257 clear_tv(tv);
2258 return FAIL;
2259 }
2260 return OK;
2261}
2262
2263
2264#endif // FEAT_EVAL