blob: fed7fed0e64faef7d73ca1aa1066074ae18171d1 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
75/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010076 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010077 */
78 static int
79ufunc_argcount(ufunc_T *ufunc)
80{
81 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
82}
83
84/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010085 * Set the instruction index, depending on omitted arguments, where the default
86 * values are to be computed. If all optional arguments are present, start
87 * with the function body.
88 * The expression evaluation is at the start of the instructions:
89 * 0 -> EVAL default1
90 * STORE arg[-2]
91 * 1 -> EVAL default2
92 * STORE arg[-1]
93 * 2 -> function body
94 */
95 static void
96init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
97{
98 if (ufunc->uf_def_args.ga_len == 0)
99 ectx->ec_iidx = 0;
100 else
101 {
102 int defcount = ufunc->uf_args.ga_len - argcount;
103
104 // If there is a varargs argument defcount can be negative, no defaults
105 // to evaluate then.
106 if (defcount < 0)
107 defcount = 0;
108 ectx->ec_iidx = ufunc->uf_def_arg_idx[
109 ufunc->uf_def_args.ga_len - defcount];
110 }
111}
112
113/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200114 * Create a new list from "count" items at the bottom of the stack.
115 * When "count" is zero an empty list is added to the stack.
116 */
117 static int
118exe_newlist(int count, ectx_T *ectx)
119{
120 list_T *list = list_alloc_with_items(count);
121 int idx;
122 typval_T *tv;
123
124 if (list == NULL)
125 return FAIL;
126 for (idx = 0; idx < count; ++idx)
127 list_set_item(list, idx, STACK_TV_BOT(idx - count));
128
129 if (count > 0)
130 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200131 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200132 return FAIL;
133 else
134 ++ectx->ec_stack.ga_len;
135 tv = STACK_TV_BOT(-1);
136 tv->v_type = VAR_LIST;
137 tv->vval.v_list = list;
138 ++list->lv_refcount;
139 return OK;
140}
141
142/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100143 * Call compiled function "cdf_idx" from compiled code.
144 *
145 * Stack has:
146 * - current arguments (already there)
147 * - omitted optional argument (default values) added here
148 * - stack frame:
149 * - pointer to calling function
150 * - Index of next instruction in calling function
151 * - previous frame pointer
152 * - reserved space for local variables
153 */
154 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200155call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100156{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200157 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
159 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200160 int arg_to_add;
161 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200163 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164
165 if (dfunc->df_deleted)
166 {
167 emsg_funcname(e_func_deleted, ufunc->uf_name);
168 return FAIL;
169 }
170
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 if (ufunc->uf_va_name != NULL)
172 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200173 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200174 // Stack at time of call with 2 varargs:
175 // normal_arg
176 // optional_arg
177 // vararg_1
178 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // After creating the list:
180 // normal_arg
181 // optional_arg
182 // vararg-list
183 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list
186 // normal_arg
187 // (space for optional_arg)
188 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200189 vararg_count = argcount - ufunc->uf_args.ga_len;
190 if (vararg_count < 0)
191 vararg_count = 0;
192 else
193 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196
197 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 }
199
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 if (arg_to_add < 0)
202 {
203 iemsg("Argument count wrong?");
204 return FAIL;
205 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200206 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
207 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100208 return FAIL;
209
Bram Moolenaarfe270812020-04-11 22:31:27 +0200210 // Move the vararg-list to below the missing optional arguments.
211 if (vararg_count > 0 && arg_to_add > 0)
212 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100213
214 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200215 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200217 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100218
219 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100220 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
221 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200222 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
223 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200226 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100227 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
229 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Set execution state to the start of the called function.
232 ectx->ec_dfunc_idx = cdf_idx;
233 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200234 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
235 if (entry != NULL)
236 {
237 // Set the script context to the script where the function was defined.
238 // TODO: save more than the SID?
239 entry->es_save_sid = current_sctx.sc_sid;
240 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
241 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100242
243 // Decide where to start execution, handles optional arguments.
244 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100245
246 return OK;
247}
248
249// Get pointer to item in the stack.
250#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
251
252/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200253 * Used when returning from a function: Check if any closure is still
254 * referenced. If so then move the arguments and variables to a separate piece
255 * of stack to be used when the closure is called.
256 * When "free_arguments" is TRUE the arguments are to be freed.
257 * Returns FAIL when out of memory.
258 */
259 static int
260handle_closure_in_use(ectx_T *ectx, int free_arguments)
261{
262 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
263 + ectx->ec_dfunc_idx;
264 int argcount = ufunc_argcount(dfunc->df_ufunc);
265 int top = ectx->ec_frame_idx - argcount;
266 int idx;
267 typval_T *tv;
268 int closure_in_use = FALSE;
269
270 // Check if any created closure is still in use.
271 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
272 {
273 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
274 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200275 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
276 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200277 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200278 int refcount = tv->vval.v_partial->pt_refcount;
279 int i;
280
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200281 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200282 // unreferenced on return.
283 for (i = 0; i < dfunc->df_varcount; ++i)
284 {
285 typval_T *stv = STACK_TV(ectx->ec_frame_idx
286 + STACK_FRAME_SIZE + i);
287 if (stv->v_type == VAR_PARTIAL
288 && tv->vval.v_partial == stv->vval.v_partial)
289 --refcount;
290 }
291 if (refcount > 1)
292 {
293 closure_in_use = TRUE;
294 break;
295 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200296 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200297 }
298
299 if (closure_in_use)
300 {
301 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
302 typval_T *stack;
303
304 // A closure is using the arguments and/or local variables.
305 // Move them to the called function.
306 if (funcstack == NULL)
307 return FAIL;
308 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
309 + dfunc->df_varcount;
310 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
311 funcstack->fs_ga.ga_data = stack;
312 if (stack == NULL)
313 {
314 vim_free(funcstack);
315 return FAIL;
316 }
317
318 // Move or copy the arguments.
319 for (idx = 0; idx < argcount; ++idx)
320 {
321 tv = STACK_TV(top + idx);
322 if (free_arguments)
323 {
324 *(stack + idx) = *tv;
325 tv->v_type = VAR_UNKNOWN;
326 }
327 else
328 copy_tv(tv, stack + idx);
329 }
330 // Move the local variables.
331 for (idx = 0; idx < dfunc->df_varcount; ++idx)
332 {
333 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200334
335 // Do not copy a partial created for a local function.
336 // TODO: this won't work if the closure actually uses it. But when
337 // keeping it it gets complicated: it will create a reference cycle
338 // inside the partial, thus needs special handling for garbage
339 // collection.
340 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
341 {
342 int i;
343 typval_T *ctv;
344
345 for (i = 0; i < dfunc->df_closure_count; ++i)
346 {
347 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
348 + dfunc->df_varcount + i);
349 if (tv->vval.v_partial == ctv->vval.v_partial)
350 break;
351 }
352 if (i < dfunc->df_closure_count)
353 {
354 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
355 VAR_UNKNOWN;
356 continue;
357 }
358 }
359
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
361 tv->v_type = VAR_UNKNOWN;
362 }
363
364 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
365 {
366 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
367 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200369 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200370 partial_T *partial = tv->vval.v_partial;
371
372 if (partial->pt_refcount > 1)
373 {
374 ++funcstack->fs_refcount;
375 partial->pt_funcstack = funcstack;
376 partial->pt_ectx_stack = &funcstack->fs_ga;
377 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
378 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 }
380 }
381 }
382
383 return OK;
384}
385
386/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100387 * Return from the current function.
388 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200389 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100390func_return(ectx_T *ectx)
391{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100392 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200393 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
394 + ectx->ec_dfunc_idx;
395 int argcount = ufunc_argcount(dfunc->df_ufunc);
396 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200397 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398
399 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200400 entry = estack_pop();
401 if (entry != NULL)
402 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100403
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200404 if (handle_closure_in_use(ectx, TRUE) == FAIL)
405 return FAIL;
406
407 // Clear the arguments.
408 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
409 clear_tv(STACK_TV(idx));
410
411 // Clear local variables and temp values, but not the return value.
412 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100413 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100414 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100415
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100416 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200417 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
418 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
419 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
421 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422
423 // Reset the stack to the position before the call, move the return value
424 // to the top of the stack.
425 idx = ectx->ec_stack.ga_len - 1;
426 ectx->ec_stack.ga_len = top + 1;
427 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200428
429 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100430}
431
432#undef STACK_TV
433
434/*
435 * Prepare arguments and rettv for calling a builtin or user function.
436 */
437 static int
438call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
439{
440 int idx;
441 typval_T *tv;
442
443 // Move arguments from bottom of the stack to argvars[] and add terminator.
444 for (idx = 0; idx < argcount; ++idx)
445 argvars[idx] = *STACK_TV_BOT(idx - argcount);
446 argvars[argcount].v_type = VAR_UNKNOWN;
447
448 // Result replaces the arguments on the stack.
449 if (argcount > 0)
450 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200451 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100452 return FAIL;
453 else
454 ++ectx->ec_stack.ga_len;
455
456 // Default return value is zero.
457 tv = STACK_TV_BOT(-1);
458 tv->v_type = VAR_NUMBER;
459 tv->vval.v_number = 0;
460
461 return OK;
462}
463
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200464// Ugly global to avoid passing the execution context around through many
465// layers.
466static ectx_T *current_ectx = NULL;
467
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100468/*
469 * Call a builtin function by index.
470 */
471 static int
472call_bfunc(int func_idx, int argcount, ectx_T *ectx)
473{
474 typval_T argvars[MAX_FUNC_ARGS];
475 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200476 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200477 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100478
479 if (call_prepare(argcount, argvars, ectx) == FAIL)
480 return FAIL;
481
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200482 // Call the builtin function. Set "current_ectx" so that when it
483 // recursively invokes call_def_function() a closure context can be set.
484 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100485 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200486 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100487
488 // Clear the arguments.
489 for (idx = 0; idx < argcount; ++idx)
490 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200491
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200492 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200493 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100494 return OK;
495}
496
497/*
498 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100499 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 */
501 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100502call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100503{
504 typval_T argvars[MAX_FUNC_ARGS];
505 funcexe_T funcexe;
506 int error;
507 int idx;
508
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200509 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200510 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
511 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200512 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100513 {
514 // The function has been compiled, can call it quickly. For a function
515 // that was defined later: we can call it directly next time.
516 if (iptr != NULL)
517 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100518 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100519 iptr->isn_type = ISN_DCALL;
520 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
521 iptr->isn_arg.dfunc.cdf_argcount = argcount;
522 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100524 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100525
526 if (call_prepare(argcount, argvars, ectx) == FAIL)
527 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200528 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100529 funcexe.evaluate = TRUE;
530
531 // Call the user function. Result goes in last position on the stack.
532 // TODO: add selfdict if there is one
533 error = call_user_func_check(ufunc, argcount, argvars,
534 STACK_TV_BOT(-1), &funcexe, NULL);
535
536 // Clear the arguments.
537 for (idx = 0; idx < argcount; ++idx)
538 clear_tv(&argvars[idx]);
539
540 if (error != FCERR_NONE)
541 {
542 user_func_error(error, ufunc->uf_name);
543 return FAIL;
544 }
545 return OK;
546}
547
548/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200549 * Return TRUE if an error was given or CTRL-C was pressed.
550 */
551 static int
552vim9_aborting(int prev_called_emsg)
553{
554 return called_emsg > prev_called_emsg || got_int || did_throw;
555}
556
557/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100558 * Execute a function by "name".
559 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100560 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100561 * Returns FAIL if not found without an error message.
562 */
563 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100564call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100565{
566 ufunc_T *ufunc;
567
568 if (builtin_function(name, -1))
569 {
570 int func_idx = find_internal_func(name);
571
572 if (func_idx < 0)
573 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200574 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575 return FAIL;
576 return call_bfunc(func_idx, argcount, ectx);
577 }
578
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200579 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200580
581 if (ufunc == NULL)
582 {
583 int called_emsg_before = called_emsg;
584
585 if (script_autoload(name, TRUE))
586 // loaded a package, search for the function again
587 ufunc = find_func(name, FALSE, NULL);
588 if (vim9_aborting(called_emsg_before))
589 return FAIL; // bail out if loading the script caused an error
590 }
591
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100593 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594
595 return FAIL;
596}
597
598 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200599call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100600{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200601 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200602 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603 int called_emsg_before = called_emsg;
604
605 if (tv->v_type == VAR_PARTIAL)
606 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200607 partial_T *pt = tv->vval.v_partial;
608 int i;
609
610 if (pt->pt_argc > 0)
611 {
612 // Make space for arguments from the partial, shift the "argcount"
613 // arguments up.
614 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
615 return FAIL;
616 for (i = 1; i <= argcount; ++i)
617 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
618 ectx->ec_stack.ga_len += pt->pt_argc;
619 argcount += pt->pt_argc;
620
621 // copy the arguments from the partial onto the stack
622 for (i = 0; i < pt->pt_argc; ++i)
623 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
624 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625
626 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200627 {
628 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
629
630 // closure may need the function context where it was defined
631 ectx->ec_outer_stack = pt->pt_ectx_stack;
632 ectx->ec_outer_frame = pt->pt_ectx_frame;
633
634 return ret;
635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100636 name = pt->pt_name;
637 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200638 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200640 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641 {
642 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200643 semsg(_(e_unknownfunc),
644 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 return FAIL;
646 }
647 return OK;
648}
649
650/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100651 * Store "tv" in variable "name".
652 * This is for s: and g: variables.
653 */
654 static void
655store_var(char_u *name, typval_T *tv)
656{
657 funccal_entry_T entry;
658
659 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200660 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100661 restore_funccal();
662}
663
Bram Moolenaard3aac292020-04-19 14:32:17 +0200664
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100665/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 * Execute a function by "name".
667 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100668 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 */
670 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100671call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672{
673 int called_emsg_before = called_emsg;
674
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100675 if (call_by_name(name, argcount, ectx, iptr) == FAIL
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 && called_emsg == called_emsg_before)
677 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200678 dictitem_T *v;
679
680 v = find_var(name, NULL, FALSE);
681 if (v == NULL)
682 {
683 semsg(_(e_unknownfunc), name);
684 return FAIL;
685 }
686 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
687 {
688 semsg(_(e_unknownfunc), name);
689 return FAIL;
690 }
691 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 }
693 return OK;
694}
695
696/*
697 * Call a "def" function from old Vim script.
698 * Return OK or FAIL.
699 */
700 int
701call_def_function(
702 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200703 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100704 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200705 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706 typval_T *rettv) // return value
707{
708 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200709 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200710 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 typval_T *tv;
712 int idx;
713 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100714 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200715 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200716 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200717 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718
719// Get pointer to item in the stack.
720#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
721
722// Get pointer to item at the bottom of the stack, -1 is the bottom.
723#undef STACK_TV_BOT
724#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
725
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200726// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200727#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 +0100728
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200729// Like STACK_TV_VAR but use the outer scope
730#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
731
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200732 if (ufunc->uf_def_status == UF_NOT_COMPILED
733 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200734 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200735 {
736 if (called_emsg == called_emsg_before)
737 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200738 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200739 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200740 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200741
Bram Moolenaar09689a02020-05-09 22:50:08 +0200742 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200743 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200744 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
745 + ufunc->uf_dfunc_idx;
746 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200747 {
748 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200749 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200750 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200753 CLEAR_FIELD(ectx);
754 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
755 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
756 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
757 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
759
760 // Put arguments on the stack.
761 for (idx = 0; idx < argc; ++idx)
762 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200763 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200764 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
765 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200766 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 copy_tv(&argv[idx], STACK_TV_BOT(0));
768 ++ectx.ec_stack.ga_len;
769 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200770
771 // Turn varargs into a list. Empty list if no args.
772 if (ufunc->uf_va_name != NULL)
773 {
774 int vararg_count = argc - ufunc->uf_args.ga_len;
775
776 if (vararg_count < 0)
777 vararg_count = 0;
778 else
779 argc -= vararg_count;
780 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200781 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200782
783 // Check the type of the list items.
784 tv = STACK_TV_BOT(-1);
785 if (ufunc->uf_va_type != NULL
786 && ufunc->uf_va_type->tt_member != &t_any
787 && tv->vval.v_list != NULL)
788 {
789 type_T *expected = ufunc->uf_va_type->tt_member;
790 listitem_T *li = tv->vval.v_list->lv_first;
791
792 for (idx = 0; idx < vararg_count; ++idx)
793 {
794 if (check_typval_type(expected, &li->li_tv) == FAIL)
795 goto failed_early;
796 li = li->li_next;
797 }
798 }
799
Bram Moolenaar23e03252020-04-12 22:22:31 +0200800 if (defcount > 0)
801 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200802 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200803 --ectx.ec_stack.ga_len;
804 }
805
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100806 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200807 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100808 if (defcount > 0)
809 for (idx = 0; idx < defcount; ++idx)
810 {
811 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
812 ++ectx.ec_stack.ga_len;
813 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 if (ufunc->uf_va_name != NULL)
815 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816
817 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200818 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
819 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200821 if (partial != NULL)
822 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200823 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
824 {
825 // TODO: is this always the right way?
826 ectx.ec_outer_stack = &current_ectx->ec_stack;
827 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
828 }
829 else
830 {
831 ectx.ec_outer_stack = partial->pt_ectx_stack;
832 ectx.ec_outer_frame = partial->pt_ectx_frame;
833 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200834 }
835
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836 // dummy frame entries
837 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
838 {
839 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
840 ++ectx.ec_stack.ga_len;
841 }
842
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200843 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200844 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200845 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
846 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200847 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100848
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200849 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200850 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200851 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200852
853 ectx.ec_instr = dfunc->df_instr;
854 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100855
Bram Moolenaara26b9702020-04-18 19:53:28 +0200856 // Commands behave like vim9script.
857 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
858
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100859 // Decide where to start execution, handles optional arguments.
860 init_instr_idx(ufunc, argc, &ectx);
861
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 for (;;)
863 {
864 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100865
Bram Moolenaar270d0382020-05-15 21:42:53 +0200866 if (++breakcheck_count >= 100)
867 {
868 line_breakcheck();
869 breakcheck_count = 0;
870 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100871 if (got_int)
872 {
873 // Turn CTRL-C into an exception.
874 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100875 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100876 goto failed;
877 did_throw = TRUE;
878 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879
Bram Moolenaara26b9702020-04-18 19:53:28 +0200880 if (did_emsg && msg_list != NULL && *msg_list != NULL)
881 {
882 // Turn an error message into an exception.
883 did_emsg = FALSE;
884 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
885 goto failed;
886 did_throw = TRUE;
887 *msg_list = NULL;
888 }
889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 if (did_throw && !ectx.ec_in_catch)
891 {
892 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100893 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894
895 // An exception jumps to the first catch, finally, or returns from
896 // the current function.
897 if (trystack->ga_len > 0)
898 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200899 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 {
901 // jump to ":catch" or ":finally"
902 ectx.ec_in_catch = TRUE;
903 ectx.ec_iidx = trycmd->tcd_catch_idx;
904 }
905 else
906 {
907 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200908 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 {
910 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200911 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100912 goto failed;
913 tv = STACK_TV_BOT(0);
914 tv->v_type = VAR_NUMBER;
915 tv->vval.v_number = 0;
916 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100917 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200918 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
919 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 goto done;
921 }
922
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200923 if (func_return(&ectx) == FAIL)
924 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100925 }
926 continue;
927 }
928
929 iptr = &ectx.ec_instr[ectx.ec_iidx++];
930 switch (iptr->isn_type)
931 {
932 // execute Ex command line
933 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200934 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100935 do_cmdline_cmd(iptr->isn_arg.string);
936 break;
937
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200938 // execute Ex command from pieces on the stack
939 case ISN_EXECCONCAT:
940 {
941 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200942 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200943 int pass;
944 int i;
945 char_u *cmd = NULL;
946 char_u *str;
947
948 for (pass = 1; pass <= 2; ++pass)
949 {
950 for (i = 0; i < count; ++i)
951 {
952 tv = STACK_TV_BOT(i - count);
953 str = tv->vval.v_string;
954 if (str != NULL && *str != NUL)
955 {
956 if (pass == 2)
957 STRCPY(cmd + len, str);
958 len += STRLEN(str);
959 }
960 if (pass == 2)
961 clear_tv(tv);
962 }
963 if (pass == 1)
964 {
965 cmd = alloc(len + 1);
966 if (cmd == NULL)
967 goto failed;
968 len = 0;
969 }
970 }
971
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200972 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200973 do_cmdline_cmd(cmd);
974 vim_free(cmd);
975 }
976 break;
977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100978 // execute :echo {string} ...
979 case ISN_ECHO:
980 {
981 int count = iptr->isn_arg.echo.echo_count;
982 int atstart = TRUE;
983 int needclr = TRUE;
984
985 for (idx = 0; idx < count; ++idx)
986 {
987 tv = STACK_TV_BOT(idx - count);
988 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
989 &atstart, &needclr);
990 clear_tv(tv);
991 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +0100992 if (needclr)
993 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100994 ectx.ec_stack.ga_len -= count;
995 }
996 break;
997
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +0200998 // :execute {string} ...
999 // :echomsg {string} ...
1000 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001001 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001002 case ISN_ECHOMSG:
1003 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001004 {
1005 int count = iptr->isn_arg.number;
1006 garray_T ga;
1007 char_u buf[NUMBUFLEN];
1008 char_u *p;
1009 int len;
1010 int failed = FALSE;
1011
1012 ga_init2(&ga, 1, 80);
1013 for (idx = 0; idx < count; ++idx)
1014 {
1015 tv = STACK_TV_BOT(idx - count);
1016 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1017 {
1018 emsg(_(e_inval_string));
1019 break;
1020 }
1021 else
1022 p = tv_get_string_buf(tv, buf);
1023
1024 len = (int)STRLEN(p);
1025 if (ga_grow(&ga, len + 2) == FAIL)
1026 failed = TRUE;
1027 else
1028 {
1029 if (ga.ga_len > 0)
1030 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1031 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1032 ga.ga_len += len;
1033 }
1034 clear_tv(tv);
1035 }
1036 ectx.ec_stack.ga_len -= count;
1037
1038 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001039 {
1040 if (iptr->isn_type == ISN_EXECUTE)
1041 do_cmdline_cmd((char_u *)ga.ga_data);
1042 else
1043 {
1044 msg_sb_eol();
1045 if (iptr->isn_type == ISN_ECHOMSG)
1046 {
1047 msg_attr(ga.ga_data, echo_attr);
1048 out_flush();
1049 }
1050 else
1051 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001052 SOURCING_LNUM = iptr->isn_lnum;
1053 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001054 }
1055 }
1056 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001057 ga_clear(&ga);
1058 }
1059 break;
1060
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001061 // load local variable or argument
1062 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001063 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001064 goto failed;
1065 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1066 ++ectx.ec_stack.ga_len;
1067 break;
1068
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001069 // load variable or argument from outer scope
1070 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001071 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001072 goto failed;
1073 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1074 STACK_TV_BOT(0));
1075 ++ectx.ec_stack.ga_len;
1076 break;
1077
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001078 // load v: variable
1079 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001080 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001081 goto failed;
1082 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1083 ++ectx.ec_stack.ga_len;
1084 break;
1085
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001086 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001087 case ISN_LOADSCRIPT:
1088 {
1089 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001090 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001091 svar_T *sv;
1092
1093 sv = ((svar_T *)si->sn_var_vals.ga_data)
1094 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001095 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 goto failed;
1097 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1098 ++ectx.ec_stack.ga_len;
1099 }
1100 break;
1101
1102 // load s: variable in old script
1103 case ISN_LOADS:
1104 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001105 hashtab_T *ht = &SCRIPT_VARS(
1106 iptr->isn_arg.loadstore.ls_sid);
1107 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 if (di == NULL)
1111 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001112 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001113 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001114 }
1115 else
1116 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001117 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 goto failed;
1119 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1120 ++ectx.ec_stack.ga_len;
1121 }
1122 }
1123 break;
1124
Bram Moolenaard3aac292020-04-19 14:32:17 +02001125 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001127 case ISN_LOADB:
1128 case ISN_LOADW:
1129 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001131 dictitem_T *di = NULL;
1132 hashtab_T *ht = NULL;
1133 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001134
Bram Moolenaard3aac292020-04-19 14:32:17 +02001135 switch (iptr->isn_type)
1136 {
1137 case ISN_LOADG:
1138 ht = get_globvar_ht();
1139 namespace = 'g';
1140 break;
1141 case ISN_LOADB:
1142 ht = &curbuf->b_vars->dv_hashtab;
1143 namespace = 'b';
1144 break;
1145 case ISN_LOADW:
1146 ht = &curwin->w_vars->dv_hashtab;
1147 namespace = 'w';
1148 break;
1149 case ISN_LOADT:
1150 ht = &curtab->tp_vars->dv_hashtab;
1151 namespace = 't';
1152 break;
1153 default: // Cannot reach here
1154 goto failed;
1155 }
1156 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001158 if (di == NULL)
1159 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001160 semsg(_("E121: Undefined variable: %c:%s"),
1161 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001162 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163 }
1164 else
1165 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001166 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 goto failed;
1168 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1169 ++ectx.ec_stack.ga_len;
1170 }
1171 }
1172 break;
1173
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001174 // load g:/b:/w:/t: namespace
1175 case ISN_LOADGDICT:
1176 case ISN_LOADBDICT:
1177 case ISN_LOADWDICT:
1178 case ISN_LOADTDICT:
1179 {
1180 dict_T *d = NULL;
1181
1182 switch (iptr->isn_type)
1183 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001184 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1185 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1186 case ISN_LOADWDICT: d = curwin->w_vars; break;
1187 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001188 default: // Cannot reach here
1189 goto failed;
1190 }
1191 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1192 goto failed;
1193 tv = STACK_TV_BOT(0);
1194 tv->v_type = VAR_DICT;
1195 tv->v_lock = 0;
1196 tv->vval.v_dict = d;
1197 ++ectx.ec_stack.ga_len;
1198 }
1199 break;
1200
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 // load &option
1202 case ISN_LOADOPT:
1203 {
1204 typval_T optval;
1205 char_u *name = iptr->isn_arg.string;
1206
Bram Moolenaara8c17702020-04-01 21:17:24 +02001207 // This is not expected to fail, name is checked during
1208 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001209 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001211 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001212 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001213 *STACK_TV_BOT(0) = optval;
1214 ++ectx.ec_stack.ga_len;
1215 }
1216 break;
1217
1218 // load $ENV
1219 case ISN_LOADENV:
1220 {
1221 typval_T optval;
1222 char_u *name = iptr->isn_arg.string;
1223
Bram Moolenaar270d0382020-05-15 21:42:53 +02001224 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001225 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001226 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001227 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228 *STACK_TV_BOT(0) = optval;
1229 ++ectx.ec_stack.ga_len;
1230 }
1231 break;
1232
1233 // load @register
1234 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001235 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001236 goto failed;
1237 tv = STACK_TV_BOT(0);
1238 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001239 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001240 tv->vval.v_string = get_reg_contents(
1241 iptr->isn_arg.number, GREG_EXPR_SRC);
1242 ++ectx.ec_stack.ga_len;
1243 break;
1244
1245 // store local variable
1246 case ISN_STORE:
1247 --ectx.ec_stack.ga_len;
1248 tv = STACK_TV_VAR(iptr->isn_arg.number);
1249 clear_tv(tv);
1250 *tv = *STACK_TV_BOT(0);
1251 break;
1252
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001253 // store variable or argument in outer scope
1254 case ISN_STOREOUTER:
1255 --ectx.ec_stack.ga_len;
1256 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1257 clear_tv(tv);
1258 *tv = *STACK_TV_BOT(0);
1259 break;
1260
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001261 // store s: variable in old script
1262 case ISN_STORES:
1263 {
1264 hashtab_T *ht = &SCRIPT_VARS(
1265 iptr->isn_arg.loadstore.ls_sid);
1266 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001267 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001268
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001269 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001270 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001271 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001272 else
1273 {
1274 clear_tv(&di->di_tv);
1275 di->di_tv = *STACK_TV_BOT(0);
1276 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001277 }
1278 break;
1279
1280 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 case ISN_STORESCRIPT:
1282 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001283 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001284 iptr->isn_arg.script.script_sid);
1285 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1286 + iptr->isn_arg.script.script_idx;
1287
1288 --ectx.ec_stack.ga_len;
1289 clear_tv(sv->sv_tv);
1290 *sv->sv_tv = *STACK_TV_BOT(0);
1291 }
1292 break;
1293
1294 // store option
1295 case ISN_STOREOPT:
1296 {
1297 long n = 0;
1298 char_u *s = NULL;
1299 char *msg;
1300
1301 --ectx.ec_stack.ga_len;
1302 tv = STACK_TV_BOT(0);
1303 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001304 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001306 if (s == NULL)
1307 s = (char_u *)"";
1308 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001309 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001310 // must be VAR_NUMBER, CHECKTYPE makes sure
1311 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001312 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1313 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001314 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001315 if (msg != NULL)
1316 {
1317 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001318 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 }
1321 break;
1322
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001323 // store $ENV
1324 case ISN_STOREENV:
1325 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001326 tv = STACK_TV_BOT(0);
1327 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1328 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001329 break;
1330
1331 // store @r
1332 case ISN_STOREREG:
1333 {
1334 int reg = iptr->isn_arg.number;
1335
1336 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001337 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001338 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001339 tv_get_string(tv), -1, FALSE);
1340 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001341 }
1342 break;
1343
1344 // store v: variable
1345 case ISN_STOREV:
1346 --ectx.ec_stack.ga_len;
1347 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1348 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001349 // should not happen, type is checked when compiling
1350 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 break;
1352
Bram Moolenaard3aac292020-04-19 14:32:17 +02001353 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001355 case ISN_STOREB:
1356 case ISN_STOREW:
1357 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001358 {
1359 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001360 hashtab_T *ht;
1361 switch (iptr->isn_type)
1362 {
1363 case ISN_STOREG:
1364 ht = get_globvar_ht();
1365 break;
1366 case ISN_STOREB:
1367 ht = &curbuf->b_vars->dv_hashtab;
1368 break;
1369 case ISN_STOREW:
1370 ht = &curwin->w_vars->dv_hashtab;
1371 break;
1372 case ISN_STORET:
1373 ht = &curtab->tp_vars->dv_hashtab;
1374 break;
1375 default: // Cannot reach here
1376 goto failed;
1377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001378
1379 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001380 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001382 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 else
1384 {
1385 clear_tv(&di->di_tv);
1386 di->di_tv = *STACK_TV_BOT(0);
1387 }
1388 }
1389 break;
1390
1391 // store number in local variable
1392 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001393 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 clear_tv(tv);
1395 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001396 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 break;
1398
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001399 // store value in list variable
1400 case ISN_STORELIST:
1401 {
1402 typval_T *tv_idx = STACK_TV_BOT(-2);
1403 varnumber_T lidx = tv_idx->vval.v_number;
1404 typval_T *tv_list = STACK_TV_BOT(-1);
1405 list_T *list = tv_list->vval.v_list;
1406
1407 if (lidx < 0 && list->lv_len + lidx >= 0)
1408 // negative index is relative to the end
1409 lidx = list->lv_len + lidx;
1410 if (lidx < 0 || lidx > list->lv_len)
1411 {
1412 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001413 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001414 }
1415 tv = STACK_TV_BOT(-3);
1416 if (lidx < list->lv_len)
1417 {
1418 listitem_T *li = list_find(list, lidx);
1419
1420 // overwrite existing list item
1421 clear_tv(&li->li_tv);
1422 li->li_tv = *tv;
1423 }
1424 else
1425 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001426 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001427 if (list_append_tv(list, tv) == FAIL)
1428 goto failed;
1429 clear_tv(tv);
1430 }
1431 clear_tv(tv_idx);
1432 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001433 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001434 }
1435 break;
1436
1437 // store value in dict variable
1438 case ISN_STOREDICT:
1439 {
1440 typval_T *tv_key = STACK_TV_BOT(-2);
1441 char_u *key = tv_key->vval.v_string;
1442 typval_T *tv_dict = STACK_TV_BOT(-1);
1443 dict_T *dict = tv_dict->vval.v_dict;
1444 dictitem_T *di;
1445
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001446 if (dict == NULL)
1447 {
1448 emsg(_(e_dictnull));
1449 goto on_error;
1450 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001451 if (key == NULL)
1452 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001453 tv = STACK_TV_BOT(-3);
1454 di = dict_find(dict, key, -1);
1455 if (di != NULL)
1456 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001457 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001458 clear_tv(&di->di_tv);
1459 di->di_tv = *tv;
1460 }
1461 else
1462 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001463 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001464 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1465 goto failed;
1466 clear_tv(tv);
1467 }
1468 clear_tv(tv_key);
1469 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001470 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001471 }
1472 break;
1473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 // push constant
1475 case ISN_PUSHNR:
1476 case ISN_PUSHBOOL:
1477 case ISN_PUSHSPEC:
1478 case ISN_PUSHF:
1479 case ISN_PUSHS:
1480 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001481 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001482 case ISN_PUSHCHANNEL:
1483 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001484 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 goto failed;
1486 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001487 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 ++ectx.ec_stack.ga_len;
1489 switch (iptr->isn_type)
1490 {
1491 case ISN_PUSHNR:
1492 tv->v_type = VAR_NUMBER;
1493 tv->vval.v_number = iptr->isn_arg.number;
1494 break;
1495 case ISN_PUSHBOOL:
1496 tv->v_type = VAR_BOOL;
1497 tv->vval.v_number = iptr->isn_arg.number;
1498 break;
1499 case ISN_PUSHSPEC:
1500 tv->v_type = VAR_SPECIAL;
1501 tv->vval.v_number = iptr->isn_arg.number;
1502 break;
1503#ifdef FEAT_FLOAT
1504 case ISN_PUSHF:
1505 tv->v_type = VAR_FLOAT;
1506 tv->vval.v_float = iptr->isn_arg.fnumber;
1507 break;
1508#endif
1509 case ISN_PUSHBLOB:
1510 blob_copy(iptr->isn_arg.blob, tv);
1511 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001512 case ISN_PUSHFUNC:
1513 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001514 if (iptr->isn_arg.string == NULL)
1515 tv->vval.v_string = NULL;
1516 else
1517 tv->vval.v_string =
1518 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001519 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001520 case ISN_PUSHCHANNEL:
1521#ifdef FEAT_JOB_CHANNEL
1522 tv->v_type = VAR_CHANNEL;
1523 tv->vval.v_channel = iptr->isn_arg.channel;
1524 if (tv->vval.v_channel != NULL)
1525 ++tv->vval.v_channel->ch_refcount;
1526#endif
1527 break;
1528 case ISN_PUSHJOB:
1529#ifdef FEAT_JOB_CHANNEL
1530 tv->v_type = VAR_JOB;
1531 tv->vval.v_job = iptr->isn_arg.job;
1532 if (tv->vval.v_job != NULL)
1533 ++tv->vval.v_job->jv_refcount;
1534#endif
1535 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 default:
1537 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001538 tv->vval.v_string = vim_strsave(
1539 iptr->isn_arg.string == NULL
1540 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001541 }
1542 break;
1543
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001544 case ISN_UNLET:
1545 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1546 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001547 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001548 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001549 case ISN_UNLETENV:
1550 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1551 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001552
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 // create a list from items on the stack; uses a single allocation
1554 // for the list header and the items
1555 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001556 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1557 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001558 break;
1559
1560 // create a dict from items on the stack
1561 case ISN_NEWDICT:
1562 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001563 int count = iptr->isn_arg.number;
1564 dict_T *dict = dict_alloc();
1565 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566
1567 if (dict == NULL)
1568 goto failed;
1569 for (idx = 0; idx < count; ++idx)
1570 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001571 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001573 // check key is unique
1574 item = dict_find(dict, tv->vval.v_string, -1);
1575 if (item != NULL)
1576 {
1577 semsg(_(e_duplicate_key), tv->vval.v_string);
1578 dict_unref(dict);
1579 goto on_error;
1580 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001581 item = dictitem_alloc(tv->vval.v_string);
1582 clear_tv(tv);
1583 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001584 {
1585 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001586 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001587 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1589 item->di_tv.v_lock = 0;
1590 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001591 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001592 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001593 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001595 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 }
1597
1598 if (count > 0)
1599 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001600 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 goto failed;
1602 else
1603 ++ectx.ec_stack.ga_len;
1604 tv = STACK_TV_BOT(-1);
1605 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001606 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 tv->vval.v_dict = dict;
1608 ++dict->dv_refcount;
1609 }
1610 break;
1611
1612 // call a :def function
1613 case ISN_DCALL:
1614 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1615 iptr->isn_arg.dfunc.cdf_argcount,
1616 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001617 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 break;
1619
1620 // call a builtin function
1621 case ISN_BCALL:
1622 SOURCING_LNUM = iptr->isn_lnum;
1623 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1624 iptr->isn_arg.bfunc.cbf_argcount,
1625 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001626 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 break;
1628
1629 // call a funcref or partial
1630 case ISN_PCALL:
1631 {
1632 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1633 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001634 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635
1636 SOURCING_LNUM = iptr->isn_lnum;
1637 if (pfunc->cpf_top)
1638 {
1639 // funcref is above the arguments
1640 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1641 }
1642 else
1643 {
1644 // Get the funcref from the stack.
1645 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001646 partial_tv = *STACK_TV_BOT(0);
1647 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 }
1649 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001650 if (tv == &partial_tv)
1651 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001653 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 }
1655 break;
1656
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001657 case ISN_PCALL_END:
1658 // PCALL finished, arguments have been consumed and replaced by
1659 // the return value. Now clear the funcref from the stack,
1660 // and move the return value in its place.
1661 --ectx.ec_stack.ga_len;
1662 clear_tv(STACK_TV_BOT(-1));
1663 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1664 break;
1665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 // call a user defined function or funcref/partial
1667 case ISN_UCALL:
1668 {
1669 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1670
1671 SOURCING_LNUM = iptr->isn_lnum;
1672 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001673 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001674 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 }
1676 break;
1677
1678 // return from a :def function call
1679 case ISN_RETURN:
1680 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001681 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001682 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001683
1684 if (trystack->ga_len > 0)
1685 trycmd = ((trycmd_T *)trystack->ga_data)
1686 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001687 if (trycmd != NULL
1688 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 && trycmd->tcd_finally_idx != 0)
1690 {
1691 // jump to ":finally"
1692 ectx.ec_iidx = trycmd->tcd_finally_idx;
1693 trycmd->tcd_return = TRUE;
1694 }
1695 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001696 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001697 }
1698 break;
1699
1700 // push a function reference to a compiled function
1701 case ISN_FUNCREF:
1702 {
1703 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001704 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001705
1706 pt = ALLOC_CLEAR_ONE(partial_T);
1707 if (pt == NULL)
1708 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001709 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001710 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001711 vim_free(pt);
1712 goto failed;
1713 }
1714 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1715 + iptr->isn_arg.funcref.fr_func;
1716 pt->pt_func = pt_dfunc->df_ufunc;
1717 pt->pt_refcount = 1;
1718 ++pt_dfunc->df_ufunc->uf_refcount;
1719
1720 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1721 {
1722 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1723 + ectx.ec_dfunc_idx;
1724
1725 // The closure needs to find arguments and local
1726 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001727 pt->pt_ectx_stack = &ectx.ec_stack;
1728 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001729
1730 // If this function returns and the closure is still
1731 // used, we need to make a copy of the context
1732 // (arguments and local variables). Store a reference
1733 // to the partial so we can handle that.
1734 ++pt->pt_refcount;
1735 tv = STACK_TV_VAR(dfunc->df_varcount
1736 + iptr->isn_arg.funcref.fr_var_idx);
1737 if (tv->v_type == VAR_PARTIAL)
1738 {
1739 // TODO: use a garray_T on ectx.
1740 emsg("Multiple closures not supported yet");
1741 goto failed;
1742 }
1743 tv->v_type = VAR_PARTIAL;
1744 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001745 }
1746
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001747 tv = STACK_TV_BOT(0);
1748 ++ectx.ec_stack.ga_len;
1749 tv->vval.v_partial = pt;
1750 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001751 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 }
1753 break;
1754
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001755 // Create a global function from a lambda.
1756 case ISN_NEWFUNC:
1757 {
1758 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1759
1760 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1761 }
1762 break;
1763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 // jump if a condition is met
1765 case ISN_JUMP:
1766 {
1767 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1768 int jump = TRUE;
1769
1770 if (when != JUMP_ALWAYS)
1771 {
1772 tv = STACK_TV_BOT(-1);
1773 jump = tv2bool(tv);
1774 if (when == JUMP_IF_FALSE
1775 || when == JUMP_AND_KEEP_IF_FALSE)
1776 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001777 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 {
1779 // drop the value from the stack
1780 clear_tv(tv);
1781 --ectx.ec_stack.ga_len;
1782 }
1783 }
1784 if (jump)
1785 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1786 }
1787 break;
1788
1789 // top of a for loop
1790 case ISN_FOR:
1791 {
1792 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1793 typval_T *idxtv =
1794 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1795
1796 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001797 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 goto failed;
1799 if (++idxtv->vval.v_number >= list->lv_len)
1800 // past the end of the list, jump to "endfor"
1801 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1802 else if (list->lv_first == &range_list_item)
1803 {
1804 // non-materialized range() list
1805 tv = STACK_TV_BOT(0);
1806 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001807 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 tv->vval.v_number = list_find_nr(
1809 list, idxtv->vval.v_number, NULL);
1810 ++ectx.ec_stack.ga_len;
1811 }
1812 else
1813 {
1814 listitem_T *li = list_find(list, idxtv->vval.v_number);
1815
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1817 ++ectx.ec_stack.ga_len;
1818 }
1819 }
1820 break;
1821
1822 // start of ":try" block
1823 case ISN_TRY:
1824 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001825 trycmd_T *trycmd = NULL;
1826
Bram Moolenaar270d0382020-05-15 21:42:53 +02001827 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 goto failed;
1829 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1830 + ectx.ec_trystack.ga_len;
1831 ++ectx.ec_trystack.ga_len;
1832 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001833 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1835 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001836 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 }
1838 break;
1839
1840 case ISN_PUSHEXC:
1841 if (current_exception == NULL)
1842 {
1843 iemsg("Evaluating catch while current_exception is NULL");
1844 goto failed;
1845 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001846 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 goto failed;
1848 tv = STACK_TV_BOT(0);
1849 ++ectx.ec_stack.ga_len;
1850 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001851 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 tv->vval.v_string = vim_strsave(
1853 (char_u *)current_exception->value);
1854 break;
1855
1856 case ISN_CATCH:
1857 {
1858 garray_T *trystack = &ectx.ec_trystack;
1859
1860 if (trystack->ga_len > 0)
1861 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001862 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 + trystack->ga_len - 1;
1864 trycmd->tcd_caught = TRUE;
1865 }
1866 did_emsg = got_int = did_throw = FALSE;
1867 catch_exception(current_exception);
1868 }
1869 break;
1870
1871 // end of ":try" block
1872 case ISN_ENDTRY:
1873 {
1874 garray_T *trystack = &ectx.ec_trystack;
1875
1876 if (trystack->ga_len > 0)
1877 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001878 trycmd_T *trycmd = NULL;
1879
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 --trystack->ga_len;
1881 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001882 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001883 trycmd = ((trycmd_T *)trystack->ga_data)
1884 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001885 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 {
1887 // discard the exception
1888 if (caught_stack == current_exception)
1889 caught_stack = caught_stack->caught;
1890 discard_current_exception();
1891 }
1892
1893 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001894 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 }
1896 }
1897 break;
1898
1899 case ISN_THROW:
1900 --ectx.ec_stack.ga_len;
1901 tv = STACK_TV_BOT(0);
1902 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1903 {
1904 vim_free(tv->vval.v_string);
1905 goto failed;
1906 }
1907 did_throw = TRUE;
1908 break;
1909
1910 // compare with special values
1911 case ISN_COMPAREBOOL:
1912 case ISN_COMPARESPECIAL:
1913 {
1914 typval_T *tv1 = STACK_TV_BOT(-2);
1915 typval_T *tv2 = STACK_TV_BOT(-1);
1916 varnumber_T arg1 = tv1->vval.v_number;
1917 varnumber_T arg2 = tv2->vval.v_number;
1918 int res;
1919
1920 switch (iptr->isn_arg.op.op_type)
1921 {
1922 case EXPR_EQUAL: res = arg1 == arg2; break;
1923 case EXPR_NEQUAL: res = arg1 != arg2; break;
1924 default: res = 0; break;
1925 }
1926
1927 --ectx.ec_stack.ga_len;
1928 tv1->v_type = VAR_BOOL;
1929 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1930 }
1931 break;
1932
1933 // Operation with two number arguments
1934 case ISN_OPNR:
1935 case ISN_COMPARENR:
1936 {
1937 typval_T *tv1 = STACK_TV_BOT(-2);
1938 typval_T *tv2 = STACK_TV_BOT(-1);
1939 varnumber_T arg1 = tv1->vval.v_number;
1940 varnumber_T arg2 = tv2->vval.v_number;
1941 varnumber_T res;
1942
1943 switch (iptr->isn_arg.op.op_type)
1944 {
1945 case EXPR_MULT: res = arg1 * arg2; break;
1946 case EXPR_DIV: res = arg1 / arg2; break;
1947 case EXPR_REM: res = arg1 % arg2; break;
1948 case EXPR_SUB: res = arg1 - arg2; break;
1949 case EXPR_ADD: res = arg1 + arg2; break;
1950
1951 case EXPR_EQUAL: res = arg1 == arg2; break;
1952 case EXPR_NEQUAL: res = arg1 != arg2; break;
1953 case EXPR_GREATER: res = arg1 > arg2; break;
1954 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1955 case EXPR_SMALLER: res = arg1 < arg2; break;
1956 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1957 default: res = 0; break;
1958 }
1959
1960 --ectx.ec_stack.ga_len;
1961 if (iptr->isn_type == ISN_COMPARENR)
1962 {
1963 tv1->v_type = VAR_BOOL;
1964 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1965 }
1966 else
1967 tv1->vval.v_number = res;
1968 }
1969 break;
1970
1971 // Computation with two float arguments
1972 case ISN_OPFLOAT:
1973 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001974#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 {
1976 typval_T *tv1 = STACK_TV_BOT(-2);
1977 typval_T *tv2 = STACK_TV_BOT(-1);
1978 float_T arg1 = tv1->vval.v_float;
1979 float_T arg2 = tv2->vval.v_float;
1980 float_T res = 0;
1981 int cmp = FALSE;
1982
1983 switch (iptr->isn_arg.op.op_type)
1984 {
1985 case EXPR_MULT: res = arg1 * arg2; break;
1986 case EXPR_DIV: res = arg1 / arg2; break;
1987 case EXPR_SUB: res = arg1 - arg2; break;
1988 case EXPR_ADD: res = arg1 + arg2; break;
1989
1990 case EXPR_EQUAL: cmp = arg1 == arg2; break;
1991 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
1992 case EXPR_GREATER: cmp = arg1 > arg2; break;
1993 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
1994 case EXPR_SMALLER: cmp = arg1 < arg2; break;
1995 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
1996 default: cmp = 0; break;
1997 }
1998 --ectx.ec_stack.ga_len;
1999 if (iptr->isn_type == ISN_COMPAREFLOAT)
2000 {
2001 tv1->v_type = VAR_BOOL;
2002 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2003 }
2004 else
2005 tv1->vval.v_float = res;
2006 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002007#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002008 break;
2009
2010 case ISN_COMPARELIST:
2011 {
2012 typval_T *tv1 = STACK_TV_BOT(-2);
2013 typval_T *tv2 = STACK_TV_BOT(-1);
2014 list_T *arg1 = tv1->vval.v_list;
2015 list_T *arg2 = tv2->vval.v_list;
2016 int cmp = FALSE;
2017 int ic = iptr->isn_arg.op.op_ic;
2018
2019 switch (iptr->isn_arg.op.op_type)
2020 {
2021 case EXPR_EQUAL: cmp =
2022 list_equal(arg1, arg2, ic, FALSE); break;
2023 case EXPR_NEQUAL: cmp =
2024 !list_equal(arg1, arg2, ic, FALSE); break;
2025 case EXPR_IS: cmp = arg1 == arg2; break;
2026 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2027 default: cmp = 0; break;
2028 }
2029 --ectx.ec_stack.ga_len;
2030 clear_tv(tv1);
2031 clear_tv(tv2);
2032 tv1->v_type = VAR_BOOL;
2033 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2034 }
2035 break;
2036
2037 case ISN_COMPAREBLOB:
2038 {
2039 typval_T *tv1 = STACK_TV_BOT(-2);
2040 typval_T *tv2 = STACK_TV_BOT(-1);
2041 blob_T *arg1 = tv1->vval.v_blob;
2042 blob_T *arg2 = tv2->vval.v_blob;
2043 int cmp = FALSE;
2044
2045 switch (iptr->isn_arg.op.op_type)
2046 {
2047 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2048 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2049 case EXPR_IS: cmp = arg1 == arg2; break;
2050 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2051 default: cmp = 0; break;
2052 }
2053 --ectx.ec_stack.ga_len;
2054 clear_tv(tv1);
2055 clear_tv(tv2);
2056 tv1->v_type = VAR_BOOL;
2057 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2058 }
2059 break;
2060
2061 // TODO: handle separately
2062 case ISN_COMPARESTRING:
2063 case ISN_COMPAREDICT:
2064 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065 case ISN_COMPAREANY:
2066 {
2067 typval_T *tv1 = STACK_TV_BOT(-2);
2068 typval_T *tv2 = STACK_TV_BOT(-1);
2069 exptype_T exptype = iptr->isn_arg.op.op_type;
2070 int ic = iptr->isn_arg.op.op_ic;
2071
2072 typval_compare(tv1, tv2, exptype, ic);
2073 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002074 --ectx.ec_stack.ga_len;
2075 }
2076 break;
2077
2078 case ISN_ADDLIST:
2079 case ISN_ADDBLOB:
2080 {
2081 typval_T *tv1 = STACK_TV_BOT(-2);
2082 typval_T *tv2 = STACK_TV_BOT(-1);
2083
2084 if (iptr->isn_type == ISN_ADDLIST)
2085 eval_addlist(tv1, tv2);
2086 else
2087 eval_addblob(tv1, tv2);
2088 clear_tv(tv2);
2089 --ectx.ec_stack.ga_len;
2090 }
2091 break;
2092
2093 // Computation with two arguments of unknown type
2094 case ISN_OPANY:
2095 {
2096 typval_T *tv1 = STACK_TV_BOT(-2);
2097 typval_T *tv2 = STACK_TV_BOT(-1);
2098 varnumber_T n1, n2;
2099#ifdef FEAT_FLOAT
2100 float_T f1 = 0, f2 = 0;
2101#endif
2102 int error = FALSE;
2103
2104 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2105 {
2106 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2107 {
2108 eval_addlist(tv1, tv2);
2109 clear_tv(tv2);
2110 --ectx.ec_stack.ga_len;
2111 break;
2112 }
2113 else if (tv1->v_type == VAR_BLOB
2114 && tv2->v_type == VAR_BLOB)
2115 {
2116 eval_addblob(tv1, tv2);
2117 clear_tv(tv2);
2118 --ectx.ec_stack.ga_len;
2119 break;
2120 }
2121 }
2122#ifdef FEAT_FLOAT
2123 if (tv1->v_type == VAR_FLOAT)
2124 {
2125 f1 = tv1->vval.v_float;
2126 n1 = 0;
2127 }
2128 else
2129#endif
2130 {
2131 n1 = tv_get_number_chk(tv1, &error);
2132 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002133 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002134#ifdef FEAT_FLOAT
2135 if (tv2->v_type == VAR_FLOAT)
2136 f1 = n1;
2137#endif
2138 }
2139#ifdef FEAT_FLOAT
2140 if (tv2->v_type == VAR_FLOAT)
2141 {
2142 f2 = tv2->vval.v_float;
2143 n2 = 0;
2144 }
2145 else
2146#endif
2147 {
2148 n2 = tv_get_number_chk(tv2, &error);
2149 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002150 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151#ifdef FEAT_FLOAT
2152 if (tv1->v_type == VAR_FLOAT)
2153 f2 = n2;
2154#endif
2155 }
2156#ifdef FEAT_FLOAT
2157 // if there is a float on either side the result is a float
2158 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2159 {
2160 switch (iptr->isn_arg.op.op_type)
2161 {
2162 case EXPR_MULT: f1 = f1 * f2; break;
2163 case EXPR_DIV: f1 = f1 / f2; break;
2164 case EXPR_SUB: f1 = f1 - f2; break;
2165 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002166 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002167 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002168 }
2169 clear_tv(tv1);
2170 clear_tv(tv2);
2171 tv1->v_type = VAR_FLOAT;
2172 tv1->vval.v_float = f1;
2173 --ectx.ec_stack.ga_len;
2174 }
2175 else
2176#endif
2177 {
2178 switch (iptr->isn_arg.op.op_type)
2179 {
2180 case EXPR_MULT: n1 = n1 * n2; break;
2181 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2182 case EXPR_SUB: n1 = n1 - n2; break;
2183 case EXPR_ADD: n1 = n1 + n2; break;
2184 default: n1 = num_modulus(n1, n2); break;
2185 }
2186 clear_tv(tv1);
2187 clear_tv(tv2);
2188 tv1->v_type = VAR_NUMBER;
2189 tv1->vval.v_number = n1;
2190 --ectx.ec_stack.ga_len;
2191 }
2192 }
2193 break;
2194
2195 case ISN_CONCAT:
2196 {
2197 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2198 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2199 char_u *res;
2200
2201 res = concat_str(str1, str2);
2202 clear_tv(STACK_TV_BOT(-2));
2203 clear_tv(STACK_TV_BOT(-1));
2204 --ectx.ec_stack.ga_len;
2205 STACK_TV_BOT(-1)->vval.v_string = res;
2206 }
2207 break;
2208
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002209 case ISN_STRINDEX:
2210 {
2211 char_u *s;
2212 varnumber_T n;
2213 char_u *res;
2214
2215 // string index: string is at stack-2, index at stack-1
2216 tv = STACK_TV_BOT(-2);
2217 if (tv->v_type != VAR_STRING)
2218 {
2219 emsg(_(e_stringreq));
2220 goto on_error;
2221 }
2222 s = tv->vval.v_string;
2223
2224 tv = STACK_TV_BOT(-1);
2225 if (tv->v_type != VAR_NUMBER)
2226 {
2227 emsg(_(e_number_exp));
2228 goto on_error;
2229 }
2230 n = tv->vval.v_number;
2231
2232 // The resulting variable is a string of a single
2233 // character. If the index is too big or negative the
2234 // result is empty.
2235 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2236 res = NULL;
2237 else
2238 res = vim_strnsave(s + n, 1);
2239 --ectx.ec_stack.ga_len;
2240 tv = STACK_TV_BOT(-1);
2241 vim_free(tv->vval.v_string);
2242 tv->vval.v_string = res;
2243 }
2244 break;
2245
2246 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 {
2248 list_T *list;
2249 varnumber_T n;
2250 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002251 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252
2253 // list index: list is at stack-2, index at stack-1
2254 tv = STACK_TV_BOT(-2);
2255 if (tv->v_type != VAR_LIST)
2256 {
2257 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002258 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002259 }
2260 list = tv->vval.v_list;
2261
2262 tv = STACK_TV_BOT(-1);
2263 if (tv->v_type != VAR_NUMBER)
2264 {
2265 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002266 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002267 }
2268 n = tv->vval.v_number;
2269 clear_tv(tv);
2270 if ((li = list_find(list, n)) == NULL)
2271 {
2272 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002273 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 }
2275 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002276 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002277 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002278 tv = STACK_TV_BOT(-1);
2279 temp_tv = *tv;
2280 copy_tv(&li->li_tv, tv);
2281 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282 }
2283 break;
2284
Bram Moolenaar9af78762020-06-16 11:34:42 +02002285 case ISN_SLICE:
2286 {
2287 list_T *list;
2288 int count = iptr->isn_arg.number;
2289
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002290 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002291 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002292 list = tv->vval.v_list;
2293
2294 // no error for short list, expect it to be checked earlier
2295 if (list != NULL && list->lv_len >= count)
2296 {
2297 list_T *newlist = list_slice(list,
2298 count, list->lv_len - 1);
2299
2300 if (newlist != NULL)
2301 {
2302 list_unref(list);
2303 tv->vval.v_list = newlist;
2304 ++newlist->lv_refcount;
2305 }
2306 }
2307 }
2308 break;
2309
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002310 case ISN_GETITEM:
2311 {
2312 listitem_T *li;
2313 int index = iptr->isn_arg.number;
2314
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002315 // Get list item: list is at stack-1, push item.
2316 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002317 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002318 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002319
2320 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2321 goto failed;
2322 ++ectx.ec_stack.ga_len;
2323 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2324 }
2325 break;
2326
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 case ISN_MEMBER:
2328 {
2329 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002330 char_u *key;
2331 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002332 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002333
2334 // dict member: dict is at stack-2, key at stack-1
2335 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002336 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002337 dict = tv->vval.v_dict;
2338
2339 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002340 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002341 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002342
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002343 if ((di = dict_find(dict, key, -1)) == NULL)
2344 {
2345 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002346 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002347 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002348 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002349 --ectx.ec_stack.ga_len;
2350 // Clear the dict after getting the item, to avoid that it
2351 // make the item invalid.
2352 tv = STACK_TV_BOT(-1);
2353 temp_tv = *tv;
2354 copy_tv(&di->di_tv, tv);
2355 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002356 }
2357 break;
2358
2359 // dict member with string key
2360 case ISN_STRINGMEMBER:
2361 {
2362 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002363 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002364 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365
2366 tv = STACK_TV_BOT(-1);
2367 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2368 {
2369 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002370 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002371 }
2372 dict = tv->vval.v_dict;
2373
2374 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2375 == NULL)
2376 {
2377 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002378 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002380 // Clear the dict after getting the item, to avoid that it
2381 // make the item invalid.
2382 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002384 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002385 }
2386 break;
2387
2388 case ISN_NEGATENR:
2389 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002390 if (tv->v_type != VAR_NUMBER
2391#ifdef FEAT_FLOAT
2392 && tv->v_type != VAR_FLOAT
2393#endif
2394 )
2395 {
2396 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002397 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002398 }
2399#ifdef FEAT_FLOAT
2400 if (tv->v_type == VAR_FLOAT)
2401 tv->vval.v_float = -tv->vval.v_float;
2402 else
2403#endif
2404 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 break;
2406
2407 case ISN_CHECKNR:
2408 {
2409 int error = FALSE;
2410
2411 tv = STACK_TV_BOT(-1);
2412 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002413 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002414 (void)tv_get_number_chk(tv, &error);
2415 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002416 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 }
2418 break;
2419
2420 case ISN_CHECKTYPE:
2421 {
2422 checktype_T *ct = &iptr->isn_arg.type;
2423
2424 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002425 // TODO: better type comparison
2426 if (tv->v_type != ct->ct_type
2427 && !((tv->v_type == VAR_PARTIAL
2428 && ct->ct_type == VAR_FUNC)
2429 || (tv->v_type == VAR_FUNC
2430 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002431 {
2432 semsg(_("E1029: Expected %s but got %s"),
2433 vartype_name(ct->ct_type),
2434 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002435 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 }
2437 }
2438 break;
2439
Bram Moolenaar9af78762020-06-16 11:34:42 +02002440 case ISN_CHECKLEN:
2441 {
2442 int min_len = iptr->isn_arg.checklen.cl_min_len;
2443 list_T *list = NULL;
2444
2445 tv = STACK_TV_BOT(-1);
2446 if (tv->v_type == VAR_LIST)
2447 list = tv->vval.v_list;
2448 if (list == NULL || list->lv_len < min_len
2449 || (list->lv_len > min_len
2450 && !iptr->isn_arg.checklen.cl_more_OK))
2451 {
2452 semsg(_("E1093: Expected %d items but got %d"),
2453 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002454 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002455 }
2456 }
2457 break;
2458
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002459 case ISN_2BOOL:
2460 {
2461 int n;
2462
2463 tv = STACK_TV_BOT(-1);
2464 n = tv2bool(tv);
2465 if (iptr->isn_arg.number) // invert
2466 n = !n;
2467 clear_tv(tv);
2468 tv->v_type = VAR_BOOL;
2469 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2470 }
2471 break;
2472
2473 case ISN_2STRING:
2474 {
2475 char_u *str;
2476
2477 tv = STACK_TV_BOT(iptr->isn_arg.number);
2478 if (tv->v_type != VAR_STRING)
2479 {
2480 str = typval_tostring(tv);
2481 clear_tv(tv);
2482 tv->v_type = VAR_STRING;
2483 tv->vval.v_string = str;
2484 }
2485 }
2486 break;
2487
Bram Moolenaar389df252020-07-09 21:20:47 +02002488 case ISN_SHUFFLE:
2489 {
2490 typval_T tmp_tv;
2491 int item = iptr->isn_arg.shuffle.shfl_item;
2492 int up = iptr->isn_arg.shuffle.shfl_up;
2493
2494 tmp_tv = *STACK_TV_BOT(-item);
2495 for ( ; up > 0 && item > 1; --up)
2496 {
2497 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2498 --item;
2499 }
2500 *STACK_TV_BOT(-item) = tmp_tv;
2501 }
2502 break;
2503
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 case ISN_DROP:
2505 --ectx.ec_stack.ga_len;
2506 clear_tv(STACK_TV_BOT(0));
2507 break;
2508 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002509 continue;
2510
Bram Moolenaard032f342020-07-18 18:13:02 +02002511func_return:
2512 // Restore previous function. If the frame pointer is zero then there
2513 // is none and we are done.
2514 if (ectx.ec_frame_idx == initial_frame_idx)
2515 {
2516 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2517 // only fails when out of memory
2518 goto failed;
2519 goto done;
2520 }
2521 if (func_return(&ectx) == FAIL)
2522 // only fails when out of memory
2523 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002524 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002525
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002526on_error:
2527 if (trylevel == 0)
2528 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 }
2530
2531done:
2532 // function finished, get result from the stack.
2533 tv = STACK_TV_BOT(-1);
2534 *rettv = *tv;
2535 tv->v_type = VAR_UNKNOWN;
2536 ret = OK;
2537
2538failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002539 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002540 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002541 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002542failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002543 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002544
2545 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2547 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002548
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002550 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002551
2552 if (ret != OK && called_emsg == called_emsg_before)
2553 semsg(_("E1099: Unknown error while executing %s"),
2554 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 return ret;
2556}
2557
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558/*
2559 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002560 * We don't really need this at runtime, but we do have tests that require it,
2561 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 */
2563 void
2564ex_disassemble(exarg_T *eap)
2565{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002566 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002567 char_u *fname;
2568 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 dfunc_T *dfunc;
2570 isn_T *instr;
2571 int current;
2572 int line_idx = 0;
2573 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002574 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002576 if (STRNCMP(arg, "<lambda>", 8) == 0)
2577 {
2578 arg += 8;
2579 (void)getdigits(&arg);
2580 fname = vim_strnsave(eap->arg, arg - eap->arg);
2581 }
2582 else
2583 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002584 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002585 if (fname == NULL)
2586 {
2587 semsg(_(e_invarg2), eap->arg);
2588 return;
2589 }
2590
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002591 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002592 if (ufunc == NULL)
2593 {
2594 char_u *p = untrans_function_name(fname);
2595
2596 if (p != NULL)
2597 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002598 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002599 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002600 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 if (ufunc == NULL)
2602 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002603 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 return;
2605 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002606 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002607 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2608 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002609 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002610 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002611 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002612 return;
2613 }
2614 if (ufunc->uf_name_exp != NULL)
2615 msg((char *)ufunc->uf_name_exp);
2616 else
2617 msg((char *)ufunc->uf_name);
2618
2619 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2620 instr = dfunc->df_instr;
2621 for (current = 0; current < dfunc->df_instr_count; ++current)
2622 {
2623 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002624 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625
2626 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2627 {
2628 if (current > prev_current)
2629 {
2630 msg_puts("\n\n");
2631 prev_current = current;
2632 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002633 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2634 if (line != NULL)
2635 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002636 }
2637
2638 switch (iptr->isn_type)
2639 {
2640 case ISN_EXEC:
2641 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2642 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002643 case ISN_EXECCONCAT:
2644 smsg("%4d EXECCONCAT %lld", current,
2645 (long long)iptr->isn_arg.number);
2646 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 case ISN_ECHO:
2648 {
2649 echo_T *echo = &iptr->isn_arg.echo;
2650
2651 smsg("%4d %s %d", current,
2652 echo->echo_with_white ? "ECHO" : "ECHON",
2653 echo->echo_count);
2654 }
2655 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002656 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002657 smsg("%4d EXECUTE %lld", current,
2658 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002659 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002660 case ISN_ECHOMSG:
2661 smsg("%4d ECHOMSG %lld", current,
2662 (long long)(iptr->isn_arg.number));
2663 break;
2664 case ISN_ECHOERR:
2665 smsg("%4d ECHOERR %lld", current,
2666 (long long)(iptr->isn_arg.number));
2667 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002669 case ISN_LOADOUTER:
2670 {
2671 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2672
2673 if (iptr->isn_arg.number < 0)
2674 smsg("%4d LOAD%s arg[%lld]", current, add,
2675 (long long)(iptr->isn_arg.number
2676 + STACK_FRAME_SIZE));
2677 else
2678 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002679 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002680 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002681 break;
2682 case ISN_LOADV:
2683 smsg("%4d LOADV v:%s", current,
2684 get_vim_var_name(iptr->isn_arg.number));
2685 break;
2686 case ISN_LOADSCRIPT:
2687 {
2688 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002689 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002690 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2691 + iptr->isn_arg.script.script_idx;
2692
2693 smsg("%4d LOADSCRIPT %s from %s", current,
2694 sv->sv_name, si->sn_name);
2695 }
2696 break;
2697 case ISN_LOADS:
2698 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002699 scriptitem_T *si = SCRIPT_ITEM(
2700 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701
2702 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002703 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002704 }
2705 break;
2706 case ISN_LOADG:
2707 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2708 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002709 case ISN_LOADB:
2710 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2711 break;
2712 case ISN_LOADW:
2713 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2714 break;
2715 case ISN_LOADT:
2716 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2717 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002718 case ISN_LOADGDICT:
2719 smsg("%4d LOAD g:", current);
2720 break;
2721 case ISN_LOADBDICT:
2722 smsg("%4d LOAD b:", current);
2723 break;
2724 case ISN_LOADWDICT:
2725 smsg("%4d LOAD w:", current);
2726 break;
2727 case ISN_LOADTDICT:
2728 smsg("%4d LOAD t:", current);
2729 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 case ISN_LOADOPT:
2731 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2732 break;
2733 case ISN_LOADENV:
2734 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2735 break;
2736 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002737 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 break;
2739
2740 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002741 case ISN_STOREOUTER:
2742 {
2743 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2744
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002745 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002746 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002747 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002748 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002749 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002750 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002751 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002752 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002753 case ISN_STOREV:
2754 smsg("%4d STOREV v:%s", current,
2755 get_vim_var_name(iptr->isn_arg.number));
2756 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002757 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002758 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2759 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002760 case ISN_STOREB:
2761 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2762 break;
2763 case ISN_STOREW:
2764 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2765 break;
2766 case ISN_STORET:
2767 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2768 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002769 case ISN_STORES:
2770 {
2771 scriptitem_T *si = SCRIPT_ITEM(
2772 iptr->isn_arg.loadstore.ls_sid);
2773
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002774 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002775 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 break;
2778 case ISN_STORESCRIPT:
2779 {
2780 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002781 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2783 + iptr->isn_arg.script.script_idx;
2784
2785 smsg("%4d STORESCRIPT %s in %s", current,
2786 sv->sv_name, si->sn_name);
2787 }
2788 break;
2789 case ISN_STOREOPT:
2790 smsg("%4d STOREOPT &%s", current,
2791 iptr->isn_arg.storeopt.so_name);
2792 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002793 case ISN_STOREENV:
2794 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2795 break;
2796 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002797 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002798 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 case ISN_STORENR:
2800 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002801 iptr->isn_arg.storenr.stnr_val,
2802 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 break;
2804
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002805 case ISN_STORELIST:
2806 smsg("%4d STORELIST", current);
2807 break;
2808
2809 case ISN_STOREDICT:
2810 smsg("%4d STOREDICT", current);
2811 break;
2812
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002813 // constants
2814 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002815 smsg("%4d PUSHNR %lld", current,
2816 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 break;
2818 case ISN_PUSHBOOL:
2819 case ISN_PUSHSPEC:
2820 smsg("%4d PUSH %s", current,
2821 get_var_special_name(iptr->isn_arg.number));
2822 break;
2823 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002824#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002825 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002826#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 break;
2828 case ISN_PUSHS:
2829 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2830 break;
2831 case ISN_PUSHBLOB:
2832 {
2833 char_u *r;
2834 char_u numbuf[NUMBUFLEN];
2835 char_u *tofree;
2836
2837 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002838 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 vim_free(tofree);
2840 }
2841 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002842 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002843 {
2844 char *name = (char *)iptr->isn_arg.string;
2845
2846 smsg("%4d PUSHFUNC \"%s\"", current,
2847 name == NULL ? "[none]" : name);
2848 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002849 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002850 case ISN_PUSHCHANNEL:
2851#ifdef FEAT_JOB_CHANNEL
2852 {
2853 channel_T *channel = iptr->isn_arg.channel;
2854
2855 smsg("%4d PUSHCHANNEL %d", current,
2856 channel == NULL ? 0 : channel->ch_id);
2857 }
2858#endif
2859 break;
2860 case ISN_PUSHJOB:
2861#ifdef FEAT_JOB_CHANNEL
2862 {
2863 typval_T tv;
2864 char_u *name;
2865
2866 tv.v_type = VAR_JOB;
2867 tv.vval.v_job = iptr->isn_arg.job;
2868 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002869 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002870 }
2871#endif
2872 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 case ISN_PUSHEXC:
2874 smsg("%4d PUSH v:exception", current);
2875 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002876 case ISN_UNLET:
2877 smsg("%4d UNLET%s %s", current,
2878 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2879 iptr->isn_arg.unlet.ul_name);
2880 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002881 case ISN_UNLETENV:
2882 smsg("%4d UNLETENV%s $%s", current,
2883 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2884 iptr->isn_arg.unlet.ul_name);
2885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002887 smsg("%4d NEWLIST size %lld", current,
2888 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002889 break;
2890 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002891 smsg("%4d NEWDICT size %lld", current,
2892 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002893 break;
2894
2895 // function call
2896 case ISN_BCALL:
2897 {
2898 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2899
2900 smsg("%4d BCALL %s(argc %d)", current,
2901 internal_func_name(cbfunc->cbf_idx),
2902 cbfunc->cbf_argcount);
2903 }
2904 break;
2905 case ISN_DCALL:
2906 {
2907 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2908 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2909 + cdfunc->cdf_idx;
2910
2911 smsg("%4d DCALL %s(argc %d)", current,
2912 df->df_ufunc->uf_name_exp != NULL
2913 ? df->df_ufunc->uf_name_exp
2914 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2915 }
2916 break;
2917 case ISN_UCALL:
2918 {
2919 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2920
2921 smsg("%4d UCALL %s(argc %d)", current,
2922 cufunc->cuf_name, cufunc->cuf_argcount);
2923 }
2924 break;
2925 case ISN_PCALL:
2926 {
2927 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2928
2929 smsg("%4d PCALL%s (argc %d)", current,
2930 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2931 }
2932 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002933 case ISN_PCALL_END:
2934 smsg("%4d PCALL end", current);
2935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002936 case ISN_RETURN:
2937 smsg("%4d RETURN", current);
2938 break;
2939 case ISN_FUNCREF:
2940 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002941 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002943 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002945 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002946 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 }
2948 break;
2949
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002950 case ISN_NEWFUNC:
2951 {
2952 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2953
2954 smsg("%4d NEWFUNC %s %s", current,
2955 newfunc->nf_lambda, newfunc->nf_global);
2956 }
2957 break;
2958
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002959 case ISN_JUMP:
2960 {
2961 char *when = "?";
2962
2963 switch (iptr->isn_arg.jump.jump_when)
2964 {
2965 case JUMP_ALWAYS:
2966 when = "JUMP";
2967 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968 case JUMP_AND_KEEP_IF_TRUE:
2969 when = "JUMP_AND_KEEP_IF_TRUE";
2970 break;
2971 case JUMP_IF_FALSE:
2972 when = "JUMP_IF_FALSE";
2973 break;
2974 case JUMP_AND_KEEP_IF_FALSE:
2975 when = "JUMP_AND_KEEP_IF_FALSE";
2976 break;
2977 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01002978 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002979 iptr->isn_arg.jump.jump_where);
2980 }
2981 break;
2982
2983 case ISN_FOR:
2984 {
2985 forloop_T *forloop = &iptr->isn_arg.forloop;
2986
2987 smsg("%4d FOR $%d -> %d", current,
2988 forloop->for_idx, forloop->for_end);
2989 }
2990 break;
2991
2992 case ISN_TRY:
2993 {
2994 try_T *try = &iptr->isn_arg.try;
2995
2996 smsg("%4d TRY catch -> %d, finally -> %d", current,
2997 try->try_catch, try->try_finally);
2998 }
2999 break;
3000 case ISN_CATCH:
3001 // TODO
3002 smsg("%4d CATCH", current);
3003 break;
3004 case ISN_ENDTRY:
3005 smsg("%4d ENDTRY", current);
3006 break;
3007 case ISN_THROW:
3008 smsg("%4d THROW", current);
3009 break;
3010
3011 // expression operations on number
3012 case ISN_OPNR:
3013 case ISN_OPFLOAT:
3014 case ISN_OPANY:
3015 {
3016 char *what;
3017 char *ins;
3018
3019 switch (iptr->isn_arg.op.op_type)
3020 {
3021 case EXPR_MULT: what = "*"; break;
3022 case EXPR_DIV: what = "/"; break;
3023 case EXPR_REM: what = "%"; break;
3024 case EXPR_SUB: what = "-"; break;
3025 case EXPR_ADD: what = "+"; break;
3026 default: what = "???"; break;
3027 }
3028 switch (iptr->isn_type)
3029 {
3030 case ISN_OPNR: ins = "OPNR"; break;
3031 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3032 case ISN_OPANY: ins = "OPANY"; break;
3033 default: ins = "???"; break;
3034 }
3035 smsg("%4d %s %s", current, ins, what);
3036 }
3037 break;
3038
3039 case ISN_COMPAREBOOL:
3040 case ISN_COMPARESPECIAL:
3041 case ISN_COMPARENR:
3042 case ISN_COMPAREFLOAT:
3043 case ISN_COMPARESTRING:
3044 case ISN_COMPAREBLOB:
3045 case ISN_COMPARELIST:
3046 case ISN_COMPAREDICT:
3047 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003048 case ISN_COMPAREANY:
3049 {
3050 char *p;
3051 char buf[10];
3052 char *type;
3053
3054 switch (iptr->isn_arg.op.op_type)
3055 {
3056 case EXPR_EQUAL: p = "=="; break;
3057 case EXPR_NEQUAL: p = "!="; break;
3058 case EXPR_GREATER: p = ">"; break;
3059 case EXPR_GEQUAL: p = ">="; break;
3060 case EXPR_SMALLER: p = "<"; break;
3061 case EXPR_SEQUAL: p = "<="; break;
3062 case EXPR_MATCH: p = "=~"; break;
3063 case EXPR_IS: p = "is"; break;
3064 case EXPR_ISNOT: p = "isnot"; break;
3065 case EXPR_NOMATCH: p = "!~"; break;
3066 default: p = "???"; break;
3067 }
3068 STRCPY(buf, p);
3069 if (iptr->isn_arg.op.op_ic == TRUE)
3070 strcat(buf, "?");
3071 switch(iptr->isn_type)
3072 {
3073 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3074 case ISN_COMPARESPECIAL:
3075 type = "COMPARESPECIAL"; break;
3076 case ISN_COMPARENR: type = "COMPARENR"; break;
3077 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3078 case ISN_COMPARESTRING:
3079 type = "COMPARESTRING"; break;
3080 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3081 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3082 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3083 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3085 default: type = "???"; break;
3086 }
3087
3088 smsg("%4d %s %s", current, type, buf);
3089 }
3090 break;
3091
3092 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3093 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3094
3095 // expression operations
3096 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003097 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3098 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003099 case ISN_SLICE: smsg("%4d SLICE %lld",
3100 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003101 case ISN_GETITEM: smsg("%4d ITEM %lld",
3102 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003103 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3104 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003105 iptr->isn_arg.string); break;
3106 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3107
3108 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3109 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3110 vartype_name(iptr->isn_arg.type.ct_type),
3111 iptr->isn_arg.type.ct_off);
3112 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003113 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3114 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3115 iptr->isn_arg.checklen.cl_min_len);
3116 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003117 case ISN_2BOOL: if (iptr->isn_arg.number)
3118 smsg("%4d INVERT (!val)", current);
3119 else
3120 smsg("%4d 2BOOL (!!val)", current);
3121 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003122 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3123 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003124 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003125
Bram Moolenaar389df252020-07-09 21:20:47 +02003126 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3127 iptr->isn_arg.shuffle.shfl_item,
3128 iptr->isn_arg.shuffle.shfl_up);
3129 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 case ISN_DROP: smsg("%4d DROP", current); break;
3131 }
3132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003133}
3134
3135/*
3136 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3137 * list, etc. Mostly like what JavaScript does, except that empty list and
3138 * empty dictionary are FALSE.
3139 */
3140 int
3141tv2bool(typval_T *tv)
3142{
3143 switch (tv->v_type)
3144 {
3145 case VAR_NUMBER:
3146 return tv->vval.v_number != 0;
3147 case VAR_FLOAT:
3148#ifdef FEAT_FLOAT
3149 return tv->vval.v_float != 0.0;
3150#else
3151 break;
3152#endif
3153 case VAR_PARTIAL:
3154 return tv->vval.v_partial != NULL;
3155 case VAR_FUNC:
3156 case VAR_STRING:
3157 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3158 case VAR_LIST:
3159 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3160 case VAR_DICT:
3161 return tv->vval.v_dict != NULL
3162 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3163 case VAR_BOOL:
3164 case VAR_SPECIAL:
3165 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3166 case VAR_JOB:
3167#ifdef FEAT_JOB_CHANNEL
3168 return tv->vval.v_job != NULL;
3169#else
3170 break;
3171#endif
3172 case VAR_CHANNEL:
3173#ifdef FEAT_JOB_CHANNEL
3174 return tv->vval.v_channel != NULL;
3175#else
3176 break;
3177#endif
3178 case VAR_BLOB:
3179 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3180 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003181 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 case VAR_VOID:
3183 break;
3184 }
3185 return FALSE;
3186}
3187
3188/*
3189 * If "tv" is a string give an error and return FAIL.
3190 */
3191 int
3192check_not_string(typval_T *tv)
3193{
3194 if (tv->v_type == VAR_STRING)
3195 {
3196 emsg(_("E1030: Using a String as a Number"));
3197 clear_tv(tv);
3198 return FAIL;
3199 }
3200 return OK;
3201}
3202
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003203
3204#endif // FEAT_EVAL