blob: aee1c80a2b53aebe3a3b8e25909eef7a1991236a [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
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
131 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
163
164 if (dfunc->df_deleted)
165 {
166 emsg_funcname(e_func_deleted, ufunc->uf_name);
167 return FAIL;
168 }
169
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 if (ufunc->uf_va_name != NULL)
171 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200172 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200173 // Stack at time of call with 2 varargs:
174 // normal_arg
175 // optional_arg
176 // vararg_1
177 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200178 // After creating the list:
179 // normal_arg
180 // optional_arg
181 // vararg-list
182 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // After creating the list
185 // normal_arg
186 // (space for optional_arg)
187 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200188 vararg_count = argcount - ufunc->uf_args.ga_len;
189 if (vararg_count < 0)
190 vararg_count = 0;
191 else
192 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200193 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200195
196 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 }
198
Bram Moolenaarfe270812020-04-11 22:31:27 +0200199 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 if (arg_to_add < 0)
201 {
202 iemsg("Argument count wrong?");
203 return FAIL;
204 }
205 if (ga_grow(&ectx->ec_stack, arg_to_add + 3 + dfunc->df_varcount) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100206 return FAIL;
207
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // Move the vararg-list to below the missing optional arguments.
209 if (vararg_count > 0 && arg_to_add > 0)
210 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100211
212 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200214 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100216
217 // Store current execution state in stack frame for ISN_RETURN.
218 // TODO: If the actual number of arguments doesn't match what the called
219 // function expects things go bad.
220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame;
223 ectx->ec_frame = ectx->ec_stack.ga_len;
224
225 // Initialize local variables
226 for (idx = 0; idx < dfunc->df_varcount; ++idx)
227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + dfunc->df_varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
230 // Set execution state to the start of the called function.
231 ectx->ec_dfunc_idx = cdf_idx;
232 ectx->ec_instr = dfunc->df_instr;
233 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100234
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200235 // used for closures
236 ectx->ec_outer_stack = ufunc->uf_ectx_stack;
237 ectx->ec_outer_frame = ufunc->uf_ectx_frame;
238
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100239 // Decide where to start execution, handles optional arguments.
240 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100241
242 return OK;
243}
244
245// Get pointer to item in the stack.
246#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
247
248/*
249 * Return from the current function.
250 */
251 static void
252func_return(ectx_T *ectx)
253{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100254 int idx;
255 dfunc_T *dfunc;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100256 int top;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
258 // execution context goes one level up
259 estack_pop();
260
261 // Clear the local variables and temporary values, but not
262 // the return value.
263 for (idx = ectx->ec_frame + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100264 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100266
267 // Clear the arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100268 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100269 top = ectx->ec_frame - ufunc_argcount(dfunc->df_ufunc);
270 for (idx = top; idx < ectx->ec_frame; ++idx)
271 clear_tv(STACK_TV(idx));
272
273 // Restore the previous frame.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100274 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame)->vval.v_number;
275 ectx->ec_iidx = STACK_TV(ectx->ec_frame + 1)->vval.v_number;
276 ectx->ec_frame = STACK_TV(ectx->ec_frame + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
278 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100279
280 // Reset the stack to the position before the call, move the return value
281 // to the top of the stack.
282 idx = ectx->ec_stack.ga_len - 1;
283 ectx->ec_stack.ga_len = top + 1;
284 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285}
286
287#undef STACK_TV
288
289/*
290 * Prepare arguments and rettv for calling a builtin or user function.
291 */
292 static int
293call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
294{
295 int idx;
296 typval_T *tv;
297
298 // Move arguments from bottom of the stack to argvars[] and add terminator.
299 for (idx = 0; idx < argcount; ++idx)
300 argvars[idx] = *STACK_TV_BOT(idx - argcount);
301 argvars[argcount].v_type = VAR_UNKNOWN;
302
303 // Result replaces the arguments on the stack.
304 if (argcount > 0)
305 ectx->ec_stack.ga_len -= argcount - 1;
306 else if (ga_grow(&ectx->ec_stack, 1) == FAIL)
307 return FAIL;
308 else
309 ++ectx->ec_stack.ga_len;
310
311 // Default return value is zero.
312 tv = STACK_TV_BOT(-1);
313 tv->v_type = VAR_NUMBER;
314 tv->vval.v_number = 0;
315
316 return OK;
317}
318
319/*
320 * Call a builtin function by index.
321 */
322 static int
323call_bfunc(int func_idx, int argcount, ectx_T *ectx)
324{
325 typval_T argvars[MAX_FUNC_ARGS];
326 int idx;
327
328 if (call_prepare(argcount, argvars, ectx) == FAIL)
329 return FAIL;
330
331 // Call the builtin function.
332 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
333
334 // Clear the arguments.
335 for (idx = 0; idx < argcount; ++idx)
336 clear_tv(&argvars[idx]);
337 return OK;
338}
339
340/*
341 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100342 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100343 */
344 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100345call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100346{
347 typval_T argvars[MAX_FUNC_ARGS];
348 funcexe_T funcexe;
349 int error;
350 int idx;
351
352 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100353 {
354 // The function has been compiled, can call it quickly. For a function
355 // that was defined later: we can call it directly next time.
356 if (iptr != NULL)
357 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100358 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100359 iptr->isn_type = ISN_DCALL;
360 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
361 iptr->isn_arg.dfunc.cdf_argcount = argcount;
362 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100363 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100364 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100365
366 if (call_prepare(argcount, argvars, ectx) == FAIL)
367 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200368 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100369 funcexe.evaluate = TRUE;
370
371 // Call the user function. Result goes in last position on the stack.
372 // TODO: add selfdict if there is one
373 error = call_user_func_check(ufunc, argcount, argvars,
374 STACK_TV_BOT(-1), &funcexe, NULL);
375
376 // Clear the arguments.
377 for (idx = 0; idx < argcount; ++idx)
378 clear_tv(&argvars[idx]);
379
380 if (error != FCERR_NONE)
381 {
382 user_func_error(error, ufunc->uf_name);
383 return FAIL;
384 }
385 return OK;
386}
387
388/*
389 * Execute a function by "name".
390 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100391 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 * Returns FAIL if not found without an error message.
393 */
394 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100395call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396{
397 ufunc_T *ufunc;
398
399 if (builtin_function(name, -1))
400 {
401 int func_idx = find_internal_func(name);
402
403 if (func_idx < 0)
404 return FAIL;
405 if (check_internal_func(func_idx, argcount) == FAIL)
406 return FAIL;
407 return call_bfunc(func_idx, argcount, ectx);
408 }
409
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200410 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100411 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100412 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100413
414 return FAIL;
415}
416
417 static int
418call_partial(typval_T *tv, int argcount, ectx_T *ectx)
419{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200420 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100421 int called_emsg_before = called_emsg;
422
423 if (tv->v_type == VAR_PARTIAL)
424 {
425 partial_T *pt = tv->vval.v_partial;
426
427 if (pt->pt_func != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100428 return call_ufunc(pt->pt_func, argcount, ectx, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100429 name = pt->pt_name;
430 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200431 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200433 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 {
435 if (called_emsg == called_emsg_before)
436 semsg(_(e_unknownfunc), name);
437 return FAIL;
438 }
439 return OK;
440}
441
442/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100443 * Store "tv" in variable "name".
444 * This is for s: and g: variables.
445 */
446 static void
447store_var(char_u *name, typval_T *tv)
448{
449 funccal_entry_T entry;
450
451 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200452 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100453 restore_funccal();
454}
455
Bram Moolenaard3aac292020-04-19 14:32:17 +0200456
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100457/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 * Execute a function by "name".
459 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100460 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100461 */
462 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100463call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464{
465 int called_emsg_before = called_emsg;
466
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100467 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468 && called_emsg == called_emsg_before)
469 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200470 dictitem_T *v;
471
472 v = find_var(name, NULL, FALSE);
473 if (v == NULL)
474 {
475 semsg(_(e_unknownfunc), name);
476 return FAIL;
477 }
478 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
479 {
480 semsg(_(e_unknownfunc), name);
481 return FAIL;
482 }
483 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484 }
485 return OK;
486}
487
488/*
489 * Call a "def" function from old Vim script.
490 * Return OK or FAIL.
491 */
492 int
493call_def_function(
494 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200495 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100496 typval_T *argv, // arguments
497 typval_T *rettv) // return value
498{
499 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200500 int argc = argc_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100501 int initial_frame_ptr;
502 typval_T *tv;
503 int idx;
504 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100505 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200506 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507
508// Get pointer to item in the stack.
509#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
510
511// Get pointer to item at the bottom of the stack, -1 is the bottom.
512#undef STACK_TV_BOT
513#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
514
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200515// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame + STACK_FRAME_SIZE + idx)
517
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200518// Like STACK_TV_VAR but use the outer scope
519#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
520
Bram Moolenaara80faa82020-04-12 19:37:17 +0200521 CLEAR_FIELD(ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100522 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
523 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaard5aec0c2020-02-27 21:48:51 +0100524 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
526
527 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
528
529 // Put arguments on the stack.
530 for (idx = 0; idx < argc; ++idx)
531 {
532 copy_tv(&argv[idx], STACK_TV_BOT(0));
533 ++ectx.ec_stack.ga_len;
534 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200535
536 // Turn varargs into a list. Empty list if no args.
537 if (ufunc->uf_va_name != NULL)
538 {
539 int vararg_count = argc - ufunc->uf_args.ga_len;
540
541 if (vararg_count < 0)
542 vararg_count = 0;
543 else
544 argc -= vararg_count;
545 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200546 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200547 if (defcount > 0)
548 // Move varargs list to below missing default arguments.
549 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
550 --ectx.ec_stack.ga_len;
551 }
552
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100553 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200554 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100555 if (defcount > 0)
556 for (idx = 0; idx < defcount; ++idx)
557 {
558 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
559 ++ectx.ec_stack.ga_len;
560 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200561 if (ufunc->uf_va_name != NULL)
562 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
564 // Frame pointer points to just after arguments.
565 ectx.ec_frame = ectx.ec_stack.ga_len;
566 initial_frame_ptr = ectx.ec_frame;
567
568 // dummy frame entries
569 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
570 {
571 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
572 ++ectx.ec_stack.ga_len;
573 }
574
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200575 {
576 // Reserve space for local variables.
577 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
578 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200580 for (idx = 0; idx < dfunc->df_varcount; ++idx)
581 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
582 ectx.ec_stack.ga_len += dfunc->df_varcount;
583
584 ectx.ec_instr = dfunc->df_instr;
585 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100586
Bram Moolenaara26b9702020-04-18 19:53:28 +0200587 // Commands behave like vim9script.
588 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
589
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100590 // Decide where to start execution, handles optional arguments.
591 init_instr_idx(ufunc, argc, &ectx);
592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 for (;;)
594 {
595 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100596
597 veryfast_breakcheck();
598 if (got_int)
599 {
600 // Turn CTRL-C into an exception.
601 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100602 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100603 goto failed;
604 did_throw = TRUE;
605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100606
Bram Moolenaara26b9702020-04-18 19:53:28 +0200607 if (did_emsg && msg_list != NULL && *msg_list != NULL)
608 {
609 // Turn an error message into an exception.
610 did_emsg = FALSE;
611 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
612 goto failed;
613 did_throw = TRUE;
614 *msg_list = NULL;
615 }
616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100617 if (did_throw && !ectx.ec_in_catch)
618 {
619 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100620 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100621
622 // An exception jumps to the first catch, finally, or returns from
623 // the current function.
624 if (trystack->ga_len > 0)
625 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
626 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame)
627 {
628 // jump to ":catch" or ":finally"
629 ectx.ec_in_catch = TRUE;
630 ectx.ec_iidx = trycmd->tcd_catch_idx;
631 }
632 else
633 {
634 // not inside try or need to return from current functions.
635 if (ectx.ec_frame == initial_frame_ptr)
636 {
637 // At the toplevel we are done. Push a dummy return value.
638 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
639 goto failed;
640 tv = STACK_TV_BOT(0);
641 tv->v_type = VAR_NUMBER;
642 tv->vval.v_number = 0;
643 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100644 need_rethrow = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 goto done;
646 }
647
648 func_return(&ectx);
649 }
650 continue;
651 }
652
653 iptr = &ectx.ec_instr[ectx.ec_iidx++];
654 switch (iptr->isn_type)
655 {
656 // execute Ex command line
657 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200658 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 do_cmdline_cmd(iptr->isn_arg.string);
660 break;
661
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200662 // execute Ex command from pieces on the stack
663 case ISN_EXECCONCAT:
664 {
665 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200666 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200667 int pass;
668 int i;
669 char_u *cmd = NULL;
670 char_u *str;
671
672 for (pass = 1; pass <= 2; ++pass)
673 {
674 for (i = 0; i < count; ++i)
675 {
676 tv = STACK_TV_BOT(i - count);
677 str = tv->vval.v_string;
678 if (str != NULL && *str != NUL)
679 {
680 if (pass == 2)
681 STRCPY(cmd + len, str);
682 len += STRLEN(str);
683 }
684 if (pass == 2)
685 clear_tv(tv);
686 }
687 if (pass == 1)
688 {
689 cmd = alloc(len + 1);
690 if (cmd == NULL)
691 goto failed;
692 len = 0;
693 }
694 }
695
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200696 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200697 do_cmdline_cmd(cmd);
698 vim_free(cmd);
699 }
700 break;
701
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702 // execute :echo {string} ...
703 case ISN_ECHO:
704 {
705 int count = iptr->isn_arg.echo.echo_count;
706 int atstart = TRUE;
707 int needclr = TRUE;
708
709 for (idx = 0; idx < count; ++idx)
710 {
711 tv = STACK_TV_BOT(idx - count);
712 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
713 &atstart, &needclr);
714 clear_tv(tv);
715 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100716 if (needclr)
717 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718 ectx.ec_stack.ga_len -= count;
719 }
720 break;
721
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200722 // :execute {string} ...
723 // :echomsg {string} ...
724 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100725 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200726 case ISN_ECHOMSG:
727 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100728 {
729 int count = iptr->isn_arg.number;
730 garray_T ga;
731 char_u buf[NUMBUFLEN];
732 char_u *p;
733 int len;
734 int failed = FALSE;
735
736 ga_init2(&ga, 1, 80);
737 for (idx = 0; idx < count; ++idx)
738 {
739 tv = STACK_TV_BOT(idx - count);
740 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
741 {
742 emsg(_(e_inval_string));
743 break;
744 }
745 else
746 p = tv_get_string_buf(tv, buf);
747
748 len = (int)STRLEN(p);
749 if (ga_grow(&ga, len + 2) == FAIL)
750 failed = TRUE;
751 else
752 {
753 if (ga.ga_len > 0)
754 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
755 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
756 ga.ga_len += len;
757 }
758 clear_tv(tv);
759 }
760 ectx.ec_stack.ga_len -= count;
761
762 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200763 {
764 if (iptr->isn_type == ISN_EXECUTE)
765 do_cmdline_cmd((char_u *)ga.ga_data);
766 else
767 {
768 msg_sb_eol();
769 if (iptr->isn_type == ISN_ECHOMSG)
770 {
771 msg_attr(ga.ga_data, echo_attr);
772 out_flush();
773 }
774 else
775 {
776 int save_did_emsg = did_emsg;
777
778 SOURCING_LNUM = iptr->isn_lnum;
779 emsg(ga.ga_data);
780 if (!force_abort)
781 // We don't want to abort following
782 // commands, restore did_emsg.
783 did_emsg = save_did_emsg;
784 }
785 }
786 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100787 ga_clear(&ga);
788 }
789 break;
790
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100791 // load local variable or argument
792 case ISN_LOAD:
793 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
794 goto failed;
795 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
796 ++ectx.ec_stack.ga_len;
797 break;
798
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200799 // load variable or argument from outer scope
800 case ISN_LOADOUTER:
801 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
802 goto failed;
803 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
804 STACK_TV_BOT(0));
805 ++ectx.ec_stack.ga_len;
806 break;
807
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 // load v: variable
809 case ISN_LOADV:
810 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
811 goto failed;
812 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
813 ++ectx.ec_stack.ga_len;
814 break;
815
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100816 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100817 case ISN_LOADSCRIPT:
818 {
819 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100820 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 svar_T *sv;
822
823 sv = ((svar_T *)si->sn_var_vals.ga_data)
824 + iptr->isn_arg.script.script_idx;
825 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
826 goto failed;
827 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
828 ++ectx.ec_stack.ga_len;
829 }
830 break;
831
832 // load s: variable in old script
833 case ISN_LOADS:
834 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100835 hashtab_T *ht = &SCRIPT_VARS(
836 iptr->isn_arg.loadstore.ls_sid);
837 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 if (di == NULL)
841 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100842 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 goto failed;
844 }
845 else
846 {
847 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
848 goto failed;
849 copy_tv(&di->di_tv, STACK_TV_BOT(0));
850 ++ectx.ec_stack.ga_len;
851 }
852 }
853 break;
854
Bram Moolenaard3aac292020-04-19 14:32:17 +0200855 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +0200857 case ISN_LOADB:
858 case ISN_LOADW:
859 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200861 dictitem_T *di = NULL;
862 hashtab_T *ht = NULL;
863 char namespace;
864 switch (iptr->isn_type)
865 {
866 case ISN_LOADG:
867 ht = get_globvar_ht();
868 namespace = 'g';
869 break;
870 case ISN_LOADB:
871 ht = &curbuf->b_vars->dv_hashtab;
872 namespace = 'b';
873 break;
874 case ISN_LOADW:
875 ht = &curwin->w_vars->dv_hashtab;
876 namespace = 'w';
877 break;
878 case ISN_LOADT:
879 ht = &curtab->tp_vars->dv_hashtab;
880 namespace = 't';
881 break;
882 default: // Cannot reach here
883 goto failed;
884 }
885 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 if (di == NULL)
888 {
Bram Moolenaard3aac292020-04-19 14:32:17 +0200889 semsg(_("E121: Undefined variable: %c:%s"),
890 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 goto failed;
892 }
893 else
894 {
895 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
896 goto failed;
897 copy_tv(&di->di_tv, STACK_TV_BOT(0));
898 ++ectx.ec_stack.ga_len;
899 }
900 }
901 break;
902
903 // load &option
904 case ISN_LOADOPT:
905 {
906 typval_T optval;
907 char_u *name = iptr->isn_arg.string;
908
Bram Moolenaara8c17702020-04-01 21:17:24 +0200909 // This is not expected to fail, name is checked during
910 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
912 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +0100913 if (get_option_tv(&name, &optval, TRUE) == FAIL)
914 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 *STACK_TV_BOT(0) = optval;
916 ++ectx.ec_stack.ga_len;
917 }
918 break;
919
920 // load $ENV
921 case ISN_LOADENV:
922 {
923 typval_T optval;
924 char_u *name = iptr->isn_arg.string;
925
926 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
927 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100928 // name is always valid, checked when compiling
929 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 *STACK_TV_BOT(0) = optval;
931 ++ectx.ec_stack.ga_len;
932 }
933 break;
934
935 // load @register
936 case ISN_LOADREG:
937 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
938 goto failed;
939 tv = STACK_TV_BOT(0);
940 tv->v_type = VAR_STRING;
941 tv->vval.v_string = get_reg_contents(
942 iptr->isn_arg.number, GREG_EXPR_SRC);
943 ++ectx.ec_stack.ga_len;
944 break;
945
946 // store local variable
947 case ISN_STORE:
948 --ectx.ec_stack.ga_len;
949 tv = STACK_TV_VAR(iptr->isn_arg.number);
950 clear_tv(tv);
951 *tv = *STACK_TV_BOT(0);
952 break;
953
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100954 // store s: variable in old script
955 case ISN_STORES:
956 {
957 hashtab_T *ht = &SCRIPT_VARS(
958 iptr->isn_arg.loadstore.ls_sid);
959 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100960 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100961
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100962 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100963 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200964 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100965 else
966 {
967 clear_tv(&di->di_tv);
968 di->di_tv = *STACK_TV_BOT(0);
969 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100970 }
971 break;
972
973 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100974 case ISN_STORESCRIPT:
975 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100976 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100977 iptr->isn_arg.script.script_sid);
978 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
979 + iptr->isn_arg.script.script_idx;
980
981 --ectx.ec_stack.ga_len;
982 clear_tv(sv->sv_tv);
983 *sv->sv_tv = *STACK_TV_BOT(0);
984 }
985 break;
986
987 // store option
988 case ISN_STOREOPT:
989 {
990 long n = 0;
991 char_u *s = NULL;
992 char *msg;
993
994 --ectx.ec_stack.ga_len;
995 tv = STACK_TV_BOT(0);
996 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100997 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +0100999 if (s == NULL)
1000 s = (char_u *)"";
1001 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001002 else if (tv->v_type == VAR_NUMBER)
1003 n = tv->vval.v_number;
1004 else
1005 {
1006 emsg(_("E1051: Expected string or number"));
1007 goto failed;
1008 }
1009 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1010 n, s, iptr->isn_arg.storeopt.so_flags);
1011 if (msg != NULL)
1012 {
1013 emsg(_(msg));
1014 goto failed;
1015 }
1016 clear_tv(tv);
1017 }
1018 break;
1019
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001020 // store $ENV
1021 case ISN_STOREENV:
1022 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001023 tv = STACK_TV_BOT(0);
1024 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1025 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001026 break;
1027
1028 // store @r
1029 case ISN_STOREREG:
1030 {
1031 int reg = iptr->isn_arg.number;
1032
1033 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001034 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001035 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001036 tv_get_string(tv), -1, FALSE);
1037 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001038 }
1039 break;
1040
1041 // store v: variable
1042 case ISN_STOREV:
1043 --ectx.ec_stack.ga_len;
1044 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1045 == FAIL)
1046 goto failed;
1047 break;
1048
Bram Moolenaard3aac292020-04-19 14:32:17 +02001049 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001051 case ISN_STOREB:
1052 case ISN_STOREW:
1053 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001054 {
1055 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001056 hashtab_T *ht;
1057 switch (iptr->isn_type)
1058 {
1059 case ISN_STOREG:
1060 ht = get_globvar_ht();
1061 break;
1062 case ISN_STOREB:
1063 ht = &curbuf->b_vars->dv_hashtab;
1064 break;
1065 case ISN_STOREW:
1066 ht = &curwin->w_vars->dv_hashtab;
1067 break;
1068 case ISN_STORET:
1069 ht = &curtab->tp_vars->dv_hashtab;
1070 break;
1071 default: // Cannot reach here
1072 goto failed;
1073 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074
1075 --ectx.ec_stack.ga_len;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001076 di = find_var_in_ht(ht, 0,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001077 iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001079 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001080 else
1081 {
1082 clear_tv(&di->di_tv);
1083 di->di_tv = *STACK_TV_BOT(0);
1084 }
1085 }
1086 break;
1087
1088 // store number in local variable
1089 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001090 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 clear_tv(tv);
1092 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001093 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 break;
1095
1096 // push constant
1097 case ISN_PUSHNR:
1098 case ISN_PUSHBOOL:
1099 case ISN_PUSHSPEC:
1100 case ISN_PUSHF:
1101 case ISN_PUSHS:
1102 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001103 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001104 case ISN_PUSHCHANNEL:
1105 case ISN_PUSHJOB:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1107 goto failed;
1108 tv = STACK_TV_BOT(0);
1109 ++ectx.ec_stack.ga_len;
1110 switch (iptr->isn_type)
1111 {
1112 case ISN_PUSHNR:
1113 tv->v_type = VAR_NUMBER;
1114 tv->vval.v_number = iptr->isn_arg.number;
1115 break;
1116 case ISN_PUSHBOOL:
1117 tv->v_type = VAR_BOOL;
1118 tv->vval.v_number = iptr->isn_arg.number;
1119 break;
1120 case ISN_PUSHSPEC:
1121 tv->v_type = VAR_SPECIAL;
1122 tv->vval.v_number = iptr->isn_arg.number;
1123 break;
1124#ifdef FEAT_FLOAT
1125 case ISN_PUSHF:
1126 tv->v_type = VAR_FLOAT;
1127 tv->vval.v_float = iptr->isn_arg.fnumber;
1128 break;
1129#endif
1130 case ISN_PUSHBLOB:
1131 blob_copy(iptr->isn_arg.blob, tv);
1132 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001133 case ISN_PUSHFUNC:
1134 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001135 if (iptr->isn_arg.string == NULL)
1136 tv->vval.v_string = NULL;
1137 else
1138 tv->vval.v_string =
1139 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001140 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001141 case ISN_PUSHCHANNEL:
1142#ifdef FEAT_JOB_CHANNEL
1143 tv->v_type = VAR_CHANNEL;
1144 tv->vval.v_channel = iptr->isn_arg.channel;
1145 if (tv->vval.v_channel != NULL)
1146 ++tv->vval.v_channel->ch_refcount;
1147#endif
1148 break;
1149 case ISN_PUSHJOB:
1150#ifdef FEAT_JOB_CHANNEL
1151 tv->v_type = VAR_JOB;
1152 tv->vval.v_job = iptr->isn_arg.job;
1153 if (tv->vval.v_job != NULL)
1154 ++tv->vval.v_job->jv_refcount;
1155#endif
1156 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 default:
1158 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001159 tv->vval.v_string = vim_strsave(
1160 iptr->isn_arg.string == NULL
1161 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001162 }
1163 break;
1164
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001165 case ISN_UNLET:
1166 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1167 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1168 goto failed;
1169 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001170 case ISN_UNLETENV:
1171 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1172 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001173
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 // create a list from items on the stack; uses a single allocation
1175 // for the list header and the items
1176 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001177 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1178 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 break;
1180
1181 // create a dict from items on the stack
1182 case ISN_NEWDICT:
1183 {
1184 int count = iptr->isn_arg.number;
1185 dict_T *dict = dict_alloc();
1186 dictitem_T *item;
1187
1188 if (dict == NULL)
1189 goto failed;
1190 for (idx = 0; idx < count; ++idx)
1191 {
1192 // check key type is VAR_STRING
1193 tv = STACK_TV_BOT(2 * (idx - count));
1194 item = dictitem_alloc(tv->vval.v_string);
1195 clear_tv(tv);
1196 if (item == NULL)
1197 goto failed;
1198 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1199 item->di_tv.v_lock = 0;
1200 if (dict_add(dict, item) == FAIL)
1201 goto failed;
1202 }
1203
1204 if (count > 0)
1205 ectx.ec_stack.ga_len -= 2 * count - 1;
1206 else if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1207 goto failed;
1208 else
1209 ++ectx.ec_stack.ga_len;
1210 tv = STACK_TV_BOT(-1);
1211 tv->v_type = VAR_DICT;
1212 tv->vval.v_dict = dict;
1213 ++dict->dv_refcount;
1214 }
1215 break;
1216
1217 // call a :def function
1218 case ISN_DCALL:
1219 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1220 iptr->isn_arg.dfunc.cdf_argcount,
1221 &ectx) == FAIL)
1222 goto failed;
1223 break;
1224
1225 // call a builtin function
1226 case ISN_BCALL:
1227 SOURCING_LNUM = iptr->isn_lnum;
1228 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1229 iptr->isn_arg.bfunc.cbf_argcount,
1230 &ectx) == FAIL)
1231 goto failed;
1232 break;
1233
1234 // call a funcref or partial
1235 case ISN_PCALL:
1236 {
1237 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1238 int r;
1239 typval_T partial;
1240
1241 SOURCING_LNUM = iptr->isn_lnum;
1242 if (pfunc->cpf_top)
1243 {
1244 // funcref is above the arguments
1245 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1246 }
1247 else
1248 {
1249 // Get the funcref from the stack.
1250 --ectx.ec_stack.ga_len;
1251 partial = *STACK_TV_BOT(0);
1252 tv = &partial;
1253 }
1254 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
1255 if (tv == &partial)
1256 clear_tv(&partial);
1257 if (r == FAIL)
1258 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259 }
1260 break;
1261
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001262 case ISN_PCALL_END:
1263 // PCALL finished, arguments have been consumed and replaced by
1264 // the return value. Now clear the funcref from the stack,
1265 // and move the return value in its place.
1266 --ectx.ec_stack.ga_len;
1267 clear_tv(STACK_TV_BOT(-1));
1268 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1269 break;
1270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271 // call a user defined function or funcref/partial
1272 case ISN_UCALL:
1273 {
1274 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1275
1276 SOURCING_LNUM = iptr->isn_lnum;
1277 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001278 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 goto failed;
1280 }
1281 break;
1282
1283 // return from a :def function call
1284 case ISN_RETURN:
1285 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001286 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001287 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001288
1289 if (trystack->ga_len > 0)
1290 trycmd = ((trycmd_T *)trystack->ga_data)
1291 + trystack->ga_len - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 if (trycmd != NULL && trycmd->tcd_frame == ectx.ec_frame
1293 && trycmd->tcd_finally_idx != 0)
1294 {
1295 // jump to ":finally"
1296 ectx.ec_iidx = trycmd->tcd_finally_idx;
1297 trycmd->tcd_return = TRUE;
1298 }
1299 else
1300 {
1301 // Restore previous function. If the frame pointer
1302 // is zero then there is none and we are done.
1303 if (ectx.ec_frame == initial_frame_ptr)
1304 goto done;
1305
1306 func_return(&ectx);
1307 }
1308 }
1309 break;
1310
1311 // push a function reference to a compiled function
1312 case ISN_FUNCREF:
1313 {
1314 partial_T *pt = NULL;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001315 dfunc_T *dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316
1317 pt = ALLOC_CLEAR_ONE(partial_T);
1318 if (pt == NULL)
1319 goto failed;
1320 dfunc = ((dfunc_T *)def_functions.ga_data)
1321 + iptr->isn_arg.number;
1322 pt->pt_func = dfunc->df_ufunc;
1323 pt->pt_refcount = 1;
1324 ++dfunc->df_ufunc->uf_refcount;
1325
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001326 if (dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1327 {
1328 // Closure needs to find local variables in the current
1329 // stack.
1330 dfunc->df_ufunc->uf_ectx_stack = &ectx.ec_stack;
1331 dfunc->df_ufunc->uf_ectx_frame = ectx.ec_frame;
1332 }
1333
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001334 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1335 goto failed;
1336 tv = STACK_TV_BOT(0);
1337 ++ectx.ec_stack.ga_len;
1338 tv->vval.v_partial = pt;
1339 tv->v_type = VAR_PARTIAL;
1340 }
1341 break;
1342
1343 // jump if a condition is met
1344 case ISN_JUMP:
1345 {
1346 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1347 int jump = TRUE;
1348
1349 if (when != JUMP_ALWAYS)
1350 {
1351 tv = STACK_TV_BOT(-1);
1352 jump = tv2bool(tv);
1353 if (when == JUMP_IF_FALSE
1354 || when == JUMP_AND_KEEP_IF_FALSE)
1355 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001356 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 {
1358 // drop the value from the stack
1359 clear_tv(tv);
1360 --ectx.ec_stack.ga_len;
1361 }
1362 }
1363 if (jump)
1364 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1365 }
1366 break;
1367
1368 // top of a for loop
1369 case ISN_FOR:
1370 {
1371 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1372 typval_T *idxtv =
1373 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1374
1375 // push the next item from the list
1376 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1377 goto failed;
1378 if (++idxtv->vval.v_number >= list->lv_len)
1379 // past the end of the list, jump to "endfor"
1380 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1381 else if (list->lv_first == &range_list_item)
1382 {
1383 // non-materialized range() list
1384 tv = STACK_TV_BOT(0);
1385 tv->v_type = VAR_NUMBER;
1386 tv->vval.v_number = list_find_nr(
1387 list, idxtv->vval.v_number, NULL);
1388 ++ectx.ec_stack.ga_len;
1389 }
1390 else
1391 {
1392 listitem_T *li = list_find(list, idxtv->vval.v_number);
1393
1394 if (li == NULL)
1395 goto failed;
1396 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1397 ++ectx.ec_stack.ga_len;
1398 }
1399 }
1400 break;
1401
1402 // start of ":try" block
1403 case ISN_TRY:
1404 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001405 trycmd_T *trycmd = NULL;
1406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 if (ga_grow(&ectx.ec_trystack, 1) == FAIL)
1408 goto failed;
1409 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1410 + ectx.ec_trystack.ga_len;
1411 ++ectx.ec_trystack.ga_len;
1412 ++trylevel;
1413 trycmd->tcd_frame = ectx.ec_frame;
1414 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1415 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001416 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001417 }
1418 break;
1419
1420 case ISN_PUSHEXC:
1421 if (current_exception == NULL)
1422 {
1423 iemsg("Evaluating catch while current_exception is NULL");
1424 goto failed;
1425 }
1426 if (ga_grow(&ectx.ec_stack, 1) == FAIL)
1427 goto failed;
1428 tv = STACK_TV_BOT(0);
1429 ++ectx.ec_stack.ga_len;
1430 tv->v_type = VAR_STRING;
1431 tv->vval.v_string = vim_strsave(
1432 (char_u *)current_exception->value);
1433 break;
1434
1435 case ISN_CATCH:
1436 {
1437 garray_T *trystack = &ectx.ec_trystack;
1438
1439 if (trystack->ga_len > 0)
1440 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001441 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 + trystack->ga_len - 1;
1443 trycmd->tcd_caught = TRUE;
1444 }
1445 did_emsg = got_int = did_throw = FALSE;
1446 catch_exception(current_exception);
1447 }
1448 break;
1449
1450 // end of ":try" block
1451 case ISN_ENDTRY:
1452 {
1453 garray_T *trystack = &ectx.ec_trystack;
1454
1455 if (trystack->ga_len > 0)
1456 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001457 trycmd_T *trycmd = NULL;
1458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 --trystack->ga_len;
1460 --trylevel;
1461 trycmd = ((trycmd_T *)trystack->ga_data)
1462 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001463 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001464 {
1465 // discard the exception
1466 if (caught_stack == current_exception)
1467 caught_stack = caught_stack->caught;
1468 discard_current_exception();
1469 }
1470
1471 if (trycmd->tcd_return)
1472 {
1473 // Restore previous function. If the frame pointer
1474 // is zero then there is none and we are done.
1475 if (ectx.ec_frame == initial_frame_ptr)
1476 goto done;
1477
1478 func_return(&ectx);
1479 }
1480 }
1481 }
1482 break;
1483
1484 case ISN_THROW:
1485 --ectx.ec_stack.ga_len;
1486 tv = STACK_TV_BOT(0);
1487 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1488 {
1489 vim_free(tv->vval.v_string);
1490 goto failed;
1491 }
1492 did_throw = TRUE;
1493 break;
1494
1495 // compare with special values
1496 case ISN_COMPAREBOOL:
1497 case ISN_COMPARESPECIAL:
1498 {
1499 typval_T *tv1 = STACK_TV_BOT(-2);
1500 typval_T *tv2 = STACK_TV_BOT(-1);
1501 varnumber_T arg1 = tv1->vval.v_number;
1502 varnumber_T arg2 = tv2->vval.v_number;
1503 int res;
1504
1505 switch (iptr->isn_arg.op.op_type)
1506 {
1507 case EXPR_EQUAL: res = arg1 == arg2; break;
1508 case EXPR_NEQUAL: res = arg1 != arg2; break;
1509 default: res = 0; break;
1510 }
1511
1512 --ectx.ec_stack.ga_len;
1513 tv1->v_type = VAR_BOOL;
1514 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1515 }
1516 break;
1517
1518 // Operation with two number arguments
1519 case ISN_OPNR:
1520 case ISN_COMPARENR:
1521 {
1522 typval_T *tv1 = STACK_TV_BOT(-2);
1523 typval_T *tv2 = STACK_TV_BOT(-1);
1524 varnumber_T arg1 = tv1->vval.v_number;
1525 varnumber_T arg2 = tv2->vval.v_number;
1526 varnumber_T res;
1527
1528 switch (iptr->isn_arg.op.op_type)
1529 {
1530 case EXPR_MULT: res = arg1 * arg2; break;
1531 case EXPR_DIV: res = arg1 / arg2; break;
1532 case EXPR_REM: res = arg1 % arg2; break;
1533 case EXPR_SUB: res = arg1 - arg2; break;
1534 case EXPR_ADD: res = arg1 + arg2; break;
1535
1536 case EXPR_EQUAL: res = arg1 == arg2; break;
1537 case EXPR_NEQUAL: res = arg1 != arg2; break;
1538 case EXPR_GREATER: res = arg1 > arg2; break;
1539 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1540 case EXPR_SMALLER: res = arg1 < arg2; break;
1541 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1542 default: res = 0; break;
1543 }
1544
1545 --ectx.ec_stack.ga_len;
1546 if (iptr->isn_type == ISN_COMPARENR)
1547 {
1548 tv1->v_type = VAR_BOOL;
1549 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1550 }
1551 else
1552 tv1->vval.v_number = res;
1553 }
1554 break;
1555
1556 // Computation with two float arguments
1557 case ISN_OPFLOAT:
1558 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001559#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 {
1561 typval_T *tv1 = STACK_TV_BOT(-2);
1562 typval_T *tv2 = STACK_TV_BOT(-1);
1563 float_T arg1 = tv1->vval.v_float;
1564 float_T arg2 = tv2->vval.v_float;
1565 float_T res = 0;
1566 int cmp = FALSE;
1567
1568 switch (iptr->isn_arg.op.op_type)
1569 {
1570 case EXPR_MULT: res = arg1 * arg2; break;
1571 case EXPR_DIV: res = arg1 / arg2; break;
1572 case EXPR_SUB: res = arg1 - arg2; break;
1573 case EXPR_ADD: res = arg1 + arg2; break;
1574
1575 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1576 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1577 case EXPR_GREATER: cmp = arg1 > arg2; break;
1578 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1579 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1580 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1581 default: cmp = 0; break;
1582 }
1583 --ectx.ec_stack.ga_len;
1584 if (iptr->isn_type == ISN_COMPAREFLOAT)
1585 {
1586 tv1->v_type = VAR_BOOL;
1587 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1588 }
1589 else
1590 tv1->vval.v_float = res;
1591 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001592#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 break;
1594
1595 case ISN_COMPARELIST:
1596 {
1597 typval_T *tv1 = STACK_TV_BOT(-2);
1598 typval_T *tv2 = STACK_TV_BOT(-1);
1599 list_T *arg1 = tv1->vval.v_list;
1600 list_T *arg2 = tv2->vval.v_list;
1601 int cmp = FALSE;
1602 int ic = iptr->isn_arg.op.op_ic;
1603
1604 switch (iptr->isn_arg.op.op_type)
1605 {
1606 case EXPR_EQUAL: cmp =
1607 list_equal(arg1, arg2, ic, FALSE); break;
1608 case EXPR_NEQUAL: cmp =
1609 !list_equal(arg1, arg2, ic, FALSE); break;
1610 case EXPR_IS: cmp = arg1 == arg2; break;
1611 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1612 default: cmp = 0; break;
1613 }
1614 --ectx.ec_stack.ga_len;
1615 clear_tv(tv1);
1616 clear_tv(tv2);
1617 tv1->v_type = VAR_BOOL;
1618 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1619 }
1620 break;
1621
1622 case ISN_COMPAREBLOB:
1623 {
1624 typval_T *tv1 = STACK_TV_BOT(-2);
1625 typval_T *tv2 = STACK_TV_BOT(-1);
1626 blob_T *arg1 = tv1->vval.v_blob;
1627 blob_T *arg2 = tv2->vval.v_blob;
1628 int cmp = FALSE;
1629
1630 switch (iptr->isn_arg.op.op_type)
1631 {
1632 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1633 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1634 case EXPR_IS: cmp = arg1 == arg2; break;
1635 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1636 default: cmp = 0; break;
1637 }
1638 --ectx.ec_stack.ga_len;
1639 clear_tv(tv1);
1640 clear_tv(tv2);
1641 tv1->v_type = VAR_BOOL;
1642 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1643 }
1644 break;
1645
1646 // TODO: handle separately
1647 case ISN_COMPARESTRING:
1648 case ISN_COMPAREDICT:
1649 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 case ISN_COMPAREANY:
1651 {
1652 typval_T *tv1 = STACK_TV_BOT(-2);
1653 typval_T *tv2 = STACK_TV_BOT(-1);
1654 exptype_T exptype = iptr->isn_arg.op.op_type;
1655 int ic = iptr->isn_arg.op.op_ic;
1656
1657 typval_compare(tv1, tv2, exptype, ic);
1658 clear_tv(tv2);
1659 tv1->v_type = VAR_BOOL;
1660 tv1->vval.v_number = tv1->vval.v_number
1661 ? VVAL_TRUE : VVAL_FALSE;
1662 --ectx.ec_stack.ga_len;
1663 }
1664 break;
1665
1666 case ISN_ADDLIST:
1667 case ISN_ADDBLOB:
1668 {
1669 typval_T *tv1 = STACK_TV_BOT(-2);
1670 typval_T *tv2 = STACK_TV_BOT(-1);
1671
1672 if (iptr->isn_type == ISN_ADDLIST)
1673 eval_addlist(tv1, tv2);
1674 else
1675 eval_addblob(tv1, tv2);
1676 clear_tv(tv2);
1677 --ectx.ec_stack.ga_len;
1678 }
1679 break;
1680
1681 // Computation with two arguments of unknown type
1682 case ISN_OPANY:
1683 {
1684 typval_T *tv1 = STACK_TV_BOT(-2);
1685 typval_T *tv2 = STACK_TV_BOT(-1);
1686 varnumber_T n1, n2;
1687#ifdef FEAT_FLOAT
1688 float_T f1 = 0, f2 = 0;
1689#endif
1690 int error = FALSE;
1691
1692 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1693 {
1694 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1695 {
1696 eval_addlist(tv1, tv2);
1697 clear_tv(tv2);
1698 --ectx.ec_stack.ga_len;
1699 break;
1700 }
1701 else if (tv1->v_type == VAR_BLOB
1702 && tv2->v_type == VAR_BLOB)
1703 {
1704 eval_addblob(tv1, tv2);
1705 clear_tv(tv2);
1706 --ectx.ec_stack.ga_len;
1707 break;
1708 }
1709 }
1710#ifdef FEAT_FLOAT
1711 if (tv1->v_type == VAR_FLOAT)
1712 {
1713 f1 = tv1->vval.v_float;
1714 n1 = 0;
1715 }
1716 else
1717#endif
1718 {
1719 n1 = tv_get_number_chk(tv1, &error);
1720 if (error)
1721 goto failed;
1722#ifdef FEAT_FLOAT
1723 if (tv2->v_type == VAR_FLOAT)
1724 f1 = n1;
1725#endif
1726 }
1727#ifdef FEAT_FLOAT
1728 if (tv2->v_type == VAR_FLOAT)
1729 {
1730 f2 = tv2->vval.v_float;
1731 n2 = 0;
1732 }
1733 else
1734#endif
1735 {
1736 n2 = tv_get_number_chk(tv2, &error);
1737 if (error)
1738 goto failed;
1739#ifdef FEAT_FLOAT
1740 if (tv1->v_type == VAR_FLOAT)
1741 f2 = n2;
1742#endif
1743 }
1744#ifdef FEAT_FLOAT
1745 // if there is a float on either side the result is a float
1746 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
1747 {
1748 switch (iptr->isn_arg.op.op_type)
1749 {
1750 case EXPR_MULT: f1 = f1 * f2; break;
1751 case EXPR_DIV: f1 = f1 / f2; break;
1752 case EXPR_SUB: f1 = f1 - f2; break;
1753 case EXPR_ADD: f1 = f1 + f2; break;
1754 default: emsg(_(e_modulus)); goto failed;
1755 }
1756 clear_tv(tv1);
1757 clear_tv(tv2);
1758 tv1->v_type = VAR_FLOAT;
1759 tv1->vval.v_float = f1;
1760 --ectx.ec_stack.ga_len;
1761 }
1762 else
1763#endif
1764 {
1765 switch (iptr->isn_arg.op.op_type)
1766 {
1767 case EXPR_MULT: n1 = n1 * n2; break;
1768 case EXPR_DIV: n1 = num_divide(n1, n2); break;
1769 case EXPR_SUB: n1 = n1 - n2; break;
1770 case EXPR_ADD: n1 = n1 + n2; break;
1771 default: n1 = num_modulus(n1, n2); break;
1772 }
1773 clear_tv(tv1);
1774 clear_tv(tv2);
1775 tv1->v_type = VAR_NUMBER;
1776 tv1->vval.v_number = n1;
1777 --ectx.ec_stack.ga_len;
1778 }
1779 }
1780 break;
1781
1782 case ISN_CONCAT:
1783 {
1784 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
1785 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
1786 char_u *res;
1787
1788 res = concat_str(str1, str2);
1789 clear_tv(STACK_TV_BOT(-2));
1790 clear_tv(STACK_TV_BOT(-1));
1791 --ectx.ec_stack.ga_len;
1792 STACK_TV_BOT(-1)->vval.v_string = res;
1793 }
1794 break;
1795
1796 case ISN_INDEX:
1797 {
1798 list_T *list;
1799 varnumber_T n;
1800 listitem_T *li;
1801
1802 // list index: list is at stack-2, index at stack-1
1803 tv = STACK_TV_BOT(-2);
1804 if (tv->v_type != VAR_LIST)
1805 {
1806 emsg(_(e_listreq));
1807 goto failed;
1808 }
1809 list = tv->vval.v_list;
1810
1811 tv = STACK_TV_BOT(-1);
1812 if (tv->v_type != VAR_NUMBER)
1813 {
1814 emsg(_(e_number_exp));
1815 goto failed;
1816 }
1817 n = tv->vval.v_number;
1818 clear_tv(tv);
1819 if ((li = list_find(list, n)) == NULL)
1820 {
1821 semsg(_(e_listidx), n);
1822 goto failed;
1823 }
1824 --ectx.ec_stack.ga_len;
1825 clear_tv(STACK_TV_BOT(-1));
1826 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
1827 }
1828 break;
1829
1830 // dict member with string key
1831 case ISN_MEMBER:
1832 {
1833 dict_T *dict;
1834 dictitem_T *di;
1835
1836 tv = STACK_TV_BOT(-1);
1837 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
1838 {
1839 emsg(_(e_dictreq));
1840 goto failed;
1841 }
1842 dict = tv->vval.v_dict;
1843
1844 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
1845 == NULL)
1846 {
1847 semsg(_(e_dictkey), iptr->isn_arg.string);
1848 goto failed;
1849 }
1850 clear_tv(tv);
1851 copy_tv(&di->di_tv, tv);
1852 }
1853 break;
1854
1855 case ISN_NEGATENR:
1856 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02001857 if (tv->v_type != VAR_NUMBER
1858#ifdef FEAT_FLOAT
1859 && tv->v_type != VAR_FLOAT
1860#endif
1861 )
1862 {
1863 emsg(_(e_number_exp));
1864 goto failed;
1865 }
1866#ifdef FEAT_FLOAT
1867 if (tv->v_type == VAR_FLOAT)
1868 tv->vval.v_float = -tv->vval.v_float;
1869 else
1870#endif
1871 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 break;
1873
1874 case ISN_CHECKNR:
1875 {
1876 int error = FALSE;
1877
1878 tv = STACK_TV_BOT(-1);
1879 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 (void)tv_get_number_chk(tv, &error);
1882 if (error)
1883 goto failed;
1884 }
1885 break;
1886
1887 case ISN_CHECKTYPE:
1888 {
1889 checktype_T *ct = &iptr->isn_arg.type;
1890
1891 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001892 // TODO: better type comparison
1893 if (tv->v_type != ct->ct_type
1894 && !((tv->v_type == VAR_PARTIAL
1895 && ct->ct_type == VAR_FUNC)
1896 || (tv->v_type == VAR_FUNC
1897 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 {
1899 semsg(_("E1029: Expected %s but got %s"),
1900 vartype_name(ct->ct_type),
1901 vartype_name(tv->v_type));
1902 goto failed;
1903 }
1904 }
1905 break;
1906
1907 case ISN_2BOOL:
1908 {
1909 int n;
1910
1911 tv = STACK_TV_BOT(-1);
1912 n = tv2bool(tv);
1913 if (iptr->isn_arg.number) // invert
1914 n = !n;
1915 clear_tv(tv);
1916 tv->v_type = VAR_BOOL;
1917 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
1918 }
1919 break;
1920
1921 case ISN_2STRING:
1922 {
1923 char_u *str;
1924
1925 tv = STACK_TV_BOT(iptr->isn_arg.number);
1926 if (tv->v_type != VAR_STRING)
1927 {
1928 str = typval_tostring(tv);
1929 clear_tv(tv);
1930 tv->v_type = VAR_STRING;
1931 tv->vval.v_string = str;
1932 }
1933 }
1934 break;
1935
1936 case ISN_DROP:
1937 --ectx.ec_stack.ga_len;
1938 clear_tv(STACK_TV_BOT(0));
1939 break;
1940 }
1941 }
1942
1943done:
1944 // function finished, get result from the stack.
1945 tv = STACK_TV_BOT(-1);
1946 *rettv = *tv;
1947 tv->v_type = VAR_UNKNOWN;
1948 ret = OK;
1949
1950failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001951 // When failed need to unwind the call stack.
1952 while (ectx.ec_frame != initial_frame_ptr)
1953 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001954failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02001955 current_sctx.sc_version = save_sc_version;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
1957 clear_tv(STACK_TV(idx));
1958 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01001959 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001960 return ret;
1961}
1962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963/*
1964 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01001965 * We don't really need this at runtime, but we do have tests that require it,
1966 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001967 */
1968 void
1969ex_disassemble(exarg_T *eap)
1970{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001971 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001972 char_u *fname;
1973 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 dfunc_T *dfunc;
1975 isn_T *instr;
1976 int current;
1977 int line_idx = 0;
1978 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001979 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001981 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001982 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD | TFN_NO_DEREF, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01001983 if (fname == NULL)
1984 {
1985 semsg(_(e_invarg2), eap->arg);
1986 return;
1987 }
1988
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001989 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001990 if (ufunc == NULL)
1991 {
1992 char_u *p = untrans_function_name(fname);
1993
1994 if (p != NULL)
1995 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02001996 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02001997 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01001998 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 if (ufunc == NULL)
2000 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002001 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 return;
2003 }
2004 if (ufunc->uf_dfunc_idx < 0)
2005 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002006 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 return;
2008 }
2009 if (ufunc->uf_name_exp != NULL)
2010 msg((char *)ufunc->uf_name_exp);
2011 else
2012 msg((char *)ufunc->uf_name);
2013
2014 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2015 instr = dfunc->df_instr;
2016 for (current = 0; current < dfunc->df_instr_count; ++current)
2017 {
2018 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002019 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020
2021 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2022 {
2023 if (current > prev_current)
2024 {
2025 msg_puts("\n\n");
2026 prev_current = current;
2027 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002028 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2029 if (line != NULL)
2030 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002031 }
2032
2033 switch (iptr->isn_type)
2034 {
2035 case ISN_EXEC:
2036 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2037 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002038 case ISN_EXECCONCAT:
2039 smsg("%4d EXECCONCAT %lld", current,
2040 (long long)iptr->isn_arg.number);
2041 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002042 case ISN_ECHO:
2043 {
2044 echo_T *echo = &iptr->isn_arg.echo;
2045
2046 smsg("%4d %s %d", current,
2047 echo->echo_with_white ? "ECHO" : "ECHON",
2048 echo->echo_count);
2049 }
2050 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002051 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002052 smsg("%4d EXECUTE %lld", current,
2053 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002054 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002055 case ISN_ECHOMSG:
2056 smsg("%4d ECHOMSG %lld", current,
2057 (long long)(iptr->isn_arg.number));
2058 break;
2059 case ISN_ECHOERR:
2060 smsg("%4d ECHOERR %lld", current,
2061 (long long)(iptr->isn_arg.number));
2062 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002063 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002064 case ISN_LOADOUTER:
2065 {
2066 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2067
2068 if (iptr->isn_arg.number < 0)
2069 smsg("%4d LOAD%s arg[%lld]", current, add,
2070 (long long)(iptr->isn_arg.number
2071 + STACK_FRAME_SIZE));
2072 else
2073 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002074 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002075 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 break;
2077 case ISN_LOADV:
2078 smsg("%4d LOADV v:%s", current,
2079 get_vim_var_name(iptr->isn_arg.number));
2080 break;
2081 case ISN_LOADSCRIPT:
2082 {
2083 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002084 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2086 + iptr->isn_arg.script.script_idx;
2087
2088 smsg("%4d LOADSCRIPT %s from %s", current,
2089 sv->sv_name, si->sn_name);
2090 }
2091 break;
2092 case ISN_LOADS:
2093 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002094 scriptitem_T *si = SCRIPT_ITEM(
2095 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096
2097 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002098 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 }
2100 break;
2101 case ISN_LOADG:
2102 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2103 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002104 case ISN_LOADB:
2105 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2106 break;
2107 case ISN_LOADW:
2108 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2109 break;
2110 case ISN_LOADT:
2111 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2112 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002113 case ISN_LOADOPT:
2114 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2115 break;
2116 case ISN_LOADENV:
2117 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2118 break;
2119 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002120 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002121 break;
2122
2123 case ISN_STORE:
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002124 if (iptr->isn_arg.number < 0)
2125 smsg("%4d STORE arg[%lld]", current,
Bram Moolenaar10827722020-03-23 22:53:22 +01002126 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002127 else
Bram Moolenaar10827722020-03-23 22:53:22 +01002128 smsg("%4d STORE $%lld", current,
2129 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002130 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002131 case ISN_STOREV:
2132 smsg("%4d STOREV v:%s", current,
2133 get_vim_var_name(iptr->isn_arg.number));
2134 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002135 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002136 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2137 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002138 case ISN_STOREB:
2139 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2140 break;
2141 case ISN_STOREW:
2142 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2143 break;
2144 case ISN_STORET:
2145 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2146 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002147 case ISN_STORES:
2148 {
2149 scriptitem_T *si = SCRIPT_ITEM(
2150 iptr->isn_arg.loadstore.ls_sid);
2151
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002152 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002153 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002154 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 break;
2156 case ISN_STORESCRIPT:
2157 {
2158 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002159 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2161 + iptr->isn_arg.script.script_idx;
2162
2163 smsg("%4d STORESCRIPT %s in %s", current,
2164 sv->sv_name, si->sn_name);
2165 }
2166 break;
2167 case ISN_STOREOPT:
2168 smsg("%4d STOREOPT &%s", current,
2169 iptr->isn_arg.storeopt.so_name);
2170 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002171 case ISN_STOREENV:
2172 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2173 break;
2174 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002175 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002176 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 case ISN_STORENR:
2178 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002179 iptr->isn_arg.storenr.stnr_val,
2180 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 break;
2182
2183 // constants
2184 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002185 smsg("%4d PUSHNR %lld", current,
2186 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 break;
2188 case ISN_PUSHBOOL:
2189 case ISN_PUSHSPEC:
2190 smsg("%4d PUSH %s", current,
2191 get_var_special_name(iptr->isn_arg.number));
2192 break;
2193 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002194#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002196#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 break;
2198 case ISN_PUSHS:
2199 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2200 break;
2201 case ISN_PUSHBLOB:
2202 {
2203 char_u *r;
2204 char_u numbuf[NUMBUFLEN];
2205 char_u *tofree;
2206
2207 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002208 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002209 vim_free(tofree);
2210 }
2211 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002212 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002213 {
2214 char *name = (char *)iptr->isn_arg.string;
2215
2216 smsg("%4d PUSHFUNC \"%s\"", current,
2217 name == NULL ? "[none]" : name);
2218 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002219 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002220 case ISN_PUSHCHANNEL:
2221#ifdef FEAT_JOB_CHANNEL
2222 {
2223 channel_T *channel = iptr->isn_arg.channel;
2224
2225 smsg("%4d PUSHCHANNEL %d", current,
2226 channel == NULL ? 0 : channel->ch_id);
2227 }
2228#endif
2229 break;
2230 case ISN_PUSHJOB:
2231#ifdef FEAT_JOB_CHANNEL
2232 {
2233 typval_T tv;
2234 char_u *name;
2235
2236 tv.v_type = VAR_JOB;
2237 tv.vval.v_job = iptr->isn_arg.job;
2238 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002239 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002240 }
2241#endif
2242 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 case ISN_PUSHEXC:
2244 smsg("%4d PUSH v:exception", current);
2245 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002246 case ISN_UNLET:
2247 smsg("%4d UNLET%s %s", current,
2248 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2249 iptr->isn_arg.unlet.ul_name);
2250 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002251 case ISN_UNLETENV:
2252 smsg("%4d UNLETENV%s $%s", current,
2253 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2254 iptr->isn_arg.unlet.ul_name);
2255 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002257 smsg("%4d NEWLIST size %lld", current,
2258 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 break;
2260 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002261 smsg("%4d NEWDICT size %lld", current,
2262 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263 break;
2264
2265 // function call
2266 case ISN_BCALL:
2267 {
2268 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2269
2270 smsg("%4d BCALL %s(argc %d)", current,
2271 internal_func_name(cbfunc->cbf_idx),
2272 cbfunc->cbf_argcount);
2273 }
2274 break;
2275 case ISN_DCALL:
2276 {
2277 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2278 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2279 + cdfunc->cdf_idx;
2280
2281 smsg("%4d DCALL %s(argc %d)", current,
2282 df->df_ufunc->uf_name_exp != NULL
2283 ? df->df_ufunc->uf_name_exp
2284 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2285 }
2286 break;
2287 case ISN_UCALL:
2288 {
2289 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2290
2291 smsg("%4d UCALL %s(argc %d)", current,
2292 cufunc->cuf_name, cufunc->cuf_argcount);
2293 }
2294 break;
2295 case ISN_PCALL:
2296 {
2297 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2298
2299 smsg("%4d PCALL%s (argc %d)", current,
2300 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2301 }
2302 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002303 case ISN_PCALL_END:
2304 smsg("%4d PCALL end", current);
2305 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 case ISN_RETURN:
2307 smsg("%4d RETURN", current);
2308 break;
2309 case ISN_FUNCREF:
2310 {
2311 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2312 + iptr->isn_arg.number;
2313
2314 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
2315 }
2316 break;
2317
2318 case ISN_JUMP:
2319 {
2320 char *when = "?";
2321
2322 switch (iptr->isn_arg.jump.jump_when)
2323 {
2324 case JUMP_ALWAYS:
2325 when = "JUMP";
2326 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 case JUMP_AND_KEEP_IF_TRUE:
2328 when = "JUMP_AND_KEEP_IF_TRUE";
2329 break;
2330 case JUMP_IF_FALSE:
2331 when = "JUMP_IF_FALSE";
2332 break;
2333 case JUMP_AND_KEEP_IF_FALSE:
2334 when = "JUMP_AND_KEEP_IF_FALSE";
2335 break;
2336 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002337 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 iptr->isn_arg.jump.jump_where);
2339 }
2340 break;
2341
2342 case ISN_FOR:
2343 {
2344 forloop_T *forloop = &iptr->isn_arg.forloop;
2345
2346 smsg("%4d FOR $%d -> %d", current,
2347 forloop->for_idx, forloop->for_end);
2348 }
2349 break;
2350
2351 case ISN_TRY:
2352 {
2353 try_T *try = &iptr->isn_arg.try;
2354
2355 smsg("%4d TRY catch -> %d, finally -> %d", current,
2356 try->try_catch, try->try_finally);
2357 }
2358 break;
2359 case ISN_CATCH:
2360 // TODO
2361 smsg("%4d CATCH", current);
2362 break;
2363 case ISN_ENDTRY:
2364 smsg("%4d ENDTRY", current);
2365 break;
2366 case ISN_THROW:
2367 smsg("%4d THROW", current);
2368 break;
2369
2370 // expression operations on number
2371 case ISN_OPNR:
2372 case ISN_OPFLOAT:
2373 case ISN_OPANY:
2374 {
2375 char *what;
2376 char *ins;
2377
2378 switch (iptr->isn_arg.op.op_type)
2379 {
2380 case EXPR_MULT: what = "*"; break;
2381 case EXPR_DIV: what = "/"; break;
2382 case EXPR_REM: what = "%"; break;
2383 case EXPR_SUB: what = "-"; break;
2384 case EXPR_ADD: what = "+"; break;
2385 default: what = "???"; break;
2386 }
2387 switch (iptr->isn_type)
2388 {
2389 case ISN_OPNR: ins = "OPNR"; break;
2390 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2391 case ISN_OPANY: ins = "OPANY"; break;
2392 default: ins = "???"; break;
2393 }
2394 smsg("%4d %s %s", current, ins, what);
2395 }
2396 break;
2397
2398 case ISN_COMPAREBOOL:
2399 case ISN_COMPARESPECIAL:
2400 case ISN_COMPARENR:
2401 case ISN_COMPAREFLOAT:
2402 case ISN_COMPARESTRING:
2403 case ISN_COMPAREBLOB:
2404 case ISN_COMPARELIST:
2405 case ISN_COMPAREDICT:
2406 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 case ISN_COMPAREANY:
2408 {
2409 char *p;
2410 char buf[10];
2411 char *type;
2412
2413 switch (iptr->isn_arg.op.op_type)
2414 {
2415 case EXPR_EQUAL: p = "=="; break;
2416 case EXPR_NEQUAL: p = "!="; break;
2417 case EXPR_GREATER: p = ">"; break;
2418 case EXPR_GEQUAL: p = ">="; break;
2419 case EXPR_SMALLER: p = "<"; break;
2420 case EXPR_SEQUAL: p = "<="; break;
2421 case EXPR_MATCH: p = "=~"; break;
2422 case EXPR_IS: p = "is"; break;
2423 case EXPR_ISNOT: p = "isnot"; break;
2424 case EXPR_NOMATCH: p = "!~"; break;
2425 default: p = "???"; break;
2426 }
2427 STRCPY(buf, p);
2428 if (iptr->isn_arg.op.op_ic == TRUE)
2429 strcat(buf, "?");
2430 switch(iptr->isn_type)
2431 {
2432 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2433 case ISN_COMPARESPECIAL:
2434 type = "COMPARESPECIAL"; break;
2435 case ISN_COMPARENR: type = "COMPARENR"; break;
2436 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2437 case ISN_COMPARESTRING:
2438 type = "COMPARESTRING"; break;
2439 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2440 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2441 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2442 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2444 default: type = "???"; break;
2445 }
2446
2447 smsg("%4d %s %s", current, type, buf);
2448 }
2449 break;
2450
2451 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2452 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2453
2454 // expression operations
2455 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2456 case ISN_INDEX: smsg("%4d INDEX", current); break;
2457 case ISN_MEMBER: smsg("%4d MEMBER %s", current,
2458 iptr->isn_arg.string); break;
2459 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2460
2461 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2462 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2463 vartype_name(iptr->isn_arg.type.ct_type),
2464 iptr->isn_arg.type.ct_off);
2465 break;
2466 case ISN_2BOOL: if (iptr->isn_arg.number)
2467 smsg("%4d INVERT (!val)", current);
2468 else
2469 smsg("%4d 2BOOL (!!val)", current);
2470 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002471 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2472 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 break;
2474
2475 case ISN_DROP: smsg("%4d DROP", current); break;
2476 }
2477 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478}
2479
2480/*
2481 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2482 * list, etc. Mostly like what JavaScript does, except that empty list and
2483 * empty dictionary are FALSE.
2484 */
2485 int
2486tv2bool(typval_T *tv)
2487{
2488 switch (tv->v_type)
2489 {
2490 case VAR_NUMBER:
2491 return tv->vval.v_number != 0;
2492 case VAR_FLOAT:
2493#ifdef FEAT_FLOAT
2494 return tv->vval.v_float != 0.0;
2495#else
2496 break;
2497#endif
2498 case VAR_PARTIAL:
2499 return tv->vval.v_partial != NULL;
2500 case VAR_FUNC:
2501 case VAR_STRING:
2502 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2503 case VAR_LIST:
2504 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2505 case VAR_DICT:
2506 return tv->vval.v_dict != NULL
2507 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2508 case VAR_BOOL:
2509 case VAR_SPECIAL:
2510 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2511 case VAR_JOB:
2512#ifdef FEAT_JOB_CHANNEL
2513 return tv->vval.v_job != NULL;
2514#else
2515 break;
2516#endif
2517 case VAR_CHANNEL:
2518#ifdef FEAT_JOB_CHANNEL
2519 return tv->vval.v_channel != NULL;
2520#else
2521 break;
2522#endif
2523 case VAR_BLOB:
2524 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2525 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002526 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 case VAR_VOID:
2528 break;
2529 }
2530 return FALSE;
2531}
2532
2533/*
2534 * If "tv" is a string give an error and return FAIL.
2535 */
2536 int
2537check_not_string(typval_T *tv)
2538{
2539 if (tv->v_type == VAR_STRING)
2540 {
2541 emsg(_("E1030: Using a String as a Number"));
2542 clear_tv(tv);
2543 return FAIL;
2544 }
2545 return OK;
2546}
2547
2548
2549#endif // FEAT_EVAL