blob: 7462b68c98a60d9e343a0088cb0bdc81c421d78e [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;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200233 estack_push_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 Moolenaar25e0f582020-05-25 22:36:50 +0200659 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100660
661// Get pointer to item in the stack.
662#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
663
664// Get pointer to item at the bottom of the stack, -1 is the bottom.
665#undef STACK_TV_BOT
666#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
667
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200668// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200669#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 +0100670
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200671// Like STACK_TV_VAR but use the outer scope
672#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
673
Bram Moolenaar822ba242020-05-24 23:00:18 +0200674 if (ufunc->uf_dfunc_idx == UF_NOT_COMPILED
675 || (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
676 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200677 {
678 if (called_emsg == called_emsg_before)
679 semsg(_("E1091: Function is not compiled: %s"),
680 ufunc->uf_name_exp == NULL
681 ? ufunc->uf_name : ufunc->uf_name_exp);
Bram Moolenaar822ba242020-05-24 23:00:18 +0200682 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200683 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200684
Bram Moolenaar09689a02020-05-09 22:50:08 +0200685 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200686 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200687 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
688 + ufunc->uf_dfunc_idx;
689 if (dfunc->df_instr == NULL)
690 return FAIL;
691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200693 CLEAR_FIELD(ectx);
694 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
695 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
696 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
699
700 // Put arguments on the stack.
701 for (idx = 0; idx < argc; ++idx)
702 {
703 copy_tv(&argv[idx], STACK_TV_BOT(0));
704 ++ectx.ec_stack.ga_len;
705 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200706
707 // Turn varargs into a list. Empty list if no args.
708 if (ufunc->uf_va_name != NULL)
709 {
710 int vararg_count = argc - ufunc->uf_args.ga_len;
711
712 if (vararg_count < 0)
713 vararg_count = 0;
714 else
715 argc -= vararg_count;
716 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200717 goto failed_early;
Bram Moolenaar23e03252020-04-12 22:22:31 +0200718 if (defcount > 0)
719 // Move varargs list to below missing default arguments.
720 *STACK_TV_BOT(defcount- 1) = *STACK_TV_BOT(-1);
721 --ectx.ec_stack.ga_len;
722 }
723
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100724 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200725 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100726 if (defcount > 0)
727 for (idx = 0; idx < defcount; ++idx)
728 {
729 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
730 ++ectx.ec_stack.ga_len;
731 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200732 if (ufunc->uf_va_name != NULL)
733 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
735 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200736 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
737 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200739 if (partial != NULL)
740 {
741 ectx.ec_outer_stack = partial->pt_ectx_stack;
742 ectx.ec_outer_frame = partial->pt_ectx_frame;
743 }
744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 // dummy frame entries
746 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
747 {
748 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
749 ++ectx.ec_stack.ga_len;
750 }
751
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200752 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200753 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200754 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
755 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200756 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200758 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200759 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200760 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200761
762 ectx.ec_instr = dfunc->df_instr;
763 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100764
Bram Moolenaara26b9702020-04-18 19:53:28 +0200765 // Commands behave like vim9script.
766 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
767
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100768 // Decide where to start execution, handles optional arguments.
769 init_instr_idx(ufunc, argc, &ectx);
770
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 for (;;)
772 {
773 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100774
Bram Moolenaar270d0382020-05-15 21:42:53 +0200775 if (++breakcheck_count >= 100)
776 {
777 line_breakcheck();
778 breakcheck_count = 0;
779 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100780 if (got_int)
781 {
782 // Turn CTRL-C into an exception.
783 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100784 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100785 goto failed;
786 did_throw = TRUE;
787 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100788
Bram Moolenaara26b9702020-04-18 19:53:28 +0200789 if (did_emsg && msg_list != NULL && *msg_list != NULL)
790 {
791 // Turn an error message into an exception.
792 did_emsg = FALSE;
793 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
794 goto failed;
795 did_throw = TRUE;
796 *msg_list = NULL;
797 }
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if (did_throw && !ectx.ec_in_catch)
800 {
801 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100802 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803
804 // An exception jumps to the first catch, finally, or returns from
805 // the current function.
806 if (trystack->ga_len > 0)
807 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200808 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100809 {
810 // jump to ":catch" or ":finally"
811 ectx.ec_in_catch = TRUE;
812 ectx.ec_iidx = trycmd->tcd_catch_idx;
813 }
814 else
815 {
816 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200817 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100818 {
819 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200820 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 goto failed;
822 tv = STACK_TV_BOT(0);
823 tv->v_type = VAR_NUMBER;
824 tv->vval.v_number = 0;
825 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100826 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200827 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
828 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 goto done;
830 }
831
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200832 if (func_return(&ectx) == FAIL)
833 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100834 }
835 continue;
836 }
837
838 iptr = &ectx.ec_instr[ectx.ec_iidx++];
839 switch (iptr->isn_type)
840 {
841 // execute Ex command line
842 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200843 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100844 do_cmdline_cmd(iptr->isn_arg.string);
845 break;
846
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200847 // execute Ex command from pieces on the stack
848 case ISN_EXECCONCAT:
849 {
850 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200851 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200852 int pass;
853 int i;
854 char_u *cmd = NULL;
855 char_u *str;
856
857 for (pass = 1; pass <= 2; ++pass)
858 {
859 for (i = 0; i < count; ++i)
860 {
861 tv = STACK_TV_BOT(i - count);
862 str = tv->vval.v_string;
863 if (str != NULL && *str != NUL)
864 {
865 if (pass == 2)
866 STRCPY(cmd + len, str);
867 len += STRLEN(str);
868 }
869 if (pass == 2)
870 clear_tv(tv);
871 }
872 if (pass == 1)
873 {
874 cmd = alloc(len + 1);
875 if (cmd == NULL)
876 goto failed;
877 len = 0;
878 }
879 }
880
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200881 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200882 do_cmdline_cmd(cmd);
883 vim_free(cmd);
884 }
885 break;
886
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887 // execute :echo {string} ...
888 case ISN_ECHO:
889 {
890 int count = iptr->isn_arg.echo.echo_count;
891 int atstart = TRUE;
892 int needclr = TRUE;
893
894 for (idx = 0; idx < count; ++idx)
895 {
896 tv = STACK_TV_BOT(idx - count);
897 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
898 &atstart, &needclr);
899 clear_tv(tv);
900 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100901 if (needclr)
902 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100903 ectx.ec_stack.ga_len -= count;
904 }
905 break;
906
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200907 // :execute {string} ...
908 // :echomsg {string} ...
909 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +0100910 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200911 case ISN_ECHOMSG:
912 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +0100913 {
914 int count = iptr->isn_arg.number;
915 garray_T ga;
916 char_u buf[NUMBUFLEN];
917 char_u *p;
918 int len;
919 int failed = FALSE;
920
921 ga_init2(&ga, 1, 80);
922 for (idx = 0; idx < count; ++idx)
923 {
924 tv = STACK_TV_BOT(idx - count);
925 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
926 {
927 emsg(_(e_inval_string));
928 break;
929 }
930 else
931 p = tv_get_string_buf(tv, buf);
932
933 len = (int)STRLEN(p);
934 if (ga_grow(&ga, len + 2) == FAIL)
935 failed = TRUE;
936 else
937 {
938 if (ga.ga_len > 0)
939 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
940 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
941 ga.ga_len += len;
942 }
943 clear_tv(tv);
944 }
945 ectx.ec_stack.ga_len -= count;
946
947 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200948 {
949 if (iptr->isn_type == ISN_EXECUTE)
950 do_cmdline_cmd((char_u *)ga.ga_data);
951 else
952 {
953 msg_sb_eol();
954 if (iptr->isn_type == ISN_ECHOMSG)
955 {
956 msg_attr(ga.ga_data, echo_attr);
957 out_flush();
958 }
959 else
960 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200961 SOURCING_LNUM = iptr->isn_lnum;
962 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200963 }
964 }
965 }
Bram Moolenaarad39c092020-02-26 18:23:43 +0100966 ga_clear(&ga);
967 }
968 break;
969
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100970 // load local variable or argument
971 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200972 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100973 goto failed;
974 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
975 ++ectx.ec_stack.ga_len;
976 break;
977
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200978 // load variable or argument from outer scope
979 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200980 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200981 goto failed;
982 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
983 STACK_TV_BOT(0));
984 ++ectx.ec_stack.ga_len;
985 break;
986
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100987 // load v: variable
988 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +0200989 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 goto failed;
991 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
992 ++ectx.ec_stack.ga_len;
993 break;
994
Bram Moolenaarb283a8a2020-02-02 22:24:04 +0100995 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100996 case ISN_LOADSCRIPT:
997 {
998 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +0100999 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001000 svar_T *sv;
1001
1002 sv = ((svar_T *)si->sn_var_vals.ga_data)
1003 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001004 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 goto failed;
1006 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1007 ++ectx.ec_stack.ga_len;
1008 }
1009 break;
1010
1011 // load s: variable in old script
1012 case ISN_LOADS:
1013 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001014 hashtab_T *ht = &SCRIPT_VARS(
1015 iptr->isn_arg.loadstore.ls_sid);
1016 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001017 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001018
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001019 if (di == NULL)
1020 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001021 semsg(_(e_undefvar), name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022 goto failed;
1023 }
1024 else
1025 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001026 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 goto failed;
1028 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1029 ++ectx.ec_stack.ga_len;
1030 }
1031 }
1032 break;
1033
Bram Moolenaard3aac292020-04-19 14:32:17 +02001034 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001035 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001036 case ISN_LOADB:
1037 case ISN_LOADW:
1038 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001039 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001040 dictitem_T *di = NULL;
1041 hashtab_T *ht = NULL;
1042 char namespace;
1043 switch (iptr->isn_type)
1044 {
1045 case ISN_LOADG:
1046 ht = get_globvar_ht();
1047 namespace = 'g';
1048 break;
1049 case ISN_LOADB:
1050 ht = &curbuf->b_vars->dv_hashtab;
1051 namespace = 'b';
1052 break;
1053 case ISN_LOADW:
1054 ht = &curwin->w_vars->dv_hashtab;
1055 namespace = 'w';
1056 break;
1057 case ISN_LOADT:
1058 ht = &curtab->tp_vars->dv_hashtab;
1059 namespace = 't';
1060 break;
1061 default: // Cannot reach here
1062 goto failed;
1063 }
1064 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 if (di == NULL)
1067 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001068 semsg(_("E121: Undefined variable: %c:%s"),
1069 namespace, iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070 goto failed;
1071 }
1072 else
1073 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001074 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 goto failed;
1076 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1077 ++ectx.ec_stack.ga_len;
1078 }
1079 }
1080 break;
1081
1082 // load &option
1083 case ISN_LOADOPT:
1084 {
1085 typval_T optval;
1086 char_u *name = iptr->isn_arg.string;
1087
Bram Moolenaara8c17702020-04-01 21:17:24 +02001088 // This is not expected to fail, name is checked during
1089 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001090 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 goto failed;
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001092 if (get_option_tv(&name, &optval, TRUE) == FAIL)
1093 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 *STACK_TV_BOT(0) = optval;
1095 ++ectx.ec_stack.ga_len;
1096 }
1097 break;
1098
1099 // load $ENV
1100 case ISN_LOADENV:
1101 {
1102 typval_T optval;
1103 char_u *name = iptr->isn_arg.string;
1104
Bram Moolenaar270d0382020-05-15 21:42:53 +02001105 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001106 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001107 // name is always valid, checked when compiling
1108 (void)get_env_tv(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 *STACK_TV_BOT(0) = optval;
1110 ++ectx.ec_stack.ga_len;
1111 }
1112 break;
1113
1114 // load @register
1115 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001116 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001117 goto failed;
1118 tv = STACK_TV_BOT(0);
1119 tv->v_type = VAR_STRING;
1120 tv->vval.v_string = get_reg_contents(
1121 iptr->isn_arg.number, GREG_EXPR_SRC);
1122 ++ectx.ec_stack.ga_len;
1123 break;
1124
1125 // store local variable
1126 case ISN_STORE:
1127 --ectx.ec_stack.ga_len;
1128 tv = STACK_TV_VAR(iptr->isn_arg.number);
1129 clear_tv(tv);
1130 *tv = *STACK_TV_BOT(0);
1131 break;
1132
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001133 // store variable or argument in outer scope
1134 case ISN_STOREOUTER:
1135 --ectx.ec_stack.ga_len;
1136 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1137 clear_tv(tv);
1138 *tv = *STACK_TV_BOT(0);
1139 break;
1140
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001141 // store s: variable in old script
1142 case ISN_STORES:
1143 {
1144 hashtab_T *ht = &SCRIPT_VARS(
1145 iptr->isn_arg.loadstore.ls_sid);
1146 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001147 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001148
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001149 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001150 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001151 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001152 else
1153 {
1154 clear_tv(&di->di_tv);
1155 di->di_tv = *STACK_TV_BOT(0);
1156 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001157 }
1158 break;
1159
1160 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 case ISN_STORESCRIPT:
1162 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001163 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001164 iptr->isn_arg.script.script_sid);
1165 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1166 + iptr->isn_arg.script.script_idx;
1167
1168 --ectx.ec_stack.ga_len;
1169 clear_tv(sv->sv_tv);
1170 *sv->sv_tv = *STACK_TV_BOT(0);
1171 }
1172 break;
1173
1174 // store option
1175 case ISN_STOREOPT:
1176 {
1177 long n = 0;
1178 char_u *s = NULL;
1179 char *msg;
1180
1181 --ectx.ec_stack.ga_len;
1182 tv = STACK_TV_BOT(0);
1183 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001184 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001186 if (s == NULL)
1187 s = (char_u *)"";
1188 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001189 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001190 // must be VAR_NUMBER, CHECKTYPE makes sure
1191 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1193 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001194 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 if (msg != NULL)
1196 {
1197 emsg(_(msg));
1198 goto failed;
1199 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 }
1201 break;
1202
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001203 // store $ENV
1204 case ISN_STOREENV:
1205 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001206 tv = STACK_TV_BOT(0);
1207 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1208 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001209 break;
1210
1211 // store @r
1212 case ISN_STOREREG:
1213 {
1214 int reg = iptr->isn_arg.number;
1215
1216 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001217 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001218 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001219 tv_get_string(tv), -1, FALSE);
1220 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001221 }
1222 break;
1223
1224 // store v: variable
1225 case ISN_STOREV:
1226 --ectx.ec_stack.ga_len;
1227 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1228 == FAIL)
1229 goto failed;
1230 break;
1231
Bram Moolenaard3aac292020-04-19 14:32:17 +02001232 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001234 case ISN_STOREB:
1235 case ISN_STOREW:
1236 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 {
1238 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001239 hashtab_T *ht;
1240 switch (iptr->isn_type)
1241 {
1242 case ISN_STOREG:
1243 ht = get_globvar_ht();
1244 break;
1245 case ISN_STOREB:
1246 ht = &curbuf->b_vars->dv_hashtab;
1247 break;
1248 case ISN_STOREW:
1249 ht = &curwin->w_vars->dv_hashtab;
1250 break;
1251 case ISN_STORET:
1252 ht = &curtab->tp_vars->dv_hashtab;
1253 break;
1254 default: // Cannot reach here
1255 goto failed;
1256 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257
1258 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001259 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001261 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 else
1263 {
1264 clear_tv(&di->di_tv);
1265 di->di_tv = *STACK_TV_BOT(0);
1266 }
1267 }
1268 break;
1269
1270 // store number in local variable
1271 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001272 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001273 clear_tv(tv);
1274 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001275 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276 break;
1277
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001278 // store value in list variable
1279 case ISN_STORELIST:
1280 {
1281 typval_T *tv_idx = STACK_TV_BOT(-2);
1282 varnumber_T lidx = tv_idx->vval.v_number;
1283 typval_T *tv_list = STACK_TV_BOT(-1);
1284 list_T *list = tv_list->vval.v_list;
1285
1286 if (lidx < 0 && list->lv_len + lidx >= 0)
1287 // negative index is relative to the end
1288 lidx = list->lv_len + lidx;
1289 if (lidx < 0 || lidx > list->lv_len)
1290 {
1291 semsg(_(e_listidx), lidx);
1292 goto failed;
1293 }
1294 tv = STACK_TV_BOT(-3);
1295 if (lidx < list->lv_len)
1296 {
1297 listitem_T *li = list_find(list, lidx);
1298
1299 // overwrite existing list item
1300 clear_tv(&li->li_tv);
1301 li->li_tv = *tv;
1302 }
1303 else
1304 {
1305 // append to list
1306 if (list_append_tv(list, tv) == FAIL)
1307 goto failed;
1308 clear_tv(tv);
1309 }
1310 clear_tv(tv_idx);
1311 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001312 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001313 }
1314 break;
1315
1316 // store value in dict variable
1317 case ISN_STOREDICT:
1318 {
1319 typval_T *tv_key = STACK_TV_BOT(-2);
1320 char_u *key = tv_key->vval.v_string;
1321 typval_T *tv_dict = STACK_TV_BOT(-1);
1322 dict_T *dict = tv_dict->vval.v_dict;
1323 dictitem_T *di;
1324
1325 if (key == NULL || *key == NUL)
1326 {
1327 emsg(_(e_emptykey));
1328 goto failed;
1329 }
1330 tv = STACK_TV_BOT(-3);
1331 di = dict_find(dict, key, -1);
1332 if (di != NULL)
1333 {
1334 clear_tv(&di->di_tv);
1335 di->di_tv = *tv;
1336 }
1337 else
1338 {
1339 // add to dict
1340 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1341 goto failed;
1342 clear_tv(tv);
1343 }
1344 clear_tv(tv_key);
1345 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001346 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001347 }
1348 break;
1349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001350 // push constant
1351 case ISN_PUSHNR:
1352 case ISN_PUSHBOOL:
1353 case ISN_PUSHSPEC:
1354 case ISN_PUSHF:
1355 case ISN_PUSHS:
1356 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001357 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001358 case ISN_PUSHCHANNEL:
1359 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001360 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361 goto failed;
1362 tv = STACK_TV_BOT(0);
1363 ++ectx.ec_stack.ga_len;
1364 switch (iptr->isn_type)
1365 {
1366 case ISN_PUSHNR:
1367 tv->v_type = VAR_NUMBER;
1368 tv->vval.v_number = iptr->isn_arg.number;
1369 break;
1370 case ISN_PUSHBOOL:
1371 tv->v_type = VAR_BOOL;
1372 tv->vval.v_number = iptr->isn_arg.number;
1373 break;
1374 case ISN_PUSHSPEC:
1375 tv->v_type = VAR_SPECIAL;
1376 tv->vval.v_number = iptr->isn_arg.number;
1377 break;
1378#ifdef FEAT_FLOAT
1379 case ISN_PUSHF:
1380 tv->v_type = VAR_FLOAT;
1381 tv->vval.v_float = iptr->isn_arg.fnumber;
1382 break;
1383#endif
1384 case ISN_PUSHBLOB:
1385 blob_copy(iptr->isn_arg.blob, tv);
1386 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001387 case ISN_PUSHFUNC:
1388 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001389 if (iptr->isn_arg.string == NULL)
1390 tv->vval.v_string = NULL;
1391 else
1392 tv->vval.v_string =
1393 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001394 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001395 case ISN_PUSHCHANNEL:
1396#ifdef FEAT_JOB_CHANNEL
1397 tv->v_type = VAR_CHANNEL;
1398 tv->vval.v_channel = iptr->isn_arg.channel;
1399 if (tv->vval.v_channel != NULL)
1400 ++tv->vval.v_channel->ch_refcount;
1401#endif
1402 break;
1403 case ISN_PUSHJOB:
1404#ifdef FEAT_JOB_CHANNEL
1405 tv->v_type = VAR_JOB;
1406 tv->vval.v_job = iptr->isn_arg.job;
1407 if (tv->vval.v_job != NULL)
1408 ++tv->vval.v_job->jv_refcount;
1409#endif
1410 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001411 default:
1412 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001413 tv->vval.v_string = vim_strsave(
1414 iptr->isn_arg.string == NULL
1415 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 }
1417 break;
1418
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001419 case ISN_UNLET:
1420 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1421 iptr->isn_arg.unlet.ul_forceit) == FAIL)
1422 goto failed;
1423 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001424 case ISN_UNLETENV:
1425 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1426 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001427
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428 // create a list from items on the stack; uses a single allocation
1429 // for the list header and the items
1430 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001431 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1432 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433 break;
1434
1435 // create a dict from items on the stack
1436 case ISN_NEWDICT:
1437 {
1438 int count = iptr->isn_arg.number;
1439 dict_T *dict = dict_alloc();
1440 dictitem_T *item;
1441
1442 if (dict == NULL)
1443 goto failed;
1444 for (idx = 0; idx < count; ++idx)
1445 {
1446 // check key type is VAR_STRING
1447 tv = STACK_TV_BOT(2 * (idx - count));
1448 item = dictitem_alloc(tv->vval.v_string);
1449 clear_tv(tv);
1450 if (item == NULL)
1451 goto failed;
1452 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1453 item->di_tv.v_lock = 0;
1454 if (dict_add(dict, item) == FAIL)
1455 goto failed;
1456 }
1457
1458 if (count > 0)
1459 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001460 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461 goto failed;
1462 else
1463 ++ectx.ec_stack.ga_len;
1464 tv = STACK_TV_BOT(-1);
1465 tv->v_type = VAR_DICT;
1466 tv->vval.v_dict = dict;
1467 ++dict->dv_refcount;
1468 }
1469 break;
1470
1471 // call a :def function
1472 case ISN_DCALL:
1473 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1474 iptr->isn_arg.dfunc.cdf_argcount,
1475 &ectx) == FAIL)
1476 goto failed;
1477 break;
1478
1479 // call a builtin function
1480 case ISN_BCALL:
1481 SOURCING_LNUM = iptr->isn_lnum;
1482 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1483 iptr->isn_arg.bfunc.cbf_argcount,
1484 &ectx) == FAIL)
1485 goto failed;
1486 break;
1487
1488 // call a funcref or partial
1489 case ISN_PCALL:
1490 {
1491 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1492 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001493 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494
1495 SOURCING_LNUM = iptr->isn_lnum;
1496 if (pfunc->cpf_top)
1497 {
1498 // funcref is above the arguments
1499 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1500 }
1501 else
1502 {
1503 // Get the funcref from the stack.
1504 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001505 partial_tv = *STACK_TV_BOT(0);
1506 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 }
1508 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001509 if (tv == &partial_tv)
1510 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 if (r == FAIL)
1512 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001513 }
1514 break;
1515
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001516 case ISN_PCALL_END:
1517 // PCALL finished, arguments have been consumed and replaced by
1518 // the return value. Now clear the funcref from the stack,
1519 // and move the return value in its place.
1520 --ectx.ec_stack.ga_len;
1521 clear_tv(STACK_TV_BOT(-1));
1522 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1523 break;
1524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 // call a user defined function or funcref/partial
1526 case ISN_UCALL:
1527 {
1528 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1529
1530 SOURCING_LNUM = iptr->isn_lnum;
1531 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001532 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001533 goto failed;
1534 }
1535 break;
1536
1537 // return from a :def function call
1538 case ISN_RETURN:
1539 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001540 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001541 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001542
1543 if (trystack->ga_len > 0)
1544 trycmd = ((trycmd_T *)trystack->ga_data)
1545 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001546 if (trycmd != NULL
1547 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 && trycmd->tcd_finally_idx != 0)
1549 {
1550 // jump to ":finally"
1551 ectx.ec_iidx = trycmd->tcd_finally_idx;
1552 trycmd->tcd_return = TRUE;
1553 }
1554 else
1555 {
1556 // Restore previous function. If the frame pointer
1557 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001558 if (ectx.ec_frame_idx == initial_frame_idx)
1559 {
1560 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1561 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001563 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001565 if (func_return(&ectx) == FAIL)
1566 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 }
1568 }
1569 break;
1570
1571 // push a function reference to a compiled function
1572 case ISN_FUNCREF:
1573 {
1574 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001575 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576
1577 pt = ALLOC_CLEAR_ONE(partial_T);
1578 if (pt == NULL)
1579 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001580 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001581 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001582 vim_free(pt);
1583 goto failed;
1584 }
1585 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1586 + iptr->isn_arg.funcref.fr_func;
1587 pt->pt_func = pt_dfunc->df_ufunc;
1588 pt->pt_refcount = 1;
1589 ++pt_dfunc->df_ufunc->uf_refcount;
1590
1591 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1592 {
1593 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1594 + ectx.ec_dfunc_idx;
1595
1596 // The closure needs to find arguments and local
1597 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001598 pt->pt_ectx_stack = &ectx.ec_stack;
1599 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001600
1601 // If this function returns and the closure is still
1602 // used, we need to make a copy of the context
1603 // (arguments and local variables). Store a reference
1604 // to the partial so we can handle that.
1605 ++pt->pt_refcount;
1606 tv = STACK_TV_VAR(dfunc->df_varcount
1607 + iptr->isn_arg.funcref.fr_var_idx);
1608 if (tv->v_type == VAR_PARTIAL)
1609 {
1610 // TODO: use a garray_T on ectx.
1611 emsg("Multiple closures not supported yet");
1612 goto failed;
1613 }
1614 tv->v_type = VAR_PARTIAL;
1615 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001616 }
1617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 tv = STACK_TV_BOT(0);
1619 ++ectx.ec_stack.ga_len;
1620 tv->vval.v_partial = pt;
1621 tv->v_type = VAR_PARTIAL;
1622 }
1623 break;
1624
1625 // jump if a condition is met
1626 case ISN_JUMP:
1627 {
1628 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1629 int jump = TRUE;
1630
1631 if (when != JUMP_ALWAYS)
1632 {
1633 tv = STACK_TV_BOT(-1);
1634 jump = tv2bool(tv);
1635 if (when == JUMP_IF_FALSE
1636 || when == JUMP_AND_KEEP_IF_FALSE)
1637 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001638 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 {
1640 // drop the value from the stack
1641 clear_tv(tv);
1642 --ectx.ec_stack.ga_len;
1643 }
1644 }
1645 if (jump)
1646 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1647 }
1648 break;
1649
1650 // top of a for loop
1651 case ISN_FOR:
1652 {
1653 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1654 typval_T *idxtv =
1655 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1656
1657 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001658 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 goto failed;
1660 if (++idxtv->vval.v_number >= list->lv_len)
1661 // past the end of the list, jump to "endfor"
1662 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1663 else if (list->lv_first == &range_list_item)
1664 {
1665 // non-materialized range() list
1666 tv = STACK_TV_BOT(0);
1667 tv->v_type = VAR_NUMBER;
1668 tv->vval.v_number = list_find_nr(
1669 list, idxtv->vval.v_number, NULL);
1670 ++ectx.ec_stack.ga_len;
1671 }
1672 else
1673 {
1674 listitem_T *li = list_find(list, idxtv->vval.v_number);
1675
1676 if (li == NULL)
1677 goto failed;
1678 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1679 ++ectx.ec_stack.ga_len;
1680 }
1681 }
1682 break;
1683
1684 // start of ":try" block
1685 case ISN_TRY:
1686 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001687 trycmd_T *trycmd = NULL;
1688
Bram Moolenaar270d0382020-05-15 21:42:53 +02001689 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 goto failed;
1691 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1692 + ectx.ec_trystack.ga_len;
1693 ++ectx.ec_trystack.ga_len;
1694 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001695 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1697 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001698 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 }
1700 break;
1701
1702 case ISN_PUSHEXC:
1703 if (current_exception == NULL)
1704 {
1705 iemsg("Evaluating catch while current_exception is NULL");
1706 goto failed;
1707 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001708 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 goto failed;
1710 tv = STACK_TV_BOT(0);
1711 ++ectx.ec_stack.ga_len;
1712 tv->v_type = VAR_STRING;
1713 tv->vval.v_string = vim_strsave(
1714 (char_u *)current_exception->value);
1715 break;
1716
1717 case ISN_CATCH:
1718 {
1719 garray_T *trystack = &ectx.ec_trystack;
1720
1721 if (trystack->ga_len > 0)
1722 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001723 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 + trystack->ga_len - 1;
1725 trycmd->tcd_caught = TRUE;
1726 }
1727 did_emsg = got_int = did_throw = FALSE;
1728 catch_exception(current_exception);
1729 }
1730 break;
1731
1732 // end of ":try" block
1733 case ISN_ENDTRY:
1734 {
1735 garray_T *trystack = &ectx.ec_trystack;
1736
1737 if (trystack->ga_len > 0)
1738 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001739 trycmd_T *trycmd = NULL;
1740
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001741 --trystack->ga_len;
1742 --trylevel;
1743 trycmd = ((trycmd_T *)trystack->ga_data)
1744 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001745 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001746 {
1747 // discard the exception
1748 if (caught_stack == current_exception)
1749 caught_stack = caught_stack->caught;
1750 discard_current_exception();
1751 }
1752
1753 if (trycmd->tcd_return)
1754 {
1755 // Restore previous function. If the frame pointer
1756 // is zero then there is none and we are done.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001757 if (ectx.ec_frame_idx == initial_frame_idx)
1758 {
1759 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1760 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 goto done;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001764 if (func_return(&ectx) == FAIL)
1765 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 }
1767 }
1768 }
1769 break;
1770
1771 case ISN_THROW:
1772 --ectx.ec_stack.ga_len;
1773 tv = STACK_TV_BOT(0);
1774 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1775 {
1776 vim_free(tv->vval.v_string);
1777 goto failed;
1778 }
1779 did_throw = TRUE;
1780 break;
1781
1782 // compare with special values
1783 case ISN_COMPAREBOOL:
1784 case ISN_COMPARESPECIAL:
1785 {
1786 typval_T *tv1 = STACK_TV_BOT(-2);
1787 typval_T *tv2 = STACK_TV_BOT(-1);
1788 varnumber_T arg1 = tv1->vval.v_number;
1789 varnumber_T arg2 = tv2->vval.v_number;
1790 int res;
1791
1792 switch (iptr->isn_arg.op.op_type)
1793 {
1794 case EXPR_EQUAL: res = arg1 == arg2; break;
1795 case EXPR_NEQUAL: res = arg1 != arg2; break;
1796 default: res = 0; break;
1797 }
1798
1799 --ectx.ec_stack.ga_len;
1800 tv1->v_type = VAR_BOOL;
1801 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1802 }
1803 break;
1804
1805 // Operation with two number arguments
1806 case ISN_OPNR:
1807 case ISN_COMPARENR:
1808 {
1809 typval_T *tv1 = STACK_TV_BOT(-2);
1810 typval_T *tv2 = STACK_TV_BOT(-1);
1811 varnumber_T arg1 = tv1->vval.v_number;
1812 varnumber_T arg2 = tv2->vval.v_number;
1813 varnumber_T res;
1814
1815 switch (iptr->isn_arg.op.op_type)
1816 {
1817 case EXPR_MULT: res = arg1 * arg2; break;
1818 case EXPR_DIV: res = arg1 / arg2; break;
1819 case EXPR_REM: res = arg1 % arg2; break;
1820 case EXPR_SUB: res = arg1 - arg2; break;
1821 case EXPR_ADD: res = arg1 + arg2; break;
1822
1823 case EXPR_EQUAL: res = arg1 == arg2; break;
1824 case EXPR_NEQUAL: res = arg1 != arg2; break;
1825 case EXPR_GREATER: res = arg1 > arg2; break;
1826 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1827 case EXPR_SMALLER: res = arg1 < arg2; break;
1828 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1829 default: res = 0; break;
1830 }
1831
1832 --ectx.ec_stack.ga_len;
1833 if (iptr->isn_type == ISN_COMPARENR)
1834 {
1835 tv1->v_type = VAR_BOOL;
1836 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1837 }
1838 else
1839 tv1->vval.v_number = res;
1840 }
1841 break;
1842
1843 // Computation with two float arguments
1844 case ISN_OPFLOAT:
1845 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001846#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 {
1848 typval_T *tv1 = STACK_TV_BOT(-2);
1849 typval_T *tv2 = STACK_TV_BOT(-1);
1850 float_T arg1 = tv1->vval.v_float;
1851 float_T arg2 = tv2->vval.v_float;
1852 float_T res = 0;
1853 int cmp = FALSE;
1854
1855 switch (iptr->isn_arg.op.op_type)
1856 {
1857 case EXPR_MULT: res = arg1 * arg2; break;
1858 case EXPR_DIV: res = arg1 / arg2; break;
1859 case EXPR_SUB: res = arg1 - arg2; break;
1860 case EXPR_ADD: res = arg1 + arg2; break;
1861
1862 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1863 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1864 case EXPR_GREATER: cmp = arg1 > arg2; break;
1865 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1866 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1867 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1868 default: cmp = 0; break;
1869 }
1870 --ectx.ec_stack.ga_len;
1871 if (iptr->isn_type == ISN_COMPAREFLOAT)
1872 {
1873 tv1->v_type = VAR_BOOL;
1874 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1875 }
1876 else
1877 tv1->vval.v_float = res;
1878 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01001879#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 break;
1881
1882 case ISN_COMPARELIST:
1883 {
1884 typval_T *tv1 = STACK_TV_BOT(-2);
1885 typval_T *tv2 = STACK_TV_BOT(-1);
1886 list_T *arg1 = tv1->vval.v_list;
1887 list_T *arg2 = tv2->vval.v_list;
1888 int cmp = FALSE;
1889 int ic = iptr->isn_arg.op.op_ic;
1890
1891 switch (iptr->isn_arg.op.op_type)
1892 {
1893 case EXPR_EQUAL: cmp =
1894 list_equal(arg1, arg2, ic, FALSE); break;
1895 case EXPR_NEQUAL: cmp =
1896 !list_equal(arg1, arg2, ic, FALSE); break;
1897 case EXPR_IS: cmp = arg1 == arg2; break;
1898 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1899 default: cmp = 0; break;
1900 }
1901 --ectx.ec_stack.ga_len;
1902 clear_tv(tv1);
1903 clear_tv(tv2);
1904 tv1->v_type = VAR_BOOL;
1905 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1906 }
1907 break;
1908
1909 case ISN_COMPAREBLOB:
1910 {
1911 typval_T *tv1 = STACK_TV_BOT(-2);
1912 typval_T *tv2 = STACK_TV_BOT(-1);
1913 blob_T *arg1 = tv1->vval.v_blob;
1914 blob_T *arg2 = tv2->vval.v_blob;
1915 int cmp = FALSE;
1916
1917 switch (iptr->isn_arg.op.op_type)
1918 {
1919 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
1920 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
1921 case EXPR_IS: cmp = arg1 == arg2; break;
1922 case EXPR_ISNOT: cmp = arg1 != arg2; break;
1923 default: cmp = 0; break;
1924 }
1925 --ectx.ec_stack.ga_len;
1926 clear_tv(tv1);
1927 clear_tv(tv2);
1928 tv1->v_type = VAR_BOOL;
1929 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
1930 }
1931 break;
1932
1933 // TODO: handle separately
1934 case ISN_COMPARESTRING:
1935 case ISN_COMPAREDICT:
1936 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001937 case ISN_COMPAREANY:
1938 {
1939 typval_T *tv1 = STACK_TV_BOT(-2);
1940 typval_T *tv2 = STACK_TV_BOT(-1);
1941 exptype_T exptype = iptr->isn_arg.op.op_type;
1942 int ic = iptr->isn_arg.op.op_ic;
1943
1944 typval_compare(tv1, tv2, exptype, ic);
1945 clear_tv(tv2);
1946 tv1->v_type = VAR_BOOL;
1947 tv1->vval.v_number = tv1->vval.v_number
1948 ? VVAL_TRUE : VVAL_FALSE;
1949 --ectx.ec_stack.ga_len;
1950 }
1951 break;
1952
1953 case ISN_ADDLIST:
1954 case ISN_ADDBLOB:
1955 {
1956 typval_T *tv1 = STACK_TV_BOT(-2);
1957 typval_T *tv2 = STACK_TV_BOT(-1);
1958
1959 if (iptr->isn_type == ISN_ADDLIST)
1960 eval_addlist(tv1, tv2);
1961 else
1962 eval_addblob(tv1, tv2);
1963 clear_tv(tv2);
1964 --ectx.ec_stack.ga_len;
1965 }
1966 break;
1967
1968 // Computation with two arguments of unknown type
1969 case ISN_OPANY:
1970 {
1971 typval_T *tv1 = STACK_TV_BOT(-2);
1972 typval_T *tv2 = STACK_TV_BOT(-1);
1973 varnumber_T n1, n2;
1974#ifdef FEAT_FLOAT
1975 float_T f1 = 0, f2 = 0;
1976#endif
1977 int error = FALSE;
1978
1979 if (iptr->isn_arg.op.op_type == EXPR_ADD)
1980 {
1981 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
1982 {
1983 eval_addlist(tv1, tv2);
1984 clear_tv(tv2);
1985 --ectx.ec_stack.ga_len;
1986 break;
1987 }
1988 else if (tv1->v_type == VAR_BLOB
1989 && tv2->v_type == VAR_BLOB)
1990 {
1991 eval_addblob(tv1, tv2);
1992 clear_tv(tv2);
1993 --ectx.ec_stack.ga_len;
1994 break;
1995 }
1996 }
1997#ifdef FEAT_FLOAT
1998 if (tv1->v_type == VAR_FLOAT)
1999 {
2000 f1 = tv1->vval.v_float;
2001 n1 = 0;
2002 }
2003 else
2004#endif
2005 {
2006 n1 = tv_get_number_chk(tv1, &error);
2007 if (error)
2008 goto failed;
2009#ifdef FEAT_FLOAT
2010 if (tv2->v_type == VAR_FLOAT)
2011 f1 = n1;
2012#endif
2013 }
2014#ifdef FEAT_FLOAT
2015 if (tv2->v_type == VAR_FLOAT)
2016 {
2017 f2 = tv2->vval.v_float;
2018 n2 = 0;
2019 }
2020 else
2021#endif
2022 {
2023 n2 = tv_get_number_chk(tv2, &error);
2024 if (error)
2025 goto failed;
2026#ifdef FEAT_FLOAT
2027 if (tv1->v_type == VAR_FLOAT)
2028 f2 = n2;
2029#endif
2030 }
2031#ifdef FEAT_FLOAT
2032 // if there is a float on either side the result is a float
2033 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2034 {
2035 switch (iptr->isn_arg.op.op_type)
2036 {
2037 case EXPR_MULT: f1 = f1 * f2; break;
2038 case EXPR_DIV: f1 = f1 / f2; break;
2039 case EXPR_SUB: f1 = f1 - f2; break;
2040 case EXPR_ADD: f1 = f1 + f2; break;
2041 default: emsg(_(e_modulus)); goto failed;
2042 }
2043 clear_tv(tv1);
2044 clear_tv(tv2);
2045 tv1->v_type = VAR_FLOAT;
2046 tv1->vval.v_float = f1;
2047 --ectx.ec_stack.ga_len;
2048 }
2049 else
2050#endif
2051 {
2052 switch (iptr->isn_arg.op.op_type)
2053 {
2054 case EXPR_MULT: n1 = n1 * n2; break;
2055 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2056 case EXPR_SUB: n1 = n1 - n2; break;
2057 case EXPR_ADD: n1 = n1 + n2; break;
2058 default: n1 = num_modulus(n1, n2); break;
2059 }
2060 clear_tv(tv1);
2061 clear_tv(tv2);
2062 tv1->v_type = VAR_NUMBER;
2063 tv1->vval.v_number = n1;
2064 --ectx.ec_stack.ga_len;
2065 }
2066 }
2067 break;
2068
2069 case ISN_CONCAT:
2070 {
2071 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2072 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2073 char_u *res;
2074
2075 res = concat_str(str1, str2);
2076 clear_tv(STACK_TV_BOT(-2));
2077 clear_tv(STACK_TV_BOT(-1));
2078 --ectx.ec_stack.ga_len;
2079 STACK_TV_BOT(-1)->vval.v_string = res;
2080 }
2081 break;
2082
2083 case ISN_INDEX:
2084 {
2085 list_T *list;
2086 varnumber_T n;
2087 listitem_T *li;
2088
2089 // list index: list is at stack-2, index at stack-1
2090 tv = STACK_TV_BOT(-2);
2091 if (tv->v_type != VAR_LIST)
2092 {
2093 emsg(_(e_listreq));
2094 goto failed;
2095 }
2096 list = tv->vval.v_list;
2097
2098 tv = STACK_TV_BOT(-1);
2099 if (tv->v_type != VAR_NUMBER)
2100 {
2101 emsg(_(e_number_exp));
2102 goto failed;
2103 }
2104 n = tv->vval.v_number;
2105 clear_tv(tv);
2106 if ((li = list_find(list, n)) == NULL)
2107 {
2108 semsg(_(e_listidx), n);
2109 goto failed;
2110 }
2111 --ectx.ec_stack.ga_len;
2112 clear_tv(STACK_TV_BOT(-1));
2113 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2114 }
2115 break;
2116
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002117 case ISN_GETITEM:
2118 {
2119 listitem_T *li;
2120 int index = iptr->isn_arg.number;
2121
2122 // get list item: list is at stack-1, push item
2123 tv = STACK_TV_BOT(-1);
2124 if (tv->v_type != VAR_LIST)
2125 {
2126 emsg(_(e_listreq));
2127 goto failed;
2128 }
2129 if ((li = list_find(tv->vval.v_list, index)) == NULL)
2130 {
2131 semsg(_(e_listidx), index);
2132 goto failed;
2133 }
2134
2135 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2136 goto failed;
2137 ++ectx.ec_stack.ga_len;
2138 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2139 }
2140 break;
2141
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 case ISN_MEMBER:
2143 {
2144 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002145 char_u *key;
2146 dictitem_T *di;
2147
2148 // dict member: dict is at stack-2, key at stack-1
2149 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002150 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002151 dict = tv->vval.v_dict;
2152
2153 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002154 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002155 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002156
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002157 if ((di = dict_find(dict, key, -1)) == NULL)
2158 {
2159 semsg(_(e_dictkey), key);
2160 goto failed;
2161 }
2162 --ectx.ec_stack.ga_len;
2163 clear_tv(tv);
2164 clear_tv(STACK_TV_BOT(-1));
2165 copy_tv(&di->di_tv, STACK_TV_BOT(-1));
2166 }
2167 break;
2168
2169 // dict member with string key
2170 case ISN_STRINGMEMBER:
2171 {
2172 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 dictitem_T *di;
2174
2175 tv = STACK_TV_BOT(-1);
2176 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2177 {
2178 emsg(_(e_dictreq));
2179 goto failed;
2180 }
2181 dict = tv->vval.v_dict;
2182
2183 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2184 == NULL)
2185 {
2186 semsg(_(e_dictkey), iptr->isn_arg.string);
2187 goto failed;
2188 }
2189 clear_tv(tv);
2190 copy_tv(&di->di_tv, tv);
2191 }
2192 break;
2193
2194 case ISN_NEGATENR:
2195 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002196 if (tv->v_type != VAR_NUMBER
2197#ifdef FEAT_FLOAT
2198 && tv->v_type != VAR_FLOAT
2199#endif
2200 )
2201 {
2202 emsg(_(e_number_exp));
2203 goto failed;
2204 }
2205#ifdef FEAT_FLOAT
2206 if (tv->v_type == VAR_FLOAT)
2207 tv->vval.v_float = -tv->vval.v_float;
2208 else
2209#endif
2210 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002211 break;
2212
2213 case ISN_CHECKNR:
2214 {
2215 int error = FALSE;
2216
2217 tv = STACK_TV_BOT(-1);
2218 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002219 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 (void)tv_get_number_chk(tv, &error);
2221 if (error)
2222 goto failed;
2223 }
2224 break;
2225
2226 case ISN_CHECKTYPE:
2227 {
2228 checktype_T *ct = &iptr->isn_arg.type;
2229
2230 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002231 // TODO: better type comparison
2232 if (tv->v_type != ct->ct_type
2233 && !((tv->v_type == VAR_PARTIAL
2234 && ct->ct_type == VAR_FUNC)
2235 || (tv->v_type == VAR_FUNC
2236 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002237 {
2238 semsg(_("E1029: Expected %s but got %s"),
2239 vartype_name(ct->ct_type),
2240 vartype_name(tv->v_type));
2241 goto failed;
2242 }
2243 }
2244 break;
2245
2246 case ISN_2BOOL:
2247 {
2248 int n;
2249
2250 tv = STACK_TV_BOT(-1);
2251 n = tv2bool(tv);
2252 if (iptr->isn_arg.number) // invert
2253 n = !n;
2254 clear_tv(tv);
2255 tv->v_type = VAR_BOOL;
2256 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2257 }
2258 break;
2259
2260 case ISN_2STRING:
2261 {
2262 char_u *str;
2263
2264 tv = STACK_TV_BOT(iptr->isn_arg.number);
2265 if (tv->v_type != VAR_STRING)
2266 {
2267 str = typval_tostring(tv);
2268 clear_tv(tv);
2269 tv->v_type = VAR_STRING;
2270 tv->vval.v_string = str;
2271 }
2272 }
2273 break;
2274
2275 case ISN_DROP:
2276 --ectx.ec_stack.ga_len;
2277 clear_tv(STACK_TV_BOT(0));
2278 break;
2279 }
2280 }
2281
2282done:
2283 // function finished, get result from the stack.
2284 tv = STACK_TV_BOT(-1);
2285 *rettv = *tv;
2286 tv->v_type = VAR_UNKNOWN;
2287 ret = OK;
2288
2289failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002290 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002291 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002292 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002293failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002294 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002295
2296 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2298 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002300 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002301 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 return ret;
2303}
2304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305/*
2306 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002307 * We don't really need this at runtime, but we do have tests that require it,
2308 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 */
2310 void
2311ex_disassemble(exarg_T *eap)
2312{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002313 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002314 char_u *fname;
2315 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 dfunc_T *dfunc;
2317 isn_T *instr;
2318 int current;
2319 int line_idx = 0;
2320 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002321 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002323 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002324 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002325 if (fname == NULL)
2326 {
2327 semsg(_(e_invarg2), eap->arg);
2328 return;
2329 }
2330
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002331 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002332 if (ufunc == NULL)
2333 {
2334 char_u *p = untrans_function_name(fname);
2335
2336 if (p != NULL)
2337 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002338 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002339 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002340 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002341 if (ufunc == NULL)
2342 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002343 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 return;
2345 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02002346 if (ufunc->uf_dfunc_idx == UF_TO_BE_COMPILED
2347 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2348 return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 if (ufunc->uf_dfunc_idx < 0)
2350 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002351 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 return;
2353 }
2354 if (ufunc->uf_name_exp != NULL)
2355 msg((char *)ufunc->uf_name_exp);
2356 else
2357 msg((char *)ufunc->uf_name);
2358
2359 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2360 instr = dfunc->df_instr;
2361 for (current = 0; current < dfunc->df_instr_count; ++current)
2362 {
2363 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002364 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365
2366 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2367 {
2368 if (current > prev_current)
2369 {
2370 msg_puts("\n\n");
2371 prev_current = current;
2372 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002373 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2374 if (line != NULL)
2375 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 }
2377
2378 switch (iptr->isn_type)
2379 {
2380 case ISN_EXEC:
2381 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2382 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002383 case ISN_EXECCONCAT:
2384 smsg("%4d EXECCONCAT %lld", current,
2385 (long long)iptr->isn_arg.number);
2386 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 case ISN_ECHO:
2388 {
2389 echo_T *echo = &iptr->isn_arg.echo;
2390
2391 smsg("%4d %s %d", current,
2392 echo->echo_with_white ? "ECHO" : "ECHON",
2393 echo->echo_count);
2394 }
2395 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002396 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002397 smsg("%4d EXECUTE %lld", current,
2398 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002399 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002400 case ISN_ECHOMSG:
2401 smsg("%4d ECHOMSG %lld", current,
2402 (long long)(iptr->isn_arg.number));
2403 break;
2404 case ISN_ECHOERR:
2405 smsg("%4d ECHOERR %lld", current,
2406 (long long)(iptr->isn_arg.number));
2407 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002409 case ISN_LOADOUTER:
2410 {
2411 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2412
2413 if (iptr->isn_arg.number < 0)
2414 smsg("%4d LOAD%s arg[%lld]", current, add,
2415 (long long)(iptr->isn_arg.number
2416 + STACK_FRAME_SIZE));
2417 else
2418 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002419 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002420 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002421 break;
2422 case ISN_LOADV:
2423 smsg("%4d LOADV v:%s", current,
2424 get_vim_var_name(iptr->isn_arg.number));
2425 break;
2426 case ISN_LOADSCRIPT:
2427 {
2428 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002429 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2431 + iptr->isn_arg.script.script_idx;
2432
2433 smsg("%4d LOADSCRIPT %s from %s", current,
2434 sv->sv_name, si->sn_name);
2435 }
2436 break;
2437 case ISN_LOADS:
2438 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002439 scriptitem_T *si = SCRIPT_ITEM(
2440 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441
2442 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002443 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 }
2445 break;
2446 case ISN_LOADG:
2447 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2448 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002449 case ISN_LOADB:
2450 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2451 break;
2452 case ISN_LOADW:
2453 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2454 break;
2455 case ISN_LOADT:
2456 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 case ISN_LOADOPT:
2459 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2460 break;
2461 case ISN_LOADENV:
2462 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2463 break;
2464 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002465 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 break;
2467
2468 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002469 case ISN_STOREOUTER:
2470 {
2471 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2472
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002473 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002474 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002475 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002476 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002477 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002478 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002481 case ISN_STOREV:
2482 smsg("%4d STOREV v:%s", current,
2483 get_vim_var_name(iptr->isn_arg.number));
2484 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002485 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002486 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2487 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002488 case ISN_STOREB:
2489 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2490 break;
2491 case ISN_STOREW:
2492 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2493 break;
2494 case ISN_STORET:
2495 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2496 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002497 case ISN_STORES:
2498 {
2499 scriptitem_T *si = SCRIPT_ITEM(
2500 iptr->isn_arg.loadstore.ls_sid);
2501
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002502 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002503 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 break;
2506 case ISN_STORESCRIPT:
2507 {
2508 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002509 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2511 + iptr->isn_arg.script.script_idx;
2512
2513 smsg("%4d STORESCRIPT %s in %s", current,
2514 sv->sv_name, si->sn_name);
2515 }
2516 break;
2517 case ISN_STOREOPT:
2518 smsg("%4d STOREOPT &%s", current,
2519 iptr->isn_arg.storeopt.so_name);
2520 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002521 case ISN_STOREENV:
2522 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2523 break;
2524 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002525 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002526 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 case ISN_STORENR:
2528 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002529 iptr->isn_arg.storenr.stnr_val,
2530 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 break;
2532
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002533 case ISN_STORELIST:
2534 smsg("%4d STORELIST", current);
2535 break;
2536
2537 case ISN_STOREDICT:
2538 smsg("%4d STOREDICT", current);
2539 break;
2540
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002541 // constants
2542 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002543 smsg("%4d PUSHNR %lld", current,
2544 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 break;
2546 case ISN_PUSHBOOL:
2547 case ISN_PUSHSPEC:
2548 smsg("%4d PUSH %s", current,
2549 get_var_special_name(iptr->isn_arg.number));
2550 break;
2551 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002552#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002554#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 break;
2556 case ISN_PUSHS:
2557 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2558 break;
2559 case ISN_PUSHBLOB:
2560 {
2561 char_u *r;
2562 char_u numbuf[NUMBUFLEN];
2563 char_u *tofree;
2564
2565 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002566 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 vim_free(tofree);
2568 }
2569 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002570 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002571 {
2572 char *name = (char *)iptr->isn_arg.string;
2573
2574 smsg("%4d PUSHFUNC \"%s\"", current,
2575 name == NULL ? "[none]" : name);
2576 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002577 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002578 case ISN_PUSHCHANNEL:
2579#ifdef FEAT_JOB_CHANNEL
2580 {
2581 channel_T *channel = iptr->isn_arg.channel;
2582
2583 smsg("%4d PUSHCHANNEL %d", current,
2584 channel == NULL ? 0 : channel->ch_id);
2585 }
2586#endif
2587 break;
2588 case ISN_PUSHJOB:
2589#ifdef FEAT_JOB_CHANNEL
2590 {
2591 typval_T tv;
2592 char_u *name;
2593
2594 tv.v_type = VAR_JOB;
2595 tv.vval.v_job = iptr->isn_arg.job;
2596 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002597 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002598 }
2599#endif
2600 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 case ISN_PUSHEXC:
2602 smsg("%4d PUSH v:exception", current);
2603 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002604 case ISN_UNLET:
2605 smsg("%4d UNLET%s %s", current,
2606 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2607 iptr->isn_arg.unlet.ul_name);
2608 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002609 case ISN_UNLETENV:
2610 smsg("%4d UNLETENV%s $%s", current,
2611 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2612 iptr->isn_arg.unlet.ul_name);
2613 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002614 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002615 smsg("%4d NEWLIST size %lld", current,
2616 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 break;
2618 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002619 smsg("%4d NEWDICT size %lld", current,
2620 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 break;
2622
2623 // function call
2624 case ISN_BCALL:
2625 {
2626 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2627
2628 smsg("%4d BCALL %s(argc %d)", current,
2629 internal_func_name(cbfunc->cbf_idx),
2630 cbfunc->cbf_argcount);
2631 }
2632 break;
2633 case ISN_DCALL:
2634 {
2635 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2636 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2637 + cdfunc->cdf_idx;
2638
2639 smsg("%4d DCALL %s(argc %d)", current,
2640 df->df_ufunc->uf_name_exp != NULL
2641 ? df->df_ufunc->uf_name_exp
2642 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2643 }
2644 break;
2645 case ISN_UCALL:
2646 {
2647 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2648
2649 smsg("%4d UCALL %s(argc %d)", current,
2650 cufunc->cuf_name, cufunc->cuf_argcount);
2651 }
2652 break;
2653 case ISN_PCALL:
2654 {
2655 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2656
2657 smsg("%4d PCALL%s (argc %d)", current,
2658 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2659 }
2660 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002661 case ISN_PCALL_END:
2662 smsg("%4d PCALL end", current);
2663 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 case ISN_RETURN:
2665 smsg("%4d RETURN", current);
2666 break;
2667 case ISN_FUNCREF:
2668 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002669 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002670 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002671 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002673 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002674 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002675 }
2676 break;
2677
2678 case ISN_JUMP:
2679 {
2680 char *when = "?";
2681
2682 switch (iptr->isn_arg.jump.jump_when)
2683 {
2684 case JUMP_ALWAYS:
2685 when = "JUMP";
2686 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 case JUMP_AND_KEEP_IF_TRUE:
2688 when = "JUMP_AND_KEEP_IF_TRUE";
2689 break;
2690 case JUMP_IF_FALSE:
2691 when = "JUMP_IF_FALSE";
2692 break;
2693 case JUMP_AND_KEEP_IF_FALSE:
2694 when = "JUMP_AND_KEEP_IF_FALSE";
2695 break;
2696 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002697 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002698 iptr->isn_arg.jump.jump_where);
2699 }
2700 break;
2701
2702 case ISN_FOR:
2703 {
2704 forloop_T *forloop = &iptr->isn_arg.forloop;
2705
2706 smsg("%4d FOR $%d -> %d", current,
2707 forloop->for_idx, forloop->for_end);
2708 }
2709 break;
2710
2711 case ISN_TRY:
2712 {
2713 try_T *try = &iptr->isn_arg.try;
2714
2715 smsg("%4d TRY catch -> %d, finally -> %d", current,
2716 try->try_catch, try->try_finally);
2717 }
2718 break;
2719 case ISN_CATCH:
2720 // TODO
2721 smsg("%4d CATCH", current);
2722 break;
2723 case ISN_ENDTRY:
2724 smsg("%4d ENDTRY", current);
2725 break;
2726 case ISN_THROW:
2727 smsg("%4d THROW", current);
2728 break;
2729
2730 // expression operations on number
2731 case ISN_OPNR:
2732 case ISN_OPFLOAT:
2733 case ISN_OPANY:
2734 {
2735 char *what;
2736 char *ins;
2737
2738 switch (iptr->isn_arg.op.op_type)
2739 {
2740 case EXPR_MULT: what = "*"; break;
2741 case EXPR_DIV: what = "/"; break;
2742 case EXPR_REM: what = "%"; break;
2743 case EXPR_SUB: what = "-"; break;
2744 case EXPR_ADD: what = "+"; break;
2745 default: what = "???"; break;
2746 }
2747 switch (iptr->isn_type)
2748 {
2749 case ISN_OPNR: ins = "OPNR"; break;
2750 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2751 case ISN_OPANY: ins = "OPANY"; break;
2752 default: ins = "???"; break;
2753 }
2754 smsg("%4d %s %s", current, ins, what);
2755 }
2756 break;
2757
2758 case ISN_COMPAREBOOL:
2759 case ISN_COMPARESPECIAL:
2760 case ISN_COMPARENR:
2761 case ISN_COMPAREFLOAT:
2762 case ISN_COMPARESTRING:
2763 case ISN_COMPAREBLOB:
2764 case ISN_COMPARELIST:
2765 case ISN_COMPAREDICT:
2766 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 case ISN_COMPAREANY:
2768 {
2769 char *p;
2770 char buf[10];
2771 char *type;
2772
2773 switch (iptr->isn_arg.op.op_type)
2774 {
2775 case EXPR_EQUAL: p = "=="; break;
2776 case EXPR_NEQUAL: p = "!="; break;
2777 case EXPR_GREATER: p = ">"; break;
2778 case EXPR_GEQUAL: p = ">="; break;
2779 case EXPR_SMALLER: p = "<"; break;
2780 case EXPR_SEQUAL: p = "<="; break;
2781 case EXPR_MATCH: p = "=~"; break;
2782 case EXPR_IS: p = "is"; break;
2783 case EXPR_ISNOT: p = "isnot"; break;
2784 case EXPR_NOMATCH: p = "!~"; break;
2785 default: p = "???"; break;
2786 }
2787 STRCPY(buf, p);
2788 if (iptr->isn_arg.op.op_ic == TRUE)
2789 strcat(buf, "?");
2790 switch(iptr->isn_type)
2791 {
2792 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2793 case ISN_COMPARESPECIAL:
2794 type = "COMPARESPECIAL"; break;
2795 case ISN_COMPARENR: type = "COMPARENR"; break;
2796 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2797 case ISN_COMPARESTRING:
2798 type = "COMPARESTRING"; break;
2799 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2800 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2801 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2802 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2804 default: type = "???"; break;
2805 }
2806
2807 smsg("%4d %s %s", current, type, buf);
2808 }
2809 break;
2810
2811 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2812 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2813
2814 // expression operations
2815 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2816 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002817 case ISN_GETITEM: smsg("%4d ITEM %lld",
2818 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002819 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2820 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002821 iptr->isn_arg.string); break;
2822 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2823
2824 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2825 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2826 vartype_name(iptr->isn_arg.type.ct_type),
2827 iptr->isn_arg.type.ct_off);
2828 break;
2829 case ISN_2BOOL: if (iptr->isn_arg.number)
2830 smsg("%4d INVERT (!val)", current);
2831 else
2832 smsg("%4d 2BOOL (!!val)", current);
2833 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002834 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2835 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 break;
2837
2838 case ISN_DROP: smsg("%4d DROP", current); break;
2839 }
2840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841}
2842
2843/*
2844 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2845 * list, etc. Mostly like what JavaScript does, except that empty list and
2846 * empty dictionary are FALSE.
2847 */
2848 int
2849tv2bool(typval_T *tv)
2850{
2851 switch (tv->v_type)
2852 {
2853 case VAR_NUMBER:
2854 return tv->vval.v_number != 0;
2855 case VAR_FLOAT:
2856#ifdef FEAT_FLOAT
2857 return tv->vval.v_float != 0.0;
2858#else
2859 break;
2860#endif
2861 case VAR_PARTIAL:
2862 return tv->vval.v_partial != NULL;
2863 case VAR_FUNC:
2864 case VAR_STRING:
2865 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2866 case VAR_LIST:
2867 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2868 case VAR_DICT:
2869 return tv->vval.v_dict != NULL
2870 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2871 case VAR_BOOL:
2872 case VAR_SPECIAL:
2873 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2874 case VAR_JOB:
2875#ifdef FEAT_JOB_CHANNEL
2876 return tv->vval.v_job != NULL;
2877#else
2878 break;
2879#endif
2880 case VAR_CHANNEL:
2881#ifdef FEAT_JOB_CHANNEL
2882 return tv->vval.v_channel != NULL;
2883#else
2884 break;
2885#endif
2886 case VAR_BLOB:
2887 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2888 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002889 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890 case VAR_VOID:
2891 break;
2892 }
2893 return FALSE;
2894}
2895
2896/*
2897 * If "tv" is a string give an error and return FAIL.
2898 */
2899 int
2900check_not_string(typval_T *tv)
2901{
2902 if (tv->v_type == VAR_STRING)
2903 {
2904 emsg(_("E1030: Using a String as a Number"));
2905 clear_tv(tv);
2906 return FAIL;
2907 }
2908 return OK;
2909}
2910
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911
2912#endif // FEAT_EVAL