blob: 7afa3fc5ec9dad2251a588b99cb8a84986b6b0bf [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 Moolenaar0cb5bcf2020-06-20 18:19:09 +0200490 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200491 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
492 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200493 if (ufunc->uf_def_status == UF_COMPILED)
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 Moolenaar0cb5bcf2020-06-20 18:19:09 +0200674 if (ufunc->uf_def_status == UF_NOT_COMPILED
675 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200676 && 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 Moolenaar9a78e6d2020-07-01 18:29:55 +02001092 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001093 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
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001108 (void)eval_env_var(&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;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002088 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002089
2090 // list index: list is at stack-2, index at stack-1
2091 tv = STACK_TV_BOT(-2);
2092 if (tv->v_type != VAR_LIST)
2093 {
2094 emsg(_(e_listreq));
2095 goto failed;
2096 }
2097 list = tv->vval.v_list;
2098
2099 tv = STACK_TV_BOT(-1);
2100 if (tv->v_type != VAR_NUMBER)
2101 {
2102 emsg(_(e_number_exp));
2103 goto failed;
2104 }
2105 n = tv->vval.v_number;
2106 clear_tv(tv);
2107 if ((li = list_find(list, n)) == NULL)
2108 {
2109 semsg(_(e_listidx), n);
2110 goto failed;
2111 }
2112 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002113 // Clear the list after getting the item, to avoid that it
2114 // make the item invalid.
2115 tv = STACK_TV_BOT(-1);
2116 temp_tv = *tv;
2117 copy_tv(&li->li_tv, tv);
2118 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 }
2120 break;
2121
Bram Moolenaar9af78762020-06-16 11:34:42 +02002122 case ISN_SLICE:
2123 {
2124 list_T *list;
2125 int count = iptr->isn_arg.number;
2126
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002127 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002128 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002129 list = tv->vval.v_list;
2130
2131 // no error for short list, expect it to be checked earlier
2132 if (list != NULL && list->lv_len >= count)
2133 {
2134 list_T *newlist = list_slice(list,
2135 count, list->lv_len - 1);
2136
2137 if (newlist != NULL)
2138 {
2139 list_unref(list);
2140 tv->vval.v_list = newlist;
2141 ++newlist->lv_refcount;
2142 }
2143 }
2144 }
2145 break;
2146
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002147 case ISN_GETITEM:
2148 {
2149 listitem_T *li;
2150 int index = iptr->isn_arg.number;
2151
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002152 // Get list item: list is at stack-1, push item.
2153 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002154 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002155 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002156
2157 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2158 goto failed;
2159 ++ectx.ec_stack.ga_len;
2160 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2161 }
2162 break;
2163
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 case ISN_MEMBER:
2165 {
2166 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002167 char_u *key;
2168 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002169 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002170
2171 // dict member: dict is at stack-2, key at stack-1
2172 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002173 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002174 dict = tv->vval.v_dict;
2175
2176 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002177 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002178 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002179
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002180 if ((di = dict_find(dict, key, -1)) == NULL)
2181 {
2182 semsg(_(e_dictkey), key);
2183 goto failed;
2184 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002185 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002186 --ectx.ec_stack.ga_len;
2187 // Clear the dict after getting the item, to avoid that it
2188 // make the item invalid.
2189 tv = STACK_TV_BOT(-1);
2190 temp_tv = *tv;
2191 copy_tv(&di->di_tv, tv);
2192 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002193 }
2194 break;
2195
2196 // dict member with string key
2197 case ISN_STRINGMEMBER:
2198 {
2199 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002201 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202
2203 tv = STACK_TV_BOT(-1);
2204 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2205 {
2206 emsg(_(e_dictreq));
2207 goto failed;
2208 }
2209 dict = tv->vval.v_dict;
2210
2211 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2212 == NULL)
2213 {
2214 semsg(_(e_dictkey), iptr->isn_arg.string);
2215 goto failed;
2216 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002217 // Clear the dict after getting the item, to avoid that it
2218 // make the item invalid.
2219 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002221 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 break;
2224
2225 case ISN_NEGATENR:
2226 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002227 if (tv->v_type != VAR_NUMBER
2228#ifdef FEAT_FLOAT
2229 && tv->v_type != VAR_FLOAT
2230#endif
2231 )
2232 {
2233 emsg(_(e_number_exp));
2234 goto failed;
2235 }
2236#ifdef FEAT_FLOAT
2237 if (tv->v_type == VAR_FLOAT)
2238 tv->vval.v_float = -tv->vval.v_float;
2239 else
2240#endif
2241 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 break;
2243
2244 case ISN_CHECKNR:
2245 {
2246 int error = FALSE;
2247
2248 tv = STACK_TV_BOT(-1);
2249 if (check_not_string(tv) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002250 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002251 (void)tv_get_number_chk(tv, &error);
2252 if (error)
2253 goto failed;
2254 }
2255 break;
2256
2257 case ISN_CHECKTYPE:
2258 {
2259 checktype_T *ct = &iptr->isn_arg.type;
2260
2261 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002262 // TODO: better type comparison
2263 if (tv->v_type != ct->ct_type
2264 && !((tv->v_type == VAR_PARTIAL
2265 && ct->ct_type == VAR_FUNC)
2266 || (tv->v_type == VAR_FUNC
2267 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 {
2269 semsg(_("E1029: Expected %s but got %s"),
2270 vartype_name(ct->ct_type),
2271 vartype_name(tv->v_type));
2272 goto failed;
2273 }
2274 }
2275 break;
2276
Bram Moolenaar9af78762020-06-16 11:34:42 +02002277 case ISN_CHECKLEN:
2278 {
2279 int min_len = iptr->isn_arg.checklen.cl_min_len;
2280 list_T *list = NULL;
2281
2282 tv = STACK_TV_BOT(-1);
2283 if (tv->v_type == VAR_LIST)
2284 list = tv->vval.v_list;
2285 if (list == NULL || list->lv_len < min_len
2286 || (list->lv_len > min_len
2287 && !iptr->isn_arg.checklen.cl_more_OK))
2288 {
2289 semsg(_("E1093: Expected %d items but got %d"),
2290 min_len, list == NULL ? 0 : list->lv_len);
2291 goto failed;
2292 }
2293 }
2294 break;
2295
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 case ISN_2BOOL:
2297 {
2298 int n;
2299
2300 tv = STACK_TV_BOT(-1);
2301 n = tv2bool(tv);
2302 if (iptr->isn_arg.number) // invert
2303 n = !n;
2304 clear_tv(tv);
2305 tv->v_type = VAR_BOOL;
2306 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2307 }
2308 break;
2309
2310 case ISN_2STRING:
2311 {
2312 char_u *str;
2313
2314 tv = STACK_TV_BOT(iptr->isn_arg.number);
2315 if (tv->v_type != VAR_STRING)
2316 {
2317 str = typval_tostring(tv);
2318 clear_tv(tv);
2319 tv->v_type = VAR_STRING;
2320 tv->vval.v_string = str;
2321 }
2322 }
2323 break;
2324
2325 case ISN_DROP:
2326 --ectx.ec_stack.ga_len;
2327 clear_tv(STACK_TV_BOT(0));
2328 break;
2329 }
2330 }
2331
2332done:
2333 // function finished, get result from the stack.
2334 tv = STACK_TV_BOT(-1);
2335 *rettv = *tv;
2336 tv->v_type = VAR_UNKNOWN;
2337 ret = OK;
2338
2339failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002340 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002341 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002342 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002343failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002344 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002345
2346 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2348 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002351 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002352 return ret;
2353}
2354
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355/*
2356 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002357 * We don't really need this at runtime, but we do have tests that require it,
2358 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002359 */
2360 void
2361ex_disassemble(exarg_T *eap)
2362{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002363 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002364 char_u *fname;
2365 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002366 dfunc_T *dfunc;
2367 isn_T *instr;
2368 int current;
2369 int line_idx = 0;
2370 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002371 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002373 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002374 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002375 if (fname == NULL)
2376 {
2377 semsg(_(e_invarg2), eap->arg);
2378 return;
2379 }
2380
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002381 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002382 if (ufunc == NULL)
2383 {
2384 char_u *p = untrans_function_name(fname);
2385
2386 if (p != NULL)
2387 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002388 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002389 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002390 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 if (ufunc == NULL)
2392 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002393 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 return;
2395 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002396 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002397 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2398 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002399 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002401 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002402 return;
2403 }
2404 if (ufunc->uf_name_exp != NULL)
2405 msg((char *)ufunc->uf_name_exp);
2406 else
2407 msg((char *)ufunc->uf_name);
2408
2409 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2410 instr = dfunc->df_instr;
2411 for (current = 0; current < dfunc->df_instr_count; ++current)
2412 {
2413 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002414 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415
2416 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2417 {
2418 if (current > prev_current)
2419 {
2420 msg_puts("\n\n");
2421 prev_current = current;
2422 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002423 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2424 if (line != NULL)
2425 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 }
2427
2428 switch (iptr->isn_type)
2429 {
2430 case ISN_EXEC:
2431 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2432 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002433 case ISN_EXECCONCAT:
2434 smsg("%4d EXECCONCAT %lld", current,
2435 (long long)iptr->isn_arg.number);
2436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 case ISN_ECHO:
2438 {
2439 echo_T *echo = &iptr->isn_arg.echo;
2440
2441 smsg("%4d %s %d", current,
2442 echo->echo_with_white ? "ECHO" : "ECHON",
2443 echo->echo_count);
2444 }
2445 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002446 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002447 smsg("%4d EXECUTE %lld", current,
2448 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002449 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002450 case ISN_ECHOMSG:
2451 smsg("%4d ECHOMSG %lld", current,
2452 (long long)(iptr->isn_arg.number));
2453 break;
2454 case ISN_ECHOERR:
2455 smsg("%4d ECHOERR %lld", current,
2456 (long long)(iptr->isn_arg.number));
2457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002459 case ISN_LOADOUTER:
2460 {
2461 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2462
2463 if (iptr->isn_arg.number < 0)
2464 smsg("%4d LOAD%s arg[%lld]", current, add,
2465 (long long)(iptr->isn_arg.number
2466 + STACK_FRAME_SIZE));
2467 else
2468 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002469 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002470 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 break;
2472 case ISN_LOADV:
2473 smsg("%4d LOADV v:%s", current,
2474 get_vim_var_name(iptr->isn_arg.number));
2475 break;
2476 case ISN_LOADSCRIPT:
2477 {
2478 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002479 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002480 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2481 + iptr->isn_arg.script.script_idx;
2482
2483 smsg("%4d LOADSCRIPT %s from %s", current,
2484 sv->sv_name, si->sn_name);
2485 }
2486 break;
2487 case ISN_LOADS:
2488 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002489 scriptitem_T *si = SCRIPT_ITEM(
2490 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491
2492 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002493 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 }
2495 break;
2496 case ISN_LOADG:
2497 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2498 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002499 case ISN_LOADB:
2500 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2501 break;
2502 case ISN_LOADW:
2503 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2504 break;
2505 case ISN_LOADT:
2506 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2507 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 case ISN_LOADOPT:
2509 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2510 break;
2511 case ISN_LOADENV:
2512 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2513 break;
2514 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002515 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 break;
2517
2518 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002519 case ISN_STOREOUTER:
2520 {
2521 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2522
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002523 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002524 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002525 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002526 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002527 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002528 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002531 case ISN_STOREV:
2532 smsg("%4d STOREV v:%s", current,
2533 get_vim_var_name(iptr->isn_arg.number));
2534 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002535 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002536 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2537 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002538 case ISN_STOREB:
2539 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2540 break;
2541 case ISN_STOREW:
2542 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2543 break;
2544 case ISN_STORET:
2545 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2546 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002547 case ISN_STORES:
2548 {
2549 scriptitem_T *si = SCRIPT_ITEM(
2550 iptr->isn_arg.loadstore.ls_sid);
2551
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002552 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002553 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 break;
2556 case ISN_STORESCRIPT:
2557 {
2558 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002559 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2561 + iptr->isn_arg.script.script_idx;
2562
2563 smsg("%4d STORESCRIPT %s in %s", current,
2564 sv->sv_name, si->sn_name);
2565 }
2566 break;
2567 case ISN_STOREOPT:
2568 smsg("%4d STOREOPT &%s", current,
2569 iptr->isn_arg.storeopt.so_name);
2570 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002571 case ISN_STOREENV:
2572 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2573 break;
2574 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002575 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002576 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 case ISN_STORENR:
2578 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002579 iptr->isn_arg.storenr.stnr_val,
2580 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 break;
2582
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002583 case ISN_STORELIST:
2584 smsg("%4d STORELIST", current);
2585 break;
2586
2587 case ISN_STOREDICT:
2588 smsg("%4d STOREDICT", current);
2589 break;
2590
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 // constants
2592 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002593 smsg("%4d PUSHNR %lld", current,
2594 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 break;
2596 case ISN_PUSHBOOL:
2597 case ISN_PUSHSPEC:
2598 smsg("%4d PUSH %s", current,
2599 get_var_special_name(iptr->isn_arg.number));
2600 break;
2601 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002602#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002604#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 break;
2606 case ISN_PUSHS:
2607 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2608 break;
2609 case ISN_PUSHBLOB:
2610 {
2611 char_u *r;
2612 char_u numbuf[NUMBUFLEN];
2613 char_u *tofree;
2614
2615 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002616 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 vim_free(tofree);
2618 }
2619 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002620 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002621 {
2622 char *name = (char *)iptr->isn_arg.string;
2623
2624 smsg("%4d PUSHFUNC \"%s\"", current,
2625 name == NULL ? "[none]" : name);
2626 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002627 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002628 case ISN_PUSHCHANNEL:
2629#ifdef FEAT_JOB_CHANNEL
2630 {
2631 channel_T *channel = iptr->isn_arg.channel;
2632
2633 smsg("%4d PUSHCHANNEL %d", current,
2634 channel == NULL ? 0 : channel->ch_id);
2635 }
2636#endif
2637 break;
2638 case ISN_PUSHJOB:
2639#ifdef FEAT_JOB_CHANNEL
2640 {
2641 typval_T tv;
2642 char_u *name;
2643
2644 tv.v_type = VAR_JOB;
2645 tv.vval.v_job = iptr->isn_arg.job;
2646 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002647 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002648 }
2649#endif
2650 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 case ISN_PUSHEXC:
2652 smsg("%4d PUSH v:exception", current);
2653 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002654 case ISN_UNLET:
2655 smsg("%4d UNLET%s %s", current,
2656 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2657 iptr->isn_arg.unlet.ul_name);
2658 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002659 case ISN_UNLETENV:
2660 smsg("%4d UNLETENV%s $%s", current,
2661 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2662 iptr->isn_arg.unlet.ul_name);
2663 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002664 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002665 smsg("%4d NEWLIST size %lld", current,
2666 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002667 break;
2668 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002669 smsg("%4d NEWDICT size %lld", current,
2670 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 break;
2672
2673 // function call
2674 case ISN_BCALL:
2675 {
2676 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2677
2678 smsg("%4d BCALL %s(argc %d)", current,
2679 internal_func_name(cbfunc->cbf_idx),
2680 cbfunc->cbf_argcount);
2681 }
2682 break;
2683 case ISN_DCALL:
2684 {
2685 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2686 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2687 + cdfunc->cdf_idx;
2688
2689 smsg("%4d DCALL %s(argc %d)", current,
2690 df->df_ufunc->uf_name_exp != NULL
2691 ? df->df_ufunc->uf_name_exp
2692 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2693 }
2694 break;
2695 case ISN_UCALL:
2696 {
2697 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2698
2699 smsg("%4d UCALL %s(argc %d)", current,
2700 cufunc->cuf_name, cufunc->cuf_argcount);
2701 }
2702 break;
2703 case ISN_PCALL:
2704 {
2705 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2706
2707 smsg("%4d PCALL%s (argc %d)", current,
2708 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2709 }
2710 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002711 case ISN_PCALL_END:
2712 smsg("%4d PCALL end", current);
2713 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 case ISN_RETURN:
2715 smsg("%4d RETURN", current);
2716 break;
2717 case ISN_FUNCREF:
2718 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002719 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002721 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002722
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002723 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002724 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 }
2726 break;
2727
2728 case ISN_JUMP:
2729 {
2730 char *when = "?";
2731
2732 switch (iptr->isn_arg.jump.jump_when)
2733 {
2734 case JUMP_ALWAYS:
2735 when = "JUMP";
2736 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 case JUMP_AND_KEEP_IF_TRUE:
2738 when = "JUMP_AND_KEEP_IF_TRUE";
2739 break;
2740 case JUMP_IF_FALSE:
2741 when = "JUMP_IF_FALSE";
2742 break;
2743 case JUMP_AND_KEEP_IF_FALSE:
2744 when = "JUMP_AND_KEEP_IF_FALSE";
2745 break;
2746 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002747 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748 iptr->isn_arg.jump.jump_where);
2749 }
2750 break;
2751
2752 case ISN_FOR:
2753 {
2754 forloop_T *forloop = &iptr->isn_arg.forloop;
2755
2756 smsg("%4d FOR $%d -> %d", current,
2757 forloop->for_idx, forloop->for_end);
2758 }
2759 break;
2760
2761 case ISN_TRY:
2762 {
2763 try_T *try = &iptr->isn_arg.try;
2764
2765 smsg("%4d TRY catch -> %d, finally -> %d", current,
2766 try->try_catch, try->try_finally);
2767 }
2768 break;
2769 case ISN_CATCH:
2770 // TODO
2771 smsg("%4d CATCH", current);
2772 break;
2773 case ISN_ENDTRY:
2774 smsg("%4d ENDTRY", current);
2775 break;
2776 case ISN_THROW:
2777 smsg("%4d THROW", current);
2778 break;
2779
2780 // expression operations on number
2781 case ISN_OPNR:
2782 case ISN_OPFLOAT:
2783 case ISN_OPANY:
2784 {
2785 char *what;
2786 char *ins;
2787
2788 switch (iptr->isn_arg.op.op_type)
2789 {
2790 case EXPR_MULT: what = "*"; break;
2791 case EXPR_DIV: what = "/"; break;
2792 case EXPR_REM: what = "%"; break;
2793 case EXPR_SUB: what = "-"; break;
2794 case EXPR_ADD: what = "+"; break;
2795 default: what = "???"; break;
2796 }
2797 switch (iptr->isn_type)
2798 {
2799 case ISN_OPNR: ins = "OPNR"; break;
2800 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
2801 case ISN_OPANY: ins = "OPANY"; break;
2802 default: ins = "???"; break;
2803 }
2804 smsg("%4d %s %s", current, ins, what);
2805 }
2806 break;
2807
2808 case ISN_COMPAREBOOL:
2809 case ISN_COMPARESPECIAL:
2810 case ISN_COMPARENR:
2811 case ISN_COMPAREFLOAT:
2812 case ISN_COMPARESTRING:
2813 case ISN_COMPAREBLOB:
2814 case ISN_COMPARELIST:
2815 case ISN_COMPAREDICT:
2816 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 case ISN_COMPAREANY:
2818 {
2819 char *p;
2820 char buf[10];
2821 char *type;
2822
2823 switch (iptr->isn_arg.op.op_type)
2824 {
2825 case EXPR_EQUAL: p = "=="; break;
2826 case EXPR_NEQUAL: p = "!="; break;
2827 case EXPR_GREATER: p = ">"; break;
2828 case EXPR_GEQUAL: p = ">="; break;
2829 case EXPR_SMALLER: p = "<"; break;
2830 case EXPR_SEQUAL: p = "<="; break;
2831 case EXPR_MATCH: p = "=~"; break;
2832 case EXPR_IS: p = "is"; break;
2833 case EXPR_ISNOT: p = "isnot"; break;
2834 case EXPR_NOMATCH: p = "!~"; break;
2835 default: p = "???"; break;
2836 }
2837 STRCPY(buf, p);
2838 if (iptr->isn_arg.op.op_ic == TRUE)
2839 strcat(buf, "?");
2840 switch(iptr->isn_type)
2841 {
2842 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
2843 case ISN_COMPARESPECIAL:
2844 type = "COMPARESPECIAL"; break;
2845 case ISN_COMPARENR: type = "COMPARENR"; break;
2846 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
2847 case ISN_COMPARESTRING:
2848 type = "COMPARESTRING"; break;
2849 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
2850 case ISN_COMPARELIST: type = "COMPARELIST"; break;
2851 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
2852 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 case ISN_COMPAREANY: type = "COMPAREANY"; break;
2854 default: type = "???"; break;
2855 }
2856
2857 smsg("%4d %s %s", current, type, buf);
2858 }
2859 break;
2860
2861 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
2862 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
2863
2864 // expression operations
2865 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
2866 case ISN_INDEX: smsg("%4d INDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002867 case ISN_SLICE: smsg("%4d SLICE %lld",
2868 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002869 case ISN_GETITEM: smsg("%4d ITEM %lld",
2870 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002871 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
2872 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 iptr->isn_arg.string); break;
2874 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
2875
2876 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
2877 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
2878 vartype_name(iptr->isn_arg.type.ct_type),
2879 iptr->isn_arg.type.ct_off);
2880 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002881 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
2882 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
2883 iptr->isn_arg.checklen.cl_min_len);
2884 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002885 case ISN_2BOOL: if (iptr->isn_arg.number)
2886 smsg("%4d INVERT (!val)", current);
2887 else
2888 smsg("%4d 2BOOL (!!val)", current);
2889 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01002890 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
2891 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002892 break;
2893
2894 case ISN_DROP: smsg("%4d DROP", current); break;
2895 }
2896 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897}
2898
2899/*
2900 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
2901 * list, etc. Mostly like what JavaScript does, except that empty list and
2902 * empty dictionary are FALSE.
2903 */
2904 int
2905tv2bool(typval_T *tv)
2906{
2907 switch (tv->v_type)
2908 {
2909 case VAR_NUMBER:
2910 return tv->vval.v_number != 0;
2911 case VAR_FLOAT:
2912#ifdef FEAT_FLOAT
2913 return tv->vval.v_float != 0.0;
2914#else
2915 break;
2916#endif
2917 case VAR_PARTIAL:
2918 return tv->vval.v_partial != NULL;
2919 case VAR_FUNC:
2920 case VAR_STRING:
2921 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
2922 case VAR_LIST:
2923 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
2924 case VAR_DICT:
2925 return tv->vval.v_dict != NULL
2926 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
2927 case VAR_BOOL:
2928 case VAR_SPECIAL:
2929 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
2930 case VAR_JOB:
2931#ifdef FEAT_JOB_CHANNEL
2932 return tv->vval.v_job != NULL;
2933#else
2934 break;
2935#endif
2936 case VAR_CHANNEL:
2937#ifdef FEAT_JOB_CHANNEL
2938 return tv->vval.v_channel != NULL;
2939#else
2940 break;
2941#endif
2942 case VAR_BLOB:
2943 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
2944 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02002945 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 case VAR_VOID:
2947 break;
2948 }
2949 return FALSE;
2950}
2951
2952/*
2953 * If "tv" is a string give an error and return FAIL.
2954 */
2955 int
2956check_not_string(typval_T *tv)
2957{
2958 if (tv->v_type == VAR_STRING)
2959 {
2960 emsg(_("E1030: Using a String as a Number"));
2961 clear_tv(tv);
2962 return FAIL;
2963 }
2964 return OK;
2965}
2966
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967
2968#endif // FEAT_EVAL