blob: 2197cca6c758cb0dfa9b78e7d4319f7b3919a898 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
78 semsg(_("E1105: Cannot convert %s to string"), vartype_name(vartype));
79}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
209 iemsg("Argument count wrong?");
210 return FAIL;
211 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200212 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
213 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214 return FAIL;
215
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // Move the vararg-list to below the missing optional arguments.
217 if (vararg_count > 0 && arg_to_add > 0)
218 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100219
220 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200221 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200223 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
227 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
229 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200232 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200234 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
235 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236
237 // Set execution state to the start of the called function.
238 ectx->ec_dfunc_idx = cdf_idx;
239 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200240 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
241 if (entry != NULL)
242 {
243 // Set the script context to the script where the function was defined.
244 // TODO: save more than the SID?
245 entry->es_save_sid = current_sctx.sc_sid;
246 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
247 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100248
249 // Decide where to start execution, handles optional arguments.
250 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251
252 return OK;
253}
254
255// Get pointer to item in the stack.
256#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
257
258/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200259 * Used when returning from a function: Check if any closure is still
260 * referenced. If so then move the arguments and variables to a separate piece
261 * of stack to be used when the closure is called.
262 * When "free_arguments" is TRUE the arguments are to be freed.
263 * Returns FAIL when out of memory.
264 */
265 static int
266handle_closure_in_use(ectx_T *ectx, int free_arguments)
267{
268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
269 + ectx->ec_dfunc_idx;
270 int argcount = ufunc_argcount(dfunc->df_ufunc);
271 int top = ectx->ec_frame_idx - argcount;
272 int idx;
273 typval_T *tv;
274 int closure_in_use = FALSE;
275
276 // Check if any created closure is still in use.
277 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
278 {
279 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
280 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200281 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
282 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200283 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 int refcount = tv->vval.v_partial->pt_refcount;
285 int i;
286
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200287 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200288 // unreferenced on return.
289 for (i = 0; i < dfunc->df_varcount; ++i)
290 {
291 typval_T *stv = STACK_TV(ectx->ec_frame_idx
292 + STACK_FRAME_SIZE + i);
293 if (stv->v_type == VAR_PARTIAL
294 && tv->vval.v_partial == stv->vval.v_partial)
295 --refcount;
296 }
297 if (refcount > 1)
298 {
299 closure_in_use = TRUE;
300 break;
301 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200302 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303 }
304
305 if (closure_in_use)
306 {
307 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
308 typval_T *stack;
309
310 // A closure is using the arguments and/or local variables.
311 // Move them to the called function.
312 if (funcstack == NULL)
313 return FAIL;
314 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
315 + dfunc->df_varcount;
316 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
317 funcstack->fs_ga.ga_data = stack;
318 if (stack == NULL)
319 {
320 vim_free(funcstack);
321 return FAIL;
322 }
323
324 // Move or copy the arguments.
325 for (idx = 0; idx < argcount; ++idx)
326 {
327 tv = STACK_TV(top + idx);
328 if (free_arguments)
329 {
330 *(stack + idx) = *tv;
331 tv->v_type = VAR_UNKNOWN;
332 }
333 else
334 copy_tv(tv, stack + idx);
335 }
336 // Move the local variables.
337 for (idx = 0; idx < dfunc->df_varcount; ++idx)
338 {
339 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200340
341 // Do not copy a partial created for a local function.
342 // TODO: this won't work if the closure actually uses it. But when
343 // keeping it it gets complicated: it will create a reference cycle
344 // inside the partial, thus needs special handling for garbage
345 // collection.
346 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
347 {
348 int i;
349 typval_T *ctv;
350
351 for (i = 0; i < dfunc->df_closure_count; ++i)
352 {
353 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
354 + dfunc->df_varcount + i);
355 if (tv->vval.v_partial == ctv->vval.v_partial)
356 break;
357 }
358 if (i < dfunc->df_closure_count)
359 {
360 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
361 VAR_UNKNOWN;
362 continue;
363 }
364 }
365
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
367 tv->v_type = VAR_UNKNOWN;
368 }
369
370 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
371 {
372 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
373 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200376 partial_T *partial = tv->vval.v_partial;
377
378 if (partial->pt_refcount > 1)
379 {
380 ++funcstack->fs_refcount;
381 partial->pt_funcstack = funcstack;
382 partial->pt_ectx_stack = &funcstack->fs_ga;
383 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
384 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 }
386 }
387 }
388
389 return OK;
390}
391
392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 * Return from the current function.
394 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200395 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396func_return(ectx_T *ectx)
397{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
400 + ectx->ec_dfunc_idx;
401 int argcount = ufunc_argcount(dfunc->df_ufunc);
402 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200403 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 entry = estack_pop();
407 if (entry != NULL)
408 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200410 if (handle_closure_in_use(ectx, TRUE) == FAIL)
411 return FAIL;
412
413 // Clear the arguments.
414 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
415 clear_tv(STACK_TV(idx));
416
417 // Clear local variables and temp values, but not the return value.
418 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100419 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100421
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
424 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
425 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
427 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100428
429 // Reset the stack to the position before the call, move the return value
430 // to the top of the stack.
431 idx = ectx->ec_stack.ga_len - 1;
432 ectx->ec_stack.ga_len = top + 1;
433 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434
435 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436}
437
438#undef STACK_TV
439
440/*
441 * Prepare arguments and rettv for calling a builtin or user function.
442 */
443 static int
444call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
445{
446 int idx;
447 typval_T *tv;
448
449 // Move arguments from bottom of the stack to argvars[] and add terminator.
450 for (idx = 0; idx < argcount; ++idx)
451 argvars[idx] = *STACK_TV_BOT(idx - argcount);
452 argvars[argcount].v_type = VAR_UNKNOWN;
453
454 // Result replaces the arguments on the stack.
455 if (argcount > 0)
456 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200457 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 return FAIL;
459 else
460 ++ectx->ec_stack.ga_len;
461
462 // Default return value is zero.
463 tv = STACK_TV_BOT(-1);
464 tv->v_type = VAR_NUMBER;
465 tv->vval.v_number = 0;
466
467 return OK;
468}
469
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200470// Ugly global to avoid passing the execution context around through many
471// layers.
472static ectx_T *current_ectx = NULL;
473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474/*
475 * Call a builtin function by index.
476 */
477 static int
478call_bfunc(int func_idx, int argcount, ectx_T *ectx)
479{
480 typval_T argvars[MAX_FUNC_ARGS];
481 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200482 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200483 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484
485 if (call_prepare(argcount, argvars, ectx) == FAIL)
486 return FAIL;
487
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200488 // Call the builtin function. Set "current_ectx" so that when it
489 // recursively invokes call_def_function() a closure context can be set.
490 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200492 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493
494 // Clear the arguments.
495 for (idx = 0; idx < argcount; ++idx)
496 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200497
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200498 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200499 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 return OK;
501}
502
503/*
504 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100505 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 */
507 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509{
510 typval_T argvars[MAX_FUNC_ARGS];
511 funcexe_T funcexe;
512 int error;
513 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200514 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200516 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200517 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
518 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100520 {
521 // The function has been compiled, can call it quickly. For a function
522 // that was defined later: we can call it directly next time.
523 if (iptr != NULL)
524 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100525 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100526 iptr->isn_type = ISN_DCALL;
527 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
528 iptr->isn_arg.dfunc.cdf_argcount = argcount;
529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 if (call_prepare(argcount, argvars, ectx) == FAIL)
534 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200535 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 funcexe.evaluate = TRUE;
537
538 // Call the user function. Result goes in last position on the stack.
539 // TODO: add selfdict if there is one
540 error = call_user_func_check(ufunc, argcount, argvars,
541 STACK_TV_BOT(-1), &funcexe, NULL);
542
543 // Clear the arguments.
544 for (idx = 0; idx < argcount; ++idx)
545 clear_tv(&argvars[idx]);
546
547 if (error != FCERR_NONE)
548 {
549 user_func_error(error, ufunc->uf_name);
550 return FAIL;
551 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200552 if (called_emsg > called_emsg_before)
553 // Error other than from calling the function itself.
554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 return OK;
556}
557
558/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200559 * Return TRUE if an error was given or CTRL-C was pressed.
560 */
561 static int
562vim9_aborting(int prev_called_emsg)
563{
564 return called_emsg > prev_called_emsg || got_int || did_throw;
565}
566
567/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 * Execute a function by "name".
569 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100570 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Returns FAIL if not found without an error message.
572 */
573 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100574call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575{
576 ufunc_T *ufunc;
577
578 if (builtin_function(name, -1))
579 {
580 int func_idx = find_internal_func(name);
581
582 if (func_idx < 0)
583 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200584 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 return FAIL;
586 return call_bfunc(func_idx, argcount, ectx);
587 }
588
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200589 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200590
591 if (ufunc == NULL)
592 {
593 int called_emsg_before = called_emsg;
594
595 if (script_autoload(name, TRUE))
596 // loaded a package, search for the function again
597 ufunc = find_func(name, FALSE, NULL);
598 if (vim9_aborting(called_emsg_before))
599 return FAIL; // bail out if loading the script caused an error
600 }
601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100603 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 return FAIL;
606}
607
608 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200609call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200611 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200612 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 int called_emsg_before = called_emsg;
614
615 if (tv->v_type == VAR_PARTIAL)
616 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200617 partial_T *pt = tv->vval.v_partial;
618 int i;
619
620 if (pt->pt_argc > 0)
621 {
622 // Make space for arguments from the partial, shift the "argcount"
623 // arguments up.
624 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
625 return FAIL;
626 for (i = 1; i <= argcount; ++i)
627 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
628 ectx->ec_stack.ga_len += pt->pt_argc;
629 argcount += pt->pt_argc;
630
631 // copy the arguments from the partial onto the stack
632 for (i = 0; i < pt->pt_argc; ++i)
633 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635
636 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200637 {
638 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
639
640 // closure may need the function context where it was defined
641 ectx->ec_outer_stack = pt->pt_ectx_stack;
642 ectx->ec_outer_frame = pt->pt_ectx_frame;
643
644 return ret;
645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 name = pt->pt_name;
647 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200648 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200650 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 {
652 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200653 semsg(_(e_unknownfunc),
654 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 return FAIL;
656 }
657 return OK;
658}
659
660/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100661 * Store "tv" in variable "name".
662 * This is for s: and g: variables.
663 */
664 static void
665store_var(char_u *name, typval_T *tv)
666{
667 funccal_entry_T entry;
668
669 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200670 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100671 restore_funccal();
672}
673
Bram Moolenaard3aac292020-04-19 14:32:17 +0200674
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100675/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 * Execute a function by "name".
677 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100678 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 */
680 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682{
Bram Moolenaared677f52020-08-12 16:38:10 +0200683 int called_emsg_before = called_emsg;
684 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685
Bram Moolenaared677f52020-08-12 16:38:10 +0200686 res = call_by_name(name, argcount, ectx, iptr);
687 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200689 dictitem_T *v;
690
691 v = find_var(name, NULL, FALSE);
692 if (v == NULL)
693 {
694 semsg(_(e_unknownfunc), name);
695 return FAIL;
696 }
697 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
698 {
699 semsg(_(e_unknownfunc), name);
700 return FAIL;
701 }
702 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200704 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705}
706
707/*
708 * Call a "def" function from old Vim script.
709 * Return OK or FAIL.
710 */
711 int
712call_def_function(
713 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200714 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200716 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 typval_T *rettv) // return value
718{
719 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200720 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200721 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 typval_T *tv;
723 int idx;
724 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100725 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200726 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200727 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200728 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730// Get pointer to item in the stack.
731#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
732
733// Get pointer to item at the bottom of the stack, -1 is the bottom.
734#undef STACK_TV_BOT
735#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
736
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200737// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200740// Like STACK_TV_VAR but use the outer scope
741#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
742
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200743 if (ufunc->uf_def_status == UF_NOT_COMPILED
744 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200745 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200746 {
747 if (called_emsg == called_emsg_before)
748 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200749 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200750 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200751 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200752
Bram Moolenaar09689a02020-05-09 22:50:08 +0200753 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200754 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200755 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
756 + ufunc->uf_dfunc_idx;
757 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200758 {
759 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200760 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200761 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200764 CLEAR_FIELD(ectx);
765 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
766 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
767 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
770
771 // Put arguments on the stack.
772 for (idx = 0; idx < argc; ++idx)
773 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200774 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200775 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
776 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200777 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 copy_tv(&argv[idx], STACK_TV_BOT(0));
779 ++ectx.ec_stack.ga_len;
780 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200781
782 // Turn varargs into a list. Empty list if no args.
783 if (ufunc->uf_va_name != NULL)
784 {
785 int vararg_count = argc - ufunc->uf_args.ga_len;
786
787 if (vararg_count < 0)
788 vararg_count = 0;
789 else
790 argc -= vararg_count;
791 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200792 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200793
794 // Check the type of the list items.
795 tv = STACK_TV_BOT(-1);
796 if (ufunc->uf_va_type != NULL
797 && ufunc->uf_va_type->tt_member != &t_any
798 && tv->vval.v_list != NULL)
799 {
800 type_T *expected = ufunc->uf_va_type->tt_member;
801 listitem_T *li = tv->vval.v_list->lv_first;
802
803 for (idx = 0; idx < vararg_count; ++idx)
804 {
805 if (check_typval_type(expected, &li->li_tv) == FAIL)
806 goto failed_early;
807 li = li->li_next;
808 }
809 }
810
Bram Moolenaar23e03252020-04-12 22:22:31 +0200811 if (defcount > 0)
812 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200813 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 --ectx.ec_stack.ga_len;
815 }
816
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100817 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200818 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100819 if (defcount > 0)
820 for (idx = 0; idx < defcount; ++idx)
821 {
822 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
823 ++ectx.ec_stack.ga_len;
824 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200825 if (ufunc->uf_va_name != NULL)
826 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
830 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200832 if (partial != NULL)
833 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200834 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
835 {
836 // TODO: is this always the right way?
837 ectx.ec_outer_stack = &current_ectx->ec_stack;
838 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
839 }
840 else
841 {
842 ectx.ec_outer_stack = partial->pt_ectx_stack;
843 ectx.ec_outer_frame = partial->pt_ectx_frame;
844 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200845 }
846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 // dummy frame entries
848 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
849 {
850 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
851 ++ectx.ec_stack.ga_len;
852 }
853
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200854 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200855 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200856 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
857 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200860 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200861 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200862 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200863
864 ectx.ec_instr = dfunc->df_instr;
865 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100866
Bram Moolenaara26b9702020-04-18 19:53:28 +0200867 // Commands behave like vim9script.
868 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
869
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100870 // Decide where to start execution, handles optional arguments.
871 init_instr_idx(ufunc, argc, &ectx);
872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 for (;;)
874 {
875 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100876
Bram Moolenaar270d0382020-05-15 21:42:53 +0200877 if (++breakcheck_count >= 100)
878 {
879 line_breakcheck();
880 breakcheck_count = 0;
881 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100882 if (got_int)
883 {
884 // Turn CTRL-C into an exception.
885 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100886 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100887 goto failed;
888 did_throw = TRUE;
889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaara26b9702020-04-18 19:53:28 +0200891 if (did_emsg && msg_list != NULL && *msg_list != NULL)
892 {
893 // Turn an error message into an exception.
894 did_emsg = FALSE;
895 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
896 goto failed;
897 did_throw = TRUE;
898 *msg_list = NULL;
899 }
900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 if (did_throw && !ectx.ec_in_catch)
902 {
903 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100904 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905
906 // An exception jumps to the first catch, finally, or returns from
907 // the current function.
908 if (trystack->ga_len > 0)
909 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200910 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 {
912 // jump to ":catch" or ":finally"
913 ectx.ec_in_catch = TRUE;
914 ectx.ec_iidx = trycmd->tcd_catch_idx;
915 }
916 else
917 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200918 // Not inside try or need to return from current functions.
919 // Push a dummy return value.
920 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
921 goto failed;
922 tv = STACK_TV_BOT(0);
923 tv->v_type = VAR_NUMBER;
924 tv->vval.v_number = 0;
925 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200926 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100927 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +0200928 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100929 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200930 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
931 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932 goto done;
933 }
934
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200935 if (func_return(&ectx) == FAIL)
936 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100937 }
938 continue;
939 }
940
941 iptr = &ectx.ec_instr[ectx.ec_iidx++];
942 switch (iptr->isn_type)
943 {
944 // execute Ex command line
945 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200946 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 do_cmdline_cmd(iptr->isn_arg.string);
948 break;
949
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200950 // execute Ex command from pieces on the stack
951 case ISN_EXECCONCAT:
952 {
953 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200954 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200955 int pass;
956 int i;
957 char_u *cmd = NULL;
958 char_u *str;
959
960 for (pass = 1; pass <= 2; ++pass)
961 {
962 for (i = 0; i < count; ++i)
963 {
964 tv = STACK_TV_BOT(i - count);
965 str = tv->vval.v_string;
966 if (str != NULL && *str != NUL)
967 {
968 if (pass == 2)
969 STRCPY(cmd + len, str);
970 len += STRLEN(str);
971 }
972 if (pass == 2)
973 clear_tv(tv);
974 }
975 if (pass == 1)
976 {
977 cmd = alloc(len + 1);
978 if (cmd == NULL)
979 goto failed;
980 len = 0;
981 }
982 }
983
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200984 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200985 do_cmdline_cmd(cmd);
986 vim_free(cmd);
987 }
988 break;
989
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 // execute :echo {string} ...
991 case ISN_ECHO:
992 {
993 int count = iptr->isn_arg.echo.echo_count;
994 int atstart = TRUE;
995 int needclr = TRUE;
996
997 for (idx = 0; idx < count; ++idx)
998 {
999 tv = STACK_TV_BOT(idx - count);
1000 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1001 &atstart, &needclr);
1002 clear_tv(tv);
1003 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001004 if (needclr)
1005 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006 ectx.ec_stack.ga_len -= count;
1007 }
1008 break;
1009
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001010 // :execute {string} ...
1011 // :echomsg {string} ...
1012 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001013 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001014 case ISN_ECHOMSG:
1015 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001016 {
1017 int count = iptr->isn_arg.number;
1018 garray_T ga;
1019 char_u buf[NUMBUFLEN];
1020 char_u *p;
1021 int len;
1022 int failed = FALSE;
1023
1024 ga_init2(&ga, 1, 80);
1025 for (idx = 0; idx < count; ++idx)
1026 {
1027 tv = STACK_TV_BOT(idx - count);
1028 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1029 {
1030 emsg(_(e_inval_string));
1031 break;
1032 }
1033 else
1034 p = tv_get_string_buf(tv, buf);
1035
1036 len = (int)STRLEN(p);
1037 if (ga_grow(&ga, len + 2) == FAIL)
1038 failed = TRUE;
1039 else
1040 {
1041 if (ga.ga_len > 0)
1042 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1043 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1044 ga.ga_len += len;
1045 }
1046 clear_tv(tv);
1047 }
1048 ectx.ec_stack.ga_len -= count;
1049
1050 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001051 {
1052 if (iptr->isn_type == ISN_EXECUTE)
1053 do_cmdline_cmd((char_u *)ga.ga_data);
1054 else
1055 {
1056 msg_sb_eol();
1057 if (iptr->isn_type == ISN_ECHOMSG)
1058 {
1059 msg_attr(ga.ga_data, echo_attr);
1060 out_flush();
1061 }
1062 else
1063 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001064 SOURCING_LNUM = iptr->isn_lnum;
1065 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001066 }
1067 }
1068 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001069 ga_clear(&ga);
1070 }
1071 break;
1072
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001073 // load local variable or argument
1074 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001075 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076 goto failed;
1077 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1078 ++ectx.ec_stack.ga_len;
1079 break;
1080
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001081 // load variable or argument from outer scope
1082 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001083 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001084 goto failed;
1085 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1086 STACK_TV_BOT(0));
1087 ++ectx.ec_stack.ga_len;
1088 break;
1089
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001090 // load v: variable
1091 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001092 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 goto failed;
1094 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1095 ++ectx.ec_stack.ga_len;
1096 break;
1097
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001098 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099 case ISN_LOADSCRIPT:
1100 {
1101 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001102 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001103 svar_T *sv;
1104
1105 sv = ((svar_T *)si->sn_var_vals.ga_data)
1106 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001107 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 goto failed;
1109 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1110 ++ectx.ec_stack.ga_len;
1111 }
1112 break;
1113
1114 // load s: variable in old script
1115 case ISN_LOADS:
1116 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001117 hashtab_T *ht = &SCRIPT_VARS(
1118 iptr->isn_arg.loadstore.ls_sid);
1119 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001121
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122 if (di == NULL)
1123 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001124 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001125 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 }
1127 else
1128 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001129 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 goto failed;
1131 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1132 ++ectx.ec_stack.ga_len;
1133 }
1134 }
1135 break;
1136
Bram Moolenaard3aac292020-04-19 14:32:17 +02001137 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001138 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001139 case ISN_LOADB:
1140 case ISN_LOADW:
1141 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001143 dictitem_T *di = NULL;
1144 hashtab_T *ht = NULL;
1145 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001146
Bram Moolenaard3aac292020-04-19 14:32:17 +02001147 switch (iptr->isn_type)
1148 {
1149 case ISN_LOADG:
1150 ht = get_globvar_ht();
1151 namespace = 'g';
1152 break;
1153 case ISN_LOADB:
1154 ht = &curbuf->b_vars->dv_hashtab;
1155 namespace = 'b';
1156 break;
1157 case ISN_LOADW:
1158 ht = &curwin->w_vars->dv_hashtab;
1159 namespace = 'w';
1160 break;
1161 case ISN_LOADT:
1162 ht = &curtab->tp_vars->dv_hashtab;
1163 namespace = 't';
1164 break;
1165 default: // Cannot reach here
1166 goto failed;
1167 }
1168 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001169
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001170 if (di == NULL)
1171 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001172 semsg(_("E121: Undefined variable: %c:%s"),
1173 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001174 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001175 }
1176 else
1177 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001178 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179 goto failed;
1180 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1181 ++ectx.ec_stack.ga_len;
1182 }
1183 }
1184 break;
1185
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001186 // load g:/b:/w:/t: namespace
1187 case ISN_LOADGDICT:
1188 case ISN_LOADBDICT:
1189 case ISN_LOADWDICT:
1190 case ISN_LOADTDICT:
1191 {
1192 dict_T *d = NULL;
1193
1194 switch (iptr->isn_type)
1195 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001196 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1197 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1198 case ISN_LOADWDICT: d = curwin->w_vars; break;
1199 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001200 default: // Cannot reach here
1201 goto failed;
1202 }
1203 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1204 goto failed;
1205 tv = STACK_TV_BOT(0);
1206 tv->v_type = VAR_DICT;
1207 tv->v_lock = 0;
1208 tv->vval.v_dict = d;
1209 ++ectx.ec_stack.ga_len;
1210 }
1211 break;
1212
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 // load &option
1214 case ISN_LOADOPT:
1215 {
1216 typval_T optval;
1217 char_u *name = iptr->isn_arg.string;
1218
Bram Moolenaara8c17702020-04-01 21:17:24 +02001219 // This is not expected to fail, name is checked during
1220 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001221 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001222 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001223 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001224 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 *STACK_TV_BOT(0) = optval;
1226 ++ectx.ec_stack.ga_len;
1227 }
1228 break;
1229
1230 // load $ENV
1231 case ISN_LOADENV:
1232 {
1233 typval_T optval;
1234 char_u *name = iptr->isn_arg.string;
1235
Bram Moolenaar270d0382020-05-15 21:42:53 +02001236 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001237 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001238 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001239 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 *STACK_TV_BOT(0) = optval;
1241 ++ectx.ec_stack.ga_len;
1242 }
1243 break;
1244
1245 // load @register
1246 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001247 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001248 goto failed;
1249 tv = STACK_TV_BOT(0);
1250 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001251 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001252 tv->vval.v_string = get_reg_contents(
1253 iptr->isn_arg.number, GREG_EXPR_SRC);
1254 ++ectx.ec_stack.ga_len;
1255 break;
1256
1257 // store local variable
1258 case ISN_STORE:
1259 --ectx.ec_stack.ga_len;
1260 tv = STACK_TV_VAR(iptr->isn_arg.number);
1261 clear_tv(tv);
1262 *tv = *STACK_TV_BOT(0);
1263 break;
1264
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001265 // store variable or argument in outer scope
1266 case ISN_STOREOUTER:
1267 --ectx.ec_stack.ga_len;
1268 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1269 clear_tv(tv);
1270 *tv = *STACK_TV_BOT(0);
1271 break;
1272
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001273 // store s: variable in old script
1274 case ISN_STORES:
1275 {
1276 hashtab_T *ht = &SCRIPT_VARS(
1277 iptr->isn_arg.loadstore.ls_sid);
1278 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001279 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001281 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001282 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001283 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001284 else
1285 {
1286 clear_tv(&di->di_tv);
1287 di->di_tv = *STACK_TV_BOT(0);
1288 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001289 }
1290 break;
1291
1292 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 case ISN_STORESCRIPT:
1294 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001295 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001296 iptr->isn_arg.script.script_sid);
1297 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1298 + iptr->isn_arg.script.script_idx;
1299
1300 --ectx.ec_stack.ga_len;
1301 clear_tv(sv->sv_tv);
1302 *sv->sv_tv = *STACK_TV_BOT(0);
1303 }
1304 break;
1305
1306 // store option
1307 case ISN_STOREOPT:
1308 {
1309 long n = 0;
1310 char_u *s = NULL;
1311 char *msg;
1312
1313 --ectx.ec_stack.ga_len;
1314 tv = STACK_TV_BOT(0);
1315 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001316 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001318 if (s == NULL)
1319 s = (char_u *)"";
1320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001321 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001322 // must be VAR_NUMBER, CHECKTYPE makes sure
1323 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001324 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1325 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001326 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001327 if (msg != NULL)
1328 {
1329 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001330 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001332 }
1333 break;
1334
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001335 // store $ENV
1336 case ISN_STOREENV:
1337 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001338 tv = STACK_TV_BOT(0);
1339 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1340 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341 break;
1342
1343 // store @r
1344 case ISN_STOREREG:
1345 {
1346 int reg = iptr->isn_arg.number;
1347
1348 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001349 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001350 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001351 tv_get_string(tv), -1, FALSE);
1352 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001353 }
1354 break;
1355
1356 // store v: variable
1357 case ISN_STOREV:
1358 --ectx.ec_stack.ga_len;
1359 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1360 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001361 // should not happen, type is checked when compiling
1362 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001363 break;
1364
Bram Moolenaard3aac292020-04-19 14:32:17 +02001365 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001367 case ISN_STOREB:
1368 case ISN_STOREW:
1369 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001370 {
1371 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001372 hashtab_T *ht;
1373 switch (iptr->isn_type)
1374 {
1375 case ISN_STOREG:
1376 ht = get_globvar_ht();
1377 break;
1378 case ISN_STOREB:
1379 ht = &curbuf->b_vars->dv_hashtab;
1380 break;
1381 case ISN_STOREW:
1382 ht = &curwin->w_vars->dv_hashtab;
1383 break;
1384 case ISN_STORET:
1385 ht = &curtab->tp_vars->dv_hashtab;
1386 break;
1387 default: // Cannot reach here
1388 goto failed;
1389 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001390
1391 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001392 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001394 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 else
1396 {
1397 clear_tv(&di->di_tv);
1398 di->di_tv = *STACK_TV_BOT(0);
1399 }
1400 }
1401 break;
1402
1403 // store number in local variable
1404 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001405 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 clear_tv(tv);
1407 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001408 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001409 break;
1410
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001411 // store value in list variable
1412 case ISN_STORELIST:
1413 {
1414 typval_T *tv_idx = STACK_TV_BOT(-2);
1415 varnumber_T lidx = tv_idx->vval.v_number;
1416 typval_T *tv_list = STACK_TV_BOT(-1);
1417 list_T *list = tv_list->vval.v_list;
1418
1419 if (lidx < 0 && list->lv_len + lidx >= 0)
1420 // negative index is relative to the end
1421 lidx = list->lv_len + lidx;
1422 if (lidx < 0 || lidx > list->lv_len)
1423 {
1424 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001425 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001426 }
1427 tv = STACK_TV_BOT(-3);
1428 if (lidx < list->lv_len)
1429 {
1430 listitem_T *li = list_find(list, lidx);
1431
1432 // overwrite existing list item
1433 clear_tv(&li->li_tv);
1434 li->li_tv = *tv;
1435 }
1436 else
1437 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001438 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001439 if (list_append_tv(list, tv) == FAIL)
1440 goto failed;
1441 clear_tv(tv);
1442 }
1443 clear_tv(tv_idx);
1444 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001445 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001446 }
1447 break;
1448
1449 // store value in dict variable
1450 case ISN_STOREDICT:
1451 {
1452 typval_T *tv_key = STACK_TV_BOT(-2);
1453 char_u *key = tv_key->vval.v_string;
1454 typval_T *tv_dict = STACK_TV_BOT(-1);
1455 dict_T *dict = tv_dict->vval.v_dict;
1456 dictitem_T *di;
1457
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001458 if (dict == NULL)
1459 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001460 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001461 goto on_error;
1462 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001463 if (key == NULL)
1464 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001465 tv = STACK_TV_BOT(-3);
1466 di = dict_find(dict, key, -1);
1467 if (di != NULL)
1468 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001469 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001470 clear_tv(&di->di_tv);
1471 di->di_tv = *tv;
1472 }
1473 else
1474 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001475 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001476 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1477 goto failed;
1478 clear_tv(tv);
1479 }
1480 clear_tv(tv_key);
1481 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001482 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001483 }
1484 break;
1485
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001486 // push constant
1487 case ISN_PUSHNR:
1488 case ISN_PUSHBOOL:
1489 case ISN_PUSHSPEC:
1490 case ISN_PUSHF:
1491 case ISN_PUSHS:
1492 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001493 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001494 case ISN_PUSHCHANNEL:
1495 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001496 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001497 goto failed;
1498 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001499 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001500 ++ectx.ec_stack.ga_len;
1501 switch (iptr->isn_type)
1502 {
1503 case ISN_PUSHNR:
1504 tv->v_type = VAR_NUMBER;
1505 tv->vval.v_number = iptr->isn_arg.number;
1506 break;
1507 case ISN_PUSHBOOL:
1508 tv->v_type = VAR_BOOL;
1509 tv->vval.v_number = iptr->isn_arg.number;
1510 break;
1511 case ISN_PUSHSPEC:
1512 tv->v_type = VAR_SPECIAL;
1513 tv->vval.v_number = iptr->isn_arg.number;
1514 break;
1515#ifdef FEAT_FLOAT
1516 case ISN_PUSHF:
1517 tv->v_type = VAR_FLOAT;
1518 tv->vval.v_float = iptr->isn_arg.fnumber;
1519 break;
1520#endif
1521 case ISN_PUSHBLOB:
1522 blob_copy(iptr->isn_arg.blob, tv);
1523 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001524 case ISN_PUSHFUNC:
1525 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001526 if (iptr->isn_arg.string == NULL)
1527 tv->vval.v_string = NULL;
1528 else
1529 tv->vval.v_string =
1530 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001531 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001532 case ISN_PUSHCHANNEL:
1533#ifdef FEAT_JOB_CHANNEL
1534 tv->v_type = VAR_CHANNEL;
1535 tv->vval.v_channel = iptr->isn_arg.channel;
1536 if (tv->vval.v_channel != NULL)
1537 ++tv->vval.v_channel->ch_refcount;
1538#endif
1539 break;
1540 case ISN_PUSHJOB:
1541#ifdef FEAT_JOB_CHANNEL
1542 tv->v_type = VAR_JOB;
1543 tv->vval.v_job = iptr->isn_arg.job;
1544 if (tv->vval.v_job != NULL)
1545 ++tv->vval.v_job->jv_refcount;
1546#endif
1547 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001548 default:
1549 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001550 tv->vval.v_string = vim_strsave(
1551 iptr->isn_arg.string == NULL
1552 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 }
1554 break;
1555
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001556 case ISN_UNLET:
1557 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1558 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001559 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001560 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001561 case ISN_UNLETENV:
1562 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1563 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001564
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001565 // create a list from items on the stack; uses a single allocation
1566 // for the list header and the items
1567 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001568 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1569 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001570 break;
1571
1572 // create a dict from items on the stack
1573 case ISN_NEWDICT:
1574 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001575 int count = iptr->isn_arg.number;
1576 dict_T *dict = dict_alloc();
1577 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001578
1579 if (dict == NULL)
1580 goto failed;
1581 for (idx = 0; idx < count; ++idx)
1582 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001583 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001584 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001585 // check key is unique
1586 item = dict_find(dict, tv->vval.v_string, -1);
1587 if (item != NULL)
1588 {
1589 semsg(_(e_duplicate_key), tv->vval.v_string);
1590 dict_unref(dict);
1591 goto on_error;
1592 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 item = dictitem_alloc(tv->vval.v_string);
1594 clear_tv(tv);
1595 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001596 {
1597 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001598 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001599 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1601 item->di_tv.v_lock = 0;
1602 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001603 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001604 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001605 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001607 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 }
1609
1610 if (count > 0)
1611 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001612 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 goto failed;
1614 else
1615 ++ectx.ec_stack.ga_len;
1616 tv = STACK_TV_BOT(-1);
1617 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001618 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001619 tv->vval.v_dict = dict;
1620 ++dict->dv_refcount;
1621 }
1622 break;
1623
1624 // call a :def function
1625 case ISN_DCALL:
1626 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1627 iptr->isn_arg.dfunc.cdf_argcount,
1628 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001629 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 break;
1631
1632 // call a builtin function
1633 case ISN_BCALL:
1634 SOURCING_LNUM = iptr->isn_lnum;
1635 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1636 iptr->isn_arg.bfunc.cbf_argcount,
1637 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001638 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 break;
1640
1641 // call a funcref or partial
1642 case ISN_PCALL:
1643 {
1644 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1645 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001646 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647
1648 SOURCING_LNUM = iptr->isn_lnum;
1649 if (pfunc->cpf_top)
1650 {
1651 // funcref is above the arguments
1652 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1653 }
1654 else
1655 {
1656 // Get the funcref from the stack.
1657 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001658 partial_tv = *STACK_TV_BOT(0);
1659 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 }
1661 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001662 if (tv == &partial_tv)
1663 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001665 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 }
1667 break;
1668
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001669 case ISN_PCALL_END:
1670 // PCALL finished, arguments have been consumed and replaced by
1671 // the return value. Now clear the funcref from the stack,
1672 // and move the return value in its place.
1673 --ectx.ec_stack.ga_len;
1674 clear_tv(STACK_TV_BOT(-1));
1675 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1676 break;
1677
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 // call a user defined function or funcref/partial
1679 case ISN_UCALL:
1680 {
1681 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1682
1683 SOURCING_LNUM = iptr->isn_lnum;
1684 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001685 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001686 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 }
1688 break;
1689
1690 // return from a :def function call
1691 case ISN_RETURN:
1692 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001693 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001694 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001695
1696 if (trystack->ga_len > 0)
1697 trycmd = ((trycmd_T *)trystack->ga_data)
1698 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001699 if (trycmd != NULL
1700 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 && trycmd->tcd_finally_idx != 0)
1702 {
1703 // jump to ":finally"
1704 ectx.ec_iidx = trycmd->tcd_finally_idx;
1705 trycmd->tcd_return = TRUE;
1706 }
1707 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001708 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 }
1710 break;
1711
1712 // push a function reference to a compiled function
1713 case ISN_FUNCREF:
1714 {
1715 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001716 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001717
1718 pt = ALLOC_CLEAR_ONE(partial_T);
1719 if (pt == NULL)
1720 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001721 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001722 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001723 vim_free(pt);
1724 goto failed;
1725 }
1726 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1727 + iptr->isn_arg.funcref.fr_func;
1728 pt->pt_func = pt_dfunc->df_ufunc;
1729 pt->pt_refcount = 1;
1730 ++pt_dfunc->df_ufunc->uf_refcount;
1731
1732 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1733 {
1734 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1735 + ectx.ec_dfunc_idx;
1736
1737 // The closure needs to find arguments and local
1738 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001739 pt->pt_ectx_stack = &ectx.ec_stack;
1740 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001741
1742 // If this function returns and the closure is still
1743 // used, we need to make a copy of the context
1744 // (arguments and local variables). Store a reference
1745 // to the partial so we can handle that.
1746 ++pt->pt_refcount;
1747 tv = STACK_TV_VAR(dfunc->df_varcount
1748 + iptr->isn_arg.funcref.fr_var_idx);
1749 if (tv->v_type == VAR_PARTIAL)
1750 {
1751 // TODO: use a garray_T on ectx.
1752 emsg("Multiple closures not supported yet");
1753 goto failed;
1754 }
1755 tv->v_type = VAR_PARTIAL;
1756 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001757 }
1758
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001759 tv = STACK_TV_BOT(0);
1760 ++ectx.ec_stack.ga_len;
1761 tv->vval.v_partial = pt;
1762 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001763 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 }
1765 break;
1766
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001767 // Create a global function from a lambda.
1768 case ISN_NEWFUNC:
1769 {
1770 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1771
1772 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1773 }
1774 break;
1775
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001776 // jump if a condition is met
1777 case ISN_JUMP:
1778 {
1779 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1780 int jump = TRUE;
1781
1782 if (when != JUMP_ALWAYS)
1783 {
1784 tv = STACK_TV_BOT(-1);
1785 jump = tv2bool(tv);
1786 if (when == JUMP_IF_FALSE
1787 || when == JUMP_AND_KEEP_IF_FALSE)
1788 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001789 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 {
1791 // drop the value from the stack
1792 clear_tv(tv);
1793 --ectx.ec_stack.ga_len;
1794 }
1795 }
1796 if (jump)
1797 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1798 }
1799 break;
1800
1801 // top of a for loop
1802 case ISN_FOR:
1803 {
1804 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1805 typval_T *idxtv =
1806 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1807
1808 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001809 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 goto failed;
1811 if (++idxtv->vval.v_number >= list->lv_len)
1812 // past the end of the list, jump to "endfor"
1813 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1814 else if (list->lv_first == &range_list_item)
1815 {
1816 // non-materialized range() list
1817 tv = STACK_TV_BOT(0);
1818 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001819 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 tv->vval.v_number = list_find_nr(
1821 list, idxtv->vval.v_number, NULL);
1822 ++ectx.ec_stack.ga_len;
1823 }
1824 else
1825 {
1826 listitem_T *li = list_find(list, idxtv->vval.v_number);
1827
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1829 ++ectx.ec_stack.ga_len;
1830 }
1831 }
1832 break;
1833
1834 // start of ":try" block
1835 case ISN_TRY:
1836 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001837 trycmd_T *trycmd = NULL;
1838
Bram Moolenaar270d0382020-05-15 21:42:53 +02001839 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 goto failed;
1841 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1842 + ectx.ec_trystack.ga_len;
1843 ++ectx.ec_trystack.ga_len;
1844 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001845 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001846 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1847 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001848 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 }
1850 break;
1851
1852 case ISN_PUSHEXC:
1853 if (current_exception == NULL)
1854 {
1855 iemsg("Evaluating catch while current_exception is NULL");
1856 goto failed;
1857 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001858 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 goto failed;
1860 tv = STACK_TV_BOT(0);
1861 ++ectx.ec_stack.ga_len;
1862 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001863 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001864 tv->vval.v_string = vim_strsave(
1865 (char_u *)current_exception->value);
1866 break;
1867
1868 case ISN_CATCH:
1869 {
1870 garray_T *trystack = &ectx.ec_trystack;
1871
1872 if (trystack->ga_len > 0)
1873 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001874 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 + trystack->ga_len - 1;
1876 trycmd->tcd_caught = TRUE;
1877 }
1878 did_emsg = got_int = did_throw = FALSE;
1879 catch_exception(current_exception);
1880 }
1881 break;
1882
1883 // end of ":try" block
1884 case ISN_ENDTRY:
1885 {
1886 garray_T *trystack = &ectx.ec_trystack;
1887
1888 if (trystack->ga_len > 0)
1889 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001890 trycmd_T *trycmd = NULL;
1891
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 --trystack->ga_len;
1893 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001894 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 trycmd = ((trycmd_T *)trystack->ga_data)
1896 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001897 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 {
1899 // discard the exception
1900 if (caught_stack == current_exception)
1901 caught_stack = caught_stack->caught;
1902 discard_current_exception();
1903 }
1904
1905 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001906 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 }
1908 }
1909 break;
1910
1911 case ISN_THROW:
1912 --ectx.ec_stack.ga_len;
1913 tv = STACK_TV_BOT(0);
1914 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1915 {
1916 vim_free(tv->vval.v_string);
1917 goto failed;
1918 }
1919 did_throw = TRUE;
1920 break;
1921
1922 // compare with special values
1923 case ISN_COMPAREBOOL:
1924 case ISN_COMPARESPECIAL:
1925 {
1926 typval_T *tv1 = STACK_TV_BOT(-2);
1927 typval_T *tv2 = STACK_TV_BOT(-1);
1928 varnumber_T arg1 = tv1->vval.v_number;
1929 varnumber_T arg2 = tv2->vval.v_number;
1930 int res;
1931
1932 switch (iptr->isn_arg.op.op_type)
1933 {
1934 case EXPR_EQUAL: res = arg1 == arg2; break;
1935 case EXPR_NEQUAL: res = arg1 != arg2; break;
1936 default: res = 0; break;
1937 }
1938
1939 --ectx.ec_stack.ga_len;
1940 tv1->v_type = VAR_BOOL;
1941 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1942 }
1943 break;
1944
1945 // Operation with two number arguments
1946 case ISN_OPNR:
1947 case ISN_COMPARENR:
1948 {
1949 typval_T *tv1 = STACK_TV_BOT(-2);
1950 typval_T *tv2 = STACK_TV_BOT(-1);
1951 varnumber_T arg1 = tv1->vval.v_number;
1952 varnumber_T arg2 = tv2->vval.v_number;
1953 varnumber_T res;
1954
1955 switch (iptr->isn_arg.op.op_type)
1956 {
1957 case EXPR_MULT: res = arg1 * arg2; break;
1958 case EXPR_DIV: res = arg1 / arg2; break;
1959 case EXPR_REM: res = arg1 % arg2; break;
1960 case EXPR_SUB: res = arg1 - arg2; break;
1961 case EXPR_ADD: res = arg1 + arg2; break;
1962
1963 case EXPR_EQUAL: res = arg1 == arg2; break;
1964 case EXPR_NEQUAL: res = arg1 != arg2; break;
1965 case EXPR_GREATER: res = arg1 > arg2; break;
1966 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1967 case EXPR_SMALLER: res = arg1 < arg2; break;
1968 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1969 default: res = 0; break;
1970 }
1971
1972 --ectx.ec_stack.ga_len;
1973 if (iptr->isn_type == ISN_COMPARENR)
1974 {
1975 tv1->v_type = VAR_BOOL;
1976 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1977 }
1978 else
1979 tv1->vval.v_number = res;
1980 }
1981 break;
1982
1983 // Computation with two float arguments
1984 case ISN_OPFLOAT:
1985 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001986#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987 {
1988 typval_T *tv1 = STACK_TV_BOT(-2);
1989 typval_T *tv2 = STACK_TV_BOT(-1);
1990 float_T arg1 = tv1->vval.v_float;
1991 float_T arg2 = tv2->vval.v_float;
1992 float_T res = 0;
1993 int cmp = FALSE;
1994
1995 switch (iptr->isn_arg.op.op_type)
1996 {
1997 case EXPR_MULT: res = arg1 * arg2; break;
1998 case EXPR_DIV: res = arg1 / arg2; break;
1999 case EXPR_SUB: res = arg1 - arg2; break;
2000 case EXPR_ADD: res = arg1 + arg2; break;
2001
2002 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2003 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2004 case EXPR_GREATER: cmp = arg1 > arg2; break;
2005 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2006 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2007 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2008 default: cmp = 0; break;
2009 }
2010 --ectx.ec_stack.ga_len;
2011 if (iptr->isn_type == ISN_COMPAREFLOAT)
2012 {
2013 tv1->v_type = VAR_BOOL;
2014 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2015 }
2016 else
2017 tv1->vval.v_float = res;
2018 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002019#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 break;
2021
2022 case ISN_COMPARELIST:
2023 {
2024 typval_T *tv1 = STACK_TV_BOT(-2);
2025 typval_T *tv2 = STACK_TV_BOT(-1);
2026 list_T *arg1 = tv1->vval.v_list;
2027 list_T *arg2 = tv2->vval.v_list;
2028 int cmp = FALSE;
2029 int ic = iptr->isn_arg.op.op_ic;
2030
2031 switch (iptr->isn_arg.op.op_type)
2032 {
2033 case EXPR_EQUAL: cmp =
2034 list_equal(arg1, arg2, ic, FALSE); break;
2035 case EXPR_NEQUAL: cmp =
2036 !list_equal(arg1, arg2, ic, FALSE); break;
2037 case EXPR_IS: cmp = arg1 == arg2; break;
2038 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2039 default: cmp = 0; break;
2040 }
2041 --ectx.ec_stack.ga_len;
2042 clear_tv(tv1);
2043 clear_tv(tv2);
2044 tv1->v_type = VAR_BOOL;
2045 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2046 }
2047 break;
2048
2049 case ISN_COMPAREBLOB:
2050 {
2051 typval_T *tv1 = STACK_TV_BOT(-2);
2052 typval_T *tv2 = STACK_TV_BOT(-1);
2053 blob_T *arg1 = tv1->vval.v_blob;
2054 blob_T *arg2 = tv2->vval.v_blob;
2055 int cmp = FALSE;
2056
2057 switch (iptr->isn_arg.op.op_type)
2058 {
2059 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2060 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2061 case EXPR_IS: cmp = arg1 == arg2; break;
2062 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2063 default: cmp = 0; break;
2064 }
2065 --ectx.ec_stack.ga_len;
2066 clear_tv(tv1);
2067 clear_tv(tv2);
2068 tv1->v_type = VAR_BOOL;
2069 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2070 }
2071 break;
2072
2073 // TODO: handle separately
2074 case ISN_COMPARESTRING:
2075 case ISN_COMPAREDICT:
2076 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002077 case ISN_COMPAREANY:
2078 {
2079 typval_T *tv1 = STACK_TV_BOT(-2);
2080 typval_T *tv2 = STACK_TV_BOT(-1);
2081 exptype_T exptype = iptr->isn_arg.op.op_type;
2082 int ic = iptr->isn_arg.op.op_ic;
2083
2084 typval_compare(tv1, tv2, exptype, ic);
2085 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002086 --ectx.ec_stack.ga_len;
2087 }
2088 break;
2089
2090 case ISN_ADDLIST:
2091 case ISN_ADDBLOB:
2092 {
2093 typval_T *tv1 = STACK_TV_BOT(-2);
2094 typval_T *tv2 = STACK_TV_BOT(-1);
2095
2096 if (iptr->isn_type == ISN_ADDLIST)
2097 eval_addlist(tv1, tv2);
2098 else
2099 eval_addblob(tv1, tv2);
2100 clear_tv(tv2);
2101 --ectx.ec_stack.ga_len;
2102 }
2103 break;
2104
2105 // Computation with two arguments of unknown type
2106 case ISN_OPANY:
2107 {
2108 typval_T *tv1 = STACK_TV_BOT(-2);
2109 typval_T *tv2 = STACK_TV_BOT(-1);
2110 varnumber_T n1, n2;
2111#ifdef FEAT_FLOAT
2112 float_T f1 = 0, f2 = 0;
2113#endif
2114 int error = FALSE;
2115
2116 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2117 {
2118 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2119 {
2120 eval_addlist(tv1, tv2);
2121 clear_tv(tv2);
2122 --ectx.ec_stack.ga_len;
2123 break;
2124 }
2125 else if (tv1->v_type == VAR_BLOB
2126 && tv2->v_type == VAR_BLOB)
2127 {
2128 eval_addblob(tv1, tv2);
2129 clear_tv(tv2);
2130 --ectx.ec_stack.ga_len;
2131 break;
2132 }
2133 }
2134#ifdef FEAT_FLOAT
2135 if (tv1->v_type == VAR_FLOAT)
2136 {
2137 f1 = tv1->vval.v_float;
2138 n1 = 0;
2139 }
2140 else
2141#endif
2142 {
2143 n1 = tv_get_number_chk(tv1, &error);
2144 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002145 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002146#ifdef FEAT_FLOAT
2147 if (tv2->v_type == VAR_FLOAT)
2148 f1 = n1;
2149#endif
2150 }
2151#ifdef FEAT_FLOAT
2152 if (tv2->v_type == VAR_FLOAT)
2153 {
2154 f2 = tv2->vval.v_float;
2155 n2 = 0;
2156 }
2157 else
2158#endif
2159 {
2160 n2 = tv_get_number_chk(tv2, &error);
2161 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002162 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002163#ifdef FEAT_FLOAT
2164 if (tv1->v_type == VAR_FLOAT)
2165 f2 = n2;
2166#endif
2167 }
2168#ifdef FEAT_FLOAT
2169 // if there is a float on either side the result is a float
2170 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2171 {
2172 switch (iptr->isn_arg.op.op_type)
2173 {
2174 case EXPR_MULT: f1 = f1 * f2; break;
2175 case EXPR_DIV: f1 = f1 / f2; break;
2176 case EXPR_SUB: f1 = f1 - f2; break;
2177 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002178 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002179 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181 clear_tv(tv1);
2182 clear_tv(tv2);
2183 tv1->v_type = VAR_FLOAT;
2184 tv1->vval.v_float = f1;
2185 --ectx.ec_stack.ga_len;
2186 }
2187 else
2188#endif
2189 {
2190 switch (iptr->isn_arg.op.op_type)
2191 {
2192 case EXPR_MULT: n1 = n1 * n2; break;
2193 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2194 case EXPR_SUB: n1 = n1 - n2; break;
2195 case EXPR_ADD: n1 = n1 + n2; break;
2196 default: n1 = num_modulus(n1, n2); break;
2197 }
2198 clear_tv(tv1);
2199 clear_tv(tv2);
2200 tv1->v_type = VAR_NUMBER;
2201 tv1->vval.v_number = n1;
2202 --ectx.ec_stack.ga_len;
2203 }
2204 }
2205 break;
2206
2207 case ISN_CONCAT:
2208 {
2209 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2210 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2211 char_u *res;
2212
2213 res = concat_str(str1, str2);
2214 clear_tv(STACK_TV_BOT(-2));
2215 clear_tv(STACK_TV_BOT(-1));
2216 --ectx.ec_stack.ga_len;
2217 STACK_TV_BOT(-1)->vval.v_string = res;
2218 }
2219 break;
2220
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002221 case ISN_STRINDEX:
2222 {
2223 char_u *s;
2224 varnumber_T n;
2225 char_u *res;
2226
2227 // string index: string is at stack-2, index at stack-1
2228 tv = STACK_TV_BOT(-2);
2229 if (tv->v_type != VAR_STRING)
2230 {
2231 emsg(_(e_stringreq));
2232 goto on_error;
2233 }
2234 s = tv->vval.v_string;
2235
2236 tv = STACK_TV_BOT(-1);
2237 if (tv->v_type != VAR_NUMBER)
2238 {
2239 emsg(_(e_number_exp));
2240 goto on_error;
2241 }
2242 n = tv->vval.v_number;
2243
2244 // The resulting variable is a string of a single
2245 // character. If the index is too big or negative the
2246 // result is empty.
2247 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2248 res = NULL;
2249 else
2250 res = vim_strnsave(s + n, 1);
2251 --ectx.ec_stack.ga_len;
2252 tv = STACK_TV_BOT(-1);
2253 vim_free(tv->vval.v_string);
2254 tv->vval.v_string = res;
2255 }
2256 break;
2257
2258 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 {
2260 list_T *list;
2261 varnumber_T n;
2262 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002263 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264
2265 // list index: list is at stack-2, index at stack-1
2266 tv = STACK_TV_BOT(-2);
2267 if (tv->v_type != VAR_LIST)
2268 {
2269 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002270 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 }
2272 list = tv->vval.v_list;
2273
2274 tv = STACK_TV_BOT(-1);
2275 if (tv->v_type != VAR_NUMBER)
2276 {
2277 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002278 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 }
2280 n = tv->vval.v_number;
2281 clear_tv(tv);
2282 if ((li = list_find(list, n)) == NULL)
2283 {
2284 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002285 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002286 }
2287 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002288 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002289 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002290 tv = STACK_TV_BOT(-1);
2291 temp_tv = *tv;
2292 copy_tv(&li->li_tv, tv);
2293 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002294 }
2295 break;
2296
Bram Moolenaar9af78762020-06-16 11:34:42 +02002297 case ISN_SLICE:
2298 {
2299 list_T *list;
2300 int count = iptr->isn_arg.number;
2301
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002302 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002303 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002304 list = tv->vval.v_list;
2305
2306 // no error for short list, expect it to be checked earlier
2307 if (list != NULL && list->lv_len >= count)
2308 {
2309 list_T *newlist = list_slice(list,
2310 count, list->lv_len - 1);
2311
2312 if (newlist != NULL)
2313 {
2314 list_unref(list);
2315 tv->vval.v_list = newlist;
2316 ++newlist->lv_refcount;
2317 }
2318 }
2319 }
2320 break;
2321
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002322 case ISN_GETITEM:
2323 {
2324 listitem_T *li;
2325 int index = iptr->isn_arg.number;
2326
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002327 // Get list item: list is at stack-1, push item.
2328 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002329 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002330 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002331
2332 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2333 goto failed;
2334 ++ectx.ec_stack.ga_len;
2335 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2336 }
2337 break;
2338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 case ISN_MEMBER:
2340 {
2341 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002342 char_u *key;
2343 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002344 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002345
2346 // dict member: dict is at stack-2, key at stack-1
2347 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002348 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002349 dict = tv->vval.v_dict;
2350
2351 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002352 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002353 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002354
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002355 if ((di = dict_find(dict, key, -1)) == NULL)
2356 {
2357 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002358 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002359 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002360 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002361 --ectx.ec_stack.ga_len;
2362 // Clear the dict after getting the item, to avoid that it
2363 // make the item invalid.
2364 tv = STACK_TV_BOT(-1);
2365 temp_tv = *tv;
2366 copy_tv(&di->di_tv, tv);
2367 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002368 }
2369 break;
2370
2371 // dict member with string key
2372 case ISN_STRINGMEMBER:
2373 {
2374 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002376 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002377
2378 tv = STACK_TV_BOT(-1);
2379 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2380 {
2381 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002382 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 }
2384 dict = tv->vval.v_dict;
2385
2386 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2387 == NULL)
2388 {
2389 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002392 // Clear the dict after getting the item, to avoid that it
2393 // make the item invalid.
2394 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002396 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398 break;
2399
2400 case ISN_NEGATENR:
2401 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002402 if (tv->v_type != VAR_NUMBER
2403#ifdef FEAT_FLOAT
2404 && tv->v_type != VAR_FLOAT
2405#endif
2406 )
2407 {
2408 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002409 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002410 }
2411#ifdef FEAT_FLOAT
2412 if (tv->v_type == VAR_FLOAT)
2413 tv->vval.v_float = -tv->vval.v_float;
2414 else
2415#endif
2416 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 break;
2418
2419 case ISN_CHECKNR:
2420 {
2421 int error = FALSE;
2422
2423 tv = STACK_TV_BOT(-1);
2424 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002425 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 (void)tv_get_number_chk(tv, &error);
2427 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002428 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 }
2430 break;
2431
2432 case ISN_CHECKTYPE:
2433 {
2434 checktype_T *ct = &iptr->isn_arg.type;
2435
2436 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002437 // TODO: better type comparison
2438 if (tv->v_type != ct->ct_type
2439 && !((tv->v_type == VAR_PARTIAL
2440 && ct->ct_type == VAR_FUNC)
2441 || (tv->v_type == VAR_FUNC
2442 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 {
2444 semsg(_("E1029: Expected %s but got %s"),
2445 vartype_name(ct->ct_type),
2446 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002447 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 }
2449 }
2450 break;
2451
Bram Moolenaar9af78762020-06-16 11:34:42 +02002452 case ISN_CHECKLEN:
2453 {
2454 int min_len = iptr->isn_arg.checklen.cl_min_len;
2455 list_T *list = NULL;
2456
2457 tv = STACK_TV_BOT(-1);
2458 if (tv->v_type == VAR_LIST)
2459 list = tv->vval.v_list;
2460 if (list == NULL || list->lv_len < min_len
2461 || (list->lv_len > min_len
2462 && !iptr->isn_arg.checklen.cl_more_OK))
2463 {
2464 semsg(_("E1093: Expected %d items but got %d"),
2465 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002466 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002467 }
2468 }
2469 break;
2470
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 case ISN_2BOOL:
2472 {
2473 int n;
2474
2475 tv = STACK_TV_BOT(-1);
2476 n = tv2bool(tv);
2477 if (iptr->isn_arg.number) // invert
2478 n = !n;
2479 clear_tv(tv);
2480 tv->v_type = VAR_BOOL;
2481 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2482 }
2483 break;
2484
2485 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002486 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 {
2488 char_u *str;
2489
2490 tv = STACK_TV_BOT(iptr->isn_arg.number);
2491 if (tv->v_type != VAR_STRING)
2492 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002493 if (iptr->isn_type == ISN_2STRING_ANY)
2494 {
2495 switch (tv->v_type)
2496 {
2497 case VAR_SPECIAL:
2498 case VAR_BOOL:
2499 case VAR_NUMBER:
2500 case VAR_FLOAT:
2501 case VAR_BLOB: break;
2502 default: to_string_error(tv->v_type);
2503 goto on_error;
2504 }
2505 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 str = typval_tostring(tv);
2507 clear_tv(tv);
2508 tv->v_type = VAR_STRING;
2509 tv->vval.v_string = str;
2510 }
2511 }
2512 break;
2513
Bram Moolenaar389df252020-07-09 21:20:47 +02002514 case ISN_SHUFFLE:
2515 {
2516 typval_T tmp_tv;
2517 int item = iptr->isn_arg.shuffle.shfl_item;
2518 int up = iptr->isn_arg.shuffle.shfl_up;
2519
2520 tmp_tv = *STACK_TV_BOT(-item);
2521 for ( ; up > 0 && item > 1; --up)
2522 {
2523 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2524 --item;
2525 }
2526 *STACK_TV_BOT(-item) = tmp_tv;
2527 }
2528 break;
2529
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 case ISN_DROP:
2531 --ectx.ec_stack.ga_len;
2532 clear_tv(STACK_TV_BOT(0));
2533 break;
2534 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002535 continue;
2536
Bram Moolenaard032f342020-07-18 18:13:02 +02002537func_return:
2538 // Restore previous function. If the frame pointer is zero then there
2539 // is none and we are done.
2540 if (ectx.ec_frame_idx == initial_frame_idx)
2541 {
2542 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2543 // only fails when out of memory
2544 goto failed;
2545 goto done;
2546 }
2547 if (func_return(&ectx) == FAIL)
2548 // only fails when out of memory
2549 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002550 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002551
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002552on_error:
2553 if (trylevel == 0)
2554 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
2556
2557done:
2558 // function finished, get result from the stack.
2559 tv = STACK_TV_BOT(-1);
2560 *rettv = *tv;
2561 tv->v_type = VAR_UNKNOWN;
2562 ret = OK;
2563
2564failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002565 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002566 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002567 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002568failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002569 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002570
2571 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2573 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002576 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002577
2578 if (ret != OK && called_emsg == called_emsg_before)
2579 semsg(_("E1099: Unknown error while executing %s"),
2580 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 return ret;
2582}
2583
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584/*
2585 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002586 * We don't really need this at runtime, but we do have tests that require it,
2587 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 */
2589 void
2590ex_disassemble(exarg_T *eap)
2591{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002592 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002593 char_u *fname;
2594 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 dfunc_T *dfunc;
2596 isn_T *instr;
2597 int current;
2598 int line_idx = 0;
2599 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002600 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002602 if (STRNCMP(arg, "<lambda>", 8) == 0)
2603 {
2604 arg += 8;
2605 (void)getdigits(&arg);
2606 fname = vim_strnsave(eap->arg, arg - eap->arg);
2607 }
2608 else
2609 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002610 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002611 if (fname == NULL)
2612 {
2613 semsg(_(e_invarg2), eap->arg);
2614 return;
2615 }
2616
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002617 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002618 if (ufunc == NULL)
2619 {
2620 char_u *p = untrans_function_name(fname);
2621
2622 if (p != NULL)
2623 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002624 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002625 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002626 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 if (ufunc == NULL)
2628 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002629 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 return;
2631 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002632 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002633 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2634 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002635 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002637 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002638 return;
2639 }
2640 if (ufunc->uf_name_exp != NULL)
2641 msg((char *)ufunc->uf_name_exp);
2642 else
2643 msg((char *)ufunc->uf_name);
2644
2645 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2646 instr = dfunc->df_instr;
2647 for (current = 0; current < dfunc->df_instr_count; ++current)
2648 {
2649 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002650 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651
2652 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2653 {
2654 if (current > prev_current)
2655 {
2656 msg_puts("\n\n");
2657 prev_current = current;
2658 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002659 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2660 if (line != NULL)
2661 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 }
2663
2664 switch (iptr->isn_type)
2665 {
2666 case ISN_EXEC:
2667 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2668 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002669 case ISN_EXECCONCAT:
2670 smsg("%4d EXECCONCAT %lld", current,
2671 (long long)iptr->isn_arg.number);
2672 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 case ISN_ECHO:
2674 {
2675 echo_T *echo = &iptr->isn_arg.echo;
2676
2677 smsg("%4d %s %d", current,
2678 echo->echo_with_white ? "ECHO" : "ECHON",
2679 echo->echo_count);
2680 }
2681 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002682 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002683 smsg("%4d EXECUTE %lld", current,
2684 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002685 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002686 case ISN_ECHOMSG:
2687 smsg("%4d ECHOMSG %lld", current,
2688 (long long)(iptr->isn_arg.number));
2689 break;
2690 case ISN_ECHOERR:
2691 smsg("%4d ECHOERR %lld", current,
2692 (long long)(iptr->isn_arg.number));
2693 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002695 case ISN_LOADOUTER:
2696 {
2697 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2698
2699 if (iptr->isn_arg.number < 0)
2700 smsg("%4d LOAD%s arg[%lld]", current, add,
2701 (long long)(iptr->isn_arg.number
2702 + STACK_FRAME_SIZE));
2703 else
2704 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002705 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002706 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 break;
2708 case ISN_LOADV:
2709 smsg("%4d LOADV v:%s", current,
2710 get_vim_var_name(iptr->isn_arg.number));
2711 break;
2712 case ISN_LOADSCRIPT:
2713 {
2714 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002715 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002716 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2717 + iptr->isn_arg.script.script_idx;
2718
2719 smsg("%4d LOADSCRIPT %s from %s", current,
2720 sv->sv_name, si->sn_name);
2721 }
2722 break;
2723 case ISN_LOADS:
2724 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002725 scriptitem_T *si = SCRIPT_ITEM(
2726 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727
2728 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002729 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 }
2731 break;
2732 case ISN_LOADG:
2733 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2734 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002735 case ISN_LOADB:
2736 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2737 break;
2738 case ISN_LOADW:
2739 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2740 break;
2741 case ISN_LOADT:
2742 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2743 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002744 case ISN_LOADGDICT:
2745 smsg("%4d LOAD g:", current);
2746 break;
2747 case ISN_LOADBDICT:
2748 smsg("%4d LOAD b:", current);
2749 break;
2750 case ISN_LOADWDICT:
2751 smsg("%4d LOAD w:", current);
2752 break;
2753 case ISN_LOADTDICT:
2754 smsg("%4d LOAD t:", current);
2755 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 case ISN_LOADOPT:
2757 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2758 break;
2759 case ISN_LOADENV:
2760 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2761 break;
2762 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002763 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 break;
2765
2766 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002767 case ISN_STOREOUTER:
2768 {
2769 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2770
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002771 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002772 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002773 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002774 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002775 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002776 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002777 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002779 case ISN_STOREV:
2780 smsg("%4d STOREV v:%s", current,
2781 get_vim_var_name(iptr->isn_arg.number));
2782 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002784 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2785 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002786 case ISN_STOREB:
2787 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2788 break;
2789 case ISN_STOREW:
2790 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2791 break;
2792 case ISN_STORET:
2793 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2794 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002795 case ISN_STORES:
2796 {
2797 scriptitem_T *si = SCRIPT_ITEM(
2798 iptr->isn_arg.loadstore.ls_sid);
2799
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002800 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002801 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002802 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 break;
2804 case ISN_STORESCRIPT:
2805 {
2806 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002807 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2809 + iptr->isn_arg.script.script_idx;
2810
2811 smsg("%4d STORESCRIPT %s in %s", current,
2812 sv->sv_name, si->sn_name);
2813 }
2814 break;
2815 case ISN_STOREOPT:
2816 smsg("%4d STOREOPT &%s", current,
2817 iptr->isn_arg.storeopt.so_name);
2818 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002819 case ISN_STOREENV:
2820 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2821 break;
2822 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002823 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002824 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 case ISN_STORENR:
2826 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002827 iptr->isn_arg.storenr.stnr_val,
2828 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002829 break;
2830
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002831 case ISN_STORELIST:
2832 smsg("%4d STORELIST", current);
2833 break;
2834
2835 case ISN_STOREDICT:
2836 smsg("%4d STOREDICT", current);
2837 break;
2838
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 // constants
2840 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002841 smsg("%4d PUSHNR %lld", current,
2842 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002843 break;
2844 case ISN_PUSHBOOL:
2845 case ISN_PUSHSPEC:
2846 smsg("%4d PUSH %s", current,
2847 get_var_special_name(iptr->isn_arg.number));
2848 break;
2849 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002850#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002852#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 break;
2854 case ISN_PUSHS:
2855 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2856 break;
2857 case ISN_PUSHBLOB:
2858 {
2859 char_u *r;
2860 char_u numbuf[NUMBUFLEN];
2861 char_u *tofree;
2862
2863 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002864 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 vim_free(tofree);
2866 }
2867 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002868 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002869 {
2870 char *name = (char *)iptr->isn_arg.string;
2871
2872 smsg("%4d PUSHFUNC \"%s\"", current,
2873 name == NULL ? "[none]" : name);
2874 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002875 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002876 case ISN_PUSHCHANNEL:
2877#ifdef FEAT_JOB_CHANNEL
2878 {
2879 channel_T *channel = iptr->isn_arg.channel;
2880
2881 smsg("%4d PUSHCHANNEL %d", current,
2882 channel == NULL ? 0 : channel->ch_id);
2883 }
2884#endif
2885 break;
2886 case ISN_PUSHJOB:
2887#ifdef FEAT_JOB_CHANNEL
2888 {
2889 typval_T tv;
2890 char_u *name;
2891
2892 tv.v_type = VAR_JOB;
2893 tv.vval.v_job = iptr->isn_arg.job;
2894 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002895 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002896 }
2897#endif
2898 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 case ISN_PUSHEXC:
2900 smsg("%4d PUSH v:exception", current);
2901 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002902 case ISN_UNLET:
2903 smsg("%4d UNLET%s %s", current,
2904 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2905 iptr->isn_arg.unlet.ul_name);
2906 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002907 case ISN_UNLETENV:
2908 smsg("%4d UNLETENV%s $%s", current,
2909 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2910 iptr->isn_arg.unlet.ul_name);
2911 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002913 smsg("%4d NEWLIST size %lld", current,
2914 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002915 break;
2916 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002917 smsg("%4d NEWDICT size %lld", current,
2918 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002919 break;
2920
2921 // function call
2922 case ISN_BCALL:
2923 {
2924 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2925
2926 smsg("%4d BCALL %s(argc %d)", current,
2927 internal_func_name(cbfunc->cbf_idx),
2928 cbfunc->cbf_argcount);
2929 }
2930 break;
2931 case ISN_DCALL:
2932 {
2933 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2934 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2935 + cdfunc->cdf_idx;
2936
2937 smsg("%4d DCALL %s(argc %d)", current,
2938 df->df_ufunc->uf_name_exp != NULL
2939 ? df->df_ufunc->uf_name_exp
2940 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2941 }
2942 break;
2943 case ISN_UCALL:
2944 {
2945 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2946
2947 smsg("%4d UCALL %s(argc %d)", current,
2948 cufunc->cuf_name, cufunc->cuf_argcount);
2949 }
2950 break;
2951 case ISN_PCALL:
2952 {
2953 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2954
2955 smsg("%4d PCALL%s (argc %d)", current,
2956 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2957 }
2958 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002959 case ISN_PCALL_END:
2960 smsg("%4d PCALL end", current);
2961 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 case ISN_RETURN:
2963 smsg("%4d RETURN", current);
2964 break;
2965 case ISN_FUNCREF:
2966 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002967 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002969 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002971 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002972 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973 }
2974 break;
2975
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002976 case ISN_NEWFUNC:
2977 {
2978 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2979
2980 smsg("%4d NEWFUNC %s %s", current,
2981 newfunc->nf_lambda, newfunc->nf_global);
2982 }
2983 break;
2984
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985 case ISN_JUMP:
2986 {
2987 char *when = "?";
2988
2989 switch (iptr->isn_arg.jump.jump_when)
2990 {
2991 case JUMP_ALWAYS:
2992 when = "JUMP";
2993 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002994 case JUMP_AND_KEEP_IF_TRUE:
2995 when = "JUMP_AND_KEEP_IF_TRUE";
2996 break;
2997 case JUMP_IF_FALSE:
2998 when = "JUMP_IF_FALSE";
2999 break;
3000 case JUMP_AND_KEEP_IF_FALSE:
3001 when = "JUMP_AND_KEEP_IF_FALSE";
3002 break;
3003 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003004 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003005 iptr->isn_arg.jump.jump_where);
3006 }
3007 break;
3008
3009 case ISN_FOR:
3010 {
3011 forloop_T *forloop = &iptr->isn_arg.forloop;
3012
3013 smsg("%4d FOR $%d -> %d", current,
3014 forloop->for_idx, forloop->for_end);
3015 }
3016 break;
3017
3018 case ISN_TRY:
3019 {
3020 try_T *try = &iptr->isn_arg.try;
3021
3022 smsg("%4d TRY catch -> %d, finally -> %d", current,
3023 try->try_catch, try->try_finally);
3024 }
3025 break;
3026 case ISN_CATCH:
3027 // TODO
3028 smsg("%4d CATCH", current);
3029 break;
3030 case ISN_ENDTRY:
3031 smsg("%4d ENDTRY", current);
3032 break;
3033 case ISN_THROW:
3034 smsg("%4d THROW", current);
3035 break;
3036
3037 // expression operations on number
3038 case ISN_OPNR:
3039 case ISN_OPFLOAT:
3040 case ISN_OPANY:
3041 {
3042 char *what;
3043 char *ins;
3044
3045 switch (iptr->isn_arg.op.op_type)
3046 {
3047 case EXPR_MULT: what = "*"; break;
3048 case EXPR_DIV: what = "/"; break;
3049 case EXPR_REM: what = "%"; break;
3050 case EXPR_SUB: what = "-"; break;
3051 case EXPR_ADD: what = "+"; break;
3052 default: what = "???"; break;
3053 }
3054 switch (iptr->isn_type)
3055 {
3056 case ISN_OPNR: ins = "OPNR"; break;
3057 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3058 case ISN_OPANY: ins = "OPANY"; break;
3059 default: ins = "???"; break;
3060 }
3061 smsg("%4d %s %s", current, ins, what);
3062 }
3063 break;
3064
3065 case ISN_COMPAREBOOL:
3066 case ISN_COMPARESPECIAL:
3067 case ISN_COMPARENR:
3068 case ISN_COMPAREFLOAT:
3069 case ISN_COMPARESTRING:
3070 case ISN_COMPAREBLOB:
3071 case ISN_COMPARELIST:
3072 case ISN_COMPAREDICT:
3073 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003074 case ISN_COMPAREANY:
3075 {
3076 char *p;
3077 char buf[10];
3078 char *type;
3079
3080 switch (iptr->isn_arg.op.op_type)
3081 {
3082 case EXPR_EQUAL: p = "=="; break;
3083 case EXPR_NEQUAL: p = "!="; break;
3084 case EXPR_GREATER: p = ">"; break;
3085 case EXPR_GEQUAL: p = ">="; break;
3086 case EXPR_SMALLER: p = "<"; break;
3087 case EXPR_SEQUAL: p = "<="; break;
3088 case EXPR_MATCH: p = "=~"; break;
3089 case EXPR_IS: p = "is"; break;
3090 case EXPR_ISNOT: p = "isnot"; break;
3091 case EXPR_NOMATCH: p = "!~"; break;
3092 default: p = "???"; break;
3093 }
3094 STRCPY(buf, p);
3095 if (iptr->isn_arg.op.op_ic == TRUE)
3096 strcat(buf, "?");
3097 switch(iptr->isn_type)
3098 {
3099 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3100 case ISN_COMPARESPECIAL:
3101 type = "COMPARESPECIAL"; break;
3102 case ISN_COMPARENR: type = "COMPARENR"; break;
3103 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3104 case ISN_COMPARESTRING:
3105 type = "COMPARESTRING"; break;
3106 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3107 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3108 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3109 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3111 default: type = "???"; break;
3112 }
3113
3114 smsg("%4d %s %s", current, type, buf);
3115 }
3116 break;
3117
3118 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3119 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3120
3121 // expression operations
3122 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003123 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3124 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003125 case ISN_SLICE: smsg("%4d SLICE %lld",
3126 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003127 case ISN_GETITEM: smsg("%4d ITEM %lld",
3128 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003129 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3130 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131 iptr->isn_arg.string); break;
3132 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3133
3134 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3135 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3136 vartype_name(iptr->isn_arg.type.ct_type),
3137 iptr->isn_arg.type.ct_off);
3138 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003139 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3140 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3141 iptr->isn_arg.checklen.cl_min_len);
3142 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003143 case ISN_2BOOL: if (iptr->isn_arg.number)
3144 smsg("%4d INVERT (!val)", current);
3145 else
3146 smsg("%4d 2BOOL (!!val)", current);
3147 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003148 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3149 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003150 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003151 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3152 (long long)(iptr->isn_arg.number));
3153 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154
Bram Moolenaar389df252020-07-09 21:20:47 +02003155 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3156 iptr->isn_arg.shuffle.shfl_item,
3157 iptr->isn_arg.shuffle.shfl_up);
3158 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003159 case ISN_DROP: smsg("%4d DROP", current); break;
3160 }
3161 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003162}
3163
3164/*
3165 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3166 * list, etc. Mostly like what JavaScript does, except that empty list and
3167 * empty dictionary are FALSE.
3168 */
3169 int
3170tv2bool(typval_T *tv)
3171{
3172 switch (tv->v_type)
3173 {
3174 case VAR_NUMBER:
3175 return tv->vval.v_number != 0;
3176 case VAR_FLOAT:
3177#ifdef FEAT_FLOAT
3178 return tv->vval.v_float != 0.0;
3179#else
3180 break;
3181#endif
3182 case VAR_PARTIAL:
3183 return tv->vval.v_partial != NULL;
3184 case VAR_FUNC:
3185 case VAR_STRING:
3186 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3187 case VAR_LIST:
3188 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3189 case VAR_DICT:
3190 return tv->vval.v_dict != NULL
3191 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3192 case VAR_BOOL:
3193 case VAR_SPECIAL:
3194 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3195 case VAR_JOB:
3196#ifdef FEAT_JOB_CHANNEL
3197 return tv->vval.v_job != NULL;
3198#else
3199 break;
3200#endif
3201 case VAR_CHANNEL:
3202#ifdef FEAT_JOB_CHANNEL
3203 return tv->vval.v_channel != NULL;
3204#else
3205 break;
3206#endif
3207 case VAR_BLOB:
3208 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3209 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003210 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003211 case VAR_VOID:
3212 break;
3213 }
3214 return FALSE;
3215}
3216
3217/*
3218 * If "tv" is a string give an error and return FAIL.
3219 */
3220 int
3221check_not_string(typval_T *tv)
3222{
3223 if (tv->v_type == VAR_STRING)
3224 {
3225 emsg(_("E1030: Using a String as a Number"));
3226 clear_tv(tv);
3227 return FAIL;
3228 }
3229 return OK;
3230}
3231
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003232
3233#endif // FEAT_EVAL