blob: 00adff3850d816d60df19c1fdd2e58f98fecc4ca [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200163 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
165 if (dfunc->df_deleted)
166 {
167 emsg_funcname(e_func_deleted, ufunc->uf_name);
168 return FAIL;
169 }
170
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 if (ufunc->uf_va_name != NULL)
172 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 // Stack at time of call with 2 varargs:
175 // normal_arg
176 // optional_arg
177 // vararg_1
178 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // After creating the list:
180 // normal_arg
181 // optional_arg
182 // vararg-list
183 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list
186 // normal_arg
187 // (space for optional_arg)
188 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 vararg_count = argcount - ufunc->uf_args.ga_len;
190 if (vararg_count < 0)
191 vararg_count = 0;
192 else
193 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196
197 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 }
199
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 if (arg_to_add < 0)
202 {
203 iemsg("Argument count wrong?");
204 return FAIL;
205 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 return FAIL;
209
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // Move the vararg-list to below the missing optional arguments.
211 if (vararg_count > 0 && arg_to_add > 0)
212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100213
214 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
219 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
223 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
229 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Set execution state to the start of the called function.
232 ectx->ec_dfunc_idx = cdf_idx;
233 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200234 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
235 if (entry != NULL)
236 {
237 // Set the script context to the script where the function was defined.
238 // TODO: save more than the SID?
239 entry->es_save_sid = current_sctx.sc_sid;
240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
241 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242
243 // Decide where to start execution, handles optional arguments.
244 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245
246 return OK;
247}
248
249// Get pointer to item in the stack.
250#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
251
252/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200253 * Used when returning from a function: Check if any closure is still
254 * referenced. If so then move the arguments and variables to a separate piece
255 * of stack to be used when the closure is called.
256 * When "free_arguments" is TRUE the arguments are to be freed.
257 * Returns FAIL when out of memory.
258 */
259 static int
260handle_closure_in_use(ectx_T *ectx, int free_arguments)
261{
262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
263 + ectx->ec_dfunc_idx;
264 int argcount = ufunc_argcount(dfunc->df_ufunc);
265 int top = ectx->ec_frame_idx - argcount;
266 int idx;
267 typval_T *tv;
268 int closure_in_use = FALSE;
269
270 // Check if any created closure is still in use.
271 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
272 {
273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
274 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
276 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200277 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200278 int refcount = tv->vval.v_partial->pt_refcount;
279 int i;
280
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200281 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200282 // unreferenced on return.
283 for (i = 0; i < dfunc->df_varcount; ++i)
284 {
285 typval_T *stv = STACK_TV(ectx->ec_frame_idx
286 + STACK_FRAME_SIZE + i);
287 if (stv->v_type == VAR_PARTIAL
288 && tv->vval.v_partial == stv->vval.v_partial)
289 --refcount;
290 }
291 if (refcount > 1)
292 {
293 closure_in_use = TRUE;
294 break;
295 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200296 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297 }
298
299 if (closure_in_use)
300 {
301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
302 typval_T *stack;
303
304 // A closure is using the arguments and/or local variables.
305 // Move them to the called function.
306 if (funcstack == NULL)
307 return FAIL;
308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
309 + dfunc->df_varcount;
310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
311 funcstack->fs_ga.ga_data = stack;
312 if (stack == NULL)
313 {
314 vim_free(funcstack);
315 return FAIL;
316 }
317
318 // Move or copy the arguments.
319 for (idx = 0; idx < argcount; ++idx)
320 {
321 tv = STACK_TV(top + idx);
322 if (free_arguments)
323 {
324 *(stack + idx) = *tv;
325 tv->v_type = VAR_UNKNOWN;
326 }
327 else
328 copy_tv(tv, stack + idx);
329 }
330 // Move the local variables.
331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
332 {
333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200334
335 // Do not copy a partial created for a local function.
336 // TODO: this won't work if the closure actually uses it. But when
337 // keeping it it gets complicated: it will create a reference cycle
338 // inside the partial, thus needs special handling for garbage
339 // collection.
340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
341 {
342 int i;
343 typval_T *ctv;
344
345 for (i = 0; i < dfunc->df_closure_count; ++i)
346 {
347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
348 + dfunc->df_varcount + i);
349 if (tv->vval.v_partial == ctv->vval.v_partial)
350 break;
351 }
352 if (i < dfunc->df_closure_count)
353 {
354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
355 VAR_UNKNOWN;
356 continue;
357 }
358 }
359
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
361 tv->v_type = VAR_UNKNOWN;
362 }
363
364 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
365 {
366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
367 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200370 partial_T *partial = tv->vval.v_partial;
371
372 if (partial->pt_refcount > 1)
373 {
374 ++funcstack->fs_refcount;
375 partial->pt_funcstack = funcstack;
376 partial->pt_ectx_stack = &funcstack->fs_ga;
377 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
378 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 }
380 }
381 }
382
383 return OK;
384}
385
386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Return from the current function.
388 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390func_return(ectx_T *ectx)
391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
394 + ectx->ec_dfunc_idx;
395 int argcount = ufunc_argcount(dfunc->df_ufunc);
396 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200397 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 entry = estack_pop();
401 if (entry != NULL)
402 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 if (handle_closure_in_use(ectx, TRUE) == FAIL)
405 return FAIL;
406
407 // Clear the arguments.
408 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
409 clear_tv(STACK_TV(idx));
410
411 // Clear local variables and temp values, but not the return value.
412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100413 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100416 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
421 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422
423 // Reset the stack to the position before the call, move the return value
424 // to the top of the stack.
425 idx = ectx->ec_stack.ga_len - 1;
426 ectx->ec_stack.ga_len = top + 1;
427 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428
429 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430}
431
432#undef STACK_TV
433
434/*
435 * Prepare arguments and rettv for calling a builtin or user function.
436 */
437 static int
438call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
439{
440 int idx;
441 typval_T *tv;
442
443 // Move arguments from bottom of the stack to argvars[] and add terminator.
444 for (idx = 0; idx < argcount; ++idx)
445 argvars[idx] = *STACK_TV_BOT(idx - argcount);
446 argvars[argcount].v_type = VAR_UNKNOWN;
447
448 // Result replaces the arguments on the stack.
449 if (argcount > 0)
450 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 return FAIL;
453 else
454 ++ectx->ec_stack.ga_len;
455
456 // Default return value is zero.
457 tv = STACK_TV_BOT(-1);
458 tv->v_type = VAR_NUMBER;
459 tv->vval.v_number = 0;
460
461 return OK;
462}
463
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200464// Ugly global to avoid passing the execution context around through many
465// layers.
466static ectx_T *current_ectx = NULL;
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Call a builtin function by index.
470 */
471 static int
472call_bfunc(int func_idx, int argcount, ectx_T *ectx)
473{
474 typval_T argvars[MAX_FUNC_ARGS];
475 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200476 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200477 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 if (call_prepare(argcount, argvars, ectx) == FAIL)
480 return FAIL;
481
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200482 // Call the builtin function. Set "current_ectx" so that when it
483 // recursively invokes call_def_function() a closure context can be set.
484 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200491
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200492 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return OK;
495}
496
497/*
498 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 */
501 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503{
504 typval_T argvars[MAX_FUNC_ARGS];
505 funcexe_T funcexe;
506 int error;
507 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200508 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200510 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200511 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
512 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200513 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100514 {
515 // The function has been compiled, can call it quickly. For a function
516 // that was defined later: we can call it directly next time.
517 if (iptr != NULL)
518 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100519 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100520 iptr->isn_type = ISN_DCALL;
521 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
522 iptr->isn_arg.dfunc.cdf_argcount = argcount;
523 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100525 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100526
527 if (call_prepare(argcount, argvars, ectx) == FAIL)
528 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200529 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 funcexe.evaluate = TRUE;
531
532 // Call the user function. Result goes in last position on the stack.
533 // TODO: add selfdict if there is one
534 error = call_user_func_check(ufunc, argcount, argvars,
535 STACK_TV_BOT(-1), &funcexe, NULL);
536
537 // Clear the arguments.
538 for (idx = 0; idx < argcount; ++idx)
539 clear_tv(&argvars[idx]);
540
541 if (error != FCERR_NONE)
542 {
543 user_func_error(error, ufunc->uf_name);
544 return FAIL;
545 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200546 if (called_emsg > called_emsg_before)
547 // Error other than from calling the function itself.
548 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100549 return OK;
550}
551
552/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200553 * Return TRUE if an error was given or CTRL-C was pressed.
554 */
555 static int
556vim9_aborting(int prev_called_emsg)
557{
558 return called_emsg > prev_called_emsg || got_int || did_throw;
559}
560
561/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562 * Execute a function by "name".
563 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100564 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565 * Returns FAIL if not found without an error message.
566 */
567 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100568call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100569{
570 ufunc_T *ufunc;
571
572 if (builtin_function(name, -1))
573 {
574 int func_idx = find_internal_func(name);
575
576 if (func_idx < 0)
577 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200578 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 return FAIL;
580 return call_bfunc(func_idx, argcount, ectx);
581 }
582
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200583 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200584
585 if (ufunc == NULL)
586 {
587 int called_emsg_before = called_emsg;
588
589 if (script_autoload(name, TRUE))
590 // loaded a package, search for the function again
591 ufunc = find_func(name, FALSE, NULL);
592 if (vim9_aborting(called_emsg_before))
593 return FAIL; // bail out if loading the script caused an error
594 }
595
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100597 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100598
599 return FAIL;
600}
601
602 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200603call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200605 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200606 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607 int called_emsg_before = called_emsg;
608
609 if (tv->v_type == VAR_PARTIAL)
610 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200611 partial_T *pt = tv->vval.v_partial;
612 int i;
613
614 if (pt->pt_argc > 0)
615 {
616 // Make space for arguments from the partial, shift the "argcount"
617 // arguments up.
618 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
619 return FAIL;
620 for (i = 1; i <= argcount; ++i)
621 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
622 ectx->ec_stack.ga_len += pt->pt_argc;
623 argcount += pt->pt_argc;
624
625 // copy the arguments from the partial onto the stack
626 for (i = 0; i < pt->pt_argc; ++i)
627 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629
630 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200631 {
632 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
633
634 // closure may need the function context where it was defined
635 ectx->ec_outer_stack = pt->pt_ectx_stack;
636 ectx->ec_outer_frame = pt->pt_ectx_frame;
637
638 return ret;
639 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100640 name = pt->pt_name;
641 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200642 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200644 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 {
646 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200647 semsg(_(e_unknownfunc),
648 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 return FAIL;
650 }
651 return OK;
652}
653
654/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100655 * Store "tv" in variable "name".
656 * This is for s: and g: variables.
657 */
658 static void
659store_var(char_u *name, typval_T *tv)
660{
661 funccal_entry_T entry;
662
663 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200664 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100665 restore_funccal();
666}
667
Bram Moolenaard3aac292020-04-19 14:32:17 +0200668
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100669/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 * Execute a function by "name".
671 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100672 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 */
674 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100675call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676{
Bram Moolenaared677f52020-08-12 16:38:10 +0200677 int called_emsg_before = called_emsg;
678 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679
Bram Moolenaared677f52020-08-12 16:38:10 +0200680 res = call_by_name(name, argcount, ectx, iptr);
681 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200683 dictitem_T *v;
684
685 v = find_var(name, NULL, FALSE);
686 if (v == NULL)
687 {
688 semsg(_(e_unknownfunc), name);
689 return FAIL;
690 }
691 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
692 {
693 semsg(_(e_unknownfunc), name);
694 return FAIL;
695 }
696 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100697 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200698 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100699}
700
701/*
702 * Call a "def" function from old Vim script.
703 * Return OK or FAIL.
704 */
705 int
706call_def_function(
707 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200708 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200710 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 typval_T *rettv) // return value
712{
713 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200714 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200715 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100716 typval_T *tv;
717 int idx;
718 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100719 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200720 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200721 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200722 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100723
724// Get pointer to item in the stack.
725#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
726
727// Get pointer to item at the bottom of the stack, -1 is the bottom.
728#undef STACK_TV_BOT
729#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
730
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200731// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200732#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 +0100733
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200734// Like STACK_TV_VAR but use the outer scope
735#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
736
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200737 if (ufunc->uf_def_status == UF_NOT_COMPILED
738 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200739 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200740 {
741 if (called_emsg == called_emsg_before)
742 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200743 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200744 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200745 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200746
Bram Moolenaar09689a02020-05-09 22:50:08 +0200747 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200748 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200749 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
750 + ufunc->uf_dfunc_idx;
751 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200752 {
753 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200754 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200755 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200758 CLEAR_FIELD(ectx);
759 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
760 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
761 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
762 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
764
765 // Put arguments on the stack.
766 for (idx = 0; idx < argc; ++idx)
767 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200768 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200769 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
770 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200771 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 copy_tv(&argv[idx], STACK_TV_BOT(0));
773 ++ectx.ec_stack.ga_len;
774 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200775
776 // Turn varargs into a list. Empty list if no args.
777 if (ufunc->uf_va_name != NULL)
778 {
779 int vararg_count = argc - ufunc->uf_args.ga_len;
780
781 if (vararg_count < 0)
782 vararg_count = 0;
783 else
784 argc -= vararg_count;
785 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200786 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200787
788 // Check the type of the list items.
789 tv = STACK_TV_BOT(-1);
790 if (ufunc->uf_va_type != NULL
791 && ufunc->uf_va_type->tt_member != &t_any
792 && tv->vval.v_list != NULL)
793 {
794 type_T *expected = ufunc->uf_va_type->tt_member;
795 listitem_T *li = tv->vval.v_list->lv_first;
796
797 for (idx = 0; idx < vararg_count; ++idx)
798 {
799 if (check_typval_type(expected, &li->li_tv) == FAIL)
800 goto failed_early;
801 li = li->li_next;
802 }
803 }
804
Bram Moolenaar23e03252020-04-12 22:22:31 +0200805 if (defcount > 0)
806 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200807 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200808 --ectx.ec_stack.ga_len;
809 }
810
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100811 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200812 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100813 if (defcount > 0)
814 for (idx = 0; idx < defcount; ++idx)
815 {
816 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
817 ++ectx.ec_stack.ga_len;
818 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200819 if (ufunc->uf_va_name != NULL)
820 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
822 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200823 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
824 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200826 if (partial != NULL)
827 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200828 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
829 {
830 // TODO: is this always the right way?
831 ectx.ec_outer_stack = &current_ectx->ec_stack;
832 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
833 }
834 else
835 {
836 ectx.ec_outer_stack = partial->pt_ectx_stack;
837 ectx.ec_outer_frame = partial->pt_ectx_frame;
838 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200839 }
840
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100841 // dummy frame entries
842 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
843 {
844 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
845 ++ectx.ec_stack.ga_len;
846 }
847
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200848 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200849 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200850 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
851 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200852 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200854 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200855 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200856 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200857
858 ectx.ec_instr = dfunc->df_instr;
859 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100860
Bram Moolenaara26b9702020-04-18 19:53:28 +0200861 // Commands behave like vim9script.
862 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
863
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100864 // Decide where to start execution, handles optional arguments.
865 init_instr_idx(ufunc, argc, &ectx);
866
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 for (;;)
868 {
869 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100870
Bram Moolenaar270d0382020-05-15 21:42:53 +0200871 if (++breakcheck_count >= 100)
872 {
873 line_breakcheck();
874 breakcheck_count = 0;
875 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100876 if (got_int)
877 {
878 // Turn CTRL-C into an exception.
879 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100880 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100881 goto failed;
882 did_throw = TRUE;
883 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100884
Bram Moolenaara26b9702020-04-18 19:53:28 +0200885 if (did_emsg && msg_list != NULL && *msg_list != NULL)
886 {
887 // Turn an error message into an exception.
888 did_emsg = FALSE;
889 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
890 goto failed;
891 did_throw = TRUE;
892 *msg_list = NULL;
893 }
894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100895 if (did_throw && !ectx.ec_in_catch)
896 {
897 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100898 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100899
900 // An exception jumps to the first catch, finally, or returns from
901 // the current function.
902 if (trystack->ga_len > 0)
903 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200904 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 {
906 // jump to ":catch" or ":finally"
907 ectx.ec_in_catch = TRUE;
908 ectx.ec_iidx = trycmd->tcd_catch_idx;
909 }
910 else
911 {
912 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200913 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100914 {
915 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200916 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100917 goto failed;
918 tv = STACK_TV_BOT(0);
919 tv->v_type = VAR_NUMBER;
920 tv->vval.v_number = 0;
921 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100922 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200923 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
924 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 goto done;
926 }
927
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200928 if (func_return(&ectx) == FAIL)
929 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100930 }
931 continue;
932 }
933
934 iptr = &ectx.ec_instr[ectx.ec_iidx++];
935 switch (iptr->isn_type)
936 {
937 // execute Ex command line
938 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200939 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 do_cmdline_cmd(iptr->isn_arg.string);
941 break;
942
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200943 // execute Ex command from pieces on the stack
944 case ISN_EXECCONCAT:
945 {
946 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200947 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200948 int pass;
949 int i;
950 char_u *cmd = NULL;
951 char_u *str;
952
953 for (pass = 1; pass <= 2; ++pass)
954 {
955 for (i = 0; i < count; ++i)
956 {
957 tv = STACK_TV_BOT(i - count);
958 str = tv->vval.v_string;
959 if (str != NULL && *str != NUL)
960 {
961 if (pass == 2)
962 STRCPY(cmd + len, str);
963 len += STRLEN(str);
964 }
965 if (pass == 2)
966 clear_tv(tv);
967 }
968 if (pass == 1)
969 {
970 cmd = alloc(len + 1);
971 if (cmd == NULL)
972 goto failed;
973 len = 0;
974 }
975 }
976
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200977 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200978 do_cmdline_cmd(cmd);
979 vim_free(cmd);
980 }
981 break;
982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100983 // execute :echo {string} ...
984 case ISN_ECHO:
985 {
986 int count = iptr->isn_arg.echo.echo_count;
987 int atstart = TRUE;
988 int needclr = TRUE;
989
990 for (idx = 0; idx < count; ++idx)
991 {
992 tv = STACK_TV_BOT(idx - count);
993 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
994 &atstart, &needclr);
995 clear_tv(tv);
996 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100997 if (needclr)
998 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100999 ectx.ec_stack.ga_len -= count;
1000 }
1001 break;
1002
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001003 // :execute {string} ...
1004 // :echomsg {string} ...
1005 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001006 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001007 case ISN_ECHOMSG:
1008 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001009 {
1010 int count = iptr->isn_arg.number;
1011 garray_T ga;
1012 char_u buf[NUMBUFLEN];
1013 char_u *p;
1014 int len;
1015 int failed = FALSE;
1016
1017 ga_init2(&ga, 1, 80);
1018 for (idx = 0; idx < count; ++idx)
1019 {
1020 tv = STACK_TV_BOT(idx - count);
1021 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1022 {
1023 emsg(_(e_inval_string));
1024 break;
1025 }
1026 else
1027 p = tv_get_string_buf(tv, buf);
1028
1029 len = (int)STRLEN(p);
1030 if (ga_grow(&ga, len + 2) == FAIL)
1031 failed = TRUE;
1032 else
1033 {
1034 if (ga.ga_len > 0)
1035 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1036 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1037 ga.ga_len += len;
1038 }
1039 clear_tv(tv);
1040 }
1041 ectx.ec_stack.ga_len -= count;
1042
1043 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001044 {
1045 if (iptr->isn_type == ISN_EXECUTE)
1046 do_cmdline_cmd((char_u *)ga.ga_data);
1047 else
1048 {
1049 msg_sb_eol();
1050 if (iptr->isn_type == ISN_ECHOMSG)
1051 {
1052 msg_attr(ga.ga_data, echo_attr);
1053 out_flush();
1054 }
1055 else
1056 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001057 SOURCING_LNUM = iptr->isn_lnum;
1058 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001059 }
1060 }
1061 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001062 ga_clear(&ga);
1063 }
1064 break;
1065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 // load local variable or argument
1067 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001068 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069 goto failed;
1070 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1071 ++ectx.ec_stack.ga_len;
1072 break;
1073
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001074 // load variable or argument from outer scope
1075 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001076 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001077 goto failed;
1078 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1079 STACK_TV_BOT(0));
1080 ++ectx.ec_stack.ga_len;
1081 break;
1082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001083 // load v: variable
1084 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001085 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001086 goto failed;
1087 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1088 ++ectx.ec_stack.ga_len;
1089 break;
1090
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001091 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 case ISN_LOADSCRIPT:
1093 {
1094 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001095 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 svar_T *sv;
1097
1098 sv = ((svar_T *)si->sn_var_vals.ga_data)
1099 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001100 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 goto failed;
1102 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1103 ++ectx.ec_stack.ga_len;
1104 }
1105 break;
1106
1107 // load s: variable in old script
1108 case ISN_LOADS:
1109 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001110 hashtab_T *ht = &SCRIPT_VARS(
1111 iptr->isn_arg.loadstore.ls_sid);
1112 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 if (di == NULL)
1116 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001118 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 }
1120 else
1121 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001122 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 goto failed;
1124 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1125 ++ectx.ec_stack.ga_len;
1126 }
1127 }
1128 break;
1129
Bram Moolenaard3aac292020-04-19 14:32:17 +02001130 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001131 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001132 case ISN_LOADB:
1133 case ISN_LOADW:
1134 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001136 dictitem_T *di = NULL;
1137 hashtab_T *ht = NULL;
1138 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001139
Bram Moolenaard3aac292020-04-19 14:32:17 +02001140 switch (iptr->isn_type)
1141 {
1142 case ISN_LOADG:
1143 ht = get_globvar_ht();
1144 namespace = 'g';
1145 break;
1146 case ISN_LOADB:
1147 ht = &curbuf->b_vars->dv_hashtab;
1148 namespace = 'b';
1149 break;
1150 case ISN_LOADW:
1151 ht = &curwin->w_vars->dv_hashtab;
1152 namespace = 'w';
1153 break;
1154 case ISN_LOADT:
1155 ht = &curtab->tp_vars->dv_hashtab;
1156 namespace = 't';
1157 break;
1158 default: // Cannot reach here
1159 goto failed;
1160 }
1161 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001162
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 if (di == NULL)
1164 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001165 semsg(_("E121: Undefined variable: %c:%s"),
1166 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001167 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001168 }
1169 else
1170 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001171 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 goto failed;
1173 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1174 ++ectx.ec_stack.ga_len;
1175 }
1176 }
1177 break;
1178
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001179 // load g:/b:/w:/t: namespace
1180 case ISN_LOADGDICT:
1181 case ISN_LOADBDICT:
1182 case ISN_LOADWDICT:
1183 case ISN_LOADTDICT:
1184 {
1185 dict_T *d = NULL;
1186
1187 switch (iptr->isn_type)
1188 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001189 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1190 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1191 case ISN_LOADWDICT: d = curwin->w_vars; break;
1192 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001193 default: // Cannot reach here
1194 goto failed;
1195 }
1196 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1197 goto failed;
1198 tv = STACK_TV_BOT(0);
1199 tv->v_type = VAR_DICT;
1200 tv->v_lock = 0;
1201 tv->vval.v_dict = d;
1202 ++ectx.ec_stack.ga_len;
1203 }
1204 break;
1205
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001206 // load &option
1207 case ISN_LOADOPT:
1208 {
1209 typval_T optval;
1210 char_u *name = iptr->isn_arg.string;
1211
Bram Moolenaara8c17702020-04-01 21:17:24 +02001212 // This is not expected to fail, name is checked during
1213 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001214 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001216 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001217 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001218 *STACK_TV_BOT(0) = optval;
1219 ++ectx.ec_stack.ga_len;
1220 }
1221 break;
1222
1223 // load $ENV
1224 case ISN_LOADENV:
1225 {
1226 typval_T optval;
1227 char_u *name = iptr->isn_arg.string;
1228
Bram Moolenaar270d0382020-05-15 21:42:53 +02001229 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001230 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001231 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001232 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 *STACK_TV_BOT(0) = optval;
1234 ++ectx.ec_stack.ga_len;
1235 }
1236 break;
1237
1238 // load @register
1239 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001240 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241 goto failed;
1242 tv = STACK_TV_BOT(0);
1243 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001244 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245 tv->vval.v_string = get_reg_contents(
1246 iptr->isn_arg.number, GREG_EXPR_SRC);
1247 ++ectx.ec_stack.ga_len;
1248 break;
1249
1250 // store local variable
1251 case ISN_STORE:
1252 --ectx.ec_stack.ga_len;
1253 tv = STACK_TV_VAR(iptr->isn_arg.number);
1254 clear_tv(tv);
1255 *tv = *STACK_TV_BOT(0);
1256 break;
1257
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001258 // store variable or argument in outer scope
1259 case ISN_STOREOUTER:
1260 --ectx.ec_stack.ga_len;
1261 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1262 clear_tv(tv);
1263 *tv = *STACK_TV_BOT(0);
1264 break;
1265
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001266 // store s: variable in old script
1267 case ISN_STORES:
1268 {
1269 hashtab_T *ht = &SCRIPT_VARS(
1270 iptr->isn_arg.loadstore.ls_sid);
1271 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001272 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001273
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001274 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001275 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001276 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001277 else
1278 {
1279 clear_tv(&di->di_tv);
1280 di->di_tv = *STACK_TV_BOT(0);
1281 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001282 }
1283 break;
1284
1285 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001286 case ISN_STORESCRIPT:
1287 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001288 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001289 iptr->isn_arg.script.script_sid);
1290 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1291 + iptr->isn_arg.script.script_idx;
1292
1293 --ectx.ec_stack.ga_len;
1294 clear_tv(sv->sv_tv);
1295 *sv->sv_tv = *STACK_TV_BOT(0);
1296 }
1297 break;
1298
1299 // store option
1300 case ISN_STOREOPT:
1301 {
1302 long n = 0;
1303 char_u *s = NULL;
1304 char *msg;
1305
1306 --ectx.ec_stack.ga_len;
1307 tv = STACK_TV_BOT(0);
1308 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001309 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001310 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001311 if (s == NULL)
1312 s = (char_u *)"";
1313 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001314 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001315 // must be VAR_NUMBER, CHECKTYPE makes sure
1316 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1318 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001319 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 if (msg != NULL)
1321 {
1322 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001323 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001325 }
1326 break;
1327
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001328 // store $ENV
1329 case ISN_STOREENV:
1330 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001331 tv = STACK_TV_BOT(0);
1332 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1333 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334 break;
1335
1336 // store @r
1337 case ISN_STOREREG:
1338 {
1339 int reg = iptr->isn_arg.number;
1340
1341 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001342 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001343 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001344 tv_get_string(tv), -1, FALSE);
1345 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001346 }
1347 break;
1348
1349 // store v: variable
1350 case ISN_STOREV:
1351 --ectx.ec_stack.ga_len;
1352 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1353 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001354 // should not happen, type is checked when compiling
1355 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001356 break;
1357
Bram Moolenaard3aac292020-04-19 14:32:17 +02001358 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001359 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001360 case ISN_STOREB:
1361 case ISN_STOREW:
1362 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 {
1364 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001365 hashtab_T *ht;
1366 switch (iptr->isn_type)
1367 {
1368 case ISN_STOREG:
1369 ht = get_globvar_ht();
1370 break;
1371 case ISN_STOREB:
1372 ht = &curbuf->b_vars->dv_hashtab;
1373 break;
1374 case ISN_STOREW:
1375 ht = &curwin->w_vars->dv_hashtab;
1376 break;
1377 case ISN_STORET:
1378 ht = &curtab->tp_vars->dv_hashtab;
1379 break;
1380 default: // Cannot reach here
1381 goto failed;
1382 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383
1384 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001385 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001387 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 else
1389 {
1390 clear_tv(&di->di_tv);
1391 di->di_tv = *STACK_TV_BOT(0);
1392 }
1393 }
1394 break;
1395
1396 // store number in local variable
1397 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001398 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001399 clear_tv(tv);
1400 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001401 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 break;
1403
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001404 // store value in list variable
1405 case ISN_STORELIST:
1406 {
1407 typval_T *tv_idx = STACK_TV_BOT(-2);
1408 varnumber_T lidx = tv_idx->vval.v_number;
1409 typval_T *tv_list = STACK_TV_BOT(-1);
1410 list_T *list = tv_list->vval.v_list;
1411
1412 if (lidx < 0 && list->lv_len + lidx >= 0)
1413 // negative index is relative to the end
1414 lidx = list->lv_len + lidx;
1415 if (lidx < 0 || lidx > list->lv_len)
1416 {
1417 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001418 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001419 }
1420 tv = STACK_TV_BOT(-3);
1421 if (lidx < list->lv_len)
1422 {
1423 listitem_T *li = list_find(list, lidx);
1424
1425 // overwrite existing list item
1426 clear_tv(&li->li_tv);
1427 li->li_tv = *tv;
1428 }
1429 else
1430 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001431 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001432 if (list_append_tv(list, tv) == FAIL)
1433 goto failed;
1434 clear_tv(tv);
1435 }
1436 clear_tv(tv_idx);
1437 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001438 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001439 }
1440 break;
1441
1442 // store value in dict variable
1443 case ISN_STOREDICT:
1444 {
1445 typval_T *tv_key = STACK_TV_BOT(-2);
1446 char_u *key = tv_key->vval.v_string;
1447 typval_T *tv_dict = STACK_TV_BOT(-1);
1448 dict_T *dict = tv_dict->vval.v_dict;
1449 dictitem_T *di;
1450
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001451 if (dict == NULL)
1452 {
1453 emsg(_(e_dictnull));
1454 goto on_error;
1455 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001456 if (key == NULL)
1457 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001458 tv = STACK_TV_BOT(-3);
1459 di = dict_find(dict, key, -1);
1460 if (di != NULL)
1461 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001462 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001463 clear_tv(&di->di_tv);
1464 di->di_tv = *tv;
1465 }
1466 else
1467 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001468 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001469 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1470 goto failed;
1471 clear_tv(tv);
1472 }
1473 clear_tv(tv_key);
1474 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001475 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001476 }
1477 break;
1478
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001479 // push constant
1480 case ISN_PUSHNR:
1481 case ISN_PUSHBOOL:
1482 case ISN_PUSHSPEC:
1483 case ISN_PUSHF:
1484 case ISN_PUSHS:
1485 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001486 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001487 case ISN_PUSHCHANNEL:
1488 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001489 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 goto failed;
1491 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001492 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 ++ectx.ec_stack.ga_len;
1494 switch (iptr->isn_type)
1495 {
1496 case ISN_PUSHNR:
1497 tv->v_type = VAR_NUMBER;
1498 tv->vval.v_number = iptr->isn_arg.number;
1499 break;
1500 case ISN_PUSHBOOL:
1501 tv->v_type = VAR_BOOL;
1502 tv->vval.v_number = iptr->isn_arg.number;
1503 break;
1504 case ISN_PUSHSPEC:
1505 tv->v_type = VAR_SPECIAL;
1506 tv->vval.v_number = iptr->isn_arg.number;
1507 break;
1508#ifdef FEAT_FLOAT
1509 case ISN_PUSHF:
1510 tv->v_type = VAR_FLOAT;
1511 tv->vval.v_float = iptr->isn_arg.fnumber;
1512 break;
1513#endif
1514 case ISN_PUSHBLOB:
1515 blob_copy(iptr->isn_arg.blob, tv);
1516 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001517 case ISN_PUSHFUNC:
1518 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001519 if (iptr->isn_arg.string == NULL)
1520 tv->vval.v_string = NULL;
1521 else
1522 tv->vval.v_string =
1523 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001524 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001525 case ISN_PUSHCHANNEL:
1526#ifdef FEAT_JOB_CHANNEL
1527 tv->v_type = VAR_CHANNEL;
1528 tv->vval.v_channel = iptr->isn_arg.channel;
1529 if (tv->vval.v_channel != NULL)
1530 ++tv->vval.v_channel->ch_refcount;
1531#endif
1532 break;
1533 case ISN_PUSHJOB:
1534#ifdef FEAT_JOB_CHANNEL
1535 tv->v_type = VAR_JOB;
1536 tv->vval.v_job = iptr->isn_arg.job;
1537 if (tv->vval.v_job != NULL)
1538 ++tv->vval.v_job->jv_refcount;
1539#endif
1540 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 default:
1542 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001543 tv->vval.v_string = vim_strsave(
1544 iptr->isn_arg.string == NULL
1545 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 }
1547 break;
1548
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001549 case ISN_UNLET:
1550 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1551 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001552 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001553 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001554 case ISN_UNLETENV:
1555 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1556 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 // create a list from items on the stack; uses a single allocation
1559 // for the list header and the items
1560 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001561 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1562 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 break;
1564
1565 // create a dict from items on the stack
1566 case ISN_NEWDICT:
1567 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001568 int count = iptr->isn_arg.number;
1569 dict_T *dict = dict_alloc();
1570 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571
1572 if (dict == NULL)
1573 goto failed;
1574 for (idx = 0; idx < count; ++idx)
1575 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001576 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001578 // check key is unique
1579 item = dict_find(dict, tv->vval.v_string, -1);
1580 if (item != NULL)
1581 {
1582 semsg(_(e_duplicate_key), tv->vval.v_string);
1583 dict_unref(dict);
1584 goto on_error;
1585 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 item = dictitem_alloc(tv->vval.v_string);
1587 clear_tv(tv);
1588 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001589 {
1590 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001591 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1594 item->di_tv.v_lock = 0;
1595 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001596 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001597 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001598 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 }
1602
1603 if (count > 0)
1604 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001605 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 goto failed;
1607 else
1608 ++ectx.ec_stack.ga_len;
1609 tv = STACK_TV_BOT(-1);
1610 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001611 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 tv->vval.v_dict = dict;
1613 ++dict->dv_refcount;
1614 }
1615 break;
1616
1617 // call a :def function
1618 case ISN_DCALL:
1619 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1620 iptr->isn_arg.dfunc.cdf_argcount,
1621 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 break;
1624
1625 // call a builtin function
1626 case ISN_BCALL:
1627 SOURCING_LNUM = iptr->isn_lnum;
1628 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1629 iptr->isn_arg.bfunc.cbf_argcount,
1630 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001631 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 break;
1633
1634 // call a funcref or partial
1635 case ISN_PCALL:
1636 {
1637 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1638 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001639 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640
1641 SOURCING_LNUM = iptr->isn_lnum;
1642 if (pfunc->cpf_top)
1643 {
1644 // funcref is above the arguments
1645 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1646 }
1647 else
1648 {
1649 // Get the funcref from the stack.
1650 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001651 partial_tv = *STACK_TV_BOT(0);
1652 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001653 }
1654 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001655 if (tv == &partial_tv)
1656 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001658 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 }
1660 break;
1661
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001662 case ISN_PCALL_END:
1663 // PCALL finished, arguments have been consumed and replaced by
1664 // the return value. Now clear the funcref from the stack,
1665 // and move the return value in its place.
1666 --ectx.ec_stack.ga_len;
1667 clear_tv(STACK_TV_BOT(-1));
1668 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1669 break;
1670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 // call a user defined function or funcref/partial
1672 case ISN_UCALL:
1673 {
1674 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1675
1676 SOURCING_LNUM = iptr->isn_lnum;
1677 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001678 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001679 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 }
1681 break;
1682
1683 // return from a :def function call
1684 case ISN_RETURN:
1685 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001686 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001687 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001688
1689 if (trystack->ga_len > 0)
1690 trycmd = ((trycmd_T *)trystack->ga_data)
1691 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001692 if (trycmd != NULL
1693 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001694 && trycmd->tcd_finally_idx != 0)
1695 {
1696 // jump to ":finally"
1697 ectx.ec_iidx = trycmd->tcd_finally_idx;
1698 trycmd->tcd_return = TRUE;
1699 }
1700 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001701 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 }
1703 break;
1704
1705 // push a function reference to a compiled function
1706 case ISN_FUNCREF:
1707 {
1708 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001709 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001710
1711 pt = ALLOC_CLEAR_ONE(partial_T);
1712 if (pt == NULL)
1713 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001714 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001715 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001716 vim_free(pt);
1717 goto failed;
1718 }
1719 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1720 + iptr->isn_arg.funcref.fr_func;
1721 pt->pt_func = pt_dfunc->df_ufunc;
1722 pt->pt_refcount = 1;
1723 ++pt_dfunc->df_ufunc->uf_refcount;
1724
1725 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1726 {
1727 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1728 + ectx.ec_dfunc_idx;
1729
1730 // The closure needs to find arguments and local
1731 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001732 pt->pt_ectx_stack = &ectx.ec_stack;
1733 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001734
1735 // If this function returns and the closure is still
1736 // used, we need to make a copy of the context
1737 // (arguments and local variables). Store a reference
1738 // to the partial so we can handle that.
1739 ++pt->pt_refcount;
1740 tv = STACK_TV_VAR(dfunc->df_varcount
1741 + iptr->isn_arg.funcref.fr_var_idx);
1742 if (tv->v_type == VAR_PARTIAL)
1743 {
1744 // TODO: use a garray_T on ectx.
1745 emsg("Multiple closures not supported yet");
1746 goto failed;
1747 }
1748 tv->v_type = VAR_PARTIAL;
1749 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001750 }
1751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 tv = STACK_TV_BOT(0);
1753 ++ectx.ec_stack.ga_len;
1754 tv->vval.v_partial = pt;
1755 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001756 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001757 }
1758 break;
1759
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001760 // Create a global function from a lambda.
1761 case ISN_NEWFUNC:
1762 {
1763 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1764
1765 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1766 }
1767 break;
1768
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 // jump if a condition is met
1770 case ISN_JUMP:
1771 {
1772 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1773 int jump = TRUE;
1774
1775 if (when != JUMP_ALWAYS)
1776 {
1777 tv = STACK_TV_BOT(-1);
1778 jump = tv2bool(tv);
1779 if (when == JUMP_IF_FALSE
1780 || when == JUMP_AND_KEEP_IF_FALSE)
1781 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001782 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001783 {
1784 // drop the value from the stack
1785 clear_tv(tv);
1786 --ectx.ec_stack.ga_len;
1787 }
1788 }
1789 if (jump)
1790 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1791 }
1792 break;
1793
1794 // top of a for loop
1795 case ISN_FOR:
1796 {
1797 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1798 typval_T *idxtv =
1799 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1800
1801 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001802 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 goto failed;
1804 if (++idxtv->vval.v_number >= list->lv_len)
1805 // past the end of the list, jump to "endfor"
1806 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1807 else if (list->lv_first == &range_list_item)
1808 {
1809 // non-materialized range() list
1810 tv = STACK_TV_BOT(0);
1811 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001812 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 tv->vval.v_number = list_find_nr(
1814 list, idxtv->vval.v_number, NULL);
1815 ++ectx.ec_stack.ga_len;
1816 }
1817 else
1818 {
1819 listitem_T *li = list_find(list, idxtv->vval.v_number);
1820
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1822 ++ectx.ec_stack.ga_len;
1823 }
1824 }
1825 break;
1826
1827 // start of ":try" block
1828 case ISN_TRY:
1829 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001830 trycmd_T *trycmd = NULL;
1831
Bram Moolenaar270d0382020-05-15 21:42:53 +02001832 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 goto failed;
1834 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1835 + ectx.ec_trystack.ga_len;
1836 ++ectx.ec_trystack.ga_len;
1837 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001838 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1840 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001841 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001842 }
1843 break;
1844
1845 case ISN_PUSHEXC:
1846 if (current_exception == NULL)
1847 {
1848 iemsg("Evaluating catch while current_exception is NULL");
1849 goto failed;
1850 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001851 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 goto failed;
1853 tv = STACK_TV_BOT(0);
1854 ++ectx.ec_stack.ga_len;
1855 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001856 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001857 tv->vval.v_string = vim_strsave(
1858 (char_u *)current_exception->value);
1859 break;
1860
1861 case ISN_CATCH:
1862 {
1863 garray_T *trystack = &ectx.ec_trystack;
1864
1865 if (trystack->ga_len > 0)
1866 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001867 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001868 + trystack->ga_len - 1;
1869 trycmd->tcd_caught = TRUE;
1870 }
1871 did_emsg = got_int = did_throw = FALSE;
1872 catch_exception(current_exception);
1873 }
1874 break;
1875
1876 // end of ":try" block
1877 case ISN_ENDTRY:
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 = NULL;
1884
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 --trystack->ga_len;
1886 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001887 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001888 trycmd = ((trycmd_T *)trystack->ga_data)
1889 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001890 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 {
1892 // discard the exception
1893 if (caught_stack == current_exception)
1894 caught_stack = caught_stack->caught;
1895 discard_current_exception();
1896 }
1897
1898 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001899 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 }
1901 }
1902 break;
1903
1904 case ISN_THROW:
1905 --ectx.ec_stack.ga_len;
1906 tv = STACK_TV_BOT(0);
1907 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1908 {
1909 vim_free(tv->vval.v_string);
1910 goto failed;
1911 }
1912 did_throw = TRUE;
1913 break;
1914
1915 // compare with special values
1916 case ISN_COMPAREBOOL:
1917 case ISN_COMPARESPECIAL:
1918 {
1919 typval_T *tv1 = STACK_TV_BOT(-2);
1920 typval_T *tv2 = STACK_TV_BOT(-1);
1921 varnumber_T arg1 = tv1->vval.v_number;
1922 varnumber_T arg2 = tv2->vval.v_number;
1923 int res;
1924
1925 switch (iptr->isn_arg.op.op_type)
1926 {
1927 case EXPR_EQUAL: res = arg1 == arg2; break;
1928 case EXPR_NEQUAL: res = arg1 != arg2; break;
1929 default: res = 0; break;
1930 }
1931
1932 --ectx.ec_stack.ga_len;
1933 tv1->v_type = VAR_BOOL;
1934 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1935 }
1936 break;
1937
1938 // Operation with two number arguments
1939 case ISN_OPNR:
1940 case ISN_COMPARENR:
1941 {
1942 typval_T *tv1 = STACK_TV_BOT(-2);
1943 typval_T *tv2 = STACK_TV_BOT(-1);
1944 varnumber_T arg1 = tv1->vval.v_number;
1945 varnumber_T arg2 = tv2->vval.v_number;
1946 varnumber_T res;
1947
1948 switch (iptr->isn_arg.op.op_type)
1949 {
1950 case EXPR_MULT: res = arg1 * arg2; break;
1951 case EXPR_DIV: res = arg1 / arg2; break;
1952 case EXPR_REM: res = arg1 % arg2; break;
1953 case EXPR_SUB: res = arg1 - arg2; break;
1954 case EXPR_ADD: res = arg1 + arg2; break;
1955
1956 case EXPR_EQUAL: res = arg1 == arg2; break;
1957 case EXPR_NEQUAL: res = arg1 != arg2; break;
1958 case EXPR_GREATER: res = arg1 > arg2; break;
1959 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1960 case EXPR_SMALLER: res = arg1 < arg2; break;
1961 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1962 default: res = 0; break;
1963 }
1964
1965 --ectx.ec_stack.ga_len;
1966 if (iptr->isn_type == ISN_COMPARENR)
1967 {
1968 tv1->v_type = VAR_BOOL;
1969 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1970 }
1971 else
1972 tv1->vval.v_number = res;
1973 }
1974 break;
1975
1976 // Computation with two float arguments
1977 case ISN_OPFLOAT:
1978 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001979#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 {
1981 typval_T *tv1 = STACK_TV_BOT(-2);
1982 typval_T *tv2 = STACK_TV_BOT(-1);
1983 float_T arg1 = tv1->vval.v_float;
1984 float_T arg2 = tv2->vval.v_float;
1985 float_T res = 0;
1986 int cmp = FALSE;
1987
1988 switch (iptr->isn_arg.op.op_type)
1989 {
1990 case EXPR_MULT: res = arg1 * arg2; break;
1991 case EXPR_DIV: res = arg1 / arg2; break;
1992 case EXPR_SUB: res = arg1 - arg2; break;
1993 case EXPR_ADD: res = arg1 + arg2; break;
1994
1995 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1996 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1997 case EXPR_GREATER: cmp = arg1 > arg2; break;
1998 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1999 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2000 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2001 default: cmp = 0; break;
2002 }
2003 --ectx.ec_stack.ga_len;
2004 if (iptr->isn_type == ISN_COMPAREFLOAT)
2005 {
2006 tv1->v_type = VAR_BOOL;
2007 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2008 }
2009 else
2010 tv1->vval.v_float = res;
2011 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002012#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002013 break;
2014
2015 case ISN_COMPARELIST:
2016 {
2017 typval_T *tv1 = STACK_TV_BOT(-2);
2018 typval_T *tv2 = STACK_TV_BOT(-1);
2019 list_T *arg1 = tv1->vval.v_list;
2020 list_T *arg2 = tv2->vval.v_list;
2021 int cmp = FALSE;
2022 int ic = iptr->isn_arg.op.op_ic;
2023
2024 switch (iptr->isn_arg.op.op_type)
2025 {
2026 case EXPR_EQUAL: cmp =
2027 list_equal(arg1, arg2, ic, FALSE); break;
2028 case EXPR_NEQUAL: cmp =
2029 !list_equal(arg1, arg2, ic, FALSE); break;
2030 case EXPR_IS: cmp = arg1 == arg2; break;
2031 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2032 default: cmp = 0; break;
2033 }
2034 --ectx.ec_stack.ga_len;
2035 clear_tv(tv1);
2036 clear_tv(tv2);
2037 tv1->v_type = VAR_BOOL;
2038 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2039 }
2040 break;
2041
2042 case ISN_COMPAREBLOB:
2043 {
2044 typval_T *tv1 = STACK_TV_BOT(-2);
2045 typval_T *tv2 = STACK_TV_BOT(-1);
2046 blob_T *arg1 = tv1->vval.v_blob;
2047 blob_T *arg2 = tv2->vval.v_blob;
2048 int cmp = FALSE;
2049
2050 switch (iptr->isn_arg.op.op_type)
2051 {
2052 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2053 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2054 case EXPR_IS: cmp = arg1 == arg2; break;
2055 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2056 default: cmp = 0; break;
2057 }
2058 --ectx.ec_stack.ga_len;
2059 clear_tv(tv1);
2060 clear_tv(tv2);
2061 tv1->v_type = VAR_BOOL;
2062 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2063 }
2064 break;
2065
2066 // TODO: handle separately
2067 case ISN_COMPARESTRING:
2068 case ISN_COMPAREDICT:
2069 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002070 case ISN_COMPAREANY:
2071 {
2072 typval_T *tv1 = STACK_TV_BOT(-2);
2073 typval_T *tv2 = STACK_TV_BOT(-1);
2074 exptype_T exptype = iptr->isn_arg.op.op_type;
2075 int ic = iptr->isn_arg.op.op_ic;
2076
2077 typval_compare(tv1, tv2, exptype, ic);
2078 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002079 --ectx.ec_stack.ga_len;
2080 }
2081 break;
2082
2083 case ISN_ADDLIST:
2084 case ISN_ADDBLOB:
2085 {
2086 typval_T *tv1 = STACK_TV_BOT(-2);
2087 typval_T *tv2 = STACK_TV_BOT(-1);
2088
2089 if (iptr->isn_type == ISN_ADDLIST)
2090 eval_addlist(tv1, tv2);
2091 else
2092 eval_addblob(tv1, tv2);
2093 clear_tv(tv2);
2094 --ectx.ec_stack.ga_len;
2095 }
2096 break;
2097
2098 // Computation with two arguments of unknown type
2099 case ISN_OPANY:
2100 {
2101 typval_T *tv1 = STACK_TV_BOT(-2);
2102 typval_T *tv2 = STACK_TV_BOT(-1);
2103 varnumber_T n1, n2;
2104#ifdef FEAT_FLOAT
2105 float_T f1 = 0, f2 = 0;
2106#endif
2107 int error = FALSE;
2108
2109 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2110 {
2111 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2112 {
2113 eval_addlist(tv1, tv2);
2114 clear_tv(tv2);
2115 --ectx.ec_stack.ga_len;
2116 break;
2117 }
2118 else if (tv1->v_type == VAR_BLOB
2119 && tv2->v_type == VAR_BLOB)
2120 {
2121 eval_addblob(tv1, tv2);
2122 clear_tv(tv2);
2123 --ectx.ec_stack.ga_len;
2124 break;
2125 }
2126 }
2127#ifdef FEAT_FLOAT
2128 if (tv1->v_type == VAR_FLOAT)
2129 {
2130 f1 = tv1->vval.v_float;
2131 n1 = 0;
2132 }
2133 else
2134#endif
2135 {
2136 n1 = tv_get_number_chk(tv1, &error);
2137 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002138 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139#ifdef FEAT_FLOAT
2140 if (tv2->v_type == VAR_FLOAT)
2141 f1 = n1;
2142#endif
2143 }
2144#ifdef FEAT_FLOAT
2145 if (tv2->v_type == VAR_FLOAT)
2146 {
2147 f2 = tv2->vval.v_float;
2148 n2 = 0;
2149 }
2150 else
2151#endif
2152 {
2153 n2 = tv_get_number_chk(tv2, &error);
2154 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002155 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002156#ifdef FEAT_FLOAT
2157 if (tv1->v_type == VAR_FLOAT)
2158 f2 = n2;
2159#endif
2160 }
2161#ifdef FEAT_FLOAT
2162 // if there is a float on either side the result is a float
2163 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2164 {
2165 switch (iptr->isn_arg.op.op_type)
2166 {
2167 case EXPR_MULT: f1 = f1 * f2; break;
2168 case EXPR_DIV: f1 = f1 / f2; break;
2169 case EXPR_SUB: f1 = f1 - f2; break;
2170 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002171 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002172 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002173 }
2174 clear_tv(tv1);
2175 clear_tv(tv2);
2176 tv1->v_type = VAR_FLOAT;
2177 tv1->vval.v_float = f1;
2178 --ectx.ec_stack.ga_len;
2179 }
2180 else
2181#endif
2182 {
2183 switch (iptr->isn_arg.op.op_type)
2184 {
2185 case EXPR_MULT: n1 = n1 * n2; break;
2186 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2187 case EXPR_SUB: n1 = n1 - n2; break;
2188 case EXPR_ADD: n1 = n1 + n2; break;
2189 default: n1 = num_modulus(n1, n2); break;
2190 }
2191 clear_tv(tv1);
2192 clear_tv(tv2);
2193 tv1->v_type = VAR_NUMBER;
2194 tv1->vval.v_number = n1;
2195 --ectx.ec_stack.ga_len;
2196 }
2197 }
2198 break;
2199
2200 case ISN_CONCAT:
2201 {
2202 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2203 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2204 char_u *res;
2205
2206 res = concat_str(str1, str2);
2207 clear_tv(STACK_TV_BOT(-2));
2208 clear_tv(STACK_TV_BOT(-1));
2209 --ectx.ec_stack.ga_len;
2210 STACK_TV_BOT(-1)->vval.v_string = res;
2211 }
2212 break;
2213
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002214 case ISN_STRINDEX:
2215 {
2216 char_u *s;
2217 varnumber_T n;
2218 char_u *res;
2219
2220 // string index: string is at stack-2, index at stack-1
2221 tv = STACK_TV_BOT(-2);
2222 if (tv->v_type != VAR_STRING)
2223 {
2224 emsg(_(e_stringreq));
2225 goto on_error;
2226 }
2227 s = tv->vval.v_string;
2228
2229 tv = STACK_TV_BOT(-1);
2230 if (tv->v_type != VAR_NUMBER)
2231 {
2232 emsg(_(e_number_exp));
2233 goto on_error;
2234 }
2235 n = tv->vval.v_number;
2236
2237 // The resulting variable is a string of a single
2238 // character. If the index is too big or negative the
2239 // result is empty.
2240 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2241 res = NULL;
2242 else
2243 res = vim_strnsave(s + n, 1);
2244 --ectx.ec_stack.ga_len;
2245 tv = STACK_TV_BOT(-1);
2246 vim_free(tv->vval.v_string);
2247 tv->vval.v_string = res;
2248 }
2249 break;
2250
2251 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 {
2253 list_T *list;
2254 varnumber_T n;
2255 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002256 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257
2258 // list index: list is at stack-2, index at stack-1
2259 tv = STACK_TV_BOT(-2);
2260 if (tv->v_type != VAR_LIST)
2261 {
2262 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002263 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 }
2265 list = tv->vval.v_list;
2266
2267 tv = STACK_TV_BOT(-1);
2268 if (tv->v_type != VAR_NUMBER)
2269 {
2270 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002271 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002272 }
2273 n = tv->vval.v_number;
2274 clear_tv(tv);
2275 if ((li = list_find(list, n)) == NULL)
2276 {
2277 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002278 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
2280 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002281 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002282 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002283 tv = STACK_TV_BOT(-1);
2284 temp_tv = *tv;
2285 copy_tv(&li->li_tv, tv);
2286 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 }
2288 break;
2289
Bram Moolenaar9af78762020-06-16 11:34:42 +02002290 case ISN_SLICE:
2291 {
2292 list_T *list;
2293 int count = iptr->isn_arg.number;
2294
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002295 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002296 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002297 list = tv->vval.v_list;
2298
2299 // no error for short list, expect it to be checked earlier
2300 if (list != NULL && list->lv_len >= count)
2301 {
2302 list_T *newlist = list_slice(list,
2303 count, list->lv_len - 1);
2304
2305 if (newlist != NULL)
2306 {
2307 list_unref(list);
2308 tv->vval.v_list = newlist;
2309 ++newlist->lv_refcount;
2310 }
2311 }
2312 }
2313 break;
2314
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002315 case ISN_GETITEM:
2316 {
2317 listitem_T *li;
2318 int index = iptr->isn_arg.number;
2319
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002320 // Get list item: list is at stack-1, push item.
2321 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002322 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002323 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002324
2325 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2326 goto failed;
2327 ++ectx.ec_stack.ga_len;
2328 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2329 }
2330 break;
2331
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 case ISN_MEMBER:
2333 {
2334 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002335 char_u *key;
2336 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002337 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002338
2339 // dict member: dict is at stack-2, key at stack-1
2340 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002341 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002342 dict = tv->vval.v_dict;
2343
2344 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002345 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002346 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002347
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002348 if ((di = dict_find(dict, key, -1)) == NULL)
2349 {
2350 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002351 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002353 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002354 --ectx.ec_stack.ga_len;
2355 // Clear the dict after getting the item, to avoid that it
2356 // make the item invalid.
2357 tv = STACK_TV_BOT(-1);
2358 temp_tv = *tv;
2359 copy_tv(&di->di_tv, tv);
2360 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002361 }
2362 break;
2363
2364 // dict member with string key
2365 case ISN_STRINGMEMBER:
2366 {
2367 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002369 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370
2371 tv = STACK_TV_BOT(-1);
2372 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2373 {
2374 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002375 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376 }
2377 dict = tv->vval.v_dict;
2378
2379 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2380 == NULL)
2381 {
2382 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002383 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002385 // Clear the dict after getting the item, to avoid that it
2386 // make the item invalid.
2387 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002389 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 }
2391 break;
2392
2393 case ISN_NEGATENR:
2394 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002395 if (tv->v_type != VAR_NUMBER
2396#ifdef FEAT_FLOAT
2397 && tv->v_type != VAR_FLOAT
2398#endif
2399 )
2400 {
2401 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002402 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002403 }
2404#ifdef FEAT_FLOAT
2405 if (tv->v_type == VAR_FLOAT)
2406 tv->vval.v_float = -tv->vval.v_float;
2407 else
2408#endif
2409 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 break;
2411
2412 case ISN_CHECKNR:
2413 {
2414 int error = FALSE;
2415
2416 tv = STACK_TV_BOT(-1);
2417 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002418 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 (void)tv_get_number_chk(tv, &error);
2420 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002421 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 }
2423 break;
2424
2425 case ISN_CHECKTYPE:
2426 {
2427 checktype_T *ct = &iptr->isn_arg.type;
2428
2429 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002430 // TODO: better type comparison
2431 if (tv->v_type != ct->ct_type
2432 && !((tv->v_type == VAR_PARTIAL
2433 && ct->ct_type == VAR_FUNC)
2434 || (tv->v_type == VAR_FUNC
2435 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 {
2437 semsg(_("E1029: Expected %s but got %s"),
2438 vartype_name(ct->ct_type),
2439 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 }
2442 }
2443 break;
2444
Bram Moolenaar9af78762020-06-16 11:34:42 +02002445 case ISN_CHECKLEN:
2446 {
2447 int min_len = iptr->isn_arg.checklen.cl_min_len;
2448 list_T *list = NULL;
2449
2450 tv = STACK_TV_BOT(-1);
2451 if (tv->v_type == VAR_LIST)
2452 list = tv->vval.v_list;
2453 if (list == NULL || list->lv_len < min_len
2454 || (list->lv_len > min_len
2455 && !iptr->isn_arg.checklen.cl_more_OK))
2456 {
2457 semsg(_("E1093: Expected %d items but got %d"),
2458 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002459 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002460 }
2461 }
2462 break;
2463
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002464 case ISN_2BOOL:
2465 {
2466 int n;
2467
2468 tv = STACK_TV_BOT(-1);
2469 n = tv2bool(tv);
2470 if (iptr->isn_arg.number) // invert
2471 n = !n;
2472 clear_tv(tv);
2473 tv->v_type = VAR_BOOL;
2474 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2475 }
2476 break;
2477
2478 case ISN_2STRING:
2479 {
2480 char_u *str;
2481
2482 tv = STACK_TV_BOT(iptr->isn_arg.number);
2483 if (tv->v_type != VAR_STRING)
2484 {
2485 str = typval_tostring(tv);
2486 clear_tv(tv);
2487 tv->v_type = VAR_STRING;
2488 tv->vval.v_string = str;
2489 }
2490 }
2491 break;
2492
Bram Moolenaar389df252020-07-09 21:20:47 +02002493 case ISN_SHUFFLE:
2494 {
2495 typval_T tmp_tv;
2496 int item = iptr->isn_arg.shuffle.shfl_item;
2497 int up = iptr->isn_arg.shuffle.shfl_up;
2498
2499 tmp_tv = *STACK_TV_BOT(-item);
2500 for ( ; up > 0 && item > 1; --up)
2501 {
2502 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2503 --item;
2504 }
2505 *STACK_TV_BOT(-item) = tmp_tv;
2506 }
2507 break;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 case ISN_DROP:
2510 --ectx.ec_stack.ga_len;
2511 clear_tv(STACK_TV_BOT(0));
2512 break;
2513 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002514 continue;
2515
Bram Moolenaard032f342020-07-18 18:13:02 +02002516func_return:
2517 // Restore previous function. If the frame pointer is zero then there
2518 // is none and we are done.
2519 if (ectx.ec_frame_idx == initial_frame_idx)
2520 {
2521 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2522 // only fails when out of memory
2523 goto failed;
2524 goto done;
2525 }
2526 if (func_return(&ectx) == FAIL)
2527 // only fails when out of memory
2528 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002529 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002530
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002531on_error:
2532 if (trylevel == 0)
2533 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 }
2535
2536done:
2537 // function finished, get result from the stack.
2538 tv = STACK_TV_BOT(-1);
2539 *rettv = *tv;
2540 tv->v_type = VAR_UNKNOWN;
2541 ret = OK;
2542
2543failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002544 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002545 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002546 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002547failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002548 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002549
2550 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2552 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002555 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002556
2557 if (ret != OK && called_emsg == called_emsg_before)
2558 semsg(_("E1099: Unknown error while executing %s"),
2559 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 return ret;
2561}
2562
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002563/*
2564 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002565 * We don't really need this at runtime, but we do have tests that require it,
2566 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 */
2568 void
2569ex_disassemble(exarg_T *eap)
2570{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002571 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002572 char_u *fname;
2573 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 dfunc_T *dfunc;
2575 isn_T *instr;
2576 int current;
2577 int line_idx = 0;
2578 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002579 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002581 if (STRNCMP(arg, "<lambda>", 8) == 0)
2582 {
2583 arg += 8;
2584 (void)getdigits(&arg);
2585 fname = vim_strnsave(eap->arg, arg - eap->arg);
2586 }
2587 else
2588 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002589 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002590 if (fname == NULL)
2591 {
2592 semsg(_(e_invarg2), eap->arg);
2593 return;
2594 }
2595
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002596 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002597 if (ufunc == NULL)
2598 {
2599 char_u *p = untrans_function_name(fname);
2600
2601 if (p != NULL)
2602 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002603 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002604 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002605 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 if (ufunc == NULL)
2607 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002608 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 return;
2610 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002611 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002612 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2613 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002614 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002615 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002616 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002617 return;
2618 }
2619 if (ufunc->uf_name_exp != NULL)
2620 msg((char *)ufunc->uf_name_exp);
2621 else
2622 msg((char *)ufunc->uf_name);
2623
2624 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2625 instr = dfunc->df_instr;
2626 for (current = 0; current < dfunc->df_instr_count; ++current)
2627 {
2628 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002629 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630
2631 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2632 {
2633 if (current > prev_current)
2634 {
2635 msg_puts("\n\n");
2636 prev_current = current;
2637 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002638 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2639 if (line != NULL)
2640 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002641 }
2642
2643 switch (iptr->isn_type)
2644 {
2645 case ISN_EXEC:
2646 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2647 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002648 case ISN_EXECCONCAT:
2649 smsg("%4d EXECCONCAT %lld", current,
2650 (long long)iptr->isn_arg.number);
2651 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 case ISN_ECHO:
2653 {
2654 echo_T *echo = &iptr->isn_arg.echo;
2655
2656 smsg("%4d %s %d", current,
2657 echo->echo_with_white ? "ECHO" : "ECHON",
2658 echo->echo_count);
2659 }
2660 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002661 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002662 smsg("%4d EXECUTE %lld", current,
2663 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002664 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002665 case ISN_ECHOMSG:
2666 smsg("%4d ECHOMSG %lld", current,
2667 (long long)(iptr->isn_arg.number));
2668 break;
2669 case ISN_ECHOERR:
2670 smsg("%4d ECHOERR %lld", current,
2671 (long long)(iptr->isn_arg.number));
2672 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002674 case ISN_LOADOUTER:
2675 {
2676 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2677
2678 if (iptr->isn_arg.number < 0)
2679 smsg("%4d LOAD%s arg[%lld]", current, add,
2680 (long long)(iptr->isn_arg.number
2681 + STACK_FRAME_SIZE));
2682 else
2683 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002684 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002685 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002686 break;
2687 case ISN_LOADV:
2688 smsg("%4d LOADV v:%s", current,
2689 get_vim_var_name(iptr->isn_arg.number));
2690 break;
2691 case ISN_LOADSCRIPT:
2692 {
2693 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002694 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2696 + iptr->isn_arg.script.script_idx;
2697
2698 smsg("%4d LOADSCRIPT %s from %s", current,
2699 sv->sv_name, si->sn_name);
2700 }
2701 break;
2702 case ISN_LOADS:
2703 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002704 scriptitem_T *si = SCRIPT_ITEM(
2705 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706
2707 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002708 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002709 }
2710 break;
2711 case ISN_LOADG:
2712 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2713 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002714 case ISN_LOADB:
2715 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2716 break;
2717 case ISN_LOADW:
2718 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2719 break;
2720 case ISN_LOADT:
2721 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2722 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002723 case ISN_LOADGDICT:
2724 smsg("%4d LOAD g:", current);
2725 break;
2726 case ISN_LOADBDICT:
2727 smsg("%4d LOAD b:", current);
2728 break;
2729 case ISN_LOADWDICT:
2730 smsg("%4d LOAD w:", current);
2731 break;
2732 case ISN_LOADTDICT:
2733 smsg("%4d LOAD t:", current);
2734 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 case ISN_LOADOPT:
2736 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2737 break;
2738 case ISN_LOADENV:
2739 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2740 break;
2741 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002742 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002743 break;
2744
2745 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002746 case ISN_STOREOUTER:
2747 {
2748 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2749
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002750 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002751 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002752 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002753 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002754 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002755 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002756 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002758 case ISN_STOREV:
2759 smsg("%4d STOREV v:%s", current,
2760 get_vim_var_name(iptr->isn_arg.number));
2761 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002762 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002763 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2764 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002765 case ISN_STOREB:
2766 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2767 break;
2768 case ISN_STOREW:
2769 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2770 break;
2771 case ISN_STORET:
2772 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2773 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002774 case ISN_STORES:
2775 {
2776 scriptitem_T *si = SCRIPT_ITEM(
2777 iptr->isn_arg.loadstore.ls_sid);
2778
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002779 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002780 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002781 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 break;
2783 case ISN_STORESCRIPT:
2784 {
2785 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002786 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2788 + iptr->isn_arg.script.script_idx;
2789
2790 smsg("%4d STORESCRIPT %s in %s", current,
2791 sv->sv_name, si->sn_name);
2792 }
2793 break;
2794 case ISN_STOREOPT:
2795 smsg("%4d STOREOPT &%s", current,
2796 iptr->isn_arg.storeopt.so_name);
2797 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002798 case ISN_STOREENV:
2799 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2800 break;
2801 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002802 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002803 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 case ISN_STORENR:
2805 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002806 iptr->isn_arg.storenr.stnr_val,
2807 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 break;
2809
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002810 case ISN_STORELIST:
2811 smsg("%4d STORELIST", current);
2812 break;
2813
2814 case ISN_STOREDICT:
2815 smsg("%4d STOREDICT", current);
2816 break;
2817
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 // constants
2819 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002820 smsg("%4d PUSHNR %lld", current,
2821 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002822 break;
2823 case ISN_PUSHBOOL:
2824 case ISN_PUSHSPEC:
2825 smsg("%4d PUSH %s", current,
2826 get_var_special_name(iptr->isn_arg.number));
2827 break;
2828 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002829#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002830 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002831#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002832 break;
2833 case ISN_PUSHS:
2834 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2835 break;
2836 case ISN_PUSHBLOB:
2837 {
2838 char_u *r;
2839 char_u numbuf[NUMBUFLEN];
2840 char_u *tofree;
2841
2842 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002843 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002844 vim_free(tofree);
2845 }
2846 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002847 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002848 {
2849 char *name = (char *)iptr->isn_arg.string;
2850
2851 smsg("%4d PUSHFUNC \"%s\"", current,
2852 name == NULL ? "[none]" : name);
2853 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002854 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002855 case ISN_PUSHCHANNEL:
2856#ifdef FEAT_JOB_CHANNEL
2857 {
2858 channel_T *channel = iptr->isn_arg.channel;
2859
2860 smsg("%4d PUSHCHANNEL %d", current,
2861 channel == NULL ? 0 : channel->ch_id);
2862 }
2863#endif
2864 break;
2865 case ISN_PUSHJOB:
2866#ifdef FEAT_JOB_CHANNEL
2867 {
2868 typval_T tv;
2869 char_u *name;
2870
2871 tv.v_type = VAR_JOB;
2872 tv.vval.v_job = iptr->isn_arg.job;
2873 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002874 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002875 }
2876#endif
2877 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002878 case ISN_PUSHEXC:
2879 smsg("%4d PUSH v:exception", current);
2880 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002881 case ISN_UNLET:
2882 smsg("%4d UNLET%s %s", current,
2883 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2884 iptr->isn_arg.unlet.ul_name);
2885 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002886 case ISN_UNLETENV:
2887 smsg("%4d UNLETENV%s $%s", current,
2888 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2889 iptr->isn_arg.unlet.ul_name);
2890 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002892 smsg("%4d NEWLIST size %lld", current,
2893 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002894 break;
2895 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002896 smsg("%4d NEWDICT size %lld", current,
2897 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 break;
2899
2900 // function call
2901 case ISN_BCALL:
2902 {
2903 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2904
2905 smsg("%4d BCALL %s(argc %d)", current,
2906 internal_func_name(cbfunc->cbf_idx),
2907 cbfunc->cbf_argcount);
2908 }
2909 break;
2910 case ISN_DCALL:
2911 {
2912 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2913 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2914 + cdfunc->cdf_idx;
2915
2916 smsg("%4d DCALL %s(argc %d)", current,
2917 df->df_ufunc->uf_name_exp != NULL
2918 ? df->df_ufunc->uf_name_exp
2919 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2920 }
2921 break;
2922 case ISN_UCALL:
2923 {
2924 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2925
2926 smsg("%4d UCALL %s(argc %d)", current,
2927 cufunc->cuf_name, cufunc->cuf_argcount);
2928 }
2929 break;
2930 case ISN_PCALL:
2931 {
2932 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2933
2934 smsg("%4d PCALL%s (argc %d)", current,
2935 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2936 }
2937 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002938 case ISN_PCALL_END:
2939 smsg("%4d PCALL end", current);
2940 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 case ISN_RETURN:
2942 smsg("%4d RETURN", current);
2943 break;
2944 case ISN_FUNCREF:
2945 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002946 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002948 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002949
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002950 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002951 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002952 }
2953 break;
2954
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002955 case ISN_NEWFUNC:
2956 {
2957 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2958
2959 smsg("%4d NEWFUNC %s %s", current,
2960 newfunc->nf_lambda, newfunc->nf_global);
2961 }
2962 break;
2963
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 case ISN_JUMP:
2965 {
2966 char *when = "?";
2967
2968 switch (iptr->isn_arg.jump.jump_when)
2969 {
2970 case JUMP_ALWAYS:
2971 when = "JUMP";
2972 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 case JUMP_AND_KEEP_IF_TRUE:
2974 when = "JUMP_AND_KEEP_IF_TRUE";
2975 break;
2976 case JUMP_IF_FALSE:
2977 when = "JUMP_IF_FALSE";
2978 break;
2979 case JUMP_AND_KEEP_IF_FALSE:
2980 when = "JUMP_AND_KEEP_IF_FALSE";
2981 break;
2982 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002983 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 iptr->isn_arg.jump.jump_where);
2985 }
2986 break;
2987
2988 case ISN_FOR:
2989 {
2990 forloop_T *forloop = &iptr->isn_arg.forloop;
2991
2992 smsg("%4d FOR $%d -> %d", current,
2993 forloop->for_idx, forloop->for_end);
2994 }
2995 break;
2996
2997 case ISN_TRY:
2998 {
2999 try_T *try = &iptr->isn_arg.try;
3000
3001 smsg("%4d TRY catch -> %d, finally -> %d", current,
3002 try->try_catch, try->try_finally);
3003 }
3004 break;
3005 case ISN_CATCH:
3006 // TODO
3007 smsg("%4d CATCH", current);
3008 break;
3009 case ISN_ENDTRY:
3010 smsg("%4d ENDTRY", current);
3011 break;
3012 case ISN_THROW:
3013 smsg("%4d THROW", current);
3014 break;
3015
3016 // expression operations on number
3017 case ISN_OPNR:
3018 case ISN_OPFLOAT:
3019 case ISN_OPANY:
3020 {
3021 char *what;
3022 char *ins;
3023
3024 switch (iptr->isn_arg.op.op_type)
3025 {
3026 case EXPR_MULT: what = "*"; break;
3027 case EXPR_DIV: what = "/"; break;
3028 case EXPR_REM: what = "%"; break;
3029 case EXPR_SUB: what = "-"; break;
3030 case EXPR_ADD: what = "+"; break;
3031 default: what = "???"; break;
3032 }
3033 switch (iptr->isn_type)
3034 {
3035 case ISN_OPNR: ins = "OPNR"; break;
3036 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3037 case ISN_OPANY: ins = "OPANY"; break;
3038 default: ins = "???"; break;
3039 }
3040 smsg("%4d %s %s", current, ins, what);
3041 }
3042 break;
3043
3044 case ISN_COMPAREBOOL:
3045 case ISN_COMPARESPECIAL:
3046 case ISN_COMPARENR:
3047 case ISN_COMPAREFLOAT:
3048 case ISN_COMPARESTRING:
3049 case ISN_COMPAREBLOB:
3050 case ISN_COMPARELIST:
3051 case ISN_COMPAREDICT:
3052 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003053 case ISN_COMPAREANY:
3054 {
3055 char *p;
3056 char buf[10];
3057 char *type;
3058
3059 switch (iptr->isn_arg.op.op_type)
3060 {
3061 case EXPR_EQUAL: p = "=="; break;
3062 case EXPR_NEQUAL: p = "!="; break;
3063 case EXPR_GREATER: p = ">"; break;
3064 case EXPR_GEQUAL: p = ">="; break;
3065 case EXPR_SMALLER: p = "<"; break;
3066 case EXPR_SEQUAL: p = "<="; break;
3067 case EXPR_MATCH: p = "=~"; break;
3068 case EXPR_IS: p = "is"; break;
3069 case EXPR_ISNOT: p = "isnot"; break;
3070 case EXPR_NOMATCH: p = "!~"; break;
3071 default: p = "???"; break;
3072 }
3073 STRCPY(buf, p);
3074 if (iptr->isn_arg.op.op_ic == TRUE)
3075 strcat(buf, "?");
3076 switch(iptr->isn_type)
3077 {
3078 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3079 case ISN_COMPARESPECIAL:
3080 type = "COMPARESPECIAL"; break;
3081 case ISN_COMPARENR: type = "COMPARENR"; break;
3082 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3083 case ISN_COMPARESTRING:
3084 type = "COMPARESTRING"; break;
3085 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3086 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3087 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3088 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3090 default: type = "???"; break;
3091 }
3092
3093 smsg("%4d %s %s", current, type, buf);
3094 }
3095 break;
3096
3097 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3098 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3099
3100 // expression operations
3101 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003102 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3103 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003104 case ISN_SLICE: smsg("%4d SLICE %lld",
3105 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003106 case ISN_GETITEM: smsg("%4d ITEM %lld",
3107 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003108 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3109 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 iptr->isn_arg.string); break;
3111 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3112
3113 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3114 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3115 vartype_name(iptr->isn_arg.type.ct_type),
3116 iptr->isn_arg.type.ct_off);
3117 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003118 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3119 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3120 iptr->isn_arg.checklen.cl_min_len);
3121 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003122 case ISN_2BOOL: if (iptr->isn_arg.number)
3123 smsg("%4d INVERT (!val)", current);
3124 else
3125 smsg("%4d 2BOOL (!!val)", current);
3126 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003127 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3128 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003129 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130
Bram Moolenaar389df252020-07-09 21:20:47 +02003131 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3132 iptr->isn_arg.shuffle.shfl_item,
3133 iptr->isn_arg.shuffle.shfl_up);
3134 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003135 case ISN_DROP: smsg("%4d DROP", current); break;
3136 }
3137 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003138}
3139
3140/*
3141 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3142 * list, etc. Mostly like what JavaScript does, except that empty list and
3143 * empty dictionary are FALSE.
3144 */
3145 int
3146tv2bool(typval_T *tv)
3147{
3148 switch (tv->v_type)
3149 {
3150 case VAR_NUMBER:
3151 return tv->vval.v_number != 0;
3152 case VAR_FLOAT:
3153#ifdef FEAT_FLOAT
3154 return tv->vval.v_float != 0.0;
3155#else
3156 break;
3157#endif
3158 case VAR_PARTIAL:
3159 return tv->vval.v_partial != NULL;
3160 case VAR_FUNC:
3161 case VAR_STRING:
3162 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3163 case VAR_LIST:
3164 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3165 case VAR_DICT:
3166 return tv->vval.v_dict != NULL
3167 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3168 case VAR_BOOL:
3169 case VAR_SPECIAL:
3170 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3171 case VAR_JOB:
3172#ifdef FEAT_JOB_CHANNEL
3173 return tv->vval.v_job != NULL;
3174#else
3175 break;
3176#endif
3177 case VAR_CHANNEL:
3178#ifdef FEAT_JOB_CHANNEL
3179 return tv->vval.v_channel != NULL;
3180#else
3181 break;
3182#endif
3183 case VAR_BLOB:
3184 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3185 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003186 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 case VAR_VOID:
3188 break;
3189 }
3190 return FALSE;
3191}
3192
3193/*
3194 * If "tv" is a string give an error and return FAIL.
3195 */
3196 int
3197check_not_string(typval_T *tv)
3198{
3199 if (tv->v_type == VAR_STRING)
3200 {
3201 emsg(_("E1030: Using a String as a Number"));
3202 clear_tv(tv);
3203 return FAIL;
3204 }
3205 return OK;
3206}
3207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208
3209#endif // FEAT_EVAL