blob: f6aa140653072594f7589a17bad5700bfe4cd3f1 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
163
164 if (dfunc->df_deleted)
165 {
166 emsg_funcname(e_func_deleted, ufunc->uf_name);
167 return FAIL;
168 }
169
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 if (ufunc->uf_va_name != NULL)
171 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200172 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200173 // Stack at time of call with 2 varargs:
174 // normal_arg
175 // optional_arg
176 // vararg_1
177 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200178 // After creating the list:
179 // normal_arg
180 // optional_arg
181 // vararg-list
182 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // After creating the list
185 // normal_arg
186 // (space for optional_arg)
187 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200188 vararg_count = argcount - ufunc->uf_args.ga_len;
189 if (vararg_count < 0)
190 vararg_count = 0;
191 else
192 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200193 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200195
196 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 }
198
Bram Moolenaarfe270812020-04-11 22:31:27 +0200199 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 if (arg_to_add < 0)
201 {
202 iemsg("Argument count wrong?");
203 return FAIL;
204 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200205 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
206 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100207 return FAIL;
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 // Move the vararg-list to below the missing optional arguments.
210 if (vararg_count > 0 && arg_to_add > 0)
211 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100212
213 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200215 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100217
218 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100219 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
220 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200221 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
222 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100223
224 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200225 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200227 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
228 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100229
230 // Set execution state to the start of the called function.
231 ectx->ec_dfunc_idx = cdf_idx;
232 ectx->ec_instr = dfunc->df_instr;
233 estack_push_ufunc(ETYPE_UFUNC, dfunc->df_ufunc, 1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100234
235 // Decide where to start execution, handles optional arguments.
236 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100237
238 return OK;
239}
240
241// Get pointer to item in the stack.
242#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
243
244/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200245 * Used when returning from a function: Check if any closure is still
246 * referenced. If so then move the arguments and variables to a separate piece
247 * of stack to be used when the closure is called.
248 * When "free_arguments" is TRUE the arguments are to be freed.
249 * Returns FAIL when out of memory.
250 */
251 static int
252handle_closure_in_use(ectx_T *ectx, int free_arguments)
253{
254 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
255 + ectx->ec_dfunc_idx;
256 int argcount = ufunc_argcount(dfunc->df_ufunc);
257 int top = ectx->ec_frame_idx - argcount;
258 int idx;
259 typval_T *tv;
260 int closure_in_use = FALSE;
261
262 // Check if any created closure is still in use.
263 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
264 {
265 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
266 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200267 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
268 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200269 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200270 int refcount = tv->vval.v_partial->pt_refcount;
271 int i;
272
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200273 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200274 // unreferenced on return.
275 for (i = 0; i < dfunc->df_varcount; ++i)
276 {
277 typval_T *stv = STACK_TV(ectx->ec_frame_idx
278 + STACK_FRAME_SIZE + i);
279 if (stv->v_type == VAR_PARTIAL
280 && tv->vval.v_partial == stv->vval.v_partial)
281 --refcount;
282 }
283 if (refcount > 1)
284 {
285 closure_in_use = TRUE;
286 break;
287 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200288 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200289 }
290
291 if (closure_in_use)
292 {
293 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
294 typval_T *stack;
295
296 // A closure is using the arguments and/or local variables.
297 // Move them to the called function.
298 if (funcstack == NULL)
299 return FAIL;
300 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
301 + dfunc->df_varcount;
302 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
303 funcstack->fs_ga.ga_data = stack;
304 if (stack == NULL)
305 {
306 vim_free(funcstack);
307 return FAIL;
308 }
309
310 // Move or copy the arguments.
311 for (idx = 0; idx < argcount; ++idx)
312 {
313 tv = STACK_TV(top + idx);
314 if (free_arguments)
315 {
316 *(stack + idx) = *tv;
317 tv->v_type = VAR_UNKNOWN;
318 }
319 else
320 copy_tv(tv, stack + idx);
321 }
322 // Move the local variables.
323 for (idx = 0; idx < dfunc->df_varcount; ++idx)
324 {
325 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200326
327 // Do not copy a partial created for a local function.
328 // TODO: this won't work if the closure actually uses it. But when
329 // keeping it it gets complicated: it will create a reference cycle
330 // inside the partial, thus needs special handling for garbage
331 // collection.
332 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
333 {
334 int i;
335 typval_T *ctv;
336
337 for (i = 0; i < dfunc->df_closure_count; ++i)
338 {
339 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
340 + dfunc->df_varcount + i);
341 if (tv->vval.v_partial == ctv->vval.v_partial)
342 break;
343 }
344 if (i < dfunc->df_closure_count)
345 {
346 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
347 VAR_UNKNOWN;
348 continue;
349 }
350 }
351
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200352 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
353 tv->v_type = VAR_UNKNOWN;
354 }
355
356 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
357 {
358 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
359 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200360 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200362 partial_T *partial = tv->vval.v_partial;
363
364 if (partial->pt_refcount > 1)
365 {
366 ++funcstack->fs_refcount;
367 partial->pt_funcstack = funcstack;
368 partial->pt_ectx_stack = &funcstack->fs_ga;
369 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
370 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200371 }
372 }
373 }
374
375 return OK;
376}
377
378/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100379 * Return from the current function.
380 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100382func_return(ectx_T *ectx)
383{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100384 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
386 + ectx->ec_dfunc_idx;
387 int argcount = ufunc_argcount(dfunc->df_ufunc);
388 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100389
390 // execution context goes one level up
391 estack_pop();
392
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 if (handle_closure_in_use(ectx, TRUE) == FAIL)
394 return FAIL;
395
396 // Clear the arguments.
397 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
398 clear_tv(STACK_TV(idx));
399
400 // Clear local variables and temp values, but not the return value.
401 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100402 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100404
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100405 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200406 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
407 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
408 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
410 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100411
412 // Reset the stack to the position before the call, move the return value
413 // to the top of the stack.
414 idx = ectx->ec_stack.ga_len - 1;
415 ectx->ec_stack.ga_len = top + 1;
416 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417
418 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100419}
420
421#undef STACK_TV
422
423/*
424 * Prepare arguments and rettv for calling a builtin or user function.
425 */
426 static int
427call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
428{
429 int idx;
430 typval_T *tv;
431
432 // Move arguments from bottom of the stack to argvars[] and add terminator.
433 for (idx = 0; idx < argcount; ++idx)
434 argvars[idx] = *STACK_TV_BOT(idx - argcount);
435 argvars[argcount].v_type = VAR_UNKNOWN;
436
437 // Result replaces the arguments on the stack.
438 if (argcount > 0)
439 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200440 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100441 return FAIL;
442 else
443 ++ectx->ec_stack.ga_len;
444
445 // Default return value is zero.
446 tv = STACK_TV_BOT(-1);
447 tv->v_type = VAR_NUMBER;
448 tv->vval.v_number = 0;
449
450 return OK;
451}
452
453/*
454 * Call a builtin function by index.
455 */
456 static int
457call_bfunc(int func_idx, int argcount, ectx_T *ectx)
458{
459 typval_T argvars[MAX_FUNC_ARGS];
460 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200461 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100462
463 if (call_prepare(argcount, argvars, ectx) == FAIL)
464 return FAIL;
465
466 // Call the builtin function.
467 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
468
469 // Clear the arguments.
470 for (idx = 0; idx < argcount; ++idx)
471 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200472
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200473 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200474 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475 return OK;
476}
477
478/*
479 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100480 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481 */
482 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100483call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484{
485 typval_T argvars[MAX_FUNC_ARGS];
486 funcexe_T funcexe;
487 int error;
488 int idx;
489
Bram Moolenaar822ba242020-05-24 23:00:18 +0200490 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
491 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
492 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493 if (ufunc->uf_dfunc_idx >= 0)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100494 {
495 // The function has been compiled, can call it quickly. For a function
496 // that was defined later: we can call it directly next time.
497 if (iptr != NULL)
498 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100499 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100500 iptr->isn_type = ISN_DCALL;
501 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
502 iptr->isn_arg.dfunc.cdf_argcount = argcount;
503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100504 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100505 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506
507 if (call_prepare(argcount, argvars, ectx) == FAIL)
508 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200509 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510 funcexe.evaluate = TRUE;
511
512 // Call the user function. Result goes in last position on the stack.
513 // TODO: add selfdict if there is one
514 error = call_user_func_check(ufunc, argcount, argvars,
515 STACK_TV_BOT(-1), &funcexe, NULL);
516
517 // Clear the arguments.
518 for (idx = 0; idx < argcount; ++idx)
519 clear_tv(&argvars[idx]);
520
521 if (error != FCERR_NONE)
522 {
523 user_func_error(error, ufunc->uf_name);
524 return FAIL;
525 }
526 return OK;
527}
528
529/*
530 * Execute a function by "name".
531 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100532 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 * Returns FAIL if not found without an error message.
534 */
535 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100536call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100537{
538 ufunc_T *ufunc;
539
540 if (builtin_function(name, -1))
541 {
542 int func_idx = find_internal_func(name);
543
544 if (func_idx < 0)
545 return FAIL;
546 if (check_internal_func(func_idx, argcount) == FAIL)
547 return FAIL;
548 return call_bfunc(func_idx, argcount, ectx);
549 }
550
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200551 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100552 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100553 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554
555 return FAIL;
556}
557
558 static int
559call_partial(typval_T *tv, int argcount, ectx_T *ectx)
560{
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200561 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 int called_emsg_before = called_emsg;
563
564 if (tv->v_type == VAR_PARTIAL)
565 {
566 partial_T *pt = tv->vval.v_partial;
567
568 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200569 {
570 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
571
572 // closure may need the function context where it was defined
573 ectx->ec_outer_stack = pt->pt_ectx_stack;
574 ectx->ec_outer_frame = pt->pt_ectx_frame;
575
576 return ret;
577 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100578 name = pt->pt_name;
579 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200580 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200582 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100583 {
584 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200585 semsg(_(e_unknownfunc),
586 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 return FAIL;
588 }
589 return OK;
590}
591
592/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100593 * Store "tv" in variable "name".
594 * This is for s: and g: variables.
595 */
596 static void
597store_var(char_u *name, typval_T *tv)
598{
599 funccal_entry_T entry;
600
601 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200602 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100603 restore_funccal();
604}
605
Bram Moolenaard3aac292020-04-19 14:32:17 +0200606
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100607/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608 * Execute a function by "name".
609 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100610 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611 */
612 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100613call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614{
615 int called_emsg_before = called_emsg;
616
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100617 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 && called_emsg == called_emsg_before)
619 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200620 dictitem_T *v;
621
622 v = find_var(name, NULL, FALSE);
623 if (v == NULL)
624 {
625 semsg(_(e_unknownfunc), name);
626 return FAIL;
627 }
628 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
629 {
630 semsg(_(e_unknownfunc), name);
631 return FAIL;
632 }
633 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 }
635 return OK;
636}
637
638/*
639 * Call a "def" function from old Vim script.
640 * Return OK or FAIL.
641 */
642 int
643call_def_function(
644 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200645 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200647 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100648 typval_T *rettv) // return value
649{
650 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200651 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200652 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 typval_T *tv;
654 int idx;
655 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100656 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200657 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200658 int breakcheck_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659
660// Get pointer to item in the stack.
661#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
662
663// Get pointer to item at the bottom of the stack, -1 is the bottom.
664#undef STACK_TV_BOT
665#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
666
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200667// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200668#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200670// Like STACK_TV_VAR but use the outer scope
671#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
672
Bram Moolenaar822ba242020-05-24 23:00:18 +0200673 if (ufunc->uf_dfunc_idx == UF_NOT_COMPILED
674 || (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
675 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
676 return FAIL;
677
Bram Moolenaar09689a02020-05-09 22:50:08 +0200678 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200679 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200680 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
681 + ufunc->uf_dfunc_idx;
682 if (dfunc->df_instr == NULL)
683 return FAIL;
684 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200686 CLEAR_FIELD(ectx);
687 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
688 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
689 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
690 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
692
693 // Put arguments on the stack.
694 for (idx = 0; idx < argc; ++idx)
695 {
696 copy_tv(&argv[idx], STACK_TV_BOT(0));
697 ++ectx.ec_stack.ga_len;
698 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200699
700 // Turn varargs into a list. Empty list if no args.
701 if (ufunc->uf_va_name != NULL)
702 {
703 int vararg_count = argc - ufunc->uf_args.ga_len;
704
705 if (vararg_count < 0)
706 vararg_count = 0;
707 else
708 argc -= vararg_count;
709 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200710 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200711 if (defcount > 0)
712 // Move varargs list to below missing default arguments.
713 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
714 --ectx.ec_stack.ga_len;
715 }
716
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100717 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200718 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100719 if (defcount > 0)
720 for (idx = 0; idx < defcount; ++idx)
721 {
722 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
723 ++ectx.ec_stack.ga_len;
724 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200725 if (ufunc->uf_va_name != NULL)
726 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727
728 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200729 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
730 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200732 if (partial != NULL)
733 {
734 ectx.ec_outer_stack = partial->pt_ectx_stack;
735 ectx.ec_outer_frame = partial->pt_ectx_frame;
736 }
737
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 // dummy frame entries
739 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
740 {
741 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
742 ++ectx.ec_stack.ga_len;
743 }
744
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200745 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200746 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200747 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
748 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200749 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100750
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200751 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200752 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200753 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200754
755 ectx.ec_instr = dfunc->df_instr;
756 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100757
Bram Moolenaara26b9702020-04-18 19:53:28 +0200758 // Commands behave like vim9script.
759 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
760
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100761 // Decide where to start execution, handles optional arguments.
762 init_instr_idx(ufunc, argc, &ectx);
763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764 for (;;)
765 {
766 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100767
Bram Moolenaar270d0382020-05-15 21:42:53 +0200768 if (++breakcheck_count >= 100)
769 {
770 line_breakcheck();
771 breakcheck_count = 0;
772 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100773 if (got_int)
774 {
775 // Turn CTRL-C into an exception.
776 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100777 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100778 goto failed;
779 did_throw = TRUE;
780 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100781
Bram Moolenaara26b9702020-04-18 19:53:28 +0200782 if (did_emsg && msg_list != NULL && *msg_list != NULL)
783 {
784 // Turn an error message into an exception.
785 did_emsg = FALSE;
786 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
787 goto failed;
788 did_throw = TRUE;
789 *msg_list = NULL;
790 }
791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100792 if (did_throw && !ectx.ec_in_catch)
793 {
794 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100795 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796
797 // An exception jumps to the first catch, finally, or returns from
798 // the current function.
799 if (trystack->ga_len > 0)
800 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200801 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 {
803 // jump to ":catch" or ":finally"
804 ectx.ec_in_catch = TRUE;
805 ectx.ec_iidx = trycmd->tcd_catch_idx;
806 }
807 else
808 {
809 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200810 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100811 {
812 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200813 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100814 goto failed;
815 tv = STACK_TV_BOT(0);
816 tv->v_type = VAR_NUMBER;
817 tv->vval.v_number = 0;
818 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100819 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200820 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
821 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822 goto done;
823 }
824
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200825 if (func_return(&ectx) == FAIL)
826 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 }
828 continue;
829 }
830
831 iptr = &ectx.ec_instr[ectx.ec_iidx++];
832 switch (iptr->isn_type)
833 {
834 // execute Ex command line
835 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200836 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 do_cmdline_cmd(iptr->isn_arg.string);
838 break;
839
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200840 // execute Ex command from pieces on the stack
841 case ISN_EXECCONCAT:
842 {
843 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200844 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200845 int pass;
846 int i;
847 char_u *cmd = NULL;
848 char_u *str;
849
850 for (pass = 1; pass <= 2; ++pass)
851 {
852 for (i = 0; i < count; ++i)
853 {
854 tv = STACK_TV_BOT(i - count);
855 str = tv->vval.v_string;
856 if (str != NULL && *str != NUL)
857 {
858 if (pass == 2)
859 STRCPY(cmd + len, str);
860 len += STRLEN(str);
861 }
862 if (pass == 2)
863 clear_tv(tv);
864 }
865 if (pass == 1)
866 {
867 cmd = alloc(len + 1);
868 if (cmd == NULL)
869 goto failed;
870 len = 0;
871 }
872 }
873
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200875 do_cmdline_cmd(cmd);
876 vim_free(cmd);
877 }
878 break;
879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100880 // execute :echo {string} ...
881 case ISN_ECHO:
882 {
883 int count = iptr->isn_arg.echo.echo_count;
884 int atstart = TRUE;
885 int needclr = TRUE;
886
887 for (idx = 0; idx < count; ++idx)
888 {
889 tv = STACK_TV_BOT(idx - count);
890 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
891 &atstart, &needclr);
892 clear_tv(tv);
893 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100894 if (needclr)
895 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896 ectx.ec_stack.ga_len -= count;
897 }
898 break;
899
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200900 // :execute {string} ...
901 // :echomsg {string} ...
902 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100903 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200904 case ISN_ECHOMSG:
905 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100906 {
907 int count = iptr->isn_arg.number;
908 garray_T ga;
909 char_u buf[NUMBUFLEN];
910 char_u *p;
911 int len;
912 int failed = FALSE;
913
914 ga_init2(&ga, 1, 80);
915 for (idx = 0; idx < count; ++idx)
916 {
917 tv = STACK_TV_BOT(idx - count);
918 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
919 {
920 emsg(_(e_inval_string));
921 break;
922 }
923 else
924 p = tv_get_string_buf(tv, buf);
925
926 len = (int)STRLEN(p);
927 if (ga_grow(&ga, len + 2) == FAIL)
928 failed = TRUE;
929 else
930 {
931 if (ga.ga_len > 0)
932 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
933 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
934 ga.ga_len += len;
935 }
936 clear_tv(tv);
937 }
938 ectx.ec_stack.ga_len -= count;
939
940 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200941 {
942 if (iptr->isn_type == ISN_EXECUTE)
943 do_cmdline_cmd((char_u *)ga.ga_data);
944 else
945 {
946 msg_sb_eol();
947 if (iptr->isn_type == ISN_ECHOMSG)
948 {
949 msg_attr(ga.ga_data, echo_attr);
950 out_flush();
951 }
952 else
953 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200954 SOURCING_LNUM = iptr->isn_lnum;
955 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200956 }
957 }
958 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100959 ga_clear(&ga);
960 }
961 break;
962
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963 // load local variable or argument
964 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200965 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966 goto failed;
967 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
968 ++ectx.ec_stack.ga_len;
969 break;
970
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200971 // load variable or argument from outer scope
972 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200973 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200974 goto failed;
975 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
976 STACK_TV_BOT(0));
977 ++ectx.ec_stack.ga_len;
978 break;
979
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 // load v: variable
981 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200982 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 goto failed;
984 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
985 ++ectx.ec_stack.ga_len;
986 break;
987
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100988 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 case ISN_LOADSCRIPT:
990 {
991 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100992 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100993 svar_T *sv;
994
995 sv = ((svar_T *)si->sn_var_vals.ga_data)
996 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200997 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100998 goto failed;
999 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1000 ++ectx.ec_stack.ga_len;
1001 }
1002 break;
1003
1004 // load s: variable in old script
1005 case ISN_LOADS:
1006 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001007 hashtab_T *ht = &SCRIPT_VARS(
1008 iptr->isn_arg.loadstore.ls_sid);
1009 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001010 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001011
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001012 if (di == NULL)
1013 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001014 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001015 goto failed;
1016 }
1017 else
1018 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001019 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 goto failed;
1021 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1022 ++ectx.ec_stack.ga_len;
1023 }
1024 }
1025 break;
1026
Bram Moolenaard3aac292020-04-19 14:32:17 +02001027 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001029 case ISN_LOADB:
1030 case ISN_LOADW:
1031 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001032 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001033 dictitem_T *di = NULL;
1034 hashtab_T *ht = NULL;
1035 char namespace;
1036 switch (iptr->isn_type)
1037 {
1038 case ISN_LOADG:
1039 ht = get_globvar_ht();
1040 namespace = 'g';
1041 break;
1042 case ISN_LOADB:
1043 ht = &curbuf->b_vars->dv_hashtab;
1044 namespace = 'b';
1045 break;
1046 case ISN_LOADW:
1047 ht = &curwin->w_vars->dv_hashtab;
1048 namespace = 'w';
1049 break;
1050 case ISN_LOADT:
1051 ht = &curtab->tp_vars->dv_hashtab;
1052 namespace = 't';
1053 break;
1054 default: // Cannot reach here
1055 goto failed;
1056 }
1057 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001059 if (di == NULL)
1060 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001061 semsg(_("E121: Undefined variable: %c:%s"),
1062 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001063 goto failed;
1064 }
1065 else
1066 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001067 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 goto failed;
1069 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1070 ++ectx.ec_stack.ga_len;
1071 }
1072 }
1073 break;
1074
1075 // load &option
1076 case ISN_LOADOPT:
1077 {
1078 typval_T optval;
1079 char_u *name = iptr->isn_arg.string;
1080
Bram Moolenaara8c17702020-04-01 21:17:24 +02001081 // This is not expected to fail, name is checked during
1082 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001083 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001085 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1086 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 *STACK_TV_BOT(0) = optval;
1088 ++ectx.ec_stack.ga_len;
1089 }
1090 break;
1091
1092 // load $ENV
1093 case ISN_LOADENV:
1094 {
1095 typval_T optval;
1096 char_u *name = iptr->isn_arg.string;
1097
Bram Moolenaar270d0382020-05-15 21:42:53 +02001098 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001100 // name is always valid, checked when compiling
1101 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 *STACK_TV_BOT(0) = optval;
1103 ++ectx.ec_stack.ga_len;
1104 }
1105 break;
1106
1107 // load @register
1108 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001109 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 goto failed;
1111 tv = STACK_TV_BOT(0);
1112 tv->v_type = VAR_STRING;
1113 tv->vval.v_string = get_reg_contents(
1114 iptr->isn_arg.number, GREG_EXPR_SRC);
1115 ++ectx.ec_stack.ga_len;
1116 break;
1117
1118 // store local variable
1119 case ISN_STORE:
1120 --ectx.ec_stack.ga_len;
1121 tv = STACK_TV_VAR(iptr->isn_arg.number);
1122 clear_tv(tv);
1123 *tv = *STACK_TV_BOT(0);
1124 break;
1125
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001126 // store variable or argument in outer scope
1127 case ISN_STOREOUTER:
1128 --ectx.ec_stack.ga_len;
1129 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1130 clear_tv(tv);
1131 *tv = *STACK_TV_BOT(0);
1132 break;
1133
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001134 // store s: variable in old script
1135 case ISN_STORES:
1136 {
1137 hashtab_T *ht = &SCRIPT_VARS(
1138 iptr->isn_arg.loadstore.ls_sid);
1139 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001140 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001142 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001143 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001144 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001145 else
1146 {
1147 clear_tv(&di->di_tv);
1148 di->di_tv = *STACK_TV_BOT(0);
1149 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001150 }
1151 break;
1152
1153 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001154 case ISN_STORESCRIPT:
1155 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001156 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001157 iptr->isn_arg.script.script_sid);
1158 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1159 + iptr->isn_arg.script.script_idx;
1160
1161 --ectx.ec_stack.ga_len;
1162 clear_tv(sv->sv_tv);
1163 *sv->sv_tv = *STACK_TV_BOT(0);
1164 }
1165 break;
1166
1167 // store option
1168 case ISN_STOREOPT:
1169 {
1170 long n = 0;
1171 char_u *s = NULL;
1172 char *msg;
1173
1174 --ectx.ec_stack.ga_len;
1175 tv = STACK_TV_BOT(0);
1176 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001177 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001179 if (s == NULL)
1180 s = (char_u *)"";
1181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001183 // must be VAR_NUMBER, CHECKTYPE makes sure
1184 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1186 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001187 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188 if (msg != NULL)
1189 {
1190 emsg(_(msg));
1191 goto failed;
1192 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001193 }
1194 break;
1195
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001196 // store $ENV
1197 case ISN_STOREENV:
1198 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001199 tv = STACK_TV_BOT(0);
1200 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1201 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001202 break;
1203
1204 // store @r
1205 case ISN_STOREREG:
1206 {
1207 int reg = iptr->isn_arg.number;
1208
1209 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001210 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001211 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001212 tv_get_string(tv), -1, FALSE);
1213 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001214 }
1215 break;
1216
1217 // store v: variable
1218 case ISN_STOREV:
1219 --ectx.ec_stack.ga_len;
1220 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1221 == FAIL)
1222 goto failed;
1223 break;
1224
Bram Moolenaard3aac292020-04-19 14:32:17 +02001225 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001226 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001227 case ISN_STOREB:
1228 case ISN_STOREW:
1229 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 {
1231 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001232 hashtab_T *ht;
1233 switch (iptr->isn_type)
1234 {
1235 case ISN_STOREG:
1236 ht = get_globvar_ht();
1237 break;
1238 case ISN_STOREB:
1239 ht = &curbuf->b_vars->dv_hashtab;
1240 break;
1241 case ISN_STOREW:
1242 ht = &curwin->w_vars->dv_hashtab;
1243 break;
1244 case ISN_STORET:
1245 ht = &curtab->tp_vars->dv_hashtab;
1246 break;
1247 default: // Cannot reach here
1248 goto failed;
1249 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250
1251 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001252 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001253 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001254 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 else
1256 {
1257 clear_tv(&di->di_tv);
1258 di->di_tv = *STACK_TV_BOT(0);
1259 }
1260 }
1261 break;
1262
1263 // store number in local variable
1264 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001265 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 clear_tv(tv);
1267 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001268 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 break;
1270
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001271 // store value in list variable
1272 case ISN_STORELIST:
1273 {
1274 typval_T *tv_idx = STACK_TV_BOT(-2);
1275 varnumber_T lidx = tv_idx->vval.v_number;
1276 typval_T *tv_list = STACK_TV_BOT(-1);
1277 list_T *list = tv_list->vval.v_list;
1278
1279 if (lidx < 0 && list->lv_len + lidx >= 0)
1280 // negative index is relative to the end
1281 lidx = list->lv_len + lidx;
1282 if (lidx < 0 || lidx > list->lv_len)
1283 {
1284 semsg(_(e_listidx), lidx);
1285 goto failed;
1286 }
1287 tv = STACK_TV_BOT(-3);
1288 if (lidx < list->lv_len)
1289 {
1290 listitem_T *li = list_find(list, lidx);
1291
1292 // overwrite existing list item
1293 clear_tv(&li->li_tv);
1294 li->li_tv = *tv;
1295 }
1296 else
1297 {
1298 // append to list
1299 if (list_append_tv(list, tv) == FAIL)
1300 goto failed;
1301 clear_tv(tv);
1302 }
1303 clear_tv(tv_idx);
1304 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001305 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001306 }
1307 break;
1308
1309 // store value in dict variable
1310 case ISN_STOREDICT:
1311 {
1312 typval_T *tv_key = STACK_TV_BOT(-2);
1313 char_u *key = tv_key->vval.v_string;
1314 typval_T *tv_dict = STACK_TV_BOT(-1);
1315 dict_T *dict = tv_dict->vval.v_dict;
1316 dictitem_T *di;
1317
1318 if (key == NULL || *key == NUL)
1319 {
1320 emsg(_(e_emptykey));
1321 goto failed;
1322 }
1323 tv = STACK_TV_BOT(-3);
1324 di = dict_find(dict, key, -1);
1325 if (di != NULL)
1326 {
1327 clear_tv(&di->di_tv);
1328 di->di_tv = *tv;
1329 }
1330 else
1331 {
1332 // add to dict
1333 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1334 goto failed;
1335 clear_tv(tv);
1336 }
1337 clear_tv(tv_key);
1338 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001339 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001340 }
1341 break;
1342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 // push constant
1344 case ISN_PUSHNR:
1345 case ISN_PUSHBOOL:
1346 case ISN_PUSHSPEC:
1347 case ISN_PUSHF:
1348 case ISN_PUSHS:
1349 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001350 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001351 case ISN_PUSHCHANNEL:
1352 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001353 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 goto failed;
1355 tv = STACK_TV_BOT(0);
1356 ++ectx.ec_stack.ga_len;
1357 switch (iptr->isn_type)
1358 {
1359 case ISN_PUSHNR:
1360 tv->v_type = VAR_NUMBER;
1361 tv->vval.v_number = iptr->isn_arg.number;
1362 break;
1363 case ISN_PUSHBOOL:
1364 tv->v_type = VAR_BOOL;
1365 tv->vval.v_number = iptr->isn_arg.number;
1366 break;
1367 case ISN_PUSHSPEC:
1368 tv->v_type = VAR_SPECIAL;
1369 tv->vval.v_number = iptr->isn_arg.number;
1370 break;
1371#ifdef FEAT_FLOAT
1372 case ISN_PUSHF:
1373 tv->v_type = VAR_FLOAT;
1374 tv->vval.v_float = iptr->isn_arg.fnumber;
1375 break;
1376#endif
1377 case ISN_PUSHBLOB:
1378 blob_copy(iptr->isn_arg.blob, tv);
1379 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001380 case ISN_PUSHFUNC:
1381 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001382 if (iptr->isn_arg.string == NULL)
1383 tv->vval.v_string = NULL;
1384 else
1385 tv->vval.v_string =
1386 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001387 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001388 case ISN_PUSHCHANNEL:
1389#ifdef FEAT_JOB_CHANNEL
1390 tv->v_type = VAR_CHANNEL;
1391 tv->vval.v_channel = iptr->isn_arg.channel;
1392 if (tv->vval.v_channel != NULL)
1393 ++tv->vval.v_channel->ch_refcount;
1394#endif
1395 break;
1396 case ISN_PUSHJOB:
1397#ifdef FEAT_JOB_CHANNEL
1398 tv->v_type = VAR_JOB;
1399 tv->vval.v_job = iptr->isn_arg.job;
1400 if (tv->vval.v_job != NULL)
1401 ++tv->vval.v_job->jv_refcount;
1402#endif
1403 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 default:
1405 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001406 tv->vval.v_string = vim_strsave(
1407 iptr->isn_arg.string == NULL
1408 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 }
1410 break;
1411
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001412 case ISN_UNLET:
1413 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1414 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1415 goto failed;
1416 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001417 case ISN_UNLETENV:
1418 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1419 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 // create a list from items on the stack; uses a single allocation
1422 // for the list header and the items
1423 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001424 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1425 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001426 break;
1427
1428 // create a dict from items on the stack
1429 case ISN_NEWDICT:
1430 {
1431 int count = iptr->isn_arg.number;
1432 dict_T *dict = dict_alloc();
1433 dictitem_T *item;
1434
1435 if (dict == NULL)
1436 goto failed;
1437 for (idx = 0; idx < count; ++idx)
1438 {
1439 // check key type is VAR_STRING
1440 tv = STACK_TV_BOT(2 * (idx - count));
1441 item = dictitem_alloc(tv->vval.v_string);
1442 clear_tv(tv);
1443 if (item == NULL)
1444 goto failed;
1445 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1446 item->di_tv.v_lock = 0;
1447 if (dict_add(dict, item) == FAIL)
1448 goto failed;
1449 }
1450
1451 if (count > 0)
1452 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001453 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 goto failed;
1455 else
1456 ++ectx.ec_stack.ga_len;
1457 tv = STACK_TV_BOT(-1);
1458 tv->v_type = VAR_DICT;
1459 tv->vval.v_dict = dict;
1460 ++dict->dv_refcount;
1461 }
1462 break;
1463
1464 // call a :def function
1465 case ISN_DCALL:
1466 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1467 iptr->isn_arg.dfunc.cdf_argcount,
1468 &ectx) == FAIL)
1469 goto failed;
1470 break;
1471
1472 // call a builtin function
1473 case ISN_BCALL:
1474 SOURCING_LNUM = iptr->isn_lnum;
1475 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1476 iptr->isn_arg.bfunc.cbf_argcount,
1477 &ectx) == FAIL)
1478 goto failed;
1479 break;
1480
1481 // call a funcref or partial
1482 case ISN_PCALL:
1483 {
1484 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1485 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001486 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001487
1488 SOURCING_LNUM = iptr->isn_lnum;
1489 if (pfunc->cpf_top)
1490 {
1491 // funcref is above the arguments
1492 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1493 }
1494 else
1495 {
1496 // Get the funcref from the stack.
1497 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001498 partial_tv = *STACK_TV_BOT(0);
1499 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 }
1501 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001502 if (tv == &partial_tv)
1503 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 if (r == FAIL)
1505 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 }
1507 break;
1508
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001509 case ISN_PCALL_END:
1510 // PCALL finished, arguments have been consumed and replaced by
1511 // the return value. Now clear the funcref from the stack,
1512 // and move the return value in its place.
1513 --ectx.ec_stack.ga_len;
1514 clear_tv(STACK_TV_BOT(-1));
1515 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1516 break;
1517
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 // call a user defined function or funcref/partial
1519 case ISN_UCALL:
1520 {
1521 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1522
1523 SOURCING_LNUM = iptr->isn_lnum;
1524 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001525 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 goto failed;
1527 }
1528 break;
1529
1530 // return from a :def function call
1531 case ISN_RETURN:
1532 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001533 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001534 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001535
1536 if (trystack->ga_len > 0)
1537 trycmd = ((trycmd_T *)trystack->ga_data)
1538 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001539 if (trycmd != NULL
1540 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 && trycmd->tcd_finally_idx != 0)
1542 {
1543 // jump to ":finally"
1544 ectx.ec_iidx = trycmd->tcd_finally_idx;
1545 trycmd->tcd_return = TRUE;
1546 }
1547 else
1548 {
1549 // Restore previous function. If the frame pointer
1550 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001551 if (ectx.ec_frame_idx == initial_frame_idx)
1552 {
1553 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1554 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001555 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001556 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001558 if (func_return(&ectx) == FAIL)
1559 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001560 }
1561 }
1562 break;
1563
1564 // push a function reference to a compiled function
1565 case ISN_FUNCREF:
1566 {
1567 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001568 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569
1570 pt = ALLOC_CLEAR_ONE(partial_T);
1571 if (pt == NULL)
1572 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001573 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001574 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001575 vim_free(pt);
1576 goto failed;
1577 }
1578 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1579 + iptr->isn_arg.funcref.fr_func;
1580 pt->pt_func = pt_dfunc->df_ufunc;
1581 pt->pt_refcount = 1;
1582 ++pt_dfunc->df_ufunc->uf_refcount;
1583
1584 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1585 {
1586 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1587 + ectx.ec_dfunc_idx;
1588
1589 // The closure needs to find arguments and local
1590 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001591 pt->pt_ectx_stack = &ectx.ec_stack;
1592 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001593
1594 // If this function returns and the closure is still
1595 // used, we need to make a copy of the context
1596 // (arguments and local variables). Store a reference
1597 // to the partial so we can handle that.
1598 ++pt->pt_refcount;
1599 tv = STACK_TV_VAR(dfunc->df_varcount
1600 + iptr->isn_arg.funcref.fr_var_idx);
1601 if (tv->v_type == VAR_PARTIAL)
1602 {
1603 // TODO: use a garray_T on ectx.
1604 emsg("Multiple closures not supported yet");
1605 goto failed;
1606 }
1607 tv->v_type = VAR_PARTIAL;
1608 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001609 }
1610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 tv = STACK_TV_BOT(0);
1612 ++ectx.ec_stack.ga_len;
1613 tv->vval.v_partial = pt;
1614 tv->v_type = VAR_PARTIAL;
1615 }
1616 break;
1617
1618 // jump if a condition is met
1619 case ISN_JUMP:
1620 {
1621 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1622 int jump = TRUE;
1623
1624 if (when != JUMP_ALWAYS)
1625 {
1626 tv = STACK_TV_BOT(-1);
1627 jump = tv2bool(tv);
1628 if (when == JUMP_IF_FALSE
1629 || when == JUMP_AND_KEEP_IF_FALSE)
1630 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001631 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 {
1633 // drop the value from the stack
1634 clear_tv(tv);
1635 --ectx.ec_stack.ga_len;
1636 }
1637 }
1638 if (jump)
1639 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1640 }
1641 break;
1642
1643 // top of a for loop
1644 case ISN_FOR:
1645 {
1646 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1647 typval_T *idxtv =
1648 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1649
1650 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001651 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 goto failed;
1653 if (++idxtv->vval.v_number >= list->lv_len)
1654 // past the end of the list, jump to "endfor"
1655 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1656 else if (list->lv_first == &range_list_item)
1657 {
1658 // non-materialized range() list
1659 tv = STACK_TV_BOT(0);
1660 tv->v_type = VAR_NUMBER;
1661 tv->vval.v_number = list_find_nr(
1662 list, idxtv->vval.v_number, NULL);
1663 ++ectx.ec_stack.ga_len;
1664 }
1665 else
1666 {
1667 listitem_T *li = list_find(list, idxtv->vval.v_number);
1668
1669 if (li == NULL)
1670 goto failed;
1671 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1672 ++ectx.ec_stack.ga_len;
1673 }
1674 }
1675 break;
1676
1677 // start of ":try" block
1678 case ISN_TRY:
1679 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001680 trycmd_T *trycmd = NULL;
1681
Bram Moolenaar270d0382020-05-15 21:42:53 +02001682 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001683 goto failed;
1684 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1685 + ectx.ec_trystack.ga_len;
1686 ++ectx.ec_trystack.ga_len;
1687 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001688 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1690 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001691 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001692 }
1693 break;
1694
1695 case ISN_PUSHEXC:
1696 if (current_exception == NULL)
1697 {
1698 iemsg("Evaluating catch while current_exception is NULL");
1699 goto failed;
1700 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001701 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 goto failed;
1703 tv = STACK_TV_BOT(0);
1704 ++ectx.ec_stack.ga_len;
1705 tv->v_type = VAR_STRING;
1706 tv->vval.v_string = vim_strsave(
1707 (char_u *)current_exception->value);
1708 break;
1709
1710 case ISN_CATCH:
1711 {
1712 garray_T *trystack = &ectx.ec_trystack;
1713
1714 if (trystack->ga_len > 0)
1715 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001716 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717 + trystack->ga_len - 1;
1718 trycmd->tcd_caught = TRUE;
1719 }
1720 did_emsg = got_int = did_throw = FALSE;
1721 catch_exception(current_exception);
1722 }
1723 break;
1724
1725 // end of ":try" block
1726 case ISN_ENDTRY:
1727 {
1728 garray_T *trystack = &ectx.ec_trystack;
1729
1730 if (trystack->ga_len > 0)
1731 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001732 trycmd_T *trycmd = NULL;
1733
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 --trystack->ga_len;
1735 --trylevel;
1736 trycmd = ((trycmd_T *)trystack->ga_data)
1737 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001738 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 {
1740 // discard the exception
1741 if (caught_stack == current_exception)
1742 caught_stack = caught_stack->caught;
1743 discard_current_exception();
1744 }
1745
1746 if (trycmd->tcd_return)
1747 {
1748 // Restore previous function. If the frame pointer
1749 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001750 if (ectx.ec_frame_idx == initial_frame_idx)
1751 {
1752 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1753 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001757 if (func_return(&ectx) == FAIL)
1758 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 }
1760 }
1761 }
1762 break;
1763
1764 case ISN_THROW:
1765 --ectx.ec_stack.ga_len;
1766 tv = STACK_TV_BOT(0);
1767 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1768 {
1769 vim_free(tv->vval.v_string);
1770 goto failed;
1771 }
1772 did_throw = TRUE;
1773 break;
1774
1775 // compare with special values
1776 case ISN_COMPAREBOOL:
1777 case ISN_COMPARESPECIAL:
1778 {
1779 typval_T *tv1 = STACK_TV_BOT(-2);
1780 typval_T *tv2 = STACK_TV_BOT(-1);
1781 varnumber_T arg1 = tv1->vval.v_number;
1782 varnumber_T arg2 = tv2->vval.v_number;
1783 int res;
1784
1785 switch (iptr->isn_arg.op.op_type)
1786 {
1787 case EXPR_EQUAL: res = arg1 == arg2; break;
1788 case EXPR_NEQUAL: res = arg1 != arg2; break;
1789 default: res = 0; break;
1790 }
1791
1792 --ectx.ec_stack.ga_len;
1793 tv1->v_type = VAR_BOOL;
1794 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1795 }
1796 break;
1797
1798 // Operation with two number arguments
1799 case ISN_OPNR:
1800 case ISN_COMPARENR:
1801 {
1802 typval_T *tv1 = STACK_TV_BOT(-2);
1803 typval_T *tv2 = STACK_TV_BOT(-1);
1804 varnumber_T arg1 = tv1->vval.v_number;
1805 varnumber_T arg2 = tv2->vval.v_number;
1806 varnumber_T res;
1807
1808 switch (iptr->isn_arg.op.op_type)
1809 {
1810 case EXPR_MULT: res = arg1 * arg2; break;
1811 case EXPR_DIV: res = arg1 / arg2; break;
1812 case EXPR_REM: res = arg1 % arg2; break;
1813 case EXPR_SUB: res = arg1 - arg2; break;
1814 case EXPR_ADD: res = arg1 + arg2; break;
1815
1816 case EXPR_EQUAL: res = arg1 == arg2; break;
1817 case EXPR_NEQUAL: res = arg1 != arg2; break;
1818 case EXPR_GREATER: res = arg1 > arg2; break;
1819 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1820 case EXPR_SMALLER: res = arg1 < arg2; break;
1821 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1822 default: res = 0; break;
1823 }
1824
1825 --ectx.ec_stack.ga_len;
1826 if (iptr->isn_type == ISN_COMPARENR)
1827 {
1828 tv1->v_type = VAR_BOOL;
1829 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1830 }
1831 else
1832 tv1->vval.v_number = res;
1833 }
1834 break;
1835
1836 // Computation with two float arguments
1837 case ISN_OPFLOAT:
1838 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001839#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 {
1841 typval_T *tv1 = STACK_TV_BOT(-2);
1842 typval_T *tv2 = STACK_TV_BOT(-1);
1843 float_T arg1 = tv1->vval.v_float;
1844 float_T arg2 = tv2->vval.v_float;
1845 float_T res = 0;
1846 int cmp = FALSE;
1847
1848 switch (iptr->isn_arg.op.op_type)
1849 {
1850 case EXPR_MULT: res = arg1 * arg2; break;
1851 case EXPR_DIV: res = arg1 / arg2; break;
1852 case EXPR_SUB: res = arg1 - arg2; break;
1853 case EXPR_ADD: res = arg1 + arg2; break;
1854
1855 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1856 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1857 case EXPR_GREATER: cmp = arg1 > arg2; break;
1858 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1859 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1860 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1861 default: cmp = 0; break;
1862 }
1863 --ectx.ec_stack.ga_len;
1864 if (iptr->isn_type == ISN_COMPAREFLOAT)
1865 {
1866 tv1->v_type = VAR_BOOL;
1867 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1868 }
1869 else
1870 tv1->vval.v_float = res;
1871 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001872#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 break;
1874
1875 case ISN_COMPARELIST:
1876 {
1877 typval_T *tv1 = STACK_TV_BOT(-2);
1878 typval_T *tv2 = STACK_TV_BOT(-1);
1879 list_T *arg1 = tv1->vval.v_list;
1880 list_T *arg2 = tv2->vval.v_list;
1881 int cmp = FALSE;
1882 int ic = iptr->isn_arg.op.op_ic;
1883
1884 switch (iptr->isn_arg.op.op_type)
1885 {
1886 case EXPR_EQUAL: cmp =
1887 list_equal(arg1, arg2, ic, FALSE); break;
1888 case EXPR_NEQUAL: cmp =
1889 !list_equal(arg1, arg2, ic, FALSE); break;
1890 case EXPR_IS: cmp = arg1 == arg2; break;
1891 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1892 default: cmp = 0; break;
1893 }
1894 --ectx.ec_stack.ga_len;
1895 clear_tv(tv1);
1896 clear_tv(tv2);
1897 tv1->v_type = VAR_BOOL;
1898 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1899 }
1900 break;
1901
1902 case ISN_COMPAREBLOB:
1903 {
1904 typval_T *tv1 = STACK_TV_BOT(-2);
1905 typval_T *tv2 = STACK_TV_BOT(-1);
1906 blob_T *arg1 = tv1->vval.v_blob;
1907 blob_T *arg2 = tv2->vval.v_blob;
1908 int cmp = FALSE;
1909
1910 switch (iptr->isn_arg.op.op_type)
1911 {
1912 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1913 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1914 case EXPR_IS: cmp = arg1 == arg2; break;
1915 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1916 default: cmp = 0; break;
1917 }
1918 --ectx.ec_stack.ga_len;
1919 clear_tv(tv1);
1920 clear_tv(tv2);
1921 tv1->v_type = VAR_BOOL;
1922 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1923 }
1924 break;
1925
1926 // TODO: handle separately
1927 case ISN_COMPARESTRING:
1928 case ISN_COMPAREDICT:
1929 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 case ISN_COMPAREANY:
1931 {
1932 typval_T *tv1 = STACK_TV_BOT(-2);
1933 typval_T *tv2 = STACK_TV_BOT(-1);
1934 exptype_T exptype = iptr->isn_arg.op.op_type;
1935 int ic = iptr->isn_arg.op.op_ic;
1936
1937 typval_compare(tv1, tv2, exptype, ic);
1938 clear_tv(tv2);
1939 tv1->v_type = VAR_BOOL;
1940 tv1->vval.v_number = tv1->vval.v_number
1941 ? VVAL_TRUE : VVAL_FALSE;
1942 --ectx.ec_stack.ga_len;
1943 }
1944 break;
1945
1946 case ISN_ADDLIST:
1947 case ISN_ADDBLOB:
1948 {
1949 typval_T *tv1 = STACK_TV_BOT(-2);
1950 typval_T *tv2 = STACK_TV_BOT(-1);
1951
1952 if (iptr->isn_type == ISN_ADDLIST)
1953 eval_addlist(tv1, tv2);
1954 else
1955 eval_addblob(tv1, tv2);
1956 clear_tv(tv2);
1957 --ectx.ec_stack.ga_len;
1958 }
1959 break;
1960
1961 // Computation with two arguments of unknown type
1962 case ISN_OPANY:
1963 {
1964 typval_T *tv1 = STACK_TV_BOT(-2);
1965 typval_T *tv2 = STACK_TV_BOT(-1);
1966 varnumber_T n1, n2;
1967#ifdef FEAT_FLOAT
1968 float_T f1 = 0, f2 = 0;
1969#endif
1970 int error = FALSE;
1971
1972 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1973 {
1974 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1975 {
1976 eval_addlist(tv1, tv2);
1977 clear_tv(tv2);
1978 --ectx.ec_stack.ga_len;
1979 break;
1980 }
1981 else if (tv1->v_type == VAR_BLOB
1982 && tv2->v_type == VAR_BLOB)
1983 {
1984 eval_addblob(tv1, tv2);
1985 clear_tv(tv2);
1986 --ectx.ec_stack.ga_len;
1987 break;
1988 }
1989 }
1990#ifdef FEAT_FLOAT
1991 if (tv1->v_type == VAR_FLOAT)
1992 {
1993 f1 = tv1->vval.v_float;
1994 n1 = 0;
1995 }
1996 else
1997#endif
1998 {
1999 n1 = tv_get_number_chk(tv1, &error);
2000 if (error)
2001 goto failed;
2002#ifdef FEAT_FLOAT
2003 if (tv2->v_type == VAR_FLOAT)
2004 f1 = n1;
2005#endif
2006 }
2007#ifdef FEAT_FLOAT
2008 if (tv2->v_type == VAR_FLOAT)
2009 {
2010 f2 = tv2->vval.v_float;
2011 n2 = 0;
2012 }
2013 else
2014#endif
2015 {
2016 n2 = tv_get_number_chk(tv2, &error);
2017 if (error)
2018 goto failed;
2019#ifdef FEAT_FLOAT
2020 if (tv1->v_type == VAR_FLOAT)
2021 f2 = n2;
2022#endif
2023 }
2024#ifdef FEAT_FLOAT
2025 // if there is a float on either side the result is a float
2026 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2027 {
2028 switch (iptr->isn_arg.op.op_type)
2029 {
2030 case EXPR_MULT: f1 = f1 * f2; break;
2031 case EXPR_DIV: f1 = f1 / f2; break;
2032 case EXPR_SUB: f1 = f1 - f2; break;
2033 case EXPR_ADD: f1 = f1 + f2; break;
2034 default: emsg(_(e_modulus)); goto failed;
2035 }
2036 clear_tv(tv1);
2037 clear_tv(tv2);
2038 tv1->v_type = VAR_FLOAT;
2039 tv1->vval.v_float = f1;
2040 --ectx.ec_stack.ga_len;
2041 }
2042 else
2043#endif
2044 {
2045 switch (iptr->isn_arg.op.op_type)
2046 {
2047 case EXPR_MULT: n1 = n1 * n2; break;
2048 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2049 case EXPR_SUB: n1 = n1 - n2; break;
2050 case EXPR_ADD: n1 = n1 + n2; break;
2051 default: n1 = num_modulus(n1, n2); break;
2052 }
2053 clear_tv(tv1);
2054 clear_tv(tv2);
2055 tv1->v_type = VAR_NUMBER;
2056 tv1->vval.v_number = n1;
2057 --ectx.ec_stack.ga_len;
2058 }
2059 }
2060 break;
2061
2062 case ISN_CONCAT:
2063 {
2064 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2065 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2066 char_u *res;
2067
2068 res = concat_str(str1, str2);
2069 clear_tv(STACK_TV_BOT(-2));
2070 clear_tv(STACK_TV_BOT(-1));
2071 --ectx.ec_stack.ga_len;
2072 STACK_TV_BOT(-1)->vval.v_string = res;
2073 }
2074 break;
2075
2076 case ISN_INDEX:
2077 {
2078 list_T *list;
2079 varnumber_T n;
2080 listitem_T *li;
2081
2082 // list index: list is at stack-2, index at stack-1
2083 tv = STACK_TV_BOT(-2);
2084 if (tv->v_type != VAR_LIST)
2085 {
2086 emsg(_(e_listreq));
2087 goto failed;
2088 }
2089 list = tv->vval.v_list;
2090
2091 tv = STACK_TV_BOT(-1);
2092 if (tv->v_type != VAR_NUMBER)
2093 {
2094 emsg(_(e_number_exp));
2095 goto failed;
2096 }
2097 n = tv->vval.v_number;
2098 clear_tv(tv);
2099 if ((li = list_find(list, n)) == NULL)
2100 {
2101 semsg(_(e_listidx), n);
2102 goto failed;
2103 }
2104 --ectx.ec_stack.ga_len;
2105 clear_tv(STACK_TV_BOT(-1));
2106 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2107 }
2108 break;
2109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002110 case ISN_MEMBER:
2111 {
2112 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002113 char_u *key;
2114 dictitem_T *di;
2115
2116 // dict member: dict is at stack-2, key at stack-1
2117 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002118 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002119 dict = tv->vval.v_dict;
2120
2121 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002122 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002123 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002124
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002125 if ((di = dict_find(dict, key, -1)) == NULL)
2126 {
2127 semsg(_(e_dictkey), key);
2128 goto failed;
2129 }
2130 --ectx.ec_stack.ga_len;
2131 clear_tv(tv);
2132 clear_tv(STACK_TV_BOT(-1));
2133 copy_tv(&di->di_tv, STACK_TV_BOT(-1));
2134 }
2135 break;
2136
2137 // dict member with string key
2138 case ISN_STRINGMEMBER:
2139 {
2140 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002141 dictitem_T *di;
2142
2143 tv = STACK_TV_BOT(-1);
2144 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2145 {
2146 emsg(_(e_dictreq));
2147 goto failed;
2148 }
2149 dict = tv->vval.v_dict;
2150
2151 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2152 == NULL)
2153 {
2154 semsg(_(e_dictkey), iptr->isn_arg.string);
2155 goto failed;
2156 }
2157 clear_tv(tv);
2158 copy_tv(&di->di_tv, tv);
2159 }
2160 break;
2161
2162 case ISN_NEGATENR:
2163 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002164 if (tv->v_type != VAR_NUMBER
2165#ifdef FEAT_FLOAT
2166 && tv->v_type != VAR_FLOAT
2167#endif
2168 )
2169 {
2170 emsg(_(e_number_exp));
2171 goto failed;
2172 }
2173#ifdef FEAT_FLOAT
2174 if (tv->v_type == VAR_FLOAT)
2175 tv->vval.v_float = -tv->vval.v_float;
2176 else
2177#endif
2178 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 break;
2180
2181 case ISN_CHECKNR:
2182 {
2183 int error = FALSE;
2184
2185 tv = STACK_TV_BOT(-1);
2186 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 (void)tv_get_number_chk(tv, &error);
2189 if (error)
2190 goto failed;
2191 }
2192 break;
2193
2194 case ISN_CHECKTYPE:
2195 {
2196 checktype_T *ct = &iptr->isn_arg.type;
2197
2198 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002199 // TODO: better type comparison
2200 if (tv->v_type != ct->ct_type
2201 && !((tv->v_type == VAR_PARTIAL
2202 && ct->ct_type == VAR_FUNC)
2203 || (tv->v_type == VAR_FUNC
2204 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 {
2206 semsg(_("E1029: Expected %s but got %s"),
2207 vartype_name(ct->ct_type),
2208 vartype_name(tv->v_type));
2209 goto failed;
2210 }
2211 }
2212 break;
2213
2214 case ISN_2BOOL:
2215 {
2216 int n;
2217
2218 tv = STACK_TV_BOT(-1);
2219 n = tv2bool(tv);
2220 if (iptr->isn_arg.number) // invert
2221 n = !n;
2222 clear_tv(tv);
2223 tv->v_type = VAR_BOOL;
2224 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2225 }
2226 break;
2227
2228 case ISN_2STRING:
2229 {
2230 char_u *str;
2231
2232 tv = STACK_TV_BOT(iptr->isn_arg.number);
2233 if (tv->v_type != VAR_STRING)
2234 {
2235 str = typval_tostring(tv);
2236 clear_tv(tv);
2237 tv->v_type = VAR_STRING;
2238 tv->vval.v_string = str;
2239 }
2240 }
2241 break;
2242
2243 case ISN_DROP:
2244 --ectx.ec_stack.ga_len;
2245 clear_tv(STACK_TV_BOT(0));
2246 break;
2247 }
2248 }
2249
2250done:
2251 // function finished, get result from the stack.
2252 tv = STACK_TV_BOT(-1);
2253 *rettv = *tv;
2254 tv->v_type = VAR_UNKNOWN;
2255 ret = OK;
2256
2257failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002258 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002259 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002260 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002261failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002262 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002263
2264 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002265 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2266 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002269 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 return ret;
2271}
2272
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273/*
2274 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002275 * We don't really need this at runtime, but we do have tests that require it,
2276 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 */
2278 void
2279ex_disassemble(exarg_T *eap)
2280{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002281 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002282 char_u *fname;
2283 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 dfunc_T *dfunc;
2285 isn_T *instr;
2286 int current;
2287 int line_idx = 0;
2288 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002289 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002291 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002292 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002293 if (fname == NULL)
2294 {
2295 semsg(_(e_invarg2), eap->arg);
2296 return;
2297 }
2298
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002299 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002300 if (ufunc == NULL)
2301 {
2302 char_u *p = untrans_function_name(fname);
2303
2304 if (p != NULL)
2305 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002306 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002307 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002308 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 if (ufunc == NULL)
2310 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002311 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002312 return;
2313 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02002314 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
2315 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2316 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 if (ufunc->uf_dfunc_idx < 0)
2318 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002319 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 return;
2321 }
2322 if (ufunc->uf_name_exp != NULL)
2323 msg((char *)ufunc->uf_name_exp);
2324 else
2325 msg((char *)ufunc->uf_name);
2326
2327 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2328 instr = dfunc->df_instr;
2329 for (current = 0; current < dfunc->df_instr_count; ++current)
2330 {
2331 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002332 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002333
2334 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2335 {
2336 if (current > prev_current)
2337 {
2338 msg_puts("\n\n");
2339 prev_current = current;
2340 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002341 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2342 if (line != NULL)
2343 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 }
2345
2346 switch (iptr->isn_type)
2347 {
2348 case ISN_EXEC:
2349 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2350 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002351 case ISN_EXECCONCAT:
2352 smsg("%4d EXECCONCAT %lld", current,
2353 (long long)iptr->isn_arg.number);
2354 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355 case ISN_ECHO:
2356 {
2357 echo_T *echo = &iptr->isn_arg.echo;
2358
2359 smsg("%4d %s %d", current,
2360 echo->echo_with_white ? "ECHO" : "ECHON",
2361 echo->echo_count);
2362 }
2363 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002364 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002365 smsg("%4d EXECUTE %lld", current,
2366 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002367 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002368 case ISN_ECHOMSG:
2369 smsg("%4d ECHOMSG %lld", current,
2370 (long long)(iptr->isn_arg.number));
2371 break;
2372 case ISN_ECHOERR:
2373 smsg("%4d ECHOERR %lld", current,
2374 (long long)(iptr->isn_arg.number));
2375 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002377 case ISN_LOADOUTER:
2378 {
2379 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2380
2381 if (iptr->isn_arg.number < 0)
2382 smsg("%4d LOAD%s arg[%lld]", current, add,
2383 (long long)(iptr->isn_arg.number
2384 + STACK_FRAME_SIZE));
2385 else
2386 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002387 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 break;
2390 case ISN_LOADV:
2391 smsg("%4d LOADV v:%s", current,
2392 get_vim_var_name(iptr->isn_arg.number));
2393 break;
2394 case ISN_LOADSCRIPT:
2395 {
2396 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002397 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2399 + iptr->isn_arg.script.script_idx;
2400
2401 smsg("%4d LOADSCRIPT %s from %s", current,
2402 sv->sv_name, si->sn_name);
2403 }
2404 break;
2405 case ISN_LOADS:
2406 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002407 scriptitem_T *si = SCRIPT_ITEM(
2408 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409
2410 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002411 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002412 }
2413 break;
2414 case ISN_LOADG:
2415 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2416 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002417 case ISN_LOADB:
2418 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2419 break;
2420 case ISN_LOADW:
2421 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2422 break;
2423 case ISN_LOADT:
2424 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2425 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 case ISN_LOADOPT:
2427 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2428 break;
2429 case ISN_LOADENV:
2430 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2431 break;
2432 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002433 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002434 break;
2435
2436 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002437 case ISN_STOREOUTER:
2438 {
2439 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2440
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002441 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002442 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002443 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002444 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002445 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002446 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002447 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002449 case ISN_STOREV:
2450 smsg("%4d STOREV v:%s", current,
2451 get_vim_var_name(iptr->isn_arg.number));
2452 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002454 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2455 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002456 case ISN_STOREB:
2457 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2458 break;
2459 case ISN_STOREW:
2460 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2461 break;
2462 case ISN_STORET:
2463 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2464 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002465 case ISN_STORES:
2466 {
2467 scriptitem_T *si = SCRIPT_ITEM(
2468 iptr->isn_arg.loadstore.ls_sid);
2469
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002470 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002471 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002472 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002473 break;
2474 case ISN_STORESCRIPT:
2475 {
2476 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002477 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2479 + iptr->isn_arg.script.script_idx;
2480
2481 smsg("%4d STORESCRIPT %s in %s", current,
2482 sv->sv_name, si->sn_name);
2483 }
2484 break;
2485 case ISN_STOREOPT:
2486 smsg("%4d STOREOPT &%s", current,
2487 iptr->isn_arg.storeopt.so_name);
2488 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002489 case ISN_STOREENV:
2490 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2491 break;
2492 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002493 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002494 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 case ISN_STORENR:
2496 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002497 iptr->isn_arg.storenr.stnr_val,
2498 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002499 break;
2500
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002501 case ISN_STORELIST:
2502 smsg("%4d STORELIST", current);
2503 break;
2504
2505 case ISN_STOREDICT:
2506 smsg("%4d STOREDICT", current);
2507 break;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 // constants
2510 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002511 smsg("%4d PUSHNR %lld", current,
2512 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 break;
2514 case ISN_PUSHBOOL:
2515 case ISN_PUSHSPEC:
2516 smsg("%4d PUSH %s", current,
2517 get_var_special_name(iptr->isn_arg.number));
2518 break;
2519 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002520#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002522#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 break;
2524 case ISN_PUSHS:
2525 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2526 break;
2527 case ISN_PUSHBLOB:
2528 {
2529 char_u *r;
2530 char_u numbuf[NUMBUFLEN];
2531 char_u *tofree;
2532
2533 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002534 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 vim_free(tofree);
2536 }
2537 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002538 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002539 {
2540 char *name = (char *)iptr->isn_arg.string;
2541
2542 smsg("%4d PUSHFUNC \"%s\"", current,
2543 name == NULL ? "[none]" : name);
2544 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002545 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002546 case ISN_PUSHCHANNEL:
2547#ifdef FEAT_JOB_CHANNEL
2548 {
2549 channel_T *channel = iptr->isn_arg.channel;
2550
2551 smsg("%4d PUSHCHANNEL %d", current,
2552 channel == NULL ? 0 : channel->ch_id);
2553 }
2554#endif
2555 break;
2556 case ISN_PUSHJOB:
2557#ifdef FEAT_JOB_CHANNEL
2558 {
2559 typval_T tv;
2560 char_u *name;
2561
2562 tv.v_type = VAR_JOB;
2563 tv.vval.v_job = iptr->isn_arg.job;
2564 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002565 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002566 }
2567#endif
2568 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 case ISN_PUSHEXC:
2570 smsg("%4d PUSH v:exception", current);
2571 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002572 case ISN_UNLET:
2573 smsg("%4d UNLET%s %s", current,
2574 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2575 iptr->isn_arg.unlet.ul_name);
2576 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002577 case ISN_UNLETENV:
2578 smsg("%4d UNLETENV%s $%s", current,
2579 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2580 iptr->isn_arg.unlet.ul_name);
2581 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002582 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002583 smsg("%4d NEWLIST size %lld", current,
2584 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002585 break;
2586 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002587 smsg("%4d NEWDICT size %lld", current,
2588 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002589 break;
2590
2591 // function call
2592 case ISN_BCALL:
2593 {
2594 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2595
2596 smsg("%4d BCALL %s(argc %d)", current,
2597 internal_func_name(cbfunc->cbf_idx),
2598 cbfunc->cbf_argcount);
2599 }
2600 break;
2601 case ISN_DCALL:
2602 {
2603 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2604 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2605 + cdfunc->cdf_idx;
2606
2607 smsg("%4d DCALL %s(argc %d)", current,
2608 df->df_ufunc->uf_name_exp != NULL
2609 ? df->df_ufunc->uf_name_exp
2610 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2611 }
2612 break;
2613 case ISN_UCALL:
2614 {
2615 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2616
2617 smsg("%4d UCALL %s(argc %d)", current,
2618 cufunc->cuf_name, cufunc->cuf_argcount);
2619 }
2620 break;
2621 case ISN_PCALL:
2622 {
2623 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2624
2625 smsg("%4d PCALL%s (argc %d)", current,
2626 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2627 }
2628 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002629 case ISN_PCALL_END:
2630 smsg("%4d PCALL end", current);
2631 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 case ISN_RETURN:
2633 smsg("%4d RETURN", current);
2634 break;
2635 case ISN_FUNCREF:
2636 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002637 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002639 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002641 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002642 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644 break;
2645
2646 case ISN_JUMP:
2647 {
2648 char *when = "?";
2649
2650 switch (iptr->isn_arg.jump.jump_when)
2651 {
2652 case JUMP_ALWAYS:
2653 when = "JUMP";
2654 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 case JUMP_AND_KEEP_IF_TRUE:
2656 when = "JUMP_AND_KEEP_IF_TRUE";
2657 break;
2658 case JUMP_IF_FALSE:
2659 when = "JUMP_IF_FALSE";
2660 break;
2661 case JUMP_AND_KEEP_IF_FALSE:
2662 when = "JUMP_AND_KEEP_IF_FALSE";
2663 break;
2664 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002665 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 iptr->isn_arg.jump.jump_where);
2667 }
2668 break;
2669
2670 case ISN_FOR:
2671 {
2672 forloop_T *forloop = &iptr->isn_arg.forloop;
2673
2674 smsg("%4d FOR $%d -> %d", current,
2675 forloop->for_idx, forloop->for_end);
2676 }
2677 break;
2678
2679 case ISN_TRY:
2680 {
2681 try_T *try = &iptr->isn_arg.try;
2682
2683 smsg("%4d TRY catch -> %d, finally -> %d", current,
2684 try->try_catch, try->try_finally);
2685 }
2686 break;
2687 case ISN_CATCH:
2688 // TODO
2689 smsg("%4d CATCH", current);
2690 break;
2691 case ISN_ENDTRY:
2692 smsg("%4d ENDTRY", current);
2693 break;
2694 case ISN_THROW:
2695 smsg("%4d THROW", current);
2696 break;
2697
2698 // expression operations on number
2699 case ISN_OPNR:
2700 case ISN_OPFLOAT:
2701 case ISN_OPANY:
2702 {
2703 char *what;
2704 char *ins;
2705
2706 switch (iptr->isn_arg.op.op_type)
2707 {
2708 case EXPR_MULT: what = "*"; break;
2709 case EXPR_DIV: what = "/"; break;
2710 case EXPR_REM: what = "%"; break;
2711 case EXPR_SUB: what = "-"; break;
2712 case EXPR_ADD: what = "+"; break;
2713 default: what = "???"; break;
2714 }
2715 switch (iptr->isn_type)
2716 {
2717 case ISN_OPNR: ins = "OPNR"; break;
2718 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2719 case ISN_OPANY: ins = "OPANY"; break;
2720 default: ins = "???"; break;
2721 }
2722 smsg("%4d %s %s", current, ins, what);
2723 }
2724 break;
2725
2726 case ISN_COMPAREBOOL:
2727 case ISN_COMPARESPECIAL:
2728 case ISN_COMPARENR:
2729 case ISN_COMPAREFLOAT:
2730 case ISN_COMPARESTRING:
2731 case ISN_COMPAREBLOB:
2732 case ISN_COMPARELIST:
2733 case ISN_COMPAREDICT:
2734 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 case ISN_COMPAREANY:
2736 {
2737 char *p;
2738 char buf[10];
2739 char *type;
2740
2741 switch (iptr->isn_arg.op.op_type)
2742 {
2743 case EXPR_EQUAL: p = "=="; break;
2744 case EXPR_NEQUAL: p = "!="; break;
2745 case EXPR_GREATER: p = ">"; break;
2746 case EXPR_GEQUAL: p = ">="; break;
2747 case EXPR_SMALLER: p = "<"; break;
2748 case EXPR_SEQUAL: p = "<="; break;
2749 case EXPR_MATCH: p = "=~"; break;
2750 case EXPR_IS: p = "is"; break;
2751 case EXPR_ISNOT: p = "isnot"; break;
2752 case EXPR_NOMATCH: p = "!~"; break;
2753 default: p = "???"; break;
2754 }
2755 STRCPY(buf, p);
2756 if (iptr->isn_arg.op.op_ic == TRUE)
2757 strcat(buf, "?");
2758 switch(iptr->isn_type)
2759 {
2760 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2761 case ISN_COMPARESPECIAL:
2762 type = "COMPARESPECIAL"; break;
2763 case ISN_COMPARENR: type = "COMPARENR"; break;
2764 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2765 case ISN_COMPARESTRING:
2766 type = "COMPARESTRING"; break;
2767 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2768 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2769 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2770 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002771 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2772 default: type = "???"; break;
2773 }
2774
2775 smsg("%4d %s %s", current, type, buf);
2776 }
2777 break;
2778
2779 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2780 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2781
2782 // expression operations
2783 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2784 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002785 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2786 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 iptr->isn_arg.string); break;
2788 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2789
2790 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2791 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2792 vartype_name(iptr->isn_arg.type.ct_type),
2793 iptr->isn_arg.type.ct_off);
2794 break;
2795 case ISN_2BOOL: if (iptr->isn_arg.number)
2796 smsg("%4d INVERT (!val)", current);
2797 else
2798 smsg("%4d 2BOOL (!!val)", current);
2799 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002800 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2801 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 break;
2803
2804 case ISN_DROP: smsg("%4d DROP", current); break;
2805 }
2806 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807}
2808
2809/*
2810 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2811 * list, etc. Mostly like what JavaScript does, except that empty list and
2812 * empty dictionary are FALSE.
2813 */
2814 int
2815tv2bool(typval_T *tv)
2816{
2817 switch (tv->v_type)
2818 {
2819 case VAR_NUMBER:
2820 return tv->vval.v_number != 0;
2821 case VAR_FLOAT:
2822#ifdef FEAT_FLOAT
2823 return tv->vval.v_float != 0.0;
2824#else
2825 break;
2826#endif
2827 case VAR_PARTIAL:
2828 return tv->vval.v_partial != NULL;
2829 case VAR_FUNC:
2830 case VAR_STRING:
2831 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2832 case VAR_LIST:
2833 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2834 case VAR_DICT:
2835 return tv->vval.v_dict != NULL
2836 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2837 case VAR_BOOL:
2838 case VAR_SPECIAL:
2839 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2840 case VAR_JOB:
2841#ifdef FEAT_JOB_CHANNEL
2842 return tv->vval.v_job != NULL;
2843#else
2844 break;
2845#endif
2846 case VAR_CHANNEL:
2847#ifdef FEAT_JOB_CHANNEL
2848 return tv->vval.v_channel != NULL;
2849#else
2850 break;
2851#endif
2852 case VAR_BLOB:
2853 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2854 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002855 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002856 case VAR_VOID:
2857 break;
2858 }
2859 return FALSE;
2860}
2861
2862/*
2863 * If "tv" is a string give an error and return FAIL.
2864 */
2865 int
2866check_not_string(typval_T *tv)
2867{
2868 if (tv->v_type == VAR_STRING)
2869 {
2870 emsg(_("E1030: Using a String as a Number"));
2871 clear_tv(tv);
2872 return FAIL;
2873 }
2874 return OK;
2875}
2876
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877
2878#endif // FEAT_EVAL