blob: 8575e584a413e5a79bb7b1c01c552a4532591e75 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
70} ectx_T;
71
72// Get pointer to item relative to the bottom of the stack, -1 is the last one.
73#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + idx)
74
Bram Moolenaar418f1df2020-08-12 21:34:49 +020075 void
76to_string_error(vartype_T vartype)
77{
78 semsg(_("E1105: Cannot convert %s to string"), vartype_name(vartype));
79}
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010082 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083 */
84 static int
85ufunc_argcount(ufunc_T *ufunc)
86{
87 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
88}
89
90/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Set the instruction index, depending on omitted arguments, where the default
92 * values are to be computed. If all optional arguments are present, start
93 * with the function body.
94 * The expression evaluation is at the start of the instructions:
95 * 0 -> EVAL default1
96 * STORE arg[-2]
97 * 1 -> EVAL default2
98 * STORE arg[-1]
99 * 2 -> function body
100 */
101 static void
102init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
103{
104 if (ufunc->uf_def_args.ga_len == 0)
105 ectx->ec_iidx = 0;
106 else
107 {
108 int defcount = ufunc->uf_args.ga_len - argcount;
109
110 // If there is a varargs argument defcount can be negative, no defaults
111 // to evaluate then.
112 if (defcount < 0)
113 defcount = 0;
114 ectx->ec_iidx = ufunc->uf_def_arg_idx[
115 ufunc->uf_def_args.ga_len - defcount];
116 }
117}
118
119/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200120 * Create a new list from "count" items at the bottom of the stack.
121 * When "count" is zero an empty list is added to the stack.
122 */
123 static int
124exe_newlist(int count, ectx_T *ectx)
125{
126 list_T *list = list_alloc_with_items(count);
127 int idx;
128 typval_T *tv;
129
130 if (list == NULL)
131 return FAIL;
132 for (idx = 0; idx < count; ++idx)
133 list_set_item(list, idx, STACK_TV_BOT(idx - count));
134
135 if (count > 0)
136 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200137 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200138 return FAIL;
139 else
140 ++ectx->ec_stack.ga_len;
141 tv = STACK_TV_BOT(-1);
142 tv->v_type = VAR_LIST;
143 tv->vval.v_list = list;
144 ++list->lv_refcount;
145 return OK;
146}
147
148/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100149 * Call compiled function "cdf_idx" from compiled code.
150 *
151 * Stack has:
152 * - current arguments (already there)
153 * - omitted optional argument (default values) added here
154 * - stack frame:
155 * - pointer to calling function
156 * - Index of next instruction in calling function
157 * - previous frame pointer
158 * - reserved space for local variables
159 */
160 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200161call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
165 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200166 int arg_to_add;
167 int vararg_count = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200169 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100170
171 if (dfunc->df_deleted)
172 {
173 emsg_funcname(e_func_deleted, ufunc->uf_name);
174 return FAIL;
175 }
176
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200177 if (ufunc->uf_va_name != NULL)
178 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200179 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 // Stack at time of call with 2 varargs:
181 // normal_arg
182 // optional_arg
183 // vararg_1
184 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200185 // After creating the list:
186 // normal_arg
187 // optional_arg
188 // vararg-list
189 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200190 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200191 // After creating the list
192 // normal_arg
193 // (space for optional_arg)
194 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 vararg_count = argcount - ufunc->uf_args.ga_len;
196 if (vararg_count < 0)
197 vararg_count = 0;
198 else
199 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200200 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200201 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202
203 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 }
205
Bram Moolenaarfe270812020-04-11 22:31:27 +0200206 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 if (arg_to_add < 0)
208 {
209 iemsg("Argument count wrong?");
210 return FAIL;
211 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200212 if (ga_grow(&ectx->ec_stack, arg_to_add + 3
213 + dfunc->df_varcount + dfunc->df_closure_count) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100214 return FAIL;
215
Bram Moolenaarfe270812020-04-11 22:31:27 +0200216 // Move the vararg-list to below the missing optional arguments.
217 if (vararg_count > 0 && arg_to_add > 0)
218 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100219
220 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200221 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200222 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200223 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100224
225 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100226 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
227 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200228 STACK_TV_BOT(2)->vval.v_number = ectx->ec_frame_idx;
229 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230
231 // Initialize local variables
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200232 for (idx = 0; idx < dfunc->df_varcount + dfunc->df_closure_count; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100233 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200234 ectx->ec_stack.ga_len += STACK_FRAME_SIZE
235 + dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100236
237 // Set execution state to the start of the called function.
238 ectx->ec_dfunc_idx = cdf_idx;
239 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200240 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
241 if (entry != NULL)
242 {
243 // Set the script context to the script where the function was defined.
244 // TODO: save more than the SID?
245 entry->es_save_sid = current_sctx.sc_sid;
246 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
247 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100248
249 // Decide where to start execution, handles optional arguments.
250 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100251
252 return OK;
253}
254
255// Get pointer to item in the stack.
256#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
257
258/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200259 * Used when returning from a function: Check if any closure is still
260 * referenced. If so then move the arguments and variables to a separate piece
261 * of stack to be used when the closure is called.
262 * When "free_arguments" is TRUE the arguments are to be freed.
263 * Returns FAIL when out of memory.
264 */
265 static int
266handle_closure_in_use(ectx_T *ectx, int free_arguments)
267{
268 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
269 + ectx->ec_dfunc_idx;
270 int argcount = ufunc_argcount(dfunc->df_ufunc);
271 int top = ectx->ec_frame_idx - argcount;
272 int idx;
273 typval_T *tv;
274 int closure_in_use = FALSE;
275
276 // Check if any created closure is still in use.
277 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
278 {
279 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
280 + dfunc->df_varcount + idx);
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200281 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
282 && tv->vval.v_partial->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200283 {
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200284 int refcount = tv->vval.v_partial->pt_refcount;
285 int i;
286
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200287 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200288 // unreferenced on return.
289 for (i = 0; i < dfunc->df_varcount; ++i)
290 {
291 typval_T *stv = STACK_TV(ectx->ec_frame_idx
292 + STACK_FRAME_SIZE + i);
293 if (stv->v_type == VAR_PARTIAL
294 && tv->vval.v_partial == stv->vval.v_partial)
295 --refcount;
296 }
297 if (refcount > 1)
298 {
299 closure_in_use = TRUE;
300 break;
301 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200302 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303 }
304
305 if (closure_in_use)
306 {
307 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
308 typval_T *stack;
309
310 // A closure is using the arguments and/or local variables.
311 // Move them to the called function.
312 if (funcstack == NULL)
313 return FAIL;
314 funcstack->fs_ga.ga_len = argcount + STACK_FRAME_SIZE
315 + dfunc->df_varcount;
316 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
317 funcstack->fs_ga.ga_data = stack;
318 if (stack == NULL)
319 {
320 vim_free(funcstack);
321 return FAIL;
322 }
323
324 // Move or copy the arguments.
325 for (idx = 0; idx < argcount; ++idx)
326 {
327 tv = STACK_TV(top + idx);
328 if (free_arguments)
329 {
330 *(stack + idx) = *tv;
331 tv->v_type = VAR_UNKNOWN;
332 }
333 else
334 copy_tv(tv, stack + idx);
335 }
336 // Move the local variables.
337 for (idx = 0; idx < dfunc->df_varcount; ++idx)
338 {
339 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200340
341 // Do not copy a partial created for a local function.
342 // TODO: this won't work if the closure actually uses it. But when
343 // keeping it it gets complicated: it will create a reference cycle
344 // inside the partial, thus needs special handling for garbage
345 // collection.
346 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
347 {
348 int i;
349 typval_T *ctv;
350
351 for (i = 0; i < dfunc->df_closure_count; ++i)
352 {
353 ctv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
354 + dfunc->df_varcount + i);
355 if (tv->vval.v_partial == ctv->vval.v_partial)
356 break;
357 }
358 if (i < dfunc->df_closure_count)
359 {
360 (stack + argcount + STACK_FRAME_SIZE + idx)->v_type =
361 VAR_UNKNOWN;
362 continue;
363 }
364 }
365
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366 *(stack + argcount + STACK_FRAME_SIZE + idx) = *tv;
367 tv->v_type = VAR_UNKNOWN;
368 }
369
370 for (idx = 0; idx < dfunc->df_closure_count; ++idx)
371 {
372 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE
373 + dfunc->df_varcount + idx);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 if (tv->v_type == VAR_PARTIAL)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 {
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200376 partial_T *partial = tv->vval.v_partial;
377
378 if (partial->pt_refcount > 1)
379 {
380 ++funcstack->fs_refcount;
381 partial->pt_funcstack = funcstack;
382 partial->pt_ectx_stack = &funcstack->fs_ga;
383 partial->pt_ectx_frame = ectx->ec_frame_idx - top;
384 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200385 }
386 }
387 }
388
389 return OK;
390}
391
392/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100393 * Return from the current function.
394 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200395 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100396func_return(ectx_T *ectx)
397{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100398 int idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
400 + ectx->ec_dfunc_idx;
401 int argcount = ufunc_argcount(dfunc->df_ufunc);
402 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200403 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100404
405 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200406 entry = estack_pop();
407 if (entry != NULL)
408 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100409
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200410 if (handle_closure_in_use(ectx, TRUE) == FAIL)
411 return FAIL;
412
413 // Clear the arguments.
414 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
415 clear_tv(STACK_TV(idx));
416
417 // Clear local variables and temp values, but not the return value.
418 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100419 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100420 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100421
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100422 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
424 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
425 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 2)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100426 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
427 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100428
429 // Reset the stack to the position before the call, move the return value
430 // to the top of the stack.
431 idx = ectx->ec_stack.ga_len - 1;
432 ectx->ec_stack.ga_len = top + 1;
433 *STACK_TV_BOT(-1) = *STACK_TV(idx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200434
435 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100436}
437
438#undef STACK_TV
439
440/*
441 * Prepare arguments and rettv for calling a builtin or user function.
442 */
443 static int
444call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
445{
446 int idx;
447 typval_T *tv;
448
449 // Move arguments from bottom of the stack to argvars[] and add terminator.
450 for (idx = 0; idx < argcount; ++idx)
451 argvars[idx] = *STACK_TV_BOT(idx - argcount);
452 argvars[argcount].v_type = VAR_UNKNOWN;
453
454 // Result replaces the arguments on the stack.
455 if (argcount > 0)
456 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200457 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100458 return FAIL;
459 else
460 ++ectx->ec_stack.ga_len;
461
462 // Default return value is zero.
463 tv = STACK_TV_BOT(-1);
464 tv->v_type = VAR_NUMBER;
465 tv->vval.v_number = 0;
466
467 return OK;
468}
469
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200470// Ugly global to avoid passing the execution context around through many
471// layers.
472static ectx_T *current_ectx = NULL;
473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100474/*
475 * Call a builtin function by index.
476 */
477 static int
478call_bfunc(int func_idx, int argcount, ectx_T *ectx)
479{
480 typval_T argvars[MAX_FUNC_ARGS];
481 int idx;
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200482 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200483 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484
485 if (call_prepare(argcount, argvars, ectx) == FAIL)
486 return FAIL;
487
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200488 // Call the builtin function. Set "current_ectx" so that when it
489 // recursively invokes call_def_function() a closure context can be set.
490 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100491 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200492 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100493
494 // Clear the arguments.
495 for (idx = 0; idx < argcount; ++idx)
496 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200497
Bram Moolenaar8a1c1012020-05-07 14:07:25 +0200498 if (did_emsg != did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200499 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 return OK;
501}
502
503/*
504 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100505 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 */
507 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100508call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509{
510 typval_T argvars[MAX_FUNC_ARGS];
511 funcexe_T funcexe;
512 int error;
513 int idx;
Bram Moolenaared677f52020-08-12 16:38:10 +0200514 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100515
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200516 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200517 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
518 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200519 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100520 {
521 // The function has been compiled, can call it quickly. For a function
522 // that was defined later: we can call it directly next time.
523 if (iptr != NULL)
524 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100525 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100526 iptr->isn_type = ISN_DCALL;
527 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
528 iptr->isn_arg.dfunc.cdf_argcount = argcount;
529 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532
533 if (call_prepare(argcount, argvars, ectx) == FAIL)
534 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200535 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100536 funcexe.evaluate = TRUE;
537
538 // Call the user function. Result goes in last position on the stack.
539 // TODO: add selfdict if there is one
540 error = call_user_func_check(ufunc, argcount, argvars,
541 STACK_TV_BOT(-1), &funcexe, NULL);
542
543 // Clear the arguments.
544 for (idx = 0; idx < argcount; ++idx)
545 clear_tv(&argvars[idx]);
546
547 if (error != FCERR_NONE)
548 {
549 user_func_error(error, ufunc->uf_name);
550 return FAIL;
551 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200552 if (called_emsg > called_emsg_before)
553 // Error other than from calling the function itself.
554 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 return OK;
556}
557
558/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200559 * Return TRUE if an error was given or CTRL-C was pressed.
560 */
561 static int
562vim9_aborting(int prev_called_emsg)
563{
564 return called_emsg > prev_called_emsg || got_int || did_throw;
565}
566
567/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568 * Execute a function by "name".
569 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100570 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571 * Returns FAIL if not found without an error message.
572 */
573 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100574call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100575{
576 ufunc_T *ufunc;
577
578 if (builtin_function(name, -1))
579 {
580 int func_idx = find_internal_func(name);
581
582 if (func_idx < 0)
583 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200584 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100585 return FAIL;
586 return call_bfunc(func_idx, argcount, ectx);
587 }
588
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200589 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200590
591 if (ufunc == NULL)
592 {
593 int called_emsg_before = called_emsg;
594
595 if (script_autoload(name, TRUE))
596 // loaded a package, search for the function again
597 ufunc = find_func(name, FALSE, NULL);
598 if (vim9_aborting(called_emsg_before))
599 return FAIL; // bail out if loading the script caused an error
600 }
601
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100603 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100604
605 return FAIL;
606}
607
608 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200609call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100610{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200611 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200612 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613 int called_emsg_before = called_emsg;
614
615 if (tv->v_type == VAR_PARTIAL)
616 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200617 partial_T *pt = tv->vval.v_partial;
618 int i;
619
620 if (pt->pt_argc > 0)
621 {
622 // Make space for arguments from the partial, shift the "argcount"
623 // arguments up.
624 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
625 return FAIL;
626 for (i = 1; i <= argcount; ++i)
627 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
628 ectx->ec_stack.ga_len += pt->pt_argc;
629 argcount += pt->pt_argc;
630
631 // copy the arguments from the partial onto the stack
632 for (i = 0; i < pt->pt_argc; ++i)
633 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
634 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635
636 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200637 {
638 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
639
640 // closure may need the function context where it was defined
641 ectx->ec_outer_stack = pt->pt_ectx_stack;
642 ectx->ec_outer_frame = pt->pt_ectx_frame;
643
644 return ret;
645 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646 name = pt->pt_name;
647 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200648 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100649 name = tv->vval.v_string;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200650 if (name == NULL || call_by_name(name, argcount, ectx, NULL) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651 {
652 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200653 semsg(_(e_unknownfunc),
654 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100655 return FAIL;
656 }
657 return OK;
658}
659
660/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100661 * Store "tv" in variable "name".
662 * This is for s: and g: variables.
663 */
664 static void
665store_var(char_u *name, typval_T *tv)
666{
667 funccal_entry_T entry;
668
669 save_funccal(&entry);
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +0200670 set_var_const(name, NULL, tv, FALSE, LET_NO_COMMAND);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100671 restore_funccal();
672}
673
Bram Moolenaard3aac292020-04-19 14:32:17 +0200674
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100675/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100676 * Execute a function by "name".
677 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100678 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 */
680 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682{
Bram Moolenaared677f52020-08-12 16:38:10 +0200683 int called_emsg_before = called_emsg;
684 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100685
Bram Moolenaared677f52020-08-12 16:38:10 +0200686 res = call_by_name(name, argcount, ectx, iptr);
687 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100688 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200689 dictitem_T *v;
690
691 v = find_var(name, NULL, FALSE);
692 if (v == NULL)
693 {
694 semsg(_(e_unknownfunc), name);
695 return FAIL;
696 }
697 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
698 {
699 semsg(_(e_unknownfunc), name);
700 return FAIL;
701 }
702 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200704 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100705}
706
707/*
708 * Call a "def" function from old Vim script.
709 * Return OK or FAIL.
710 */
711 int
712call_def_function(
713 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200714 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200716 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717 typval_T *rettv) // return value
718{
719 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200720 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200721 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722 typval_T *tv;
723 int idx;
724 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100725 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaara26b9702020-04-18 19:53:28 +0200726 int save_sc_version = current_sctx.sc_version;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200727 int breakcheck_count = 0;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200728 int called_emsg_before = called_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730// Get pointer to item in the stack.
731#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
732
733// Get pointer to item at the bottom of the stack, -1 is the bottom.
734#undef STACK_TV_BOT
735#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
736
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200737// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200738#define STACK_TV_VAR(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_frame_idx + STACK_FRAME_SIZE + idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100739
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200740// Like STACK_TV_VAR but use the outer scope
741#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
742
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200743 if (ufunc->uf_def_status == UF_NOT_COMPILED
744 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200745 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200746 {
747 if (called_emsg == called_emsg_before)
748 semsg(_("E1091: Function is not compiled: %s"),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200749 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200750 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200751 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200752
Bram Moolenaar09689a02020-05-09 22:50:08 +0200753 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200754 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200755 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
756 + ufunc->uf_dfunc_idx;
757 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200758 {
759 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200760 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200761 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200762 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200764 CLEAR_FIELD(ectx);
765 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
766 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
767 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
768 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
770
771 // Put arguments on the stack.
772 for (idx = 0; idx < argc; ++idx)
773 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200774 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar4cdb13c2020-07-22 21:45:14 +0200775 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx])
776 == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200777 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100778 copy_tv(&argv[idx], STACK_TV_BOT(0));
779 ++ectx.ec_stack.ga_len;
780 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200781
782 // Turn varargs into a list. Empty list if no args.
783 if (ufunc->uf_va_name != NULL)
784 {
785 int vararg_count = argc - ufunc->uf_args.ga_len;
786
787 if (vararg_count < 0)
788 vararg_count = 0;
789 else
790 argc -= vararg_count;
791 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200792 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200793
794 // Check the type of the list items.
795 tv = STACK_TV_BOT(-1);
796 if (ufunc->uf_va_type != NULL
797 && ufunc->uf_va_type->tt_member != &t_any
798 && tv->vval.v_list != NULL)
799 {
800 type_T *expected = ufunc->uf_va_type->tt_member;
801 listitem_T *li = tv->vval.v_list->lv_first;
802
803 for (idx = 0; idx < vararg_count; ++idx)
804 {
805 if (check_typval_type(expected, &li->li_tv) == FAIL)
806 goto failed_early;
807 li = li->li_next;
808 }
809 }
810
Bram Moolenaar23e03252020-04-12 22:22:31 +0200811 if (defcount > 0)
812 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200813 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +0200814 --ectx.ec_stack.ga_len;
815 }
816
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100817 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +0200818 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100819 if (defcount > 0)
820 for (idx = 0; idx < defcount; ++idx)
821 {
822 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
823 ++ectx.ec_stack.ga_len;
824 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200825 if (ufunc->uf_va_name != NULL)
826 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827
828 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200829 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
830 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100831
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200832 if (partial != NULL)
833 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200834 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
835 {
836 // TODO: is this always the right way?
837 ectx.ec_outer_stack = &current_ectx->ec_stack;
838 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
839 }
840 else
841 {
842 ectx.ec_outer_stack = partial->pt_ectx_stack;
843 ectx.ec_outer_frame = partial->pt_ectx_frame;
844 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200845 }
846
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100847 // dummy frame entries
848 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
849 {
850 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
851 ++ectx.ec_stack.ga_len;
852 }
853
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200854 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200855 // Reserve space for local variables and closure references.
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200856 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
857 + ufunc->uf_dfunc_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200858 int count = dfunc->df_varcount + dfunc->df_closure_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100859
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200860 for (idx = 0; idx < count; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200861 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200862 ectx.ec_stack.ga_len += count;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200863
864 ectx.ec_instr = dfunc->df_instr;
865 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100866
Bram Moolenaara26b9702020-04-18 19:53:28 +0200867 // Commands behave like vim9script.
868 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
869
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100870 // Decide where to start execution, handles optional arguments.
871 init_instr_idx(ufunc, argc, &ectx);
872
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873 for (;;)
874 {
875 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100876
Bram Moolenaar270d0382020-05-15 21:42:53 +0200877 if (++breakcheck_count >= 100)
878 {
879 line_breakcheck();
880 breakcheck_count = 0;
881 }
Bram Moolenaar20431c92020-03-20 18:39:46 +0100882 if (got_int)
883 {
884 // Turn CTRL-C into an exception.
885 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +0100886 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +0100887 goto failed;
888 did_throw = TRUE;
889 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890
Bram Moolenaara26b9702020-04-18 19:53:28 +0200891 if (did_emsg && msg_list != NULL && *msg_list != NULL)
892 {
893 // Turn an error message into an exception.
894 did_emsg = FALSE;
895 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
896 goto failed;
897 did_throw = TRUE;
898 *msg_list = NULL;
899 }
900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 if (did_throw && !ectx.ec_in_catch)
902 {
903 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +0100904 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905
906 // An exception jumps to the first catch, finally, or returns from
907 // the current function.
908 if (trystack->ga_len > 0)
909 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200910 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 {
912 // jump to ":catch" or ":finally"
913 ectx.ec_in_catch = TRUE;
914 ectx.ec_iidx = trycmd->tcd_catch_idx;
915 }
916 else
917 {
918 // not inside try or need to return from current functions.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200919 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100920 {
921 // At the toplevel we are done. Push a dummy return value.
Bram Moolenaar270d0382020-05-15 21:42:53 +0200922 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100923 goto failed;
924 tv = STACK_TV_BOT(0);
925 tv->v_type = VAR_NUMBER;
926 tv->vval.v_number = 0;
927 ++ectx.ec_stack.ga_len;
Bram Moolenaar257cc5e2020-02-19 17:06:11 +0100928 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200929 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
930 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931 goto done;
932 }
933
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200934 if (func_return(&ectx) == FAIL)
935 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100936 }
937 continue;
938 }
939
940 iptr = &ectx.ec_instr[ectx.ec_iidx++];
941 switch (iptr->isn_type)
942 {
943 // execute Ex command line
944 case ISN_EXEC:
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200945 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100946 do_cmdline_cmd(iptr->isn_arg.string);
947 break;
948
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200949 // execute Ex command from pieces on the stack
950 case ISN_EXECCONCAT:
951 {
952 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +0200953 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200954 int pass;
955 int i;
956 char_u *cmd = NULL;
957 char_u *str;
958
959 for (pass = 1; pass <= 2; ++pass)
960 {
961 for (i = 0; i < count; ++i)
962 {
963 tv = STACK_TV_BOT(i - count);
964 str = tv->vval.v_string;
965 if (str != NULL && *str != NUL)
966 {
967 if (pass == 2)
968 STRCPY(cmd + len, str);
969 len += STRLEN(str);
970 }
971 if (pass == 2)
972 clear_tv(tv);
973 }
974 if (pass == 1)
975 {
976 cmd = alloc(len + 1);
977 if (cmd == NULL)
978 goto failed;
979 len = 0;
980 }
981 }
982
Bram Moolenaar6378c4f2020-04-26 13:50:41 +0200983 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +0200984 do_cmdline_cmd(cmd);
985 vim_free(cmd);
986 }
987 break;
988
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 // execute :echo {string} ...
990 case ISN_ECHO:
991 {
992 int count = iptr->isn_arg.echo.echo_count;
993 int atstart = TRUE;
994 int needclr = TRUE;
995
996 for (idx = 0; idx < count; ++idx)
997 {
998 tv = STACK_TV_BOT(idx - count);
999 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1000 &atstart, &needclr);
1001 clear_tv(tv);
1002 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001003 if (needclr)
1004 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001005 ectx.ec_stack.ga_len -= count;
1006 }
1007 break;
1008
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001009 // :execute {string} ...
1010 // :echomsg {string} ...
1011 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001012 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001013 case ISN_ECHOMSG:
1014 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001015 {
1016 int count = iptr->isn_arg.number;
1017 garray_T ga;
1018 char_u buf[NUMBUFLEN];
1019 char_u *p;
1020 int len;
1021 int failed = FALSE;
1022
1023 ga_init2(&ga, 1, 80);
1024 for (idx = 0; idx < count; ++idx)
1025 {
1026 tv = STACK_TV_BOT(idx - count);
1027 if (tv->v_type == VAR_CHANNEL || tv->v_type == VAR_JOB)
1028 {
1029 emsg(_(e_inval_string));
1030 break;
1031 }
1032 else
1033 p = tv_get_string_buf(tv, buf);
1034
1035 len = (int)STRLEN(p);
1036 if (ga_grow(&ga, len + 2) == FAIL)
1037 failed = TRUE;
1038 else
1039 {
1040 if (ga.ga_len > 0)
1041 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1042 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1043 ga.ga_len += len;
1044 }
1045 clear_tv(tv);
1046 }
1047 ectx.ec_stack.ga_len -= count;
1048
1049 if (!failed && ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001050 {
1051 if (iptr->isn_type == ISN_EXECUTE)
1052 do_cmdline_cmd((char_u *)ga.ga_data);
1053 else
1054 {
1055 msg_sb_eol();
1056 if (iptr->isn_type == ISN_ECHOMSG)
1057 {
1058 msg_attr(ga.ga_data, echo_attr);
1059 out_flush();
1060 }
1061 else
1062 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001063 SOURCING_LNUM = iptr->isn_lnum;
1064 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001065 }
1066 }
1067 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001068 ga_clear(&ga);
1069 }
1070 break;
1071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072 // load local variable or argument
1073 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001074 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001075 goto failed;
1076 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1077 ++ectx.ec_stack.ga_len;
1078 break;
1079
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001080 // load variable or argument from outer scope
1081 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001082 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001083 goto failed;
1084 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1085 STACK_TV_BOT(0));
1086 ++ectx.ec_stack.ga_len;
1087 break;
1088
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001089 // load v: variable
1090 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001091 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001092 goto failed;
1093 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1094 ++ectx.ec_stack.ga_len;
1095 break;
1096
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001097 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001098 case ISN_LOADSCRIPT:
1099 {
1100 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001101 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 svar_T *sv;
1103
1104 sv = ((svar_T *)si->sn_var_vals.ga_data)
1105 + iptr->isn_arg.script.script_idx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001106 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001107 goto failed;
1108 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1109 ++ectx.ec_stack.ga_len;
1110 }
1111 break;
1112
1113 // load s: variable in old script
1114 case ISN_LOADS:
1115 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001116 hashtab_T *ht = &SCRIPT_VARS(
1117 iptr->isn_arg.loadstore.ls_sid);
1118 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001120
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 if (di == NULL)
1122 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001123 semsg(_(e_undefvar), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001124 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 }
1126 else
1127 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001128 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129 goto failed;
1130 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1131 ++ectx.ec_stack.ga_len;
1132 }
1133 }
1134 break;
1135
Bram Moolenaard3aac292020-04-19 14:32:17 +02001136 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001137 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001138 case ISN_LOADB:
1139 case ISN_LOADW:
1140 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001142 dictitem_T *di = NULL;
1143 hashtab_T *ht = NULL;
1144 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001145
Bram Moolenaard3aac292020-04-19 14:32:17 +02001146 switch (iptr->isn_type)
1147 {
1148 case ISN_LOADG:
1149 ht = get_globvar_ht();
1150 namespace = 'g';
1151 break;
1152 case ISN_LOADB:
1153 ht = &curbuf->b_vars->dv_hashtab;
1154 namespace = 'b';
1155 break;
1156 case ISN_LOADW:
1157 ht = &curwin->w_vars->dv_hashtab;
1158 namespace = 'w';
1159 break;
1160 case ISN_LOADT:
1161 ht = &curtab->tp_vars->dv_hashtab;
1162 namespace = 't';
1163 break;
1164 default: // Cannot reach here
1165 goto failed;
1166 }
1167 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001168
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001169 if (di == NULL)
1170 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001171 semsg(_("E121: Undefined variable: %c:%s"),
1172 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001173 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001174 }
1175 else
1176 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001177 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 goto failed;
1179 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1180 ++ectx.ec_stack.ga_len;
1181 }
1182 }
1183 break;
1184
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001185 // load g:/b:/w:/t: namespace
1186 case ISN_LOADGDICT:
1187 case ISN_LOADBDICT:
1188 case ISN_LOADWDICT:
1189 case ISN_LOADTDICT:
1190 {
1191 dict_T *d = NULL;
1192
1193 switch (iptr->isn_type)
1194 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001195 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1196 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1197 case ISN_LOADWDICT: d = curwin->w_vars; break;
1198 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001199 default: // Cannot reach here
1200 goto failed;
1201 }
1202 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1203 goto failed;
1204 tv = STACK_TV_BOT(0);
1205 tv->v_type = VAR_DICT;
1206 tv->v_lock = 0;
1207 tv->vval.v_dict = d;
1208 ++ectx.ec_stack.ga_len;
1209 }
1210 break;
1211
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001212 // load &option
1213 case ISN_LOADOPT:
1214 {
1215 typval_T optval;
1216 char_u *name = iptr->isn_arg.string;
1217
Bram Moolenaara8c17702020-04-01 21:17:24 +02001218 // This is not expected to fail, name is checked during
1219 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001220 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001221 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001222 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001223 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001224 *STACK_TV_BOT(0) = optval;
1225 ++ectx.ec_stack.ga_len;
1226 }
1227 break;
1228
1229 // load $ENV
1230 case ISN_LOADENV:
1231 {
1232 typval_T optval;
1233 char_u *name = iptr->isn_arg.string;
1234
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;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001237 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001238 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 *STACK_TV_BOT(0) = optval;
1240 ++ectx.ec_stack.ga_len;
1241 }
1242 break;
1243
1244 // load @register
1245 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001246 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001247 goto failed;
1248 tv = STACK_TV_BOT(0);
1249 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001250 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251 tv->vval.v_string = get_reg_contents(
1252 iptr->isn_arg.number, GREG_EXPR_SRC);
1253 ++ectx.ec_stack.ga_len;
1254 break;
1255
1256 // store local variable
1257 case ISN_STORE:
1258 --ectx.ec_stack.ga_len;
1259 tv = STACK_TV_VAR(iptr->isn_arg.number);
1260 clear_tv(tv);
1261 *tv = *STACK_TV_BOT(0);
1262 break;
1263
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001264 // store variable or argument in outer scope
1265 case ISN_STOREOUTER:
1266 --ectx.ec_stack.ga_len;
1267 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1268 clear_tv(tv);
1269 *tv = *STACK_TV_BOT(0);
1270 break;
1271
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001272 // store s: variable in old script
1273 case ISN_STORES:
1274 {
1275 hashtab_T *ht = &SCRIPT_VARS(
1276 iptr->isn_arg.loadstore.ls_sid);
1277 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001278 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001279
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001280 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001281 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001282 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001283 else
1284 {
1285 clear_tv(&di->di_tv);
1286 di->di_tv = *STACK_TV_BOT(0);
1287 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001288 }
1289 break;
1290
1291 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 case ISN_STORESCRIPT:
1293 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001294 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001295 iptr->isn_arg.script.script_sid);
1296 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1297 + iptr->isn_arg.script.script_idx;
1298
1299 --ectx.ec_stack.ga_len;
1300 clear_tv(sv->sv_tv);
1301 *sv->sv_tv = *STACK_TV_BOT(0);
1302 }
1303 break;
1304
1305 // store option
1306 case ISN_STOREOPT:
1307 {
1308 long n = 0;
1309 char_u *s = NULL;
1310 char *msg;
1311
1312 --ectx.ec_stack.ga_len;
1313 tv = STACK_TV_BOT(0);
1314 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001315 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001316 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001317 if (s == NULL)
1318 s = (char_u *)"";
1319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001320 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001321 // must be VAR_NUMBER, CHECKTYPE makes sure
1322 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001323 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1324 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001325 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 if (msg != NULL)
1327 {
1328 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001329 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001331 }
1332 break;
1333
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001334 // store $ENV
1335 case ISN_STOREENV:
1336 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001337 tv = STACK_TV_BOT(0);
1338 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1339 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001340 break;
1341
1342 // store @r
1343 case ISN_STOREREG:
1344 {
1345 int reg = iptr->isn_arg.number;
1346
1347 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001348 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001349 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001350 tv_get_string(tv), -1, FALSE);
1351 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001352 }
1353 break;
1354
1355 // store v: variable
1356 case ISN_STOREV:
1357 --ectx.ec_stack.ga_len;
1358 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1359 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001360 // should not happen, type is checked when compiling
1361 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362 break;
1363
Bram Moolenaard3aac292020-04-19 14:32:17 +02001364 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001365 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001366 case ISN_STOREB:
1367 case ISN_STOREW:
1368 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001369 {
1370 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001371 hashtab_T *ht;
1372 switch (iptr->isn_type)
1373 {
1374 case ISN_STOREG:
1375 ht = get_globvar_ht();
1376 break;
1377 case ISN_STOREB:
1378 ht = &curbuf->b_vars->dv_hashtab;
1379 break;
1380 case ISN_STOREW:
1381 ht = &curwin->w_vars->dv_hashtab;
1382 break;
1383 case ISN_STORET:
1384 ht = &curtab->tp_vars->dv_hashtab;
1385 break;
1386 default: // Cannot reach here
1387 goto failed;
1388 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001389
1390 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001391 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001393 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 else
1395 {
1396 clear_tv(&di->di_tv);
1397 di->di_tv = *STACK_TV_BOT(0);
1398 }
1399 }
1400 break;
1401
1402 // store number in local variable
1403 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001404 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001405 clear_tv(tv);
1406 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001407 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 break;
1409
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001410 // store value in list variable
1411 case ISN_STORELIST:
1412 {
1413 typval_T *tv_idx = STACK_TV_BOT(-2);
1414 varnumber_T lidx = tv_idx->vval.v_number;
1415 typval_T *tv_list = STACK_TV_BOT(-1);
1416 list_T *list = tv_list->vval.v_list;
1417
1418 if (lidx < 0 && list->lv_len + lidx >= 0)
1419 // negative index is relative to the end
1420 lidx = list->lv_len + lidx;
1421 if (lidx < 0 || lidx > list->lv_len)
1422 {
1423 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001424 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001425 }
1426 tv = STACK_TV_BOT(-3);
1427 if (lidx < list->lv_len)
1428 {
1429 listitem_T *li = list_find(list, lidx);
1430
1431 // overwrite existing list item
1432 clear_tv(&li->li_tv);
1433 li->li_tv = *tv;
1434 }
1435 else
1436 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001437 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001438 if (list_append_tv(list, tv) == FAIL)
1439 goto failed;
1440 clear_tv(tv);
1441 }
1442 clear_tv(tv_idx);
1443 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001444 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001445 }
1446 break;
1447
1448 // store value in dict variable
1449 case ISN_STOREDICT:
1450 {
1451 typval_T *tv_key = STACK_TV_BOT(-2);
1452 char_u *key = tv_key->vval.v_string;
1453 typval_T *tv_dict = STACK_TV_BOT(-1);
1454 dict_T *dict = tv_dict->vval.v_dict;
1455 dictitem_T *di;
1456
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001457 if (dict == NULL)
1458 {
1459 emsg(_(e_dictnull));
1460 goto on_error;
1461 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001462 if (key == NULL)
1463 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001464 tv = STACK_TV_BOT(-3);
1465 di = dict_find(dict, key, -1);
1466 if (di != NULL)
1467 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001468 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001469 clear_tv(&di->di_tv);
1470 di->di_tv = *tv;
1471 }
1472 else
1473 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001474 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001475 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1476 goto failed;
1477 clear_tv(tv);
1478 }
1479 clear_tv(tv_key);
1480 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001481 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001482 }
1483 break;
1484
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 // push constant
1486 case ISN_PUSHNR:
1487 case ISN_PUSHBOOL:
1488 case ISN_PUSHSPEC:
1489 case ISN_PUSHF:
1490 case ISN_PUSHS:
1491 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001492 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001493 case ISN_PUSHCHANNEL:
1494 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001495 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 goto failed;
1497 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001498 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001499 ++ectx.ec_stack.ga_len;
1500 switch (iptr->isn_type)
1501 {
1502 case ISN_PUSHNR:
1503 tv->v_type = VAR_NUMBER;
1504 tv->vval.v_number = iptr->isn_arg.number;
1505 break;
1506 case ISN_PUSHBOOL:
1507 tv->v_type = VAR_BOOL;
1508 tv->vval.v_number = iptr->isn_arg.number;
1509 break;
1510 case ISN_PUSHSPEC:
1511 tv->v_type = VAR_SPECIAL;
1512 tv->vval.v_number = iptr->isn_arg.number;
1513 break;
1514#ifdef FEAT_FLOAT
1515 case ISN_PUSHF:
1516 tv->v_type = VAR_FLOAT;
1517 tv->vval.v_float = iptr->isn_arg.fnumber;
1518 break;
1519#endif
1520 case ISN_PUSHBLOB:
1521 blob_copy(iptr->isn_arg.blob, tv);
1522 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001523 case ISN_PUSHFUNC:
1524 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001525 if (iptr->isn_arg.string == NULL)
1526 tv->vval.v_string = NULL;
1527 else
1528 tv->vval.v_string =
1529 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001530 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001531 case ISN_PUSHCHANNEL:
1532#ifdef FEAT_JOB_CHANNEL
1533 tv->v_type = VAR_CHANNEL;
1534 tv->vval.v_channel = iptr->isn_arg.channel;
1535 if (tv->vval.v_channel != NULL)
1536 ++tv->vval.v_channel->ch_refcount;
1537#endif
1538 break;
1539 case ISN_PUSHJOB:
1540#ifdef FEAT_JOB_CHANNEL
1541 tv->v_type = VAR_JOB;
1542 tv->vval.v_job = iptr->isn_arg.job;
1543 if (tv->vval.v_job != NULL)
1544 ++tv->vval.v_job->jv_refcount;
1545#endif
1546 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 default:
1548 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001549 tv->vval.v_string = vim_strsave(
1550 iptr->isn_arg.string == NULL
1551 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001552 }
1553 break;
1554
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001555 case ISN_UNLET:
1556 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1557 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001558 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001559 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001560 case ISN_UNLETENV:
1561 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1562 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001564 // create a list from items on the stack; uses a single allocation
1565 // for the list header and the items
1566 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001567 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1568 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 break;
1570
1571 // create a dict from items on the stack
1572 case ISN_NEWDICT:
1573 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001574 int count = iptr->isn_arg.number;
1575 dict_T *dict = dict_alloc();
1576 dictitem_T *item;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577
1578 if (dict == NULL)
1579 goto failed;
1580 for (idx = 0; idx < count; ++idx)
1581 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001582 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001584 // check key is unique
1585 item = dict_find(dict, tv->vval.v_string, -1);
1586 if (item != NULL)
1587 {
1588 semsg(_(e_duplicate_key), tv->vval.v_string);
1589 dict_unref(dict);
1590 goto on_error;
1591 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 item = dictitem_alloc(tv->vval.v_string);
1593 clear_tv(tv);
1594 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001595 {
1596 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1600 item->di_tv.v_lock = 0;
1601 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001602 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001603 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001604 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001606 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 }
1608
1609 if (count > 0)
1610 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001611 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001612 goto failed;
1613 else
1614 ++ectx.ec_stack.ga_len;
1615 tv = STACK_TV_BOT(-1);
1616 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001617 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 tv->vval.v_dict = dict;
1619 ++dict->dv_refcount;
1620 }
1621 break;
1622
1623 // call a :def function
1624 case ISN_DCALL:
1625 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1626 iptr->isn_arg.dfunc.cdf_argcount,
1627 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001628 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 break;
1630
1631 // call a builtin function
1632 case ISN_BCALL:
1633 SOURCING_LNUM = iptr->isn_lnum;
1634 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1635 iptr->isn_arg.bfunc.cbf_argcount,
1636 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001637 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 break;
1639
1640 // call a funcref or partial
1641 case ISN_PCALL:
1642 {
1643 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1644 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001645 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646
1647 SOURCING_LNUM = iptr->isn_lnum;
1648 if (pfunc->cpf_top)
1649 {
1650 // funcref is above the arguments
1651 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1652 }
1653 else
1654 {
1655 // Get the funcref from the stack.
1656 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001657 partial_tv = *STACK_TV_BOT(0);
1658 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001659 }
1660 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001661 if (tv == &partial_tv)
1662 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001663 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001664 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 }
1666 break;
1667
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001668 case ISN_PCALL_END:
1669 // PCALL finished, arguments have been consumed and replaced by
1670 // the return value. Now clear the funcref from the stack,
1671 // and move the return value in its place.
1672 --ectx.ec_stack.ga_len;
1673 clear_tv(STACK_TV_BOT(-1));
1674 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1675 break;
1676
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 // call a user defined function or funcref/partial
1678 case ISN_UCALL:
1679 {
1680 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1681
1682 SOURCING_LNUM = iptr->isn_lnum;
1683 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001684 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001685 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 }
1687 break;
1688
1689 // return from a :def function call
1690 case ISN_RETURN:
1691 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001692 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001693 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001694
1695 if (trystack->ga_len > 0)
1696 trycmd = ((trycmd_T *)trystack->ga_data)
1697 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001698 if (trycmd != NULL
1699 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 && trycmd->tcd_finally_idx != 0)
1701 {
1702 // jump to ":finally"
1703 ectx.ec_iidx = trycmd->tcd_finally_idx;
1704 trycmd->tcd_return = TRUE;
1705 }
1706 else
Bram Moolenaard032f342020-07-18 18:13:02 +02001707 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 }
1709 break;
1710
1711 // push a function reference to a compiled function
1712 case ISN_FUNCREF:
1713 {
1714 partial_T *pt = NULL;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001715 dfunc_T *pt_dfunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716
1717 pt = ALLOC_CLEAR_ONE(partial_T);
1718 if (pt == NULL)
1719 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001720 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001721 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001722 vim_free(pt);
1723 goto failed;
1724 }
1725 pt_dfunc = ((dfunc_T *)def_functions.ga_data)
1726 + iptr->isn_arg.funcref.fr_func;
1727 pt->pt_func = pt_dfunc->df_ufunc;
1728 pt->pt_refcount = 1;
1729 ++pt_dfunc->df_ufunc->uf_refcount;
1730
1731 if (pt_dfunc->df_ufunc->uf_flags & FC_CLOSURE)
1732 {
1733 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1734 + ectx.ec_dfunc_idx;
1735
1736 // The closure needs to find arguments and local
1737 // variables in the current stack.
Bram Moolenaarf7779c62020-05-03 15:38:16 +02001738 pt->pt_ectx_stack = &ectx.ec_stack;
1739 pt->pt_ectx_frame = ectx.ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001740
1741 // If this function returns and the closure is still
1742 // used, we need to make a copy of the context
1743 // (arguments and local variables). Store a reference
1744 // to the partial so we can handle that.
1745 ++pt->pt_refcount;
1746 tv = STACK_TV_VAR(dfunc->df_varcount
1747 + iptr->isn_arg.funcref.fr_var_idx);
1748 if (tv->v_type == VAR_PARTIAL)
1749 {
1750 // TODO: use a garray_T on ectx.
1751 emsg("Multiple closures not supported yet");
1752 goto failed;
1753 }
1754 tv->v_type = VAR_PARTIAL;
1755 tv->vval.v_partial = pt;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001756 }
1757
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 tv = STACK_TV_BOT(0);
1759 ++ectx.ec_stack.ga_len;
1760 tv->vval.v_partial = pt;
1761 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001762 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001763 }
1764 break;
1765
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001766 // Create a global function from a lambda.
1767 case ISN_NEWFUNC:
1768 {
1769 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
1770
1771 copy_func(newfunc->nf_lambda, newfunc->nf_global);
1772 }
1773 break;
1774
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001775 // jump if a condition is met
1776 case ISN_JUMP:
1777 {
1778 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
1779 int jump = TRUE;
1780
1781 if (when != JUMP_ALWAYS)
1782 {
1783 tv = STACK_TV_BOT(-1);
1784 jump = tv2bool(tv);
1785 if (when == JUMP_IF_FALSE
1786 || when == JUMP_AND_KEEP_IF_FALSE)
1787 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01001788 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001789 {
1790 // drop the value from the stack
1791 clear_tv(tv);
1792 --ectx.ec_stack.ga_len;
1793 }
1794 }
1795 if (jump)
1796 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
1797 }
1798 break;
1799
1800 // top of a for loop
1801 case ISN_FOR:
1802 {
1803 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
1804 typval_T *idxtv =
1805 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
1806
1807 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02001808 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 goto failed;
1810 if (++idxtv->vval.v_number >= list->lv_len)
1811 // past the end of the list, jump to "endfor"
1812 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
1813 else if (list->lv_first == &range_list_item)
1814 {
1815 // non-materialized range() list
1816 tv = STACK_TV_BOT(0);
1817 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001818 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 tv->vval.v_number = list_find_nr(
1820 list, idxtv->vval.v_number, NULL);
1821 ++ectx.ec_stack.ga_len;
1822 }
1823 else
1824 {
1825 listitem_T *li = list_find(list, idxtv->vval.v_number);
1826
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 copy_tv(&li->li_tv, STACK_TV_BOT(0));
1828 ++ectx.ec_stack.ga_len;
1829 }
1830 }
1831 break;
1832
1833 // start of ":try" block
1834 case ISN_TRY:
1835 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001836 trycmd_T *trycmd = NULL;
1837
Bram Moolenaar270d0382020-05-15 21:42:53 +02001838 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 goto failed;
1840 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
1841 + ectx.ec_trystack.ga_len;
1842 ++ectx.ec_trystack.ga_len;
1843 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001844 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
1846 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001847 trycmd->tcd_caught = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 }
1849 break;
1850
1851 case ISN_PUSHEXC:
1852 if (current_exception == NULL)
1853 {
1854 iemsg("Evaluating catch while current_exception is NULL");
1855 goto failed;
1856 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02001857 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 goto failed;
1859 tv = STACK_TV_BOT(0);
1860 ++ectx.ec_stack.ga_len;
1861 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001862 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001863 tv->vval.v_string = vim_strsave(
1864 (char_u *)current_exception->value);
1865 break;
1866
1867 case ISN_CATCH:
1868 {
1869 garray_T *trystack = &ectx.ec_trystack;
1870
1871 if (trystack->ga_len > 0)
1872 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001873 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 + trystack->ga_len - 1;
1875 trycmd->tcd_caught = TRUE;
1876 }
1877 did_emsg = got_int = did_throw = FALSE;
1878 catch_exception(current_exception);
1879 }
1880 break;
1881
1882 // end of ":try" block
1883 case ISN_ENDTRY:
1884 {
1885 garray_T *trystack = &ectx.ec_trystack;
1886
1887 if (trystack->ga_len > 0)
1888 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01001889 trycmd_T *trycmd = NULL;
1890
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001891 --trystack->ga_len;
1892 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02001893 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 trycmd = ((trycmd_T *)trystack->ga_data)
1895 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01001896 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 {
1898 // discard the exception
1899 if (caught_stack == current_exception)
1900 caught_stack = caught_stack->caught;
1901 discard_current_exception();
1902 }
1903
1904 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02001905 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 }
1907 }
1908 break;
1909
1910 case ISN_THROW:
1911 --ectx.ec_stack.ga_len;
1912 tv = STACK_TV_BOT(0);
1913 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
1914 {
1915 vim_free(tv->vval.v_string);
1916 goto failed;
1917 }
1918 did_throw = TRUE;
1919 break;
1920
1921 // compare with special values
1922 case ISN_COMPAREBOOL:
1923 case ISN_COMPARESPECIAL:
1924 {
1925 typval_T *tv1 = STACK_TV_BOT(-2);
1926 typval_T *tv2 = STACK_TV_BOT(-1);
1927 varnumber_T arg1 = tv1->vval.v_number;
1928 varnumber_T arg2 = tv2->vval.v_number;
1929 int res;
1930
1931 switch (iptr->isn_arg.op.op_type)
1932 {
1933 case EXPR_EQUAL: res = arg1 == arg2; break;
1934 case EXPR_NEQUAL: res = arg1 != arg2; break;
1935 default: res = 0; break;
1936 }
1937
1938 --ectx.ec_stack.ga_len;
1939 tv1->v_type = VAR_BOOL;
1940 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1941 }
1942 break;
1943
1944 // Operation with two number arguments
1945 case ISN_OPNR:
1946 case ISN_COMPARENR:
1947 {
1948 typval_T *tv1 = STACK_TV_BOT(-2);
1949 typval_T *tv2 = STACK_TV_BOT(-1);
1950 varnumber_T arg1 = tv1->vval.v_number;
1951 varnumber_T arg2 = tv2->vval.v_number;
1952 varnumber_T res;
1953
1954 switch (iptr->isn_arg.op.op_type)
1955 {
1956 case EXPR_MULT: res = arg1 * arg2; break;
1957 case EXPR_DIV: res = arg1 / arg2; break;
1958 case EXPR_REM: res = arg1 % arg2; break;
1959 case EXPR_SUB: res = arg1 - arg2; break;
1960 case EXPR_ADD: res = arg1 + arg2; break;
1961
1962 case EXPR_EQUAL: res = arg1 == arg2; break;
1963 case EXPR_NEQUAL: res = arg1 != arg2; break;
1964 case EXPR_GREATER: res = arg1 > arg2; break;
1965 case EXPR_GEQUAL: res = arg1 >= arg2; break;
1966 case EXPR_SMALLER: res = arg1 < arg2; break;
1967 case EXPR_SEQUAL: res = arg1 <= arg2; break;
1968 default: res = 0; break;
1969 }
1970
1971 --ectx.ec_stack.ga_len;
1972 if (iptr->isn_type == ISN_COMPARENR)
1973 {
1974 tv1->v_type = VAR_BOOL;
1975 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
1976 }
1977 else
1978 tv1->vval.v_number = res;
1979 }
1980 break;
1981
1982 // Computation with two float arguments
1983 case ISN_OPFLOAT:
1984 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01001985#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 {
1987 typval_T *tv1 = STACK_TV_BOT(-2);
1988 typval_T *tv2 = STACK_TV_BOT(-1);
1989 float_T arg1 = tv1->vval.v_float;
1990 float_T arg2 = tv2->vval.v_float;
1991 float_T res = 0;
1992 int cmp = FALSE;
1993
1994 switch (iptr->isn_arg.op.op_type)
1995 {
1996 case EXPR_MULT: res = arg1 * arg2; break;
1997 case EXPR_DIV: res = arg1 / arg2; break;
1998 case EXPR_SUB: res = arg1 - arg2; break;
1999 case EXPR_ADD: res = arg1 + arg2; break;
2000
2001 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2002 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2003 case EXPR_GREATER: cmp = arg1 > arg2; break;
2004 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2005 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2006 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2007 default: cmp = 0; break;
2008 }
2009 --ectx.ec_stack.ga_len;
2010 if (iptr->isn_type == ISN_COMPAREFLOAT)
2011 {
2012 tv1->v_type = VAR_BOOL;
2013 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2014 }
2015 else
2016 tv1->vval.v_float = res;
2017 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002018#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002019 break;
2020
2021 case ISN_COMPARELIST:
2022 {
2023 typval_T *tv1 = STACK_TV_BOT(-2);
2024 typval_T *tv2 = STACK_TV_BOT(-1);
2025 list_T *arg1 = tv1->vval.v_list;
2026 list_T *arg2 = tv2->vval.v_list;
2027 int cmp = FALSE;
2028 int ic = iptr->isn_arg.op.op_ic;
2029
2030 switch (iptr->isn_arg.op.op_type)
2031 {
2032 case EXPR_EQUAL: cmp =
2033 list_equal(arg1, arg2, ic, FALSE); break;
2034 case EXPR_NEQUAL: cmp =
2035 !list_equal(arg1, arg2, ic, FALSE); break;
2036 case EXPR_IS: cmp = arg1 == arg2; break;
2037 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2038 default: cmp = 0; break;
2039 }
2040 --ectx.ec_stack.ga_len;
2041 clear_tv(tv1);
2042 clear_tv(tv2);
2043 tv1->v_type = VAR_BOOL;
2044 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2045 }
2046 break;
2047
2048 case ISN_COMPAREBLOB:
2049 {
2050 typval_T *tv1 = STACK_TV_BOT(-2);
2051 typval_T *tv2 = STACK_TV_BOT(-1);
2052 blob_T *arg1 = tv1->vval.v_blob;
2053 blob_T *arg2 = tv2->vval.v_blob;
2054 int cmp = FALSE;
2055
2056 switch (iptr->isn_arg.op.op_type)
2057 {
2058 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2059 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2060 case EXPR_IS: cmp = arg1 == arg2; break;
2061 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2062 default: cmp = 0; break;
2063 }
2064 --ectx.ec_stack.ga_len;
2065 clear_tv(tv1);
2066 clear_tv(tv2);
2067 tv1->v_type = VAR_BOOL;
2068 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2069 }
2070 break;
2071
2072 // TODO: handle separately
2073 case ISN_COMPARESTRING:
2074 case ISN_COMPAREDICT:
2075 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002076 case ISN_COMPAREANY:
2077 {
2078 typval_T *tv1 = STACK_TV_BOT(-2);
2079 typval_T *tv2 = STACK_TV_BOT(-1);
2080 exptype_T exptype = iptr->isn_arg.op.op_type;
2081 int ic = iptr->isn_arg.op.op_ic;
2082
2083 typval_compare(tv1, tv2, exptype, ic);
2084 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002085 --ectx.ec_stack.ga_len;
2086 }
2087 break;
2088
2089 case ISN_ADDLIST:
2090 case ISN_ADDBLOB:
2091 {
2092 typval_T *tv1 = STACK_TV_BOT(-2);
2093 typval_T *tv2 = STACK_TV_BOT(-1);
2094
2095 if (iptr->isn_type == ISN_ADDLIST)
2096 eval_addlist(tv1, tv2);
2097 else
2098 eval_addblob(tv1, tv2);
2099 clear_tv(tv2);
2100 --ectx.ec_stack.ga_len;
2101 }
2102 break;
2103
2104 // Computation with two arguments of unknown type
2105 case ISN_OPANY:
2106 {
2107 typval_T *tv1 = STACK_TV_BOT(-2);
2108 typval_T *tv2 = STACK_TV_BOT(-1);
2109 varnumber_T n1, n2;
2110#ifdef FEAT_FLOAT
2111 float_T f1 = 0, f2 = 0;
2112#endif
2113 int error = FALSE;
2114
2115 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2116 {
2117 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2118 {
2119 eval_addlist(tv1, tv2);
2120 clear_tv(tv2);
2121 --ectx.ec_stack.ga_len;
2122 break;
2123 }
2124 else if (tv1->v_type == VAR_BLOB
2125 && tv2->v_type == VAR_BLOB)
2126 {
2127 eval_addblob(tv1, tv2);
2128 clear_tv(tv2);
2129 --ectx.ec_stack.ga_len;
2130 break;
2131 }
2132 }
2133#ifdef FEAT_FLOAT
2134 if (tv1->v_type == VAR_FLOAT)
2135 {
2136 f1 = tv1->vval.v_float;
2137 n1 = 0;
2138 }
2139 else
2140#endif
2141 {
2142 n1 = tv_get_number_chk(tv1, &error);
2143 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002144 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002145#ifdef FEAT_FLOAT
2146 if (tv2->v_type == VAR_FLOAT)
2147 f1 = n1;
2148#endif
2149 }
2150#ifdef FEAT_FLOAT
2151 if (tv2->v_type == VAR_FLOAT)
2152 {
2153 f2 = tv2->vval.v_float;
2154 n2 = 0;
2155 }
2156 else
2157#endif
2158 {
2159 n2 = tv_get_number_chk(tv2, &error);
2160 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002161 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002162#ifdef FEAT_FLOAT
2163 if (tv1->v_type == VAR_FLOAT)
2164 f2 = n2;
2165#endif
2166 }
2167#ifdef FEAT_FLOAT
2168 // if there is a float on either side the result is a float
2169 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2170 {
2171 switch (iptr->isn_arg.op.op_type)
2172 {
2173 case EXPR_MULT: f1 = f1 * f2; break;
2174 case EXPR_DIV: f1 = f1 / f2; break;
2175 case EXPR_SUB: f1 = f1 - f2; break;
2176 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002177 default: emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002179 }
2180 clear_tv(tv1);
2181 clear_tv(tv2);
2182 tv1->v_type = VAR_FLOAT;
2183 tv1->vval.v_float = f1;
2184 --ectx.ec_stack.ga_len;
2185 }
2186 else
2187#endif
2188 {
2189 switch (iptr->isn_arg.op.op_type)
2190 {
2191 case EXPR_MULT: n1 = n1 * n2; break;
2192 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2193 case EXPR_SUB: n1 = n1 - n2; break;
2194 case EXPR_ADD: n1 = n1 + n2; break;
2195 default: n1 = num_modulus(n1, n2); break;
2196 }
2197 clear_tv(tv1);
2198 clear_tv(tv2);
2199 tv1->v_type = VAR_NUMBER;
2200 tv1->vval.v_number = n1;
2201 --ectx.ec_stack.ga_len;
2202 }
2203 }
2204 break;
2205
2206 case ISN_CONCAT:
2207 {
2208 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2209 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2210 char_u *res;
2211
2212 res = concat_str(str1, str2);
2213 clear_tv(STACK_TV_BOT(-2));
2214 clear_tv(STACK_TV_BOT(-1));
2215 --ectx.ec_stack.ga_len;
2216 STACK_TV_BOT(-1)->vval.v_string = res;
2217 }
2218 break;
2219
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002220 case ISN_STRINDEX:
2221 {
2222 char_u *s;
2223 varnumber_T n;
2224 char_u *res;
2225
2226 // string index: string is at stack-2, index at stack-1
2227 tv = STACK_TV_BOT(-2);
2228 if (tv->v_type != VAR_STRING)
2229 {
2230 emsg(_(e_stringreq));
2231 goto on_error;
2232 }
2233 s = tv->vval.v_string;
2234
2235 tv = STACK_TV_BOT(-1);
2236 if (tv->v_type != VAR_NUMBER)
2237 {
2238 emsg(_(e_number_exp));
2239 goto on_error;
2240 }
2241 n = tv->vval.v_number;
2242
2243 // The resulting variable is a string of a single
2244 // character. If the index is too big or negative the
2245 // result is empty.
2246 if (n < 0 || n >= (varnumber_T)STRLEN(s))
2247 res = NULL;
2248 else
2249 res = vim_strnsave(s + n, 1);
2250 --ectx.ec_stack.ga_len;
2251 tv = STACK_TV_BOT(-1);
2252 vim_free(tv->vval.v_string);
2253 tv->vval.v_string = res;
2254 }
2255 break;
2256
2257 case ISN_LISTINDEX:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 {
2259 list_T *list;
2260 varnumber_T n;
2261 listitem_T *li;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002262 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002263
2264 // list index: list is at stack-2, index at stack-1
2265 tv = STACK_TV_BOT(-2);
2266 if (tv->v_type != VAR_LIST)
2267 {
2268 emsg(_(e_listreq));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002269 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002270 }
2271 list = tv->vval.v_list;
2272
2273 tv = STACK_TV_BOT(-1);
2274 if (tv->v_type != VAR_NUMBER)
2275 {
2276 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002277 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278 }
2279 n = tv->vval.v_number;
2280 clear_tv(tv);
2281 if ((li = list_find(list, n)) == NULL)
2282 {
2283 semsg(_(e_listidx), n);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 }
2286 --ectx.ec_stack.ga_len;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002287 // Clear the list after getting the item, to avoid that it
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002288 // makes the item invalid.
Bram Moolenaar435d8972020-07-05 16:42:13 +02002289 tv = STACK_TV_BOT(-1);
2290 temp_tv = *tv;
2291 copy_tv(&li->li_tv, tv);
2292 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 }
2294 break;
2295
Bram Moolenaar9af78762020-06-16 11:34:42 +02002296 case ISN_SLICE:
2297 {
2298 list_T *list;
2299 int count = iptr->isn_arg.number;
2300
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002301 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002302 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002303 list = tv->vval.v_list;
2304
2305 // no error for short list, expect it to be checked earlier
2306 if (list != NULL && list->lv_len >= count)
2307 {
2308 list_T *newlist = list_slice(list,
2309 count, list->lv_len - 1);
2310
2311 if (newlist != NULL)
2312 {
2313 list_unref(list);
2314 tv->vval.v_list = newlist;
2315 ++newlist->lv_refcount;
2316 }
2317 }
2318 }
2319 break;
2320
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002321 case ISN_GETITEM:
2322 {
2323 listitem_T *li;
2324 int index = iptr->isn_arg.number;
2325
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002326 // Get list item: list is at stack-1, push item.
2327 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002328 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002329 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002330
2331 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2332 goto failed;
2333 ++ectx.ec_stack.ga_len;
2334 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2335 }
2336 break;
2337
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 case ISN_MEMBER:
2339 {
2340 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002341 char_u *key;
2342 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002343 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002344
2345 // dict member: dict is at stack-2, key at stack-1
2346 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002347 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002348 dict = tv->vval.v_dict;
2349
2350 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002351 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002352 key = tv->vval.v_string;
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002353
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002354 if ((di = dict_find(dict, key, -1)) == NULL)
2355 {
2356 semsg(_(e_dictkey), key);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002357 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002358 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002359 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002360 --ectx.ec_stack.ga_len;
2361 // Clear the dict after getting the item, to avoid that it
2362 // make the item invalid.
2363 tv = STACK_TV_BOT(-1);
2364 temp_tv = *tv;
2365 copy_tv(&di->di_tv, tv);
2366 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002367 }
2368 break;
2369
2370 // dict member with string key
2371 case ISN_STRINGMEMBER:
2372 {
2373 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002375 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002376
2377 tv = STACK_TV_BOT(-1);
2378 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2379 {
2380 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002381 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 }
2383 dict = tv->vval.v_dict;
2384
2385 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2386 == NULL)
2387 {
2388 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002389 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002391 // Clear the dict after getting the item, to avoid that it
2392 // make the item invalid.
2393 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002395 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397 break;
2398
2399 case ISN_NEGATENR:
2400 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002401 if (tv->v_type != VAR_NUMBER
2402#ifdef FEAT_FLOAT
2403 && tv->v_type != VAR_FLOAT
2404#endif
2405 )
2406 {
2407 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002408 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002409 }
2410#ifdef FEAT_FLOAT
2411 if (tv->v_type == VAR_FLOAT)
2412 tv->vval.v_float = -tv->vval.v_float;
2413 else
2414#endif
2415 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 break;
2417
2418 case ISN_CHECKNR:
2419 {
2420 int error = FALSE;
2421
2422 tv = STACK_TV_BOT(-1);
2423 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002424 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002425 (void)tv_get_number_chk(tv, &error);
2426 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002427 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429 break;
2430
2431 case ISN_CHECKTYPE:
2432 {
2433 checktype_T *ct = &iptr->isn_arg.type;
2434
2435 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002436 // TODO: better type comparison
2437 if (tv->v_type != ct->ct_type
2438 && !((tv->v_type == VAR_PARTIAL
2439 && ct->ct_type == VAR_FUNC)
2440 || (tv->v_type == VAR_FUNC
2441 && ct->ct_type == VAR_PARTIAL)))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002442 {
2443 semsg(_("E1029: Expected %s but got %s"),
2444 vartype_name(ct->ct_type),
2445 vartype_name(tv->v_type));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002446 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002447 }
2448 }
2449 break;
2450
Bram Moolenaar9af78762020-06-16 11:34:42 +02002451 case ISN_CHECKLEN:
2452 {
2453 int min_len = iptr->isn_arg.checklen.cl_min_len;
2454 list_T *list = NULL;
2455
2456 tv = STACK_TV_BOT(-1);
2457 if (tv->v_type == VAR_LIST)
2458 list = tv->vval.v_list;
2459 if (list == NULL || list->lv_len < min_len
2460 || (list->lv_len > min_len
2461 && !iptr->isn_arg.checklen.cl_more_OK))
2462 {
2463 semsg(_("E1093: Expected %d items but got %d"),
2464 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002465 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002466 }
2467 }
2468 break;
2469
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 case ISN_2BOOL:
2471 {
2472 int n;
2473
2474 tv = STACK_TV_BOT(-1);
2475 n = tv2bool(tv);
2476 if (iptr->isn_arg.number) // invert
2477 n = !n;
2478 clear_tv(tv);
2479 tv->v_type = VAR_BOOL;
2480 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2481 }
2482 break;
2483
2484 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002485 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 {
2487 char_u *str;
2488
2489 tv = STACK_TV_BOT(iptr->isn_arg.number);
2490 if (tv->v_type != VAR_STRING)
2491 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002492 if (iptr->isn_type == ISN_2STRING_ANY)
2493 {
2494 switch (tv->v_type)
2495 {
2496 case VAR_SPECIAL:
2497 case VAR_BOOL:
2498 case VAR_NUMBER:
2499 case VAR_FLOAT:
2500 case VAR_BLOB: break;
2501 default: to_string_error(tv->v_type);
2502 goto on_error;
2503 }
2504 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 str = typval_tostring(tv);
2506 clear_tv(tv);
2507 tv->v_type = VAR_STRING;
2508 tv->vval.v_string = str;
2509 }
2510 }
2511 break;
2512
Bram Moolenaar389df252020-07-09 21:20:47 +02002513 case ISN_SHUFFLE:
2514 {
2515 typval_T tmp_tv;
2516 int item = iptr->isn_arg.shuffle.shfl_item;
2517 int up = iptr->isn_arg.shuffle.shfl_up;
2518
2519 tmp_tv = *STACK_TV_BOT(-item);
2520 for ( ; up > 0 && item > 1; --up)
2521 {
2522 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
2523 --item;
2524 }
2525 *STACK_TV_BOT(-item) = tmp_tv;
2526 }
2527 break;
2528
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 case ISN_DROP:
2530 --ectx.ec_stack.ga_len;
2531 clear_tv(STACK_TV_BOT(0));
2532 break;
2533 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002534 continue;
2535
Bram Moolenaard032f342020-07-18 18:13:02 +02002536func_return:
2537 // Restore previous function. If the frame pointer is zero then there
2538 // is none and we are done.
2539 if (ectx.ec_frame_idx == initial_frame_idx)
2540 {
2541 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
2542 // only fails when out of memory
2543 goto failed;
2544 goto done;
2545 }
2546 if (func_return(&ectx) == FAIL)
2547 // only fails when out of memory
2548 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02002549 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02002550
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002551on_error:
2552 if (trylevel == 0)
2553 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 }
2555
2556done:
2557 // function finished, get result from the stack.
2558 tv = STACK_TV_BOT(-1);
2559 *rettv = *tv;
2560 tv->v_type = VAR_UNKNOWN;
2561 ret = OK;
2562
2563failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002564 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002565 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002566 func_return(&ectx);
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02002567failed_early:
Bram Moolenaara26b9702020-04-18 19:53:28 +02002568 current_sctx.sc_version = save_sc_version;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002569
2570 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
2572 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002573
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01002575 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02002576
2577 if (ret != OK && called_emsg == called_emsg_before)
2578 semsg(_("E1099: Unknown error while executing %s"),
2579 printable_func_name(ufunc));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 return ret;
2581}
2582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583/*
2584 * ":dissassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01002585 * We don't really need this at runtime, but we do have tests that require it,
2586 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002587 */
2588 void
2589ex_disassemble(exarg_T *eap)
2590{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002591 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002592 char_u *fname;
2593 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002594 dfunc_T *dfunc;
2595 isn_T *instr;
2596 int current;
2597 int line_idx = 0;
2598 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002599 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002600
Bram Moolenaarbfd65582020-07-13 18:18:00 +02002601 if (STRNCMP(arg, "<lambda>", 8) == 0)
2602 {
2603 arg += 8;
2604 (void)getdigits(&arg);
2605 fname = vim_strnsave(eap->arg, arg - eap->arg);
2606 }
2607 else
2608 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002609 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01002610 if (fname == NULL)
2611 {
2612 semsg(_(e_invarg2), eap->arg);
2613 return;
2614 }
2615
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002616 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002617 if (ufunc == NULL)
2618 {
2619 char_u *p = untrans_function_name(fname);
2620
2621 if (p != NULL)
2622 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02002623 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02002624 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01002625 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 if (ufunc == NULL)
2627 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002628 semsg(_("E1061: Cannot find function %s"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 return;
2630 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002631 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02002632 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
2633 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02002634 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 {
Bram Moolenaardf2ecdd2020-02-16 15:03:48 +01002636 semsg(_("E1062: Function %s is not compiled"), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 return;
2638 }
2639 if (ufunc->uf_name_exp != NULL)
2640 msg((char *)ufunc->uf_name_exp);
2641 else
2642 msg((char *)ufunc->uf_name);
2643
2644 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
2645 instr = dfunc->df_instr;
2646 for (current = 0; current < dfunc->df_instr_count; ++current)
2647 {
2648 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002649 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
2651 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
2652 {
2653 if (current > prev_current)
2654 {
2655 msg_puts("\n\n");
2656 prev_current = current;
2657 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01002658 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
2659 if (line != NULL)
2660 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 }
2662
2663 switch (iptr->isn_type)
2664 {
2665 case ISN_EXEC:
2666 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
2667 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02002668 case ISN_EXECCONCAT:
2669 smsg("%4d EXECCONCAT %lld", current,
2670 (long long)iptr->isn_arg.number);
2671 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002672 case ISN_ECHO:
2673 {
2674 echo_T *echo = &iptr->isn_arg.echo;
2675
2676 smsg("%4d %s %d", current,
2677 echo->echo_with_white ? "ECHO" : "ECHON",
2678 echo->echo_count);
2679 }
2680 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01002681 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01002682 smsg("%4d EXECUTE %lld", current,
2683 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01002684 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02002685 case ISN_ECHOMSG:
2686 smsg("%4d ECHOMSG %lld", current,
2687 (long long)(iptr->isn_arg.number));
2688 break;
2689 case ISN_ECHOERR:
2690 smsg("%4d ECHOERR %lld", current,
2691 (long long)(iptr->isn_arg.number));
2692 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002693 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002694 case ISN_LOADOUTER:
2695 {
2696 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
2697
2698 if (iptr->isn_arg.number < 0)
2699 smsg("%4d LOAD%s arg[%lld]", current, add,
2700 (long long)(iptr->isn_arg.number
2701 + STACK_FRAME_SIZE));
2702 else
2703 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002704 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 break;
2707 case ISN_LOADV:
2708 smsg("%4d LOADV v:%s", current,
2709 get_vim_var_name(iptr->isn_arg.number));
2710 break;
2711 case ISN_LOADSCRIPT:
2712 {
2713 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002714 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2716 + iptr->isn_arg.script.script_idx;
2717
2718 smsg("%4d LOADSCRIPT %s from %s", current,
2719 sv->sv_name, si->sn_name);
2720 }
2721 break;
2722 case ISN_LOADS:
2723 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002724 scriptitem_T *si = SCRIPT_ITEM(
2725 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002726
2727 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002728 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002729 }
2730 break;
2731 case ISN_LOADG:
2732 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
2733 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002734 case ISN_LOADB:
2735 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
2736 break;
2737 case ISN_LOADW:
2738 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
2739 break;
2740 case ISN_LOADT:
2741 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
2742 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002743 case ISN_LOADGDICT:
2744 smsg("%4d LOAD g:", current);
2745 break;
2746 case ISN_LOADBDICT:
2747 smsg("%4d LOAD b:", current);
2748 break;
2749 case ISN_LOADWDICT:
2750 smsg("%4d LOAD w:", current);
2751 break;
2752 case ISN_LOADTDICT:
2753 smsg("%4d LOAD t:", current);
2754 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 case ISN_LOADOPT:
2756 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
2757 break;
2758 case ISN_LOADENV:
2759 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
2760 break;
2761 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002762 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 break;
2764
2765 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002766 case ISN_STOREOUTER:
2767 {
2768 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
2769
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002770 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002771 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002772 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01002773 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002774 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01002775 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02002776 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002777 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002778 case ISN_STOREV:
2779 smsg("%4d STOREV v:%s", current,
2780 get_vim_var_name(iptr->isn_arg.number));
2781 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002782 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002783 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
2784 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02002785 case ISN_STOREB:
2786 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
2787 break;
2788 case ISN_STOREW:
2789 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
2790 break;
2791 case ISN_STORET:
2792 smsg("%4d STORET %s", current, iptr->isn_arg.string);
2793 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002794 case ISN_STORES:
2795 {
2796 scriptitem_T *si = SCRIPT_ITEM(
2797 iptr->isn_arg.loadstore.ls_sid);
2798
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01002799 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02002800 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002801 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 break;
2803 case ISN_STORESCRIPT:
2804 {
2805 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01002806 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
2808 + iptr->isn_arg.script.script_idx;
2809
2810 smsg("%4d STORESCRIPT %s in %s", current,
2811 sv->sv_name, si->sn_name);
2812 }
2813 break;
2814 case ISN_STOREOPT:
2815 smsg("%4d STOREOPT &%s", current,
2816 iptr->isn_arg.storeopt.so_name);
2817 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002818 case ISN_STOREENV:
2819 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
2820 break;
2821 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01002822 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01002823 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002824 case ISN_STORENR:
2825 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01002826 iptr->isn_arg.storenr.stnr_val,
2827 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 break;
2829
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002830 case ISN_STORELIST:
2831 smsg("%4d STORELIST", current);
2832 break;
2833
2834 case ISN_STOREDICT:
2835 smsg("%4d STOREDICT", current);
2836 break;
2837
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 // constants
2839 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01002840 smsg("%4d PUSHNR %lld", current,
2841 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 break;
2843 case ISN_PUSHBOOL:
2844 case ISN_PUSHSPEC:
2845 smsg("%4d PUSH %s", current,
2846 get_var_special_name(iptr->isn_arg.number));
2847 break;
2848 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002849#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01002851#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 break;
2853 case ISN_PUSHS:
2854 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
2855 break;
2856 case ISN_PUSHBLOB:
2857 {
2858 char_u *r;
2859 char_u numbuf[NUMBUFLEN];
2860 char_u *tofree;
2861
2862 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01002863 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 vim_free(tofree);
2865 }
2866 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002867 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002868 {
2869 char *name = (char *)iptr->isn_arg.string;
2870
2871 smsg("%4d PUSHFUNC \"%s\"", current,
2872 name == NULL ? "[none]" : name);
2873 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002874 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002875 case ISN_PUSHCHANNEL:
2876#ifdef FEAT_JOB_CHANNEL
2877 {
2878 channel_T *channel = iptr->isn_arg.channel;
2879
2880 smsg("%4d PUSHCHANNEL %d", current,
2881 channel == NULL ? 0 : channel->ch_id);
2882 }
2883#endif
2884 break;
2885 case ISN_PUSHJOB:
2886#ifdef FEAT_JOB_CHANNEL
2887 {
2888 typval_T tv;
2889 char_u *name;
2890
2891 tv.v_type = VAR_JOB;
2892 tv.vval.v_job = iptr->isn_arg.job;
2893 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01002894 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002895 }
2896#endif
2897 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 case ISN_PUSHEXC:
2899 smsg("%4d PUSH v:exception", current);
2900 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002901 case ISN_UNLET:
2902 smsg("%4d UNLET%s %s", current,
2903 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2904 iptr->isn_arg.unlet.ul_name);
2905 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002906 case ISN_UNLETENV:
2907 smsg("%4d UNLETENV%s $%s", current,
2908 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
2909 iptr->isn_arg.unlet.ul_name);
2910 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002911 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01002912 smsg("%4d NEWLIST size %lld", current,
2913 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 break;
2915 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01002916 smsg("%4d NEWDICT size %lld", current,
2917 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 break;
2919
2920 // function call
2921 case ISN_BCALL:
2922 {
2923 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
2924
2925 smsg("%4d BCALL %s(argc %d)", current,
2926 internal_func_name(cbfunc->cbf_idx),
2927 cbfunc->cbf_argcount);
2928 }
2929 break;
2930 case ISN_DCALL:
2931 {
2932 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
2933 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
2934 + cdfunc->cdf_idx;
2935
2936 smsg("%4d DCALL %s(argc %d)", current,
2937 df->df_ufunc->uf_name_exp != NULL
2938 ? df->df_ufunc->uf_name_exp
2939 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
2940 }
2941 break;
2942 case ISN_UCALL:
2943 {
2944 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2945
2946 smsg("%4d UCALL %s(argc %d)", current,
2947 cufunc->cuf_name, cufunc->cuf_argcount);
2948 }
2949 break;
2950 case ISN_PCALL:
2951 {
2952 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
2953
2954 smsg("%4d PCALL%s (argc %d)", current,
2955 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
2956 }
2957 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002958 case ISN_PCALL_END:
2959 smsg("%4d PCALL end", current);
2960 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002961 case ISN_RETURN:
2962 smsg("%4d RETURN", current);
2963 break;
2964 case ISN_FUNCREF:
2965 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002966 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002968 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002969
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002970 smsg("%4d FUNCREF %s $%d", current, df->df_ufunc->uf_name,
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02002971 funcref->fr_var_idx + dfunc->df_varcount);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 }
2973 break;
2974
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002975 case ISN_NEWFUNC:
2976 {
2977 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2978
2979 smsg("%4d NEWFUNC %s %s", current,
2980 newfunc->nf_lambda, newfunc->nf_global);
2981 }
2982 break;
2983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002984 case ISN_JUMP:
2985 {
2986 char *when = "?";
2987
2988 switch (iptr->isn_arg.jump.jump_when)
2989 {
2990 case JUMP_ALWAYS:
2991 when = "JUMP";
2992 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 case JUMP_AND_KEEP_IF_TRUE:
2994 when = "JUMP_AND_KEEP_IF_TRUE";
2995 break;
2996 case JUMP_IF_FALSE:
2997 when = "JUMP_IF_FALSE";
2998 break;
2999 case JUMP_AND_KEEP_IF_FALSE:
3000 when = "JUMP_AND_KEEP_IF_FALSE";
3001 break;
3002 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003003 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 iptr->isn_arg.jump.jump_where);
3005 }
3006 break;
3007
3008 case ISN_FOR:
3009 {
3010 forloop_T *forloop = &iptr->isn_arg.forloop;
3011
3012 smsg("%4d FOR $%d -> %d", current,
3013 forloop->for_idx, forloop->for_end);
3014 }
3015 break;
3016
3017 case ISN_TRY:
3018 {
3019 try_T *try = &iptr->isn_arg.try;
3020
3021 smsg("%4d TRY catch -> %d, finally -> %d", current,
3022 try->try_catch, try->try_finally);
3023 }
3024 break;
3025 case ISN_CATCH:
3026 // TODO
3027 smsg("%4d CATCH", current);
3028 break;
3029 case ISN_ENDTRY:
3030 smsg("%4d ENDTRY", current);
3031 break;
3032 case ISN_THROW:
3033 smsg("%4d THROW", current);
3034 break;
3035
3036 // expression operations on number
3037 case ISN_OPNR:
3038 case ISN_OPFLOAT:
3039 case ISN_OPANY:
3040 {
3041 char *what;
3042 char *ins;
3043
3044 switch (iptr->isn_arg.op.op_type)
3045 {
3046 case EXPR_MULT: what = "*"; break;
3047 case EXPR_DIV: what = "/"; break;
3048 case EXPR_REM: what = "%"; break;
3049 case EXPR_SUB: what = "-"; break;
3050 case EXPR_ADD: what = "+"; break;
3051 default: what = "???"; break;
3052 }
3053 switch (iptr->isn_type)
3054 {
3055 case ISN_OPNR: ins = "OPNR"; break;
3056 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3057 case ISN_OPANY: ins = "OPANY"; break;
3058 default: ins = "???"; break;
3059 }
3060 smsg("%4d %s %s", current, ins, what);
3061 }
3062 break;
3063
3064 case ISN_COMPAREBOOL:
3065 case ISN_COMPARESPECIAL:
3066 case ISN_COMPARENR:
3067 case ISN_COMPAREFLOAT:
3068 case ISN_COMPARESTRING:
3069 case ISN_COMPAREBLOB:
3070 case ISN_COMPARELIST:
3071 case ISN_COMPAREDICT:
3072 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003073 case ISN_COMPAREANY:
3074 {
3075 char *p;
3076 char buf[10];
3077 char *type;
3078
3079 switch (iptr->isn_arg.op.op_type)
3080 {
3081 case EXPR_EQUAL: p = "=="; break;
3082 case EXPR_NEQUAL: p = "!="; break;
3083 case EXPR_GREATER: p = ">"; break;
3084 case EXPR_GEQUAL: p = ">="; break;
3085 case EXPR_SMALLER: p = "<"; break;
3086 case EXPR_SEQUAL: p = "<="; break;
3087 case EXPR_MATCH: p = "=~"; break;
3088 case EXPR_IS: p = "is"; break;
3089 case EXPR_ISNOT: p = "isnot"; break;
3090 case EXPR_NOMATCH: p = "!~"; break;
3091 default: p = "???"; break;
3092 }
3093 STRCPY(buf, p);
3094 if (iptr->isn_arg.op.op_ic == TRUE)
3095 strcat(buf, "?");
3096 switch(iptr->isn_type)
3097 {
3098 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3099 case ISN_COMPARESPECIAL:
3100 type = "COMPARESPECIAL"; break;
3101 case ISN_COMPARENR: type = "COMPARENR"; break;
3102 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3103 case ISN_COMPARESTRING:
3104 type = "COMPARESTRING"; break;
3105 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3106 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3107 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3108 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003109 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3110 default: type = "???"; break;
3111 }
3112
3113 smsg("%4d %s %s", current, type, buf);
3114 }
3115 break;
3116
3117 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3118 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3119
3120 // expression operations
3121 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003122 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
3123 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003124 case ISN_SLICE: smsg("%4d SLICE %lld",
3125 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003126 case ISN_GETITEM: smsg("%4d ITEM %lld",
3127 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003128 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3129 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 iptr->isn_arg.string); break;
3131 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3132
3133 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
3134 case ISN_CHECKTYPE: smsg("%4d CHECKTYPE %s stack[%d]", current,
3135 vartype_name(iptr->isn_arg.type.ct_type),
3136 iptr->isn_arg.type.ct_off);
3137 break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003138 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3139 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3140 iptr->isn_arg.checklen.cl_min_len);
3141 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003142 case ISN_2BOOL: if (iptr->isn_arg.number)
3143 smsg("%4d INVERT (!val)", current);
3144 else
3145 smsg("%4d 2BOOL (!!val)", current);
3146 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003147 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3148 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003149 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003150 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3151 (long long)(iptr->isn_arg.number));
3152 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003153
Bram Moolenaar389df252020-07-09 21:20:47 +02003154 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3155 iptr->isn_arg.shuffle.shfl_item,
3156 iptr->isn_arg.shuffle.shfl_up);
3157 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 case ISN_DROP: smsg("%4d DROP", current); break;
3159 }
3160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161}
3162
3163/*
3164 * Return TRUE when "tv" is not falsey: non-zero, non-empty string, non-empty
3165 * list, etc. Mostly like what JavaScript does, except that empty list and
3166 * empty dictionary are FALSE.
3167 */
3168 int
3169tv2bool(typval_T *tv)
3170{
3171 switch (tv->v_type)
3172 {
3173 case VAR_NUMBER:
3174 return tv->vval.v_number != 0;
3175 case VAR_FLOAT:
3176#ifdef FEAT_FLOAT
3177 return tv->vval.v_float != 0.0;
3178#else
3179 break;
3180#endif
3181 case VAR_PARTIAL:
3182 return tv->vval.v_partial != NULL;
3183 case VAR_FUNC:
3184 case VAR_STRING:
3185 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3186 case VAR_LIST:
3187 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3188 case VAR_DICT:
3189 return tv->vval.v_dict != NULL
3190 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3191 case VAR_BOOL:
3192 case VAR_SPECIAL:
3193 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3194 case VAR_JOB:
3195#ifdef FEAT_JOB_CHANNEL
3196 return tv->vval.v_job != NULL;
3197#else
3198 break;
3199#endif
3200 case VAR_CHANNEL:
3201#ifdef FEAT_JOB_CHANNEL
3202 return tv->vval.v_channel != NULL;
3203#else
3204 break;
3205#endif
3206 case VAR_BLOB:
3207 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3208 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003209 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003210 case VAR_VOID:
3211 break;
3212 }
3213 return FALSE;
3214}
3215
3216/*
3217 * If "tv" is a string give an error and return FAIL.
3218 */
3219 int
3220check_not_string(typval_T *tv)
3221{
3222 if (tv->v_type == VAR_STRING)
3223 {
3224 emsg(_("E1030: Using a String as a Number"));
3225 clear_tv(tv);
3226 return FAIL;
3227 }
3228 return OK;
3229}
3230
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231
3232#endif // FEAT_EVAL