blob: c87ac79e30ca4f6a04c6112f5f7d400cc51dbb73 [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
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
78 semsg(_("E1105: Cannot convert %s to string"), vartype_name(vartype));
79}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
209 iemsg("Argument count wrong?");
210 return FAIL;
211 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200212 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
213 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214 return FAIL;
215
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // Move the vararg-list to below the missing optional arguments.
217 if (vararg_count > 0 && arg_to_add > 0)
218 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100219
220 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200221 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200223 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
227 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
229 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200232 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200234 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
235 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236
237 // Set execution state to the start of the called function.
238 ectx->ec_dfunc_idx = cdf_idx;
239 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200240 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
241 if (entry != NULL)
242 {
243 // Set the script context to the script where the function was defined.
244 // TODO: save more than the SID?
245 entry->es_save_sid = current_sctx.sc_sid;
246 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
247 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100248
249 // Decide where to start execution, handles optional arguments.
250 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251
252 return OK;
253}
254
255// Get pointer to item in the stack.
256#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
257
258/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200259 * Used when returning from a function: Check if any closure is still
260 * referenced. If so then move the arguments and variables to a separate piece
261 * of stack to be used when the closure is called.
262 * When "free_arguments" is TRUE the arguments are to be freed.
263 * Returns FAIL when out of memory.
264 */
265 static int
266handle_closure_in_use(ectx_T *ectx, int free_arguments)
267{
268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
269 + ectx->ec_dfunc_idx;
270 int argcount = ufunc_argcount(dfunc->df_ufunc);
271 int top = ectx->ec_frame_idx - argcount;
272 int idx;
273 typval_T *tv;
274 int closure_in_use = FALSE;
275
276 // Check if any created closure is still in use.
277 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
278 {
279 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
280 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200281 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
282 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200283 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 int refcount = tv->vval.v_partial->pt_refcount;
285 int i;
286
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200287 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200288 // unreferenced on return.
289 for (i = 0; i < dfunc->df_varcount; ++i)
290 {
291 typval_T *stv = STACK_TV(ectx->ec_frame_idx
292 + STACK_FRAME_SIZE + i);
293 if (stv->v_type == VAR_PARTIAL
294 && tv->vval.v_partial == stv->vval.v_partial)
295 --refcount;
296 }
297 if (refcount > 1)
298 {
299 closure_in_use = TRUE;
300 break;
301 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200302 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303 }
304
305 if (closure_in_use)
306 {
307 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
308 typval_T *stack;
309
310 // A closure is using the arguments and/or local variables.
311 // Move them to the called function.
312 if (funcstack == NULL)
313 return FAIL;
314 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
315 + dfunc->df_varcount;
316 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
317 funcstack->fs_ga.ga_data = stack;
318 if (stack == NULL)
319 {
320 vim_free(funcstack);
321 return FAIL;
322 }
323
324 // Move or copy the arguments.
325 for (idx = 0; idx < argcount; ++idx)
326 {
327 tv = STACK_TV(top + idx);
328 if (free_arguments)
329 {
330 *(stack + idx) = *tv;
331 tv->v_type = VAR_UNKNOWN;
332 }
333 else
334 copy_tv(tv, stack + idx);
335 }
336 // Move the local variables.
337 for (idx = 0; idx < dfunc->df_varcount; ++idx)
338 {
339 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200340
341 // Do not copy a partial created for a local function.
342 // TODO: this won't work if the closure actually uses it. But when
343 // keeping it it gets complicated: it will create a reference cycle
344 // inside the partial, thus needs special handling for garbage
345 // collection.
346 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
347 {
348 int i;
349 typval_T *ctv;
350
351 for (i = 0; i < dfunc->df_closure_count; ++i)
352 {
353 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
354 + dfunc->df_varcount + i);
355 if (tv->vval.v_partial == ctv->vval.v_partial)
356 break;
357 }
358 if (i < dfunc->df_closure_count)
359 {
360 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
361 VAR_UNKNOWN;
362 continue;
363 }
364 }
365
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
367 tv->v_type = VAR_UNKNOWN;
368 }
369
370 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
371 {
372 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
373 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200376 partial_T *partial = tv->vval.v_partial;
377
378 if (partial->pt_refcount > 1)
379 {
380 ++funcstack->fs_refcount;
381 partial->pt_funcstack = funcstack;
382 partial->pt_ectx_stack = &funcstack->fs_ga;
383 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
384 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 }
386 }
387 }
388
389 return OK;
390}
391
392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 * Return from the current function.
394 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200395 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396func_return(ectx_T *ectx)
397{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
400 + ectx->ec_dfunc_idx;
401 int argcount = ufunc_argcount(dfunc->df_ufunc);
402 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200403 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 entry = estack_pop();
407 if (entry != NULL)
408 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200410 if (handle_closure_in_use(ectx, TRUE) == FAIL)
411 return FAIL;
412
413 // Clear the arguments.
414 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
415 clear_tv(STACK_TV(idx));
416
417 // Clear local variables and temp values, but not the return value.
418 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100419 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100421
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
424 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
425 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
427 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100428
429 // Reset the stack to the position before the call, move the return value
430 // to the top of the stack.
431 idx = ectx->ec_stack.ga_len - 1;
432 ectx->ec_stack.ga_len = top + 1;
433 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434
435 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436}
437
438#undef STACK_TV
439
440/*
441 * Prepare arguments and rettv for calling a builtin or user function.
442 */
443 static int
444call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
445{
446 int idx;
447 typval_T *tv;
448
449 // Move arguments from bottom of the stack to argvars[] and add terminator.
450 for (idx = 0; idx < argcount; ++idx)
451 argvars[idx] = *STACK_TV_BOT(idx - argcount);
452 argvars[argcount].v_type = VAR_UNKNOWN;
453
454 // Result replaces the arguments on the stack.
455 if (argcount > 0)
456 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200457 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 return FAIL;
459 else
460 ++ectx->ec_stack.ga_len;
461
462 // Default return value is zero.
463 tv = STACK_TV_BOT(-1);
464 tv->v_type = VAR_NUMBER;
465 tv->vval.v_number = 0;
466
467 return OK;
468}
469
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200470// Ugly global to avoid passing the execution context around through many
471// layers.
472static ectx_T *current_ectx = NULL;
473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474/*
475 * Call a builtin function by index.
476 */
477 static int
478call_bfunc(int func_idx, int argcount, ectx_T *ectx)
479{
480 typval_T argvars[MAX_FUNC_ARGS];
481 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200482 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200483 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484
485 if (call_prepare(argcount, argvars, ectx) == FAIL)
486 return FAIL;
487
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200488 // Call the builtin function. Set "current_ectx" so that when it
489 // recursively invokes call_def_function() a closure context can be set.
490 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200492 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493
494 // Clear the arguments.
495 for (idx = 0; idx < argcount; ++idx)
496 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200497
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200498 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200499 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 return OK;
501}
502
503/*
504 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100505 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 */
507 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509{
510 typval_T argvars[MAX_FUNC_ARGS];
511 funcexe_T funcexe;
512 int error;
513 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200514 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200516 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200517 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
518 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100520 {
521 // The function has been compiled, can call it quickly. For a function
522 // that was defined later: we can call it directly next time.
523 if (iptr != NULL)
524 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100525 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100526 iptr->isn_type = ISN_DCALL;
527 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
528 iptr->isn_arg.dfunc.cdf_argcount = argcount;
529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 if (call_prepare(argcount, argvars, ectx) == FAIL)
534 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200535 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 funcexe.evaluate = TRUE;
537
538 // Call the user function. Result goes in last position on the stack.
539 // TODO: add selfdict if there is one
540 error = call_user_func_check(ufunc, argcount, argvars,
541 STACK_TV_BOT(-1), &funcexe, NULL);
542
543 // Clear the arguments.
544 for (idx = 0; idx < argcount; ++idx)
545 clear_tv(&argvars[idx]);
546
547 if (error != FCERR_NONE)
548 {
549 user_func_error(error, ufunc->uf_name);
550 return FAIL;
551 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200552 if (called_emsg > called_emsg_before)
553 // Error other than from calling the function itself.
554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 return OK;
556}
557
558/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200559 * Return TRUE if an error was given or CTRL-C was pressed.
560 */
561 static int
562vim9_aborting(int prev_called_emsg)
563{
564 return called_emsg > prev_called_emsg || got_int || did_throw;
565}
566
567/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 * Execute a function by "name".
569 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100570 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Returns FAIL if not found without an error message.
572 */
573 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100574call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575{
576 ufunc_T *ufunc;
577
578 if (builtin_function(name, -1))
579 {
580 int func_idx = find_internal_func(name);
581
582 if (func_idx < 0)
583 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200584 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 return FAIL;
586 return call_bfunc(func_idx, argcount, ectx);
587 }
588
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200589 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200590
591 if (ufunc == NULL)
592 {
593 int called_emsg_before = called_emsg;
594
595 if (script_autoload(name, TRUE))
596 // loaded a package, search for the function again
597 ufunc = find_func(name, FALSE, NULL);
598 if (vim9_aborting(called_emsg_before))
599 return FAIL; // bail out if loading the script caused an error
600 }
601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100603 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 return FAIL;
606}
607
608 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200609call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200611 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200612 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 int called_emsg_before = called_emsg;
614
615 if (tv->v_type == VAR_PARTIAL)
616 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200617 partial_T *pt = tv->vval.v_partial;
618 int i;
619
620 if (pt->pt_argc > 0)
621 {
622 // Make space for arguments from the partial, shift the "argcount"
623 // arguments up.
624 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
625 return FAIL;
626 for (i = 1; i <= argcount; ++i)
627 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
628 ectx->ec_stack.ga_len += pt->pt_argc;
629 argcount += pt->pt_argc;
630
631 // copy the arguments from the partial onto the stack
632 for (i = 0; i < pt->pt_argc; ++i)
633 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635
636 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200637 {
638 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
639
640 // closure may need the function context where it was defined
641 ectx->ec_outer_stack = pt->pt_ectx_stack;
642 ectx->ec_outer_frame = pt->pt_ectx_frame;
643
644 return ret;
645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 name = pt->pt_name;
647 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200648 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200650 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 {
652 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200653 semsg(_(e_unknownfunc),
654 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 return FAIL;
656 }
657 return OK;
658}
659
660/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100661 * Store "tv" in variable "name".
662 * This is for s: and g: variables.
663 */
664 static void
665store_var(char_u *name, typval_T *tv)
666{
667 funccal_entry_T entry;
668
669 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200670 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100671 restore_funccal();
672}
673
Bram Moolenaard3aac292020-04-19 14:32:17 +0200674
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100675/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 * Execute a function by "name".
677 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100678 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 */
680 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682{
Bram Moolenaared677f52020-08-12 16:38:10 +0200683 int called_emsg_before = called_emsg;
684 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685
Bram Moolenaared677f52020-08-12 16:38:10 +0200686 res = call_by_name(name, argcount, ectx, iptr);
687 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200689 dictitem_T *v;
690
691 v = find_var(name, NULL, FALSE);
692 if (v == NULL)
693 {
694 semsg(_(e_unknownfunc), name);
695 return FAIL;
696 }
697 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
698 {
699 semsg(_(e_unknownfunc), name);
700 return FAIL;
701 }
702 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200704 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705}
706
707/*
708 * Call a "def" function from old Vim script.
709 * Return OK or FAIL.
710 */
711 int
712call_def_function(
713 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200714 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200716 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 typval_T *rettv) // return value
718{
719 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200720 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200721 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 typval_T *tv;
723 int idx;
724 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100725 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200726 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200727 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200728 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730// Get pointer to item in the stack.
731#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
732
733// Get pointer to item at the bottom of the stack, -1 is the bottom.
734#undef STACK_TV_BOT
735#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
736
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200737// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738#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 +0100739
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200740// Like STACK_TV_VAR but use the outer scope
741#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
742
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200743 if (ufunc->uf_def_status == UF_NOT_COMPILED
744 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200745 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200746 {
747 if (called_emsg == called_emsg_before)
748 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200749 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200750 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200751 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200752
Bram Moolenaar09689a02020-05-09 22:50:08 +0200753 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200754 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200755 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
756 + ufunc->uf_dfunc_idx;
757 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200758 {
759 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200760 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200761 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200764 CLEAR_FIELD(ectx);
765 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
766 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
767 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
770
771 // Put arguments on the stack.
772 for (idx = 0; idx < argc; ++idx)
773 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200774 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200775 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
776 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200777 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 copy_tv(&argv[idx], STACK_TV_BOT(0));
779 ++ectx.ec_stack.ga_len;
780 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200781
782 // Turn varargs into a list. Empty list if no args.
783 if (ufunc->uf_va_name != NULL)
784 {
785 int vararg_count = argc - ufunc->uf_args.ga_len;
786
787 if (vararg_count < 0)
788 vararg_count = 0;
789 else
790 argc -= vararg_count;
791 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200792 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200793
794 // Check the type of the list items.
795 tv = STACK_TV_BOT(-1);
796 if (ufunc->uf_va_type != NULL
797 && ufunc->uf_va_type->tt_member != &t_any
798 && tv->vval.v_list != NULL)
799 {
800 type_T *expected = ufunc->uf_va_type->tt_member;
801 listitem_T *li = tv->vval.v_list->lv_first;
802
803 for (idx = 0; idx < vararg_count; ++idx)
804 {
805 if (check_typval_type(expected, &li->li_tv) == FAIL)
806 goto failed_early;
807 li = li->li_next;
808 }
809 }
810
Bram Moolenaar23e03252020-04-12 22:22:31 +0200811 if (defcount > 0)
812 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200813 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 --ectx.ec_stack.ga_len;
815 }
816
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100817 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200818 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100819 if (defcount > 0)
820 for (idx = 0; idx < defcount; ++idx)
821 {
822 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
823 ++ectx.ec_stack.ga_len;
824 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200825 if (ufunc->uf_va_name != NULL)
826 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
830 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200832 if (partial != NULL)
833 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200834 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
835 {
836 // TODO: is this always the right way?
837 ectx.ec_outer_stack = &current_ectx->ec_stack;
838 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
839 }
840 else
841 {
842 ectx.ec_outer_stack = partial->pt_ectx_stack;
843 ectx.ec_outer_frame = partial->pt_ectx_frame;
844 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200845 }
846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 // dummy frame entries
848 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
849 {
850 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
851 ++ectx.ec_stack.ga_len;
852 }
853
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200854 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200855 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200856 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
857 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200860 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200861 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200862 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200863
864 ectx.ec_instr = dfunc->df_instr;
865 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100866
Bram Moolenaara26b9702020-04-18 19:53:28 +0200867 // Commands behave like vim9script.
868 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
869
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100870 // Decide where to start execution, handles optional arguments.
871 init_instr_idx(ufunc, argc, &ectx);
872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 for (;;)
874 {
875 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100876
Bram Moolenaar270d0382020-05-15 21:42:53 +0200877 if (++breakcheck_count >= 100)
878 {
879 line_breakcheck();
880 breakcheck_count = 0;
881 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100882 if (got_int)
883 {
884 // Turn CTRL-C into an exception.
885 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100886 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100887 goto failed;
888 did_throw = TRUE;
889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaara26b9702020-04-18 19:53:28 +0200891 if (did_emsg && msg_list != NULL && *msg_list != NULL)
892 {
893 // Turn an error message into an exception.
894 did_emsg = FALSE;
895 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
896 goto failed;
897 did_throw = TRUE;
898 *msg_list = NULL;
899 }
900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 if (did_throw && !ectx.ec_in_catch)
902 {
903 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100904 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905
906 // An exception jumps to the first catch, finally, or returns from
907 // the current function.
908 if (trystack->ga_len > 0)
909 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200910 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 {
912 // jump to ":catch" or ":finally"
913 ectx.ec_in_catch = TRUE;
914 ectx.ec_iidx = trycmd->tcd_catch_idx;
915 }
916 else
917 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200918 // Not inside try or need to return from current functions.
919 // Push a dummy return value.
920 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
921 goto failed;
922 tv = STACK_TV_BOT(0);
923 tv->v_type = VAR_NUMBER;
924 tv->vval.v_number = 0;
925 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200926 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200928 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100929 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200930 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
931 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 goto done;
933 }
934
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200935 if (func_return(&ectx) == FAIL)
936 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 }
938 continue;
939 }
940
941 iptr = &ectx.ec_instr[ectx.ec_iidx++];
942 switch (iptr->isn_type)
943 {
944 // execute Ex command line
945 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200946 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 do_cmdline_cmd(iptr->isn_arg.string);
948 break;
949
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200950 // execute Ex command from pieces on the stack
951 case ISN_EXECCONCAT:
952 {
953 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200954 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200955 int pass;
956 int i;
957 char_u *cmd = NULL;
958 char_u *str;
959
960 for (pass = 1; pass <= 2; ++pass)
961 {
962 for (i = 0; i < count; ++i)
963 {
964 tv = STACK_TV_BOT(i - count);
965 str = tv->vval.v_string;
966 if (str != NULL && *str != NUL)
967 {
968 if (pass == 2)
969 STRCPY(cmd + len, str);
970 len += STRLEN(str);
971 }
972 if (pass == 2)
973 clear_tv(tv);
974 }
975 if (pass == 1)
976 {
977 cmd = alloc(len + 1);
978 if (cmd == NULL)
979 goto failed;
980 len = 0;
981 }
982 }
983
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200984 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200985 do_cmdline_cmd(cmd);
986 vim_free(cmd);
987 }
988 break;
989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 // execute :echo {string} ...
991 case ISN_ECHO:
992 {
993 int count = iptr->isn_arg.echo.echo_count;
994 int atstart = TRUE;
995 int needclr = TRUE;
996
997 for (idx = 0; idx < count; ++idx)
998 {
999 tv = STACK_TV_BOT(idx - count);
1000 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1001 &atstart, &needclr);
1002 clear_tv(tv);
1003 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001004 if (needclr)
1005 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 ectx.ec_stack.ga_len -= count;
1007 }
1008 break;
1009
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001010 // :execute {string} ...
1011 // :echomsg {string} ...
1012 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001013 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001014 case ISN_ECHOMSG:
1015 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001016 {
1017 int count = iptr->isn_arg.number;
1018 garray_T ga;
1019 char_u buf[NUMBUFLEN];
1020 char_u *p;
1021 int len;
1022 int failed = FALSE;
1023
1024 ga_init2(&ga, 1, 80);
1025 for (idx = 0; idx < count; ++idx)
1026 {
1027 tv = STACK_TV_BOT(idx - count);
1028 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1029 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001030 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarad39c092020-02-26 18:23:43 +01001031 emsg(_(e_inval_string));
1032 break;
1033 }
1034 else
1035 p = tv_get_string_buf(tv, buf);
1036
1037 len = (int)STRLEN(p);
1038 if (ga_grow(&ga, len + 2) == FAIL)
1039 failed = TRUE;
1040 else
1041 {
1042 if (ga.ga_len > 0)
1043 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1044 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1045 ga.ga_len += len;
1046 }
1047 clear_tv(tv);
1048 }
1049 ectx.ec_stack.ga_len -= count;
1050
1051 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001052 {
1053 if (iptr->isn_type == ISN_EXECUTE)
1054 do_cmdline_cmd((char_u *)ga.ga_data);
1055 else
1056 {
1057 msg_sb_eol();
1058 if (iptr->isn_type == ISN_ECHOMSG)
1059 {
1060 msg_attr(ga.ga_data, echo_attr);
1061 out_flush();
1062 }
1063 else
1064 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001065 SOURCING_LNUM = iptr->isn_lnum;
1066 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001067 }
1068 }
1069 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001070 ga_clear(&ga);
1071 }
1072 break;
1073
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001074 // load local variable or argument
1075 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001076 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077 goto failed;
1078 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1079 ++ectx.ec_stack.ga_len;
1080 break;
1081
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001082 // load variable or argument from outer scope
1083 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001084 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001085 goto failed;
1086 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1087 STACK_TV_BOT(0));
1088 ++ectx.ec_stack.ga_len;
1089 break;
1090
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 // load v: variable
1092 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001093 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001094 goto failed;
1095 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1096 ++ectx.ec_stack.ga_len;
1097 break;
1098
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001099 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001100 case ISN_LOADSCRIPT:
1101 {
1102 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001103 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001104 svar_T *sv;
1105
1106 sv = ((svar_T *)si->sn_var_vals.ga_data)
1107 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001108 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 goto failed;
1110 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1111 ++ectx.ec_stack.ga_len;
1112 }
1113 break;
1114
1115 // load s: variable in old script
1116 case ISN_LOADS:
1117 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001118 hashtab_T *ht = &SCRIPT_VARS(
1119 iptr->isn_arg.loadstore.ls_sid);
1120 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001122
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 if (di == NULL)
1124 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001125 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001126 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001127 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 }
1129 else
1130 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001131 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 goto failed;
1133 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1134 ++ectx.ec_stack.ga_len;
1135 }
1136 }
1137 break;
1138
Bram Moolenaard3aac292020-04-19 14:32:17 +02001139 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001141 case ISN_LOADB:
1142 case ISN_LOADW:
1143 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001145 dictitem_T *di = NULL;
1146 hashtab_T *ht = NULL;
1147 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001148
Bram Moolenaard3aac292020-04-19 14:32:17 +02001149 switch (iptr->isn_type)
1150 {
1151 case ISN_LOADG:
1152 ht = get_globvar_ht();
1153 namespace = 'g';
1154 break;
1155 case ISN_LOADB:
1156 ht = &curbuf->b_vars->dv_hashtab;
1157 namespace = 'b';
1158 break;
1159 case ISN_LOADW:
1160 ht = &curwin->w_vars->dv_hashtab;
1161 namespace = 'w';
1162 break;
1163 case ISN_LOADT:
1164 ht = &curtab->tp_vars->dv_hashtab;
1165 namespace = 't';
1166 break;
1167 default: // Cannot reach here
1168 goto failed;
1169 }
1170 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if (di == NULL)
1173 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001174 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001175 semsg(_("E121: Undefined variable: %c:%s"),
1176 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001177 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 }
1179 else
1180 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 goto failed;
1183 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1184 ++ectx.ec_stack.ga_len;
1185 }
1186 }
1187 break;
1188
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001189 // load g:/b:/w:/t: namespace
1190 case ISN_LOADGDICT:
1191 case ISN_LOADBDICT:
1192 case ISN_LOADWDICT:
1193 case ISN_LOADTDICT:
1194 {
1195 dict_T *d = NULL;
1196
1197 switch (iptr->isn_type)
1198 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001199 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1200 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1201 case ISN_LOADWDICT: d = curwin->w_vars; break;
1202 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001203 default: // Cannot reach here
1204 goto failed;
1205 }
1206 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1207 goto failed;
1208 tv = STACK_TV_BOT(0);
1209 tv->v_type = VAR_DICT;
1210 tv->v_lock = 0;
1211 tv->vval.v_dict = d;
1212 ++ectx.ec_stack.ga_len;
1213 }
1214 break;
1215
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001216 // load &option
1217 case ISN_LOADOPT:
1218 {
1219 typval_T optval;
1220 char_u *name = iptr->isn_arg.string;
1221
Bram Moolenaara8c17702020-04-01 21:17:24 +02001222 // This is not expected to fail, name is checked during
1223 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001224 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001226 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001227 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 *STACK_TV_BOT(0) = optval;
1229 ++ectx.ec_stack.ga_len;
1230 }
1231 break;
1232
1233 // load $ENV
1234 case ISN_LOADENV:
1235 {
1236 typval_T optval;
1237 char_u *name = iptr->isn_arg.string;
1238
Bram Moolenaar270d0382020-05-15 21:42:53 +02001239 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001241 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001242 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001243 *STACK_TV_BOT(0) = optval;
1244 ++ectx.ec_stack.ga_len;
1245 }
1246 break;
1247
1248 // load @register
1249 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001250 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 goto failed;
1252 tv = STACK_TV_BOT(0);
1253 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001254 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255 tv->vval.v_string = get_reg_contents(
1256 iptr->isn_arg.number, GREG_EXPR_SRC);
1257 ++ectx.ec_stack.ga_len;
1258 break;
1259
1260 // store local variable
1261 case ISN_STORE:
1262 --ectx.ec_stack.ga_len;
1263 tv = STACK_TV_VAR(iptr->isn_arg.number);
1264 clear_tv(tv);
1265 *tv = *STACK_TV_BOT(0);
1266 break;
1267
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001268 // store variable or argument in outer scope
1269 case ISN_STOREOUTER:
1270 --ectx.ec_stack.ga_len;
1271 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1272 clear_tv(tv);
1273 *tv = *STACK_TV_BOT(0);
1274 break;
1275
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001276 // store s: variable in old script
1277 case ISN_STORES:
1278 {
1279 hashtab_T *ht = &SCRIPT_VARS(
1280 iptr->isn_arg.loadstore.ls_sid);
1281 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001282 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001283
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001284 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001285 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001286 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001287 else
1288 {
1289 clear_tv(&di->di_tv);
1290 di->di_tv = *STACK_TV_BOT(0);
1291 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001292 }
1293 break;
1294
1295 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 case ISN_STORESCRIPT:
1297 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001298 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001299 iptr->isn_arg.script.script_sid);
1300 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1301 + iptr->isn_arg.script.script_idx;
1302
1303 --ectx.ec_stack.ga_len;
1304 clear_tv(sv->sv_tv);
1305 *sv->sv_tv = *STACK_TV_BOT(0);
1306 }
1307 break;
1308
1309 // store option
1310 case ISN_STOREOPT:
1311 {
1312 long n = 0;
1313 char_u *s = NULL;
1314 char *msg;
1315
1316 --ectx.ec_stack.ga_len;
1317 tv = STACK_TV_BOT(0);
1318 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001319 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001321 if (s == NULL)
1322 s = (char_u *)"";
1323 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001325 // must be VAR_NUMBER, CHECKTYPE makes sure
1326 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1328 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001329 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 if (msg != NULL)
1331 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001334 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001335 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 }
1337 break;
1338
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001339 // store $ENV
1340 case ISN_STOREENV:
1341 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001342 tv = STACK_TV_BOT(0);
1343 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1344 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001345 break;
1346
1347 // store @r
1348 case ISN_STOREREG:
1349 {
1350 int reg = iptr->isn_arg.number;
1351
1352 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001353 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001354 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001355 tv_get_string(tv), -1, FALSE);
1356 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001357 }
1358 break;
1359
1360 // store v: variable
1361 case ISN_STOREV:
1362 --ectx.ec_stack.ga_len;
1363 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1364 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001365 // should not happen, type is checked when compiling
1366 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001367 break;
1368
Bram Moolenaard3aac292020-04-19 14:32:17 +02001369 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001371 case ISN_STOREB:
1372 case ISN_STOREW:
1373 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 {
1375 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001376 hashtab_T *ht;
1377 switch (iptr->isn_type)
1378 {
1379 case ISN_STOREG:
1380 ht = get_globvar_ht();
1381 break;
1382 case ISN_STOREB:
1383 ht = &curbuf->b_vars->dv_hashtab;
1384 break;
1385 case ISN_STOREW:
1386 ht = &curwin->w_vars->dv_hashtab;
1387 break;
1388 case ISN_STORET:
1389 ht = &curtab->tp_vars->dv_hashtab;
1390 break;
1391 default: // Cannot reach here
1392 goto failed;
1393 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394
1395 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001396 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001398 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 else
1400 {
1401 clear_tv(&di->di_tv);
1402 di->di_tv = *STACK_TV_BOT(0);
1403 }
1404 }
1405 break;
1406
1407 // store number in local variable
1408 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001409 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001410 clear_tv(tv);
1411 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001412 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413 break;
1414
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001415 // store value in list variable
1416 case ISN_STORELIST:
1417 {
1418 typval_T *tv_idx = STACK_TV_BOT(-2);
1419 varnumber_T lidx = tv_idx->vval.v_number;
1420 typval_T *tv_list = STACK_TV_BOT(-1);
1421 list_T *list = tv_list->vval.v_list;
1422
1423 if (lidx < 0 && list->lv_len + lidx >= 0)
1424 // negative index is relative to the end
1425 lidx = list->lv_len + lidx;
1426 if (lidx < 0 || lidx > list->lv_len)
1427 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001428 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001429 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001430 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001431 }
1432 tv = STACK_TV_BOT(-3);
1433 if (lidx < list->lv_len)
1434 {
1435 listitem_T *li = list_find(list, lidx);
1436
1437 // overwrite existing list item
1438 clear_tv(&li->li_tv);
1439 li->li_tv = *tv;
1440 }
1441 else
1442 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001443 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001444 if (list_append_tv(list, tv) == FAIL)
1445 goto failed;
1446 clear_tv(tv);
1447 }
1448 clear_tv(tv_idx);
1449 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001450 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001451 }
1452 break;
1453
1454 // store value in dict variable
1455 case ISN_STOREDICT:
1456 {
1457 typval_T *tv_key = STACK_TV_BOT(-2);
1458 char_u *key = tv_key->vval.v_string;
1459 typval_T *tv_dict = STACK_TV_BOT(-1);
1460 dict_T *dict = tv_dict->vval.v_dict;
1461 dictitem_T *di;
1462
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001463 if (dict == NULL)
1464 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001465 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001466 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001467 goto on_error;
1468 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001469 if (key == NULL)
1470 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001471 tv = STACK_TV_BOT(-3);
1472 di = dict_find(dict, key, -1);
1473 if (di != NULL)
1474 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001475 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001476 clear_tv(&di->di_tv);
1477 di->di_tv = *tv;
1478 }
1479 else
1480 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001481 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001482 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1483 goto failed;
1484 clear_tv(tv);
1485 }
1486 clear_tv(tv_key);
1487 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001488 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001489 }
1490 break;
1491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001492 // push constant
1493 case ISN_PUSHNR:
1494 case ISN_PUSHBOOL:
1495 case ISN_PUSHSPEC:
1496 case ISN_PUSHF:
1497 case ISN_PUSHS:
1498 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001499 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001500 case ISN_PUSHCHANNEL:
1501 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001502 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 goto failed;
1504 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001505 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 ++ectx.ec_stack.ga_len;
1507 switch (iptr->isn_type)
1508 {
1509 case ISN_PUSHNR:
1510 tv->v_type = VAR_NUMBER;
1511 tv->vval.v_number = iptr->isn_arg.number;
1512 break;
1513 case ISN_PUSHBOOL:
1514 tv->v_type = VAR_BOOL;
1515 tv->vval.v_number = iptr->isn_arg.number;
1516 break;
1517 case ISN_PUSHSPEC:
1518 tv->v_type = VAR_SPECIAL;
1519 tv->vval.v_number = iptr->isn_arg.number;
1520 break;
1521#ifdef FEAT_FLOAT
1522 case ISN_PUSHF:
1523 tv->v_type = VAR_FLOAT;
1524 tv->vval.v_float = iptr->isn_arg.fnumber;
1525 break;
1526#endif
1527 case ISN_PUSHBLOB:
1528 blob_copy(iptr->isn_arg.blob, tv);
1529 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001530 case ISN_PUSHFUNC:
1531 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001532 if (iptr->isn_arg.string == NULL)
1533 tv->vval.v_string = NULL;
1534 else
1535 tv->vval.v_string =
1536 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001537 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001538 case ISN_PUSHCHANNEL:
1539#ifdef FEAT_JOB_CHANNEL
1540 tv->v_type = VAR_CHANNEL;
1541 tv->vval.v_channel = iptr->isn_arg.channel;
1542 if (tv->vval.v_channel != NULL)
1543 ++tv->vval.v_channel->ch_refcount;
1544#endif
1545 break;
1546 case ISN_PUSHJOB:
1547#ifdef FEAT_JOB_CHANNEL
1548 tv->v_type = VAR_JOB;
1549 tv->vval.v_job = iptr->isn_arg.job;
1550 if (tv->vval.v_job != NULL)
1551 ++tv->vval.v_job->jv_refcount;
1552#endif
1553 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001554 default:
1555 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001556 tv->vval.v_string = vim_strsave(
1557 iptr->isn_arg.string == NULL
1558 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 }
1560 break;
1561
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001562 case ISN_UNLET:
1563 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1564 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001565 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001566 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001567 case ISN_UNLETENV:
1568 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1569 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001570
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 // create a list from items on the stack; uses a single allocation
1572 // for the list header and the items
1573 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001574 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1575 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 break;
1577
1578 // create a dict from items on the stack
1579 case ISN_NEWDICT:
1580 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001581 int count = iptr->isn_arg.number;
1582 dict_T *dict = dict_alloc();
1583 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584
1585 if (dict == NULL)
1586 goto failed;
1587 for (idx = 0; idx < count; ++idx)
1588 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001589 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001591 // check key is unique
1592 item = dict_find(dict, tv->vval.v_string, -1);
1593 if (item != NULL)
1594 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare8593122020-07-18 15:17:02 +02001596 semsg(_(e_duplicate_key), tv->vval.v_string);
1597 dict_unref(dict);
1598 goto on_error;
1599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 item = dictitem_alloc(tv->vval.v_string);
1601 clear_tv(tv);
1602 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001603 {
1604 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1608 item->di_tv.v_lock = 0;
1609 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001610 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001611 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001612 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001614 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 }
1616
1617 if (count > 0)
1618 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001619 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001620 goto failed;
1621 else
1622 ++ectx.ec_stack.ga_len;
1623 tv = STACK_TV_BOT(-1);
1624 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001625 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 tv->vval.v_dict = dict;
1627 ++dict->dv_refcount;
1628 }
1629 break;
1630
1631 // call a :def function
1632 case ISN_DCALL:
1633 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1634 iptr->isn_arg.dfunc.cdf_argcount,
1635 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001636 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001637 break;
1638
1639 // call a builtin function
1640 case ISN_BCALL:
1641 SOURCING_LNUM = iptr->isn_lnum;
1642 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1643 iptr->isn_arg.bfunc.cbf_argcount,
1644 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001645 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 break;
1647
1648 // call a funcref or partial
1649 case ISN_PCALL:
1650 {
1651 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1652 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001653 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654
1655 SOURCING_LNUM = iptr->isn_lnum;
1656 if (pfunc->cpf_top)
1657 {
1658 // funcref is above the arguments
1659 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1660 }
1661 else
1662 {
1663 // Get the funcref from the stack.
1664 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001665 partial_tv = *STACK_TV_BOT(0);
1666 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 }
1668 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001669 if (tv == &partial_tv)
1670 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001672 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 }
1674 break;
1675
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001676 case ISN_PCALL_END:
1677 // PCALL finished, arguments have been consumed and replaced by
1678 // the return value. Now clear the funcref from the stack,
1679 // and move the return value in its place.
1680 --ectx.ec_stack.ga_len;
1681 clear_tv(STACK_TV_BOT(-1));
1682 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1683 break;
1684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 // call a user defined function or funcref/partial
1686 case ISN_UCALL:
1687 {
1688 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1689
1690 SOURCING_LNUM = iptr->isn_lnum;
1691 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001692 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001693 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 }
1695 break;
1696
1697 // return from a :def function call
1698 case ISN_RETURN:
1699 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001700 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001701 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001702
1703 if (trystack->ga_len > 0)
1704 trycmd = ((trycmd_T *)trystack->ga_data)
1705 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001706 if (trycmd != NULL
1707 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 && trycmd->tcd_finally_idx != 0)
1709 {
1710 // jump to ":finally"
1711 ectx.ec_iidx = trycmd->tcd_finally_idx;
1712 trycmd->tcd_return = TRUE;
1713 }
1714 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001715 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 }
1717 break;
1718
1719 // push a function reference to a compiled function
1720 case ISN_FUNCREF:
1721 {
1722 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001723 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724
1725 pt = ALLOC_CLEAR_ONE(partial_T);
1726 if (pt == NULL)
1727 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001728 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001729 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001730 vim_free(pt);
1731 goto failed;
1732 }
1733 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1734 + iptr->isn_arg.funcref.fr_func;
1735 pt->pt_func = pt_dfunc->df_ufunc;
1736 pt->pt_refcount = 1;
1737 ++pt_dfunc->df_ufunc->uf_refcount;
1738
1739 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1740 {
1741 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1742 + ectx.ec_dfunc_idx;
1743
1744 // The closure needs to find arguments and local
1745 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001746 pt->pt_ectx_stack = &ectx.ec_stack;
1747 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001748
1749 // If this function returns and the closure is still
1750 // used, we need to make a copy of the context
1751 // (arguments and local variables). Store a reference
1752 // to the partial so we can handle that.
1753 ++pt->pt_refcount;
1754 tv = STACK_TV_VAR(dfunc->df_varcount
1755 + iptr->isn_arg.funcref.fr_var_idx);
1756 if (tv->v_type == VAR_PARTIAL)
1757 {
1758 // TODO: use a garray_T on ectx.
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001759 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001760 emsg("Multiple closures not supported yet");
1761 goto failed;
1762 }
1763 tv->v_type = VAR_PARTIAL;
1764 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001765 }
1766
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 tv = STACK_TV_BOT(0);
1768 ++ectx.ec_stack.ga_len;
1769 tv->vval.v_partial = pt;
1770 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001771 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001772 }
1773 break;
1774
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001775 // Create a global function from a lambda.
1776 case ISN_NEWFUNC:
1777 {
1778 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1779
1780 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1781 }
1782 break;
1783
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001784 // jump if a condition is met
1785 case ISN_JUMP:
1786 {
1787 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1788 int jump = TRUE;
1789
1790 if (when != JUMP_ALWAYS)
1791 {
1792 tv = STACK_TV_BOT(-1);
1793 jump = tv2bool(tv);
1794 if (when == JUMP_IF_FALSE
1795 || when == JUMP_AND_KEEP_IF_FALSE)
1796 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001797 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 {
1799 // drop the value from the stack
1800 clear_tv(tv);
1801 --ectx.ec_stack.ga_len;
1802 }
1803 }
1804 if (jump)
1805 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1806 }
1807 break;
1808
1809 // top of a for loop
1810 case ISN_FOR:
1811 {
1812 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1813 typval_T *idxtv =
1814 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1815
1816 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001817 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001818 goto failed;
1819 if (++idxtv->vval.v_number >= list->lv_len)
1820 // past the end of the list, jump to "endfor"
1821 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1822 else if (list->lv_first == &range_list_item)
1823 {
1824 // non-materialized range() list
1825 tv = STACK_TV_BOT(0);
1826 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001827 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 tv->vval.v_number = list_find_nr(
1829 list, idxtv->vval.v_number, NULL);
1830 ++ectx.ec_stack.ga_len;
1831 }
1832 else
1833 {
1834 listitem_T *li = list_find(list, idxtv->vval.v_number);
1835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1837 ++ectx.ec_stack.ga_len;
1838 }
1839 }
1840 break;
1841
1842 // start of ":try" block
1843 case ISN_TRY:
1844 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001845 trycmd_T *trycmd = NULL;
1846
Bram Moolenaar270d0382020-05-15 21:42:53 +02001847 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 goto failed;
1849 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1850 + ectx.ec_trystack.ga_len;
1851 ++ectx.ec_trystack.ga_len;
1852 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001853 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1855 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001856 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 }
1858 break;
1859
1860 case ISN_PUSHEXC:
1861 if (current_exception == NULL)
1862 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001863 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 iemsg("Evaluating catch while current_exception is NULL");
1865 goto failed;
1866 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001867 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 goto failed;
1869 tv = STACK_TV_BOT(0);
1870 ++ectx.ec_stack.ga_len;
1871 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001872 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 tv->vval.v_string = vim_strsave(
1874 (char_u *)current_exception->value);
1875 break;
1876
1877 case ISN_CATCH:
1878 {
1879 garray_T *trystack = &ectx.ec_trystack;
1880
1881 if (trystack->ga_len > 0)
1882 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001883 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 + trystack->ga_len - 1;
1885 trycmd->tcd_caught = TRUE;
1886 }
1887 did_emsg = got_int = did_throw = FALSE;
1888 catch_exception(current_exception);
1889 }
1890 break;
1891
1892 // end of ":try" block
1893 case ISN_ENDTRY:
1894 {
1895 garray_T *trystack = &ectx.ec_trystack;
1896
1897 if (trystack->ga_len > 0)
1898 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001899 trycmd_T *trycmd = NULL;
1900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 --trystack->ga_len;
1902 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001903 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 trycmd = ((trycmd_T *)trystack->ga_data)
1905 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001906 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 {
1908 // discard the exception
1909 if (caught_stack == current_exception)
1910 caught_stack = caught_stack->caught;
1911 discard_current_exception();
1912 }
1913
1914 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001915 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 }
1917 }
1918 break;
1919
1920 case ISN_THROW:
1921 --ectx.ec_stack.ga_len;
1922 tv = STACK_TV_BOT(0);
1923 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1924 {
1925 vim_free(tv->vval.v_string);
1926 goto failed;
1927 }
1928 did_throw = TRUE;
1929 break;
1930
1931 // compare with special values
1932 case ISN_COMPAREBOOL:
1933 case ISN_COMPARESPECIAL:
1934 {
1935 typval_T *tv1 = STACK_TV_BOT(-2);
1936 typval_T *tv2 = STACK_TV_BOT(-1);
1937 varnumber_T arg1 = tv1->vval.v_number;
1938 varnumber_T arg2 = tv2->vval.v_number;
1939 int res;
1940
1941 switch (iptr->isn_arg.op.op_type)
1942 {
1943 case EXPR_EQUAL: res = arg1 == arg2; break;
1944 case EXPR_NEQUAL: res = arg1 != arg2; break;
1945 default: res = 0; break;
1946 }
1947
1948 --ectx.ec_stack.ga_len;
1949 tv1->v_type = VAR_BOOL;
1950 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1951 }
1952 break;
1953
1954 // Operation with two number arguments
1955 case ISN_OPNR:
1956 case ISN_COMPARENR:
1957 {
1958 typval_T *tv1 = STACK_TV_BOT(-2);
1959 typval_T *tv2 = STACK_TV_BOT(-1);
1960 varnumber_T arg1 = tv1->vval.v_number;
1961 varnumber_T arg2 = tv2->vval.v_number;
1962 varnumber_T res;
1963
1964 switch (iptr->isn_arg.op.op_type)
1965 {
1966 case EXPR_MULT: res = arg1 * arg2; break;
1967 case EXPR_DIV: res = arg1 / arg2; break;
1968 case EXPR_REM: res = arg1 % arg2; break;
1969 case EXPR_SUB: res = arg1 - arg2; break;
1970 case EXPR_ADD: res = arg1 + arg2; break;
1971
1972 case EXPR_EQUAL: res = arg1 == arg2; break;
1973 case EXPR_NEQUAL: res = arg1 != arg2; break;
1974 case EXPR_GREATER: res = arg1 > arg2; break;
1975 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1976 case EXPR_SMALLER: res = arg1 < arg2; break;
1977 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1978 default: res = 0; break;
1979 }
1980
1981 --ectx.ec_stack.ga_len;
1982 if (iptr->isn_type == ISN_COMPARENR)
1983 {
1984 tv1->v_type = VAR_BOOL;
1985 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1986 }
1987 else
1988 tv1->vval.v_number = res;
1989 }
1990 break;
1991
1992 // Computation with two float arguments
1993 case ISN_OPFLOAT:
1994 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001995#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001996 {
1997 typval_T *tv1 = STACK_TV_BOT(-2);
1998 typval_T *tv2 = STACK_TV_BOT(-1);
1999 float_T arg1 = tv1->vval.v_float;
2000 float_T arg2 = tv2->vval.v_float;
2001 float_T res = 0;
2002 int cmp = FALSE;
2003
2004 switch (iptr->isn_arg.op.op_type)
2005 {
2006 case EXPR_MULT: res = arg1 * arg2; break;
2007 case EXPR_DIV: res = arg1 / arg2; break;
2008 case EXPR_SUB: res = arg1 - arg2; break;
2009 case EXPR_ADD: res = arg1 + arg2; break;
2010
2011 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2012 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2013 case EXPR_GREATER: cmp = arg1 > arg2; break;
2014 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2015 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2016 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2017 default: cmp = 0; break;
2018 }
2019 --ectx.ec_stack.ga_len;
2020 if (iptr->isn_type == ISN_COMPAREFLOAT)
2021 {
2022 tv1->v_type = VAR_BOOL;
2023 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2024 }
2025 else
2026 tv1->vval.v_float = res;
2027 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002028#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002029 break;
2030
2031 case ISN_COMPARELIST:
2032 {
2033 typval_T *tv1 = STACK_TV_BOT(-2);
2034 typval_T *tv2 = STACK_TV_BOT(-1);
2035 list_T *arg1 = tv1->vval.v_list;
2036 list_T *arg2 = tv2->vval.v_list;
2037 int cmp = FALSE;
2038 int ic = iptr->isn_arg.op.op_ic;
2039
2040 switch (iptr->isn_arg.op.op_type)
2041 {
2042 case EXPR_EQUAL: cmp =
2043 list_equal(arg1, arg2, ic, FALSE); break;
2044 case EXPR_NEQUAL: cmp =
2045 !list_equal(arg1, arg2, ic, FALSE); break;
2046 case EXPR_IS: cmp = arg1 == arg2; break;
2047 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2048 default: cmp = 0; break;
2049 }
2050 --ectx.ec_stack.ga_len;
2051 clear_tv(tv1);
2052 clear_tv(tv2);
2053 tv1->v_type = VAR_BOOL;
2054 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2055 }
2056 break;
2057
2058 case ISN_COMPAREBLOB:
2059 {
2060 typval_T *tv1 = STACK_TV_BOT(-2);
2061 typval_T *tv2 = STACK_TV_BOT(-1);
2062 blob_T *arg1 = tv1->vval.v_blob;
2063 blob_T *arg2 = tv2->vval.v_blob;
2064 int cmp = FALSE;
2065
2066 switch (iptr->isn_arg.op.op_type)
2067 {
2068 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2069 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2070 case EXPR_IS: cmp = arg1 == arg2; break;
2071 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2072 default: cmp = 0; break;
2073 }
2074 --ectx.ec_stack.ga_len;
2075 clear_tv(tv1);
2076 clear_tv(tv2);
2077 tv1->v_type = VAR_BOOL;
2078 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2079 }
2080 break;
2081
2082 // TODO: handle separately
2083 case ISN_COMPARESTRING:
2084 case ISN_COMPAREDICT:
2085 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 case ISN_COMPAREANY:
2087 {
2088 typval_T *tv1 = STACK_TV_BOT(-2);
2089 typval_T *tv2 = STACK_TV_BOT(-1);
2090 exptype_T exptype = iptr->isn_arg.op.op_type;
2091 int ic = iptr->isn_arg.op.op_ic;
2092
2093 typval_compare(tv1, tv2, exptype, ic);
2094 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002095 --ectx.ec_stack.ga_len;
2096 }
2097 break;
2098
2099 case ISN_ADDLIST:
2100 case ISN_ADDBLOB:
2101 {
2102 typval_T *tv1 = STACK_TV_BOT(-2);
2103 typval_T *tv2 = STACK_TV_BOT(-1);
2104
2105 if (iptr->isn_type == ISN_ADDLIST)
2106 eval_addlist(tv1, tv2);
2107 else
2108 eval_addblob(tv1, tv2);
2109 clear_tv(tv2);
2110 --ectx.ec_stack.ga_len;
2111 }
2112 break;
2113
2114 // Computation with two arguments of unknown type
2115 case ISN_OPANY:
2116 {
2117 typval_T *tv1 = STACK_TV_BOT(-2);
2118 typval_T *tv2 = STACK_TV_BOT(-1);
2119 varnumber_T n1, n2;
2120#ifdef FEAT_FLOAT
2121 float_T f1 = 0, f2 = 0;
2122#endif
2123 int error = FALSE;
2124
2125 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2126 {
2127 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2128 {
2129 eval_addlist(tv1, tv2);
2130 clear_tv(tv2);
2131 --ectx.ec_stack.ga_len;
2132 break;
2133 }
2134 else if (tv1->v_type == VAR_BLOB
2135 && tv2->v_type == VAR_BLOB)
2136 {
2137 eval_addblob(tv1, tv2);
2138 clear_tv(tv2);
2139 --ectx.ec_stack.ga_len;
2140 break;
2141 }
2142 }
2143#ifdef FEAT_FLOAT
2144 if (tv1->v_type == VAR_FLOAT)
2145 {
2146 f1 = tv1->vval.v_float;
2147 n1 = 0;
2148 }
2149 else
2150#endif
2151 {
2152 n1 = tv_get_number_chk(tv1, &error);
2153 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002154 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155#ifdef FEAT_FLOAT
2156 if (tv2->v_type == VAR_FLOAT)
2157 f1 = n1;
2158#endif
2159 }
2160#ifdef FEAT_FLOAT
2161 if (tv2->v_type == VAR_FLOAT)
2162 {
2163 f2 = tv2->vval.v_float;
2164 n2 = 0;
2165 }
2166 else
2167#endif
2168 {
2169 n2 = tv_get_number_chk(tv2, &error);
2170 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002171 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002172#ifdef FEAT_FLOAT
2173 if (tv1->v_type == VAR_FLOAT)
2174 f2 = n2;
2175#endif
2176 }
2177#ifdef FEAT_FLOAT
2178 // if there is a float on either side the result is a float
2179 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2180 {
2181 switch (iptr->isn_arg.op.op_type)
2182 {
2183 case EXPR_MULT: f1 = f1 * f2; break;
2184 case EXPR_DIV: f1 = f1 / f2; break;
2185 case EXPR_SUB: f1 = f1 - f2; break;
2186 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002187 default: SOURCING_LNUM = iptr->isn_lnum;
2188 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002189 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002190 }
2191 clear_tv(tv1);
2192 clear_tv(tv2);
2193 tv1->v_type = VAR_FLOAT;
2194 tv1->vval.v_float = f1;
2195 --ectx.ec_stack.ga_len;
2196 }
2197 else
2198#endif
2199 {
2200 switch (iptr->isn_arg.op.op_type)
2201 {
2202 case EXPR_MULT: n1 = n1 * n2; break;
2203 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2204 case EXPR_SUB: n1 = n1 - n2; break;
2205 case EXPR_ADD: n1 = n1 + n2; break;
2206 default: n1 = num_modulus(n1, n2); break;
2207 }
2208 clear_tv(tv1);
2209 clear_tv(tv2);
2210 tv1->v_type = VAR_NUMBER;
2211 tv1->vval.v_number = n1;
2212 --ectx.ec_stack.ga_len;
2213 }
2214 }
2215 break;
2216
2217 case ISN_CONCAT:
2218 {
2219 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2220 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2221 char_u *res;
2222
2223 res = concat_str(str1, str2);
2224 clear_tv(STACK_TV_BOT(-2));
2225 clear_tv(STACK_TV_BOT(-1));
2226 --ectx.ec_stack.ga_len;
2227 STACK_TV_BOT(-1)->vval.v_string = res;
2228 }
2229 break;
2230
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002231 case ISN_STRINDEX:
2232 {
2233 char_u *s;
2234 varnumber_T n;
2235 char_u *res;
2236
2237 // string index: string is at stack-2, index at stack-1
2238 tv = STACK_TV_BOT(-2);
2239 if (tv->v_type != VAR_STRING)
2240 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002241 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002242 emsg(_(e_stringreq));
2243 goto on_error;
2244 }
2245 s = tv->vval.v_string;
2246
2247 tv = STACK_TV_BOT(-1);
2248 if (tv->v_type != VAR_NUMBER)
2249 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002250 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002251 emsg(_(e_number_exp));
2252 goto on_error;
2253 }
2254 n = tv->vval.v_number;
2255
2256 // The resulting variable is a string of a single
2257 // character. If the index is too big or negative the
2258 // result is empty.
2259 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2260 res = NULL;
2261 else
2262 res = vim_strnsave(s + n, 1);
2263 --ectx.ec_stack.ga_len;
2264 tv = STACK_TV_BOT(-1);
2265 vim_free(tv->vval.v_string);
2266 tv->vval.v_string = res;
2267 }
2268 break;
2269
2270 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 {
2272 list_T *list;
2273 varnumber_T n;
2274 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002275 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002276
2277 // list index: list is at stack-2, index at stack-1
2278 tv = STACK_TV_BOT(-2);
2279 if (tv->v_type != VAR_LIST)
2280 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002283 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 }
2285 list = tv->vval.v_list;
2286
2287 tv = STACK_TV_BOT(-1);
2288 if (tv->v_type != VAR_NUMBER)
2289 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002291 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 }
2294 n = tv->vval.v_number;
2295 clear_tv(tv);
2296 if ((li = list_find(list, n)) == NULL)
2297 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002298 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002300 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 }
2302 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002303 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002304 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002305 tv = STACK_TV_BOT(-1);
2306 temp_tv = *tv;
2307 copy_tv(&li->li_tv, tv);
2308 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 }
2310 break;
2311
Bram Moolenaar9af78762020-06-16 11:34:42 +02002312 case ISN_SLICE:
2313 {
2314 list_T *list;
2315 int count = iptr->isn_arg.number;
2316
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002317 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002318 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002319 list = tv->vval.v_list;
2320
2321 // no error for short list, expect it to be checked earlier
2322 if (list != NULL && list->lv_len >= count)
2323 {
2324 list_T *newlist = list_slice(list,
2325 count, list->lv_len - 1);
2326
2327 if (newlist != NULL)
2328 {
2329 list_unref(list);
2330 tv->vval.v_list = newlist;
2331 ++newlist->lv_refcount;
2332 }
2333 }
2334 }
2335 break;
2336
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002337 case ISN_GETITEM:
2338 {
2339 listitem_T *li;
2340 int index = iptr->isn_arg.number;
2341
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002342 // Get list item: list is at stack-1, push item.
2343 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002344 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002345 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002346
2347 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2348 goto failed;
2349 ++ectx.ec_stack.ga_len;
2350 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2351 }
2352 break;
2353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 case ISN_MEMBER:
2355 {
2356 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002357 char_u *key;
2358 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002359 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002360
2361 // dict member: dict is at stack-2, key at stack-1
2362 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002363 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002364 dict = tv->vval.v_dict;
2365
2366 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002367 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002368 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002369
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002370 if ((di = dict_find(dict, key, -1)) == NULL)
2371 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002372 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002373 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002374 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002375 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002376 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002377 --ectx.ec_stack.ga_len;
2378 // Clear the dict after getting the item, to avoid that it
2379 // make the item invalid.
2380 tv = STACK_TV_BOT(-1);
2381 temp_tv = *tv;
2382 copy_tv(&di->di_tv, tv);
2383 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002384 }
2385 break;
2386
2387 // dict member with string key
2388 case ISN_STRINGMEMBER:
2389 {
2390 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002392 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002393
2394 tv = STACK_TV_BOT(-1);
2395 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2396 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002398 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002399 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002400 }
2401 dict = tv->vval.v_dict;
2402
2403 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2404 == NULL)
2405 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002406 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002408 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002410 // Clear the dict after getting the item, to avoid that it
2411 // make the item invalid.
2412 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002413 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002414 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 }
2416 break;
2417
2418 case ISN_NEGATENR:
2419 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002420 if (tv->v_type != VAR_NUMBER
2421#ifdef FEAT_FLOAT
2422 && tv->v_type != VAR_FLOAT
2423#endif
2424 )
2425 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002427 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002428 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002429 }
2430#ifdef FEAT_FLOAT
2431 if (tv->v_type == VAR_FLOAT)
2432 tv->vval.v_float = -tv->vval.v_float;
2433 else
2434#endif
2435 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 break;
2437
2438 case ISN_CHECKNR:
2439 {
2440 int error = FALSE;
2441
2442 tv = STACK_TV_BOT(-1);
2443 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002444 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002445 (void)tv_get_number_chk(tv, &error);
2446 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002447 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 }
2449 break;
2450
2451 case ISN_CHECKTYPE:
2452 {
2453 checktype_T *ct = &iptr->isn_arg.type;
2454
2455 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002456 // TODO: better type comparison
2457 if (tv->v_type != ct->ct_type
2458 && !((tv->v_type == VAR_PARTIAL
2459 && ct->ct_type == VAR_FUNC)
2460 || (tv->v_type == VAR_FUNC
2461 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 semsg(_("E1029: Expected %s but got %s"),
2465 vartype_name(ct->ct_type),
2466 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002467 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002468 }
2469 }
2470 break;
2471
Bram Moolenaar9af78762020-06-16 11:34:42 +02002472 case ISN_CHECKLEN:
2473 {
2474 int min_len = iptr->isn_arg.checklen.cl_min_len;
2475 list_T *list = NULL;
2476
2477 tv = STACK_TV_BOT(-1);
2478 if (tv->v_type == VAR_LIST)
2479 list = tv->vval.v_list;
2480 if (list == NULL || list->lv_len < min_len
2481 || (list->lv_len > min_len
2482 && !iptr->isn_arg.checklen.cl_more_OK))
2483 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002485 semsg(_("E1093: Expected %d items but got %d"),
2486 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002487 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002488 }
2489 }
2490 break;
2491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 case ISN_2BOOL:
2493 {
2494 int n;
2495
2496 tv = STACK_TV_BOT(-1);
2497 n = tv2bool(tv);
2498 if (iptr->isn_arg.number) // invert
2499 n = !n;
2500 clear_tv(tv);
2501 tv->v_type = VAR_BOOL;
2502 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2503 }
2504 break;
2505
2506 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002507 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 {
2509 char_u *str;
2510
2511 tv = STACK_TV_BOT(iptr->isn_arg.number);
2512 if (tv->v_type != VAR_STRING)
2513 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002514 if (iptr->isn_type == ISN_2STRING_ANY)
2515 {
2516 switch (tv->v_type)
2517 {
2518 case VAR_SPECIAL:
2519 case VAR_BOOL:
2520 case VAR_NUMBER:
2521 case VAR_FLOAT:
2522 case VAR_BLOB: break;
2523 default: to_string_error(tv->v_type);
2524 goto on_error;
2525 }
2526 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 str = typval_tostring(tv);
2528 clear_tv(tv);
2529 tv->v_type = VAR_STRING;
2530 tv->vval.v_string = str;
2531 }
2532 }
2533 break;
2534
Bram Moolenaar389df252020-07-09 21:20:47 +02002535 case ISN_SHUFFLE:
2536 {
2537 typval_T tmp_tv;
2538 int item = iptr->isn_arg.shuffle.shfl_item;
2539 int up = iptr->isn_arg.shuffle.shfl_up;
2540
2541 tmp_tv = *STACK_TV_BOT(-item);
2542 for ( ; up > 0 && item > 1; --up)
2543 {
2544 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2545 --item;
2546 }
2547 *STACK_TV_BOT(-item) = tmp_tv;
2548 }
2549 break;
2550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 case ISN_DROP:
2552 --ectx.ec_stack.ga_len;
2553 clear_tv(STACK_TV_BOT(0));
2554 break;
2555 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002556 continue;
2557
Bram Moolenaard032f342020-07-18 18:13:02 +02002558func_return:
2559 // Restore previous function. If the frame pointer is zero then there
2560 // is none and we are done.
2561 if (ectx.ec_frame_idx == initial_frame_idx)
2562 {
2563 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2564 // only fails when out of memory
2565 goto failed;
2566 goto done;
2567 }
2568 if (func_return(&ectx) == FAIL)
2569 // only fails when out of memory
2570 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002571 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002572
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002573on_error:
2574 if (trylevel == 0)
2575 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002576 }
2577
2578done:
2579 // function finished, get result from the stack.
2580 tv = STACK_TV_BOT(-1);
2581 *rettv = *tv;
2582 tv->v_type = VAR_UNKNOWN;
2583 ret = OK;
2584
2585failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002586 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002587 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002588 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002589failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002590 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002591
2592 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2594 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002597 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002598
2599 if (ret != OK && called_emsg == called_emsg_before)
2600 semsg(_("E1099: Unknown error while executing %s"),
2601 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 return ret;
2603}
2604
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605/*
2606 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002607 * We don't really need this at runtime, but we do have tests that require it,
2608 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 */
2610 void
2611ex_disassemble(exarg_T *eap)
2612{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002613 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002614 char_u *fname;
2615 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 dfunc_T *dfunc;
2617 isn_T *instr;
2618 int current;
2619 int line_idx = 0;
2620 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002621 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002623 if (STRNCMP(arg, "<lambda>", 8) == 0)
2624 {
2625 arg += 8;
2626 (void)getdigits(&arg);
2627 fname = vim_strnsave(eap->arg, arg - eap->arg);
2628 }
2629 else
2630 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002631 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002632 if (fname == NULL)
2633 {
2634 semsg(_(e_invarg2), eap->arg);
2635 return;
2636 }
2637
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002638 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002639 if (ufunc == NULL)
2640 {
2641 char_u *p = untrans_function_name(fname);
2642
2643 if (p != NULL)
2644 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002645 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002646 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002647 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 if (ufunc == NULL)
2649 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002650 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 return;
2652 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002653 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002654 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2655 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002656 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002658 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 return;
2660 }
2661 if (ufunc->uf_name_exp != NULL)
2662 msg((char *)ufunc->uf_name_exp);
2663 else
2664 msg((char *)ufunc->uf_name);
2665
2666 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2667 instr = dfunc->df_instr;
2668 for (current = 0; current < dfunc->df_instr_count; ++current)
2669 {
2670 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002671 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672
2673 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2674 {
2675 if (current > prev_current)
2676 {
2677 msg_puts("\n\n");
2678 prev_current = current;
2679 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002680 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2681 if (line != NULL)
2682 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 }
2684
2685 switch (iptr->isn_type)
2686 {
2687 case ISN_EXEC:
2688 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2689 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002690 case ISN_EXECCONCAT:
2691 smsg("%4d EXECCONCAT %lld", current,
2692 (long long)iptr->isn_arg.number);
2693 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 case ISN_ECHO:
2695 {
2696 echo_T *echo = &iptr->isn_arg.echo;
2697
2698 smsg("%4d %s %d", current,
2699 echo->echo_with_white ? "ECHO" : "ECHON",
2700 echo->echo_count);
2701 }
2702 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002703 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002704 smsg("%4d EXECUTE %lld", current,
2705 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002706 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002707 case ISN_ECHOMSG:
2708 smsg("%4d ECHOMSG %lld", current,
2709 (long long)(iptr->isn_arg.number));
2710 break;
2711 case ISN_ECHOERR:
2712 smsg("%4d ECHOERR %lld", current,
2713 (long long)(iptr->isn_arg.number));
2714 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002716 case ISN_LOADOUTER:
2717 {
2718 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2719
2720 if (iptr->isn_arg.number < 0)
2721 smsg("%4d LOAD%s arg[%lld]", current, add,
2722 (long long)(iptr->isn_arg.number
2723 + STACK_FRAME_SIZE));
2724 else
2725 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002726 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002727 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 break;
2729 case ISN_LOADV:
2730 smsg("%4d LOADV v:%s", current,
2731 get_vim_var_name(iptr->isn_arg.number));
2732 break;
2733 case ISN_LOADSCRIPT:
2734 {
2735 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002736 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002737 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2738 + iptr->isn_arg.script.script_idx;
2739
2740 smsg("%4d LOADSCRIPT %s from %s", current,
2741 sv->sv_name, si->sn_name);
2742 }
2743 break;
2744 case ISN_LOADS:
2745 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002746 scriptitem_T *si = SCRIPT_ITEM(
2747 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002748
2749 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002750 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 }
2752 break;
2753 case ISN_LOADG:
2754 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2755 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002756 case ISN_LOADB:
2757 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2758 break;
2759 case ISN_LOADW:
2760 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2761 break;
2762 case ISN_LOADT:
2763 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2764 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002765 case ISN_LOADGDICT:
2766 smsg("%4d LOAD g:", current);
2767 break;
2768 case ISN_LOADBDICT:
2769 smsg("%4d LOAD b:", current);
2770 break;
2771 case ISN_LOADWDICT:
2772 smsg("%4d LOAD w:", current);
2773 break;
2774 case ISN_LOADTDICT:
2775 smsg("%4d LOAD t:", current);
2776 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 case ISN_LOADOPT:
2778 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2779 break;
2780 case ISN_LOADENV:
2781 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2782 break;
2783 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002784 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 break;
2786
2787 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002788 case ISN_STOREOUTER:
2789 {
2790 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2791
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002792 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002793 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002794 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002795 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002796 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002797 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002798 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002800 case ISN_STOREV:
2801 smsg("%4d STOREV v:%s", current,
2802 get_vim_var_name(iptr->isn_arg.number));
2803 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002805 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2806 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002807 case ISN_STOREB:
2808 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2809 break;
2810 case ISN_STOREW:
2811 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2812 break;
2813 case ISN_STORET:
2814 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2815 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002816 case ISN_STORES:
2817 {
2818 scriptitem_T *si = SCRIPT_ITEM(
2819 iptr->isn_arg.loadstore.ls_sid);
2820
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002821 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002822 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 break;
2825 case ISN_STORESCRIPT:
2826 {
2827 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002828 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2830 + iptr->isn_arg.script.script_idx;
2831
2832 smsg("%4d STORESCRIPT %s in %s", current,
2833 sv->sv_name, si->sn_name);
2834 }
2835 break;
2836 case ISN_STOREOPT:
2837 smsg("%4d STOREOPT &%s", current,
2838 iptr->isn_arg.storeopt.so_name);
2839 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002840 case ISN_STOREENV:
2841 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2842 break;
2843 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002844 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002845 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 case ISN_STORENR:
2847 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002848 iptr->isn_arg.storenr.stnr_val,
2849 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 break;
2851
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002852 case ISN_STORELIST:
2853 smsg("%4d STORELIST", current);
2854 break;
2855
2856 case ISN_STOREDICT:
2857 smsg("%4d STOREDICT", current);
2858 break;
2859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002860 // constants
2861 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002862 smsg("%4d PUSHNR %lld", current,
2863 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 break;
2865 case ISN_PUSHBOOL:
2866 case ISN_PUSHSPEC:
2867 smsg("%4d PUSH %s", current,
2868 get_var_special_name(iptr->isn_arg.number));
2869 break;
2870 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002871#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002873#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 break;
2875 case ISN_PUSHS:
2876 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2877 break;
2878 case ISN_PUSHBLOB:
2879 {
2880 char_u *r;
2881 char_u numbuf[NUMBUFLEN];
2882 char_u *tofree;
2883
2884 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002885 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 vim_free(tofree);
2887 }
2888 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002889 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002890 {
2891 char *name = (char *)iptr->isn_arg.string;
2892
2893 smsg("%4d PUSHFUNC \"%s\"", current,
2894 name == NULL ? "[none]" : name);
2895 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002896 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002897 case ISN_PUSHCHANNEL:
2898#ifdef FEAT_JOB_CHANNEL
2899 {
2900 channel_T *channel = iptr->isn_arg.channel;
2901
2902 smsg("%4d PUSHCHANNEL %d", current,
2903 channel == NULL ? 0 : channel->ch_id);
2904 }
2905#endif
2906 break;
2907 case ISN_PUSHJOB:
2908#ifdef FEAT_JOB_CHANNEL
2909 {
2910 typval_T tv;
2911 char_u *name;
2912
2913 tv.v_type = VAR_JOB;
2914 tv.vval.v_job = iptr->isn_arg.job;
2915 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002916 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002917 }
2918#endif
2919 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002920 case ISN_PUSHEXC:
2921 smsg("%4d PUSH v:exception", current);
2922 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002923 case ISN_UNLET:
2924 smsg("%4d UNLET%s %s", current,
2925 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2926 iptr->isn_arg.unlet.ul_name);
2927 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002928 case ISN_UNLETENV:
2929 smsg("%4d UNLETENV%s $%s", current,
2930 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2931 iptr->isn_arg.unlet.ul_name);
2932 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002934 smsg("%4d NEWLIST size %lld", current,
2935 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 break;
2937 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002938 smsg("%4d NEWDICT size %lld", current,
2939 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002940 break;
2941
2942 // function call
2943 case ISN_BCALL:
2944 {
2945 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2946
2947 smsg("%4d BCALL %s(argc %d)", current,
2948 internal_func_name(cbfunc->cbf_idx),
2949 cbfunc->cbf_argcount);
2950 }
2951 break;
2952 case ISN_DCALL:
2953 {
2954 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2955 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2956 + cdfunc->cdf_idx;
2957
2958 smsg("%4d DCALL %s(argc %d)", current,
2959 df->df_ufunc->uf_name_exp != NULL
2960 ? df->df_ufunc->uf_name_exp
2961 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2962 }
2963 break;
2964 case ISN_UCALL:
2965 {
2966 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2967
2968 smsg("%4d UCALL %s(argc %d)", current,
2969 cufunc->cuf_name, cufunc->cuf_argcount);
2970 }
2971 break;
2972 case ISN_PCALL:
2973 {
2974 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2975
2976 smsg("%4d PCALL%s (argc %d)", current,
2977 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2978 }
2979 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002980 case ISN_PCALL_END:
2981 smsg("%4d PCALL end", current);
2982 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002983 case ISN_RETURN:
2984 smsg("%4d RETURN", current);
2985 break;
2986 case ISN_FUNCREF:
2987 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002988 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002989 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002990 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002992 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002993 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 }
2995 break;
2996
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002997 case ISN_NEWFUNC:
2998 {
2999 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3000
3001 smsg("%4d NEWFUNC %s %s", current,
3002 newfunc->nf_lambda, newfunc->nf_global);
3003 }
3004 break;
3005
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003006 case ISN_JUMP:
3007 {
3008 char *when = "?";
3009
3010 switch (iptr->isn_arg.jump.jump_when)
3011 {
3012 case JUMP_ALWAYS:
3013 when = "JUMP";
3014 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 case JUMP_AND_KEEP_IF_TRUE:
3016 when = "JUMP_AND_KEEP_IF_TRUE";
3017 break;
3018 case JUMP_IF_FALSE:
3019 when = "JUMP_IF_FALSE";
3020 break;
3021 case JUMP_AND_KEEP_IF_FALSE:
3022 when = "JUMP_AND_KEEP_IF_FALSE";
3023 break;
3024 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003025 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003026 iptr->isn_arg.jump.jump_where);
3027 }
3028 break;
3029
3030 case ISN_FOR:
3031 {
3032 forloop_T *forloop = &iptr->isn_arg.forloop;
3033
3034 smsg("%4d FOR $%d -> %d", current,
3035 forloop->for_idx, forloop->for_end);
3036 }
3037 break;
3038
3039 case ISN_TRY:
3040 {
3041 try_T *try = &iptr->isn_arg.try;
3042
3043 smsg("%4d TRY catch -> %d, finally -> %d", current,
3044 try->try_catch, try->try_finally);
3045 }
3046 break;
3047 case ISN_CATCH:
3048 // TODO
3049 smsg("%4d CATCH", current);
3050 break;
3051 case ISN_ENDTRY:
3052 smsg("%4d ENDTRY", current);
3053 break;
3054 case ISN_THROW:
3055 smsg("%4d THROW", current);
3056 break;
3057
3058 // expression operations on number
3059 case ISN_OPNR:
3060 case ISN_OPFLOAT:
3061 case ISN_OPANY:
3062 {
3063 char *what;
3064 char *ins;
3065
3066 switch (iptr->isn_arg.op.op_type)
3067 {
3068 case EXPR_MULT: what = "*"; break;
3069 case EXPR_DIV: what = "/"; break;
3070 case EXPR_REM: what = "%"; break;
3071 case EXPR_SUB: what = "-"; break;
3072 case EXPR_ADD: what = "+"; break;
3073 default: what = "???"; break;
3074 }
3075 switch (iptr->isn_type)
3076 {
3077 case ISN_OPNR: ins = "OPNR"; break;
3078 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3079 case ISN_OPANY: ins = "OPANY"; break;
3080 default: ins = "???"; break;
3081 }
3082 smsg("%4d %s %s", current, ins, what);
3083 }
3084 break;
3085
3086 case ISN_COMPAREBOOL:
3087 case ISN_COMPARESPECIAL:
3088 case ISN_COMPARENR:
3089 case ISN_COMPAREFLOAT:
3090 case ISN_COMPARESTRING:
3091 case ISN_COMPAREBLOB:
3092 case ISN_COMPARELIST:
3093 case ISN_COMPAREDICT:
3094 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 case ISN_COMPAREANY:
3096 {
3097 char *p;
3098 char buf[10];
3099 char *type;
3100
3101 switch (iptr->isn_arg.op.op_type)
3102 {
3103 case EXPR_EQUAL: p = "=="; break;
3104 case EXPR_NEQUAL: p = "!="; break;
3105 case EXPR_GREATER: p = ">"; break;
3106 case EXPR_GEQUAL: p = ">="; break;
3107 case EXPR_SMALLER: p = "<"; break;
3108 case EXPR_SEQUAL: p = "<="; break;
3109 case EXPR_MATCH: p = "=~"; break;
3110 case EXPR_IS: p = "is"; break;
3111 case EXPR_ISNOT: p = "isnot"; break;
3112 case EXPR_NOMATCH: p = "!~"; break;
3113 default: p = "???"; break;
3114 }
3115 STRCPY(buf, p);
3116 if (iptr->isn_arg.op.op_ic == TRUE)
3117 strcat(buf, "?");
3118 switch(iptr->isn_type)
3119 {
3120 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3121 case ISN_COMPARESPECIAL:
3122 type = "COMPARESPECIAL"; break;
3123 case ISN_COMPARENR: type = "COMPARENR"; break;
3124 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3125 case ISN_COMPARESTRING:
3126 type = "COMPARESTRING"; break;
3127 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3128 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3129 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3130 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3132 default: type = "???"; break;
3133 }
3134
3135 smsg("%4d %s %s", current, type, buf);
3136 }
3137 break;
3138
3139 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3140 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3141
3142 // expression operations
3143 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003144 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3145 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003146 case ISN_SLICE: smsg("%4d SLICE %lld",
3147 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003148 case ISN_GETITEM: smsg("%4d ITEM %lld",
3149 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003150 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3151 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152 iptr->isn_arg.string); break;
3153 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3154
3155 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3156 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3157 vartype_name(iptr->isn_arg.type.ct_type),
3158 iptr->isn_arg.type.ct_off);
3159 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003160 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3161 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3162 iptr->isn_arg.checklen.cl_min_len);
3163 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003164 case ISN_2BOOL: if (iptr->isn_arg.number)
3165 smsg("%4d INVERT (!val)", current);
3166 else
3167 smsg("%4d 2BOOL (!!val)", current);
3168 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003169 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3170 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003171 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003172 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3173 (long long)(iptr->isn_arg.number));
3174 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003175
Bram Moolenaar389df252020-07-09 21:20:47 +02003176 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3177 iptr->isn_arg.shuffle.shfl_item,
3178 iptr->isn_arg.shuffle.shfl_up);
3179 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180 case ISN_DROP: smsg("%4d DROP", current); break;
3181 }
3182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003183}
3184
3185/*
3186 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3187 * list, etc. Mostly like what JavaScript does, except that empty list and
3188 * empty dictionary are FALSE.
3189 */
3190 int
3191tv2bool(typval_T *tv)
3192{
3193 switch (tv->v_type)
3194 {
3195 case VAR_NUMBER:
3196 return tv->vval.v_number != 0;
3197 case VAR_FLOAT:
3198#ifdef FEAT_FLOAT
3199 return tv->vval.v_float != 0.0;
3200#else
3201 break;
3202#endif
3203 case VAR_PARTIAL:
3204 return tv->vval.v_partial != NULL;
3205 case VAR_FUNC:
3206 case VAR_STRING:
3207 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3208 case VAR_LIST:
3209 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3210 case VAR_DICT:
3211 return tv->vval.v_dict != NULL
3212 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3213 case VAR_BOOL:
3214 case VAR_SPECIAL:
3215 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3216 case VAR_JOB:
3217#ifdef FEAT_JOB_CHANNEL
3218 return tv->vval.v_job != NULL;
3219#else
3220 break;
3221#endif
3222 case VAR_CHANNEL:
3223#ifdef FEAT_JOB_CHANNEL
3224 return tv->vval.v_channel != NULL;
3225#else
3226 break;
3227#endif
3228 case VAR_BLOB:
3229 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3230 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003231 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232 case VAR_VOID:
3233 break;
3234 }
3235 return FALSE;
3236}
3237
3238/*
3239 * If "tv" is a string give an error and return FAIL.
3240 */
3241 int
3242check_not_string(typval_T *tv)
3243{
3244 if (tv->v_type == VAR_STRING)
3245 {
3246 emsg(_("E1030: Using a String as a Number"));
3247 clear_tv(tv);
3248 return FAIL;
3249 }
3250 return OK;
3251}
3252
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003253
3254#endif // FEAT_EVAL