blob: 606ce0cd3595a0f60d6d316e941b341345f99dc0 [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
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100230 // If depth of calling is getting too high, don't execute the function.
231 if (funcdepth_increment() == FAIL)
232 return FAIL;
233
Bram Moolenaarfe270812020-04-11 22:31:27 +0200234 // Move the vararg-list to below the missing optional arguments.
235 if (vararg_count > 0 && arg_to_add > 0)
236 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100237
238 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
243 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
245 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200246 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
247 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
248 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200249 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250
251 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200252 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 if (dfunc->df_has_closure)
255 {
256 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
257
258 tv->v_type = VAR_NUMBER;
259 tv->vval.v_number = 0;
260 }
261 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262
263 // Set execution state to the start of the called function.
264 ectx->ec_dfunc_idx = cdf_idx;
265 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200266 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
267 if (entry != NULL)
268 {
269 // Set the script context to the script where the function was defined.
270 // TODO: save more than the SID?
271 entry->es_save_sid = current_sctx.sc_sid;
272 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
273 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100274
275 // Decide where to start execution, handles optional arguments.
276 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 return OK;
279}
280
281// Get pointer to item in the stack.
282#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
283
284/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 * Used when returning from a function: Check if any closure is still
286 * referenced. If so then move the arguments and variables to a separate piece
287 * of stack to be used when the closure is called.
288 * When "free_arguments" is TRUE the arguments are to be freed.
289 * Returns FAIL when out of memory.
290 */
291 static int
292handle_closure_in_use(ectx_T *ectx, int free_arguments)
293{
294 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
295 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200296 int argcount;
297 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200298 int idx;
299 typval_T *tv;
300 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 garray_T *gap = &ectx->ec_funcrefs;
302 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200305 return OK; // function was freed
306 if (dfunc->df_has_closure == 0)
307 return OK; // no closures
308 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
309 closure_count = tv->vval.v_number;
310 if (closure_count == 0)
311 return OK; // no funcrefs created
312
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200313 argcount = ufunc_argcount(dfunc->df_ufunc);
314 top = ectx->ec_frame_idx - argcount;
315
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200316 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200318 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200319 partial_T *pt;
320 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200322 if (off < 0)
323 continue; // count is off or already done
324 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200326 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200328 int i;
329
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200330 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200331 // unreferenced on return.
332 for (i = 0; i < dfunc->df_varcount; ++i)
333 {
334 typval_T *stv = STACK_TV(ectx->ec_frame_idx
335 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200337 --refcount;
338 }
339 if (refcount > 1)
340 {
341 closure_in_use = TRUE;
342 break;
343 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200344 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345 }
346
347 if (closure_in_use)
348 {
349 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
350 typval_T *stack;
351
352 // A closure is using the arguments and/or local variables.
353 // Move them to the called function.
354 if (funcstack == NULL)
355 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200356 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
357 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200358 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
359 funcstack->fs_ga.ga_data = stack;
360 if (stack == NULL)
361 {
362 vim_free(funcstack);
363 return FAIL;
364 }
365
366 // Move or copy the arguments.
367 for (idx = 0; idx < argcount; ++idx)
368 {
369 tv = STACK_TV(top + idx);
370 if (free_arguments)
371 {
372 *(stack + idx) = *tv;
373 tv->v_type = VAR_UNKNOWN;
374 }
375 else
376 copy_tv(tv, stack + idx);
377 }
378 // Move the local variables.
379 for (idx = 0; idx < dfunc->df_varcount; ++idx)
380 {
381 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200382
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200383 // A partial created for a local function, that is also used as a
384 // local variable, has a reference count for the variable, thus
385 // will never go down to zero. When all these refcounts are one
386 // then the funcstack is unused. We need to count how many we have
387 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
389 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200390 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200393 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
394 gap->ga_len - closure_count + i])
395 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 }
397
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 tv->v_type = VAR_UNKNOWN;
400 }
401
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
405 - closure_count + idx];
406 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408 ++funcstack->fs_refcount;
409 pt->pt_funcstack = funcstack;
410 pt->pt_ectx_stack = &funcstack->fs_ga;
411 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413 }
414 }
415
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < closure_count; ++idx)
417 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
418 - closure_count + idx]);
419 gap->ga_len -= closure_count;
420 if (gap->ga_len == 0)
421 ga_clear(gap);
422
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 return OK;
424}
425
426/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200427 * Called when a partial is freed or its reference count goes down to one. The
428 * funcstack may be the only reference to the partials in the local variables.
429 * Go over all of them, the funcref and can be freed if all partials
430 * referencing the funcstack have a reference count of one.
431 */
432 void
433funcstack_check_refcount(funcstack_T *funcstack)
434{
435 int i;
436 garray_T *gap = &funcstack->fs_ga;
437 int done = 0;
438
439 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
440 return;
441 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
442 {
443 typval_T *tv = ((typval_T *)gap->ga_data) + i;
444
445 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
446 && tv->vval.v_partial->pt_funcstack == funcstack
447 && tv->vval.v_partial->pt_refcount == 1)
448 ++done;
449 }
450 if (done == funcstack->fs_min_refcount)
451 {
452 typval_T *stack = gap->ga_data;
453
454 // All partials referencing the funcstack have a reference count of
455 // one, thus the funcstack is no longer of use.
456 for (i = 0; i < gap->ga_len; ++i)
457 clear_tv(stack + i);
458 vim_free(stack);
459 vim_free(funcstack);
460 }
461}
462
463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 * Return from the current function.
465 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467func_return(ectx_T *ectx)
468{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100470 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
472 + ectx->ec_dfunc_idx;
473 int argcount = ufunc_argcount(dfunc->df_ufunc);
474 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200475 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
477 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200478 entry = estack_pop();
479 if (entry != NULL)
480 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200482 if (handle_closure_in_use(ectx, TRUE) == FAIL)
483 return FAIL;
484
485 // Clear the arguments.
486 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
487 clear_tv(STACK_TV(idx));
488
489 // Clear local variables and temp values, but not the return value.
490 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100491 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100494 // The return value should be on top of the stack. However, when aborting
495 // it may not be there and ec_frame_idx is the top of the stack.
496 ret_idx = ectx->ec_stack.ga_len - 1;
497 if (ret_idx == ectx->ec_frame_idx + 4)
498 ret_idx = 0;
499
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100500 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200501 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
502 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200503 ectx->ec_outer_stack =
504 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
505 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
506 // restoring ec_frame_idx must be last
507 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
509 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100510
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100511 if (ret_idx > 0)
512 {
513 // Reset the stack to the position before the call, with a spot for the
514 // return value, moved there from above the frame.
515 ectx->ec_stack.ga_len = top + 1;
516 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
517 }
518 else
519 // Reset the stack to the position before the call.
520 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100522 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200523 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524}
525
526#undef STACK_TV
527
528/*
529 * Prepare arguments and rettv for calling a builtin or user function.
530 */
531 static int
532call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
533{
534 int idx;
535 typval_T *tv;
536
537 // Move arguments from bottom of the stack to argvars[] and add terminator.
538 for (idx = 0; idx < argcount; ++idx)
539 argvars[idx] = *STACK_TV_BOT(idx - argcount);
540 argvars[argcount].v_type = VAR_UNKNOWN;
541
542 // Result replaces the arguments on the stack.
543 if (argcount > 0)
544 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200545 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 else
548 ++ectx->ec_stack.ga_len;
549
550 // Default return value is zero.
551 tv = STACK_TV_BOT(-1);
552 tv->v_type = VAR_NUMBER;
553 tv->vval.v_number = 0;
554
555 return OK;
556}
557
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200558// Ugly global to avoid passing the execution context around through many
559// layers.
560static ectx_T *current_ectx = NULL;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562/*
563 * Call a builtin function by index.
564 */
565 static int
566call_bfunc(int func_idx, int argcount, ectx_T *ectx)
567{
568 typval_T argvars[MAX_FUNC_ARGS];
569 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100570 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200571 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
573 if (call_prepare(argcount, argvars, ectx) == FAIL)
574 return FAIL;
575
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200576 // Call the builtin function. Set "current_ectx" so that when it
577 // recursively invokes call_def_function() a closure context can be set.
578 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200580 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200585
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100586 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return OK;
589}
590
591/*
592 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100593 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 */
595 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100596call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597{
598 typval_T argvars[MAX_FUNC_ARGS];
599 funcexe_T funcexe;
600 int error;
601 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100602 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200605 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200607 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100608 {
Bram Moolenaar50824712020-12-20 21:10:17 +0100609 int error = check_user_func_argcount(ufunc, argcount);
610
611 if (error != FCERR_UNKNOWN)
612 {
613 if (error == FCERR_TOOMANY)
614 semsg(_(e_toomanyarg), ufunc->uf_name);
615 else
616 semsg(_(e_toofewarg), ufunc->uf_name);
617 return FAIL;
618 }
619
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100620 // The function has been compiled, can call it quickly. For a function
621 // that was defined later: we can call it directly next time.
622 if (iptr != NULL)
623 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100624 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100625 iptr->isn_type = ISN_DCALL;
626 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
627 iptr->isn_arg.dfunc.cdf_argcount = argcount;
628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100629 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100630 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100631
632 if (call_prepare(argcount, argvars, ectx) == FAIL)
633 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200634 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 funcexe.evaluate = TRUE;
636
637 // Call the user function. Result goes in last position on the stack.
638 // TODO: add selfdict if there is one
639 error = call_user_func_check(ufunc, argcount, argvars,
640 STACK_TV_BOT(-1), &funcexe, NULL);
641
642 // Clear the arguments.
643 for (idx = 0; idx < argcount; ++idx)
644 clear_tv(&argvars[idx]);
645
646 if (error != FCERR_NONE)
647 {
648 user_func_error(error, ufunc->uf_name);
649 return FAIL;
650 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100651 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200652 // Error other than from calling the function itself.
653 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100654 return OK;
655}
656
657/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200658 * Return TRUE if an error was given or CTRL-C was pressed.
659 */
660 static int
661vim9_aborting(int prev_called_emsg)
662{
663 return called_emsg > prev_called_emsg || got_int || did_throw;
664}
665
666/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100667 * Execute a function by "name".
668 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670 * Returns FAIL if not found without an error message.
671 */
672 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100673call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100674{
675 ufunc_T *ufunc;
676
677 if (builtin_function(name, -1))
678 {
679 int func_idx = find_internal_func(name);
680
681 if (func_idx < 0)
682 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200683 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100684 return FAIL;
685 return call_bfunc(func_idx, argcount, ectx);
686 }
687
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200688 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200689
690 if (ufunc == NULL)
691 {
692 int called_emsg_before = called_emsg;
693
694 if (script_autoload(name, TRUE))
695 // loaded a package, search for the function again
696 ufunc = find_func(name, FALSE, NULL);
697 if (vim9_aborting(called_emsg_before))
698 return FAIL; // bail out if loading the script caused an error
699 }
700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100702 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703
704 return FAIL;
705}
706
707 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200708call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200710 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200711 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100712 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200713 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714
715 if (tv->v_type == VAR_PARTIAL)
716 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200717 partial_T *pt = tv->vval.v_partial;
718 int i;
719
720 if (pt->pt_argc > 0)
721 {
722 // Make space for arguments from the partial, shift the "argcount"
723 // arguments up.
724 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
725 return FAIL;
726 for (i = 1; i <= argcount; ++i)
727 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
728 ectx->ec_stack.ga_len += pt->pt_argc;
729 argcount += pt->pt_argc;
730
731 // copy the arguments from the partial onto the stack
732 for (i = 0; i < pt->pt_argc; ++i)
733 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735
736 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200737 {
738 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
739
740 // closure may need the function context where it was defined
741 ectx->ec_outer_stack = pt->pt_ectx_stack;
742 ectx->ec_outer_frame = pt->pt_ectx_frame;
743
744 return ret;
745 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100746 name = pt->pt_name;
747 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200748 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100749 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200750 if (name != NULL)
751 {
752 char_u fname_buf[FLEN_FIXED + 1];
753 char_u *tofree = NULL;
754 int error = FCERR_NONE;
755 char_u *fname;
756
757 // May need to translate <SNR>123_ to K_SNR.
758 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
759 if (error != FCERR_NONE)
760 res = FAIL;
761 else
762 res = call_by_name(fname, argcount, ectx, NULL);
763 vim_free(tofree);
764 }
765
766 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100767 {
768 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200769 semsg(_(e_unknownfunc),
770 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100771 return FAIL;
772 }
773 return OK;
774}
775
776/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200777 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
778 * TRUE.
779 */
780 static int
781error_if_locked(int lock, char *error)
782{
783 if (lock & (VAR_LOCKED | VAR_FIXED))
784 {
785 emsg(_(error));
786 return TRUE;
787 }
788 return FALSE;
789}
790
791/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100792 * Store "tv" in variable "name".
793 * This is for s: and g: variables.
794 */
795 static void
796store_var(char_u *name, typval_T *tv)
797{
798 funccal_entry_T entry;
799
800 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200801 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100802 restore_funccal();
803}
804
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100805/*
806 * When the value of "sv" is a null list of dict, allocate it.
807 */
808 static void
809allocate_if_null(typval_T *tv)
810{
811 switch (tv->v_type)
812 {
813 case VAR_LIST:
814 if (tv->vval.v_list == NULL)
815 rettv_list_alloc(tv);
816 break;
817 case VAR_DICT:
818 if (tv->vval.v_dict == NULL)
819 rettv_dict_alloc(tv);
820 break;
821 default:
822 break;
823 }
824}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200825
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100826/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827 * Execute a function by "name".
828 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100829 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 */
831 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100832call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833{
Bram Moolenaared677f52020-08-12 16:38:10 +0200834 int called_emsg_before = called_emsg;
835 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100836
Bram Moolenaared677f52020-08-12 16:38:10 +0200837 res = call_by_name(name, argcount, ectx, iptr);
838 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100839 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200840 dictitem_T *v;
841
842 v = find_var(name, NULL, FALSE);
843 if (v == NULL)
844 {
845 semsg(_(e_unknownfunc), name);
846 return FAIL;
847 }
848 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
849 {
850 semsg(_(e_unknownfunc), name);
851 return FAIL;
852 }
853 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100854 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200855 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856}
857
858/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100859 * When a function reference is used, fill a partial with the information
860 * needed, especially when it is used as a closure.
861 */
862 static int
863fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
864{
865 pt->pt_func = ufunc;
866 pt->pt_refcount = 1;
867
868 if (pt->pt_func->uf_flags & FC_CLOSURE)
869 {
870 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
871 + ectx->ec_dfunc_idx;
872
873 // The closure needs to find arguments and local
874 // variables in the current stack.
875 pt->pt_ectx_stack = &ectx->ec_stack;
876 pt->pt_ectx_frame = ectx->ec_frame_idx;
877
878 // If this function returns and the closure is still
879 // being used, we need to make a copy of the context
880 // (arguments and local variables). Store a reference
881 // to the partial so we can handle that.
882 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
883 {
884 vim_free(pt);
885 return FAIL;
886 }
887 // Extra variable keeps the count of closures created
888 // in the current function call.
889 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
890 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
891
892 ((partial_T **)ectx->ec_funcrefs.ga_data)
893 [ectx->ec_funcrefs.ga_len] = pt;
894 ++pt->pt_refcount;
895 ++ectx->ec_funcrefs.ga_len;
896 }
897 ++pt->pt_func->uf_refcount;
898 return OK;
899}
900
901/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100902 * Call a "def" function from old Vim script.
903 * Return OK or FAIL.
904 */
905 int
906call_def_function(
907 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200908 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100909 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200910 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100911 typval_T *rettv) // return value
912{
913 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200914 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200915 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100916 typval_T *tv;
917 int idx;
918 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100919 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200920 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200921 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100922 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200923 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200924 msglist_T **saved_msg_list = NULL;
925 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200926 cmdmod_T save_cmdmod;
927 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100928 int save_emsg_silent_def = emsg_silent_def;
929 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100930 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100931 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100932
933// Get pointer to item in the stack.
934#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
935
936// Get pointer to item at the bottom of the stack, -1 is the bottom.
937#undef STACK_TV_BOT
938#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
939
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200940// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200941#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 +0100942
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200943// Like STACK_TV_VAR but use the outer scope
944#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
945
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200946 if (ufunc->uf_def_status == UF_NOT_COMPILED
947 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200948 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200949 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100950 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200951 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200952 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200953 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200954 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200955
Bram Moolenaar09689a02020-05-09 22:50:08 +0200956 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200957 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200958 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
959 + ufunc->uf_dfunc_idx;
960 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200961 {
962 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200963 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200964 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200965 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100966
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100967 // If depth of calling is getting too high, don't execute the function.
968 orig_funcdepth = funcdepth_get();
969 if (funcdepth_increment() == FAIL)
970 return FAIL;
971
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200972 CLEAR_FIELD(ectx);
973 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
974 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
975 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100976 {
977 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200978 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100980 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200981 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100982
983 // Put arguments on the stack.
984 for (idx = 0; idx < argc; ++idx)
985 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200986 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200987 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
988 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200989 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100990 copy_tv(&argv[idx], STACK_TV_BOT(0));
991 ++ectx.ec_stack.ga_len;
992 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200993
994 // Turn varargs into a list. Empty list if no args.
995 if (ufunc->uf_va_name != NULL)
996 {
997 int vararg_count = argc - ufunc->uf_args.ga_len;
998
999 if (vararg_count < 0)
1000 vararg_count = 0;
1001 else
1002 argc -= vararg_count;
1003 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001004 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001005
1006 // Check the type of the list items.
1007 tv = STACK_TV_BOT(-1);
1008 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001009 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001010 && ufunc->uf_va_type->tt_member != &t_any
1011 && tv->vval.v_list != NULL)
1012 {
1013 type_T *expected = ufunc->uf_va_type->tt_member;
1014 listitem_T *li = tv->vval.v_list->lv_first;
1015
1016 for (idx = 0; idx < vararg_count; ++idx)
1017 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001018 if (check_typval_type(expected, &li->li_tv,
1019 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001020 goto failed_early;
1021 li = li->li_next;
1022 }
1023 }
1024
Bram Moolenaar23e03252020-04-12 22:22:31 +02001025 if (defcount > 0)
1026 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001027 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001028 --ectx.ec_stack.ga_len;
1029 }
1030
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001031 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001032 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001033 if (defcount > 0)
1034 for (idx = 0; idx < defcount; ++idx)
1035 {
1036 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1037 ++ectx.ec_stack.ga_len;
1038 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001039 if (ufunc->uf_va_name != NULL)
1040 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041
1042 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001043 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1044 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001045
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001046 if (partial != NULL)
1047 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001048 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1049 {
1050 // TODO: is this always the right way?
1051 ectx.ec_outer_stack = &current_ectx->ec_stack;
1052 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1053 }
1054 else
1055 {
1056 ectx.ec_outer_stack = partial->pt_ectx_stack;
1057 ectx.ec_outer_frame = partial->pt_ectx_frame;
1058 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001059 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001060 else if (ufunc->uf_partial != NULL)
1061 {
1062 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1063 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1064 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066 // dummy frame entries
1067 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1068 {
1069 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1070 ++ectx.ec_stack.ga_len;
1071 }
1072
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001073 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001074 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001075 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1076 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001077
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001078 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001079 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001080 ectx.ec_stack.ga_len += dfunc->df_varcount;
1081 if (dfunc->df_has_closure)
1082 {
1083 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1084 STACK_TV_VAR(idx)->vval.v_number = 0;
1085 ++ectx.ec_stack.ga_len;
1086 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001087
1088 ectx.ec_instr = dfunc->df_instr;
1089 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001090
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001091 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001092 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001093 estack_push_ufunc(ufunc, 1);
1094 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001095 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1096
Bram Moolenaar352134b2020-10-17 22:04:08 +02001097 // Use a specific location for storing error messages to be converted to an
1098 // exception.
1099 saved_msg_list = msg_list;
1100 msg_list = &private_msg_list;
1101
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001102 // Do turn errors into exceptions.
1103 suppress_errthrow = FALSE;
1104
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001105 // When ":silent!" was used before calling then we still abort the
1106 // function. If ":silent!" is used in the function then we don't.
1107 emsg_silent_def = emsg_silent;
1108 did_emsg_def = 0;
1109
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001110 // Decide where to start execution, handles optional arguments.
1111 init_instr_idx(ufunc, argc, &ectx);
1112
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 for (;;)
1114 {
1115 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001116
Bram Moolenaar270d0382020-05-15 21:42:53 +02001117 if (++breakcheck_count >= 100)
1118 {
1119 line_breakcheck();
1120 breakcheck_count = 0;
1121 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001122 if (got_int)
1123 {
1124 // Turn CTRL-C into an exception.
1125 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001126 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001127 goto failed;
1128 did_throw = TRUE;
1129 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130
Bram Moolenaara26b9702020-04-18 19:53:28 +02001131 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1132 {
1133 // Turn an error message into an exception.
1134 did_emsg = FALSE;
1135 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1136 goto failed;
1137 did_throw = TRUE;
1138 *msg_list = NULL;
1139 }
1140
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001141 if (did_throw && !ectx.ec_in_catch)
1142 {
1143 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001144 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145
1146 // An exception jumps to the first catch, finally, or returns from
1147 // the current function.
1148 if (trystack->ga_len > 0)
1149 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001150 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151 {
1152 // jump to ":catch" or ":finally"
1153 ectx.ec_in_catch = TRUE;
1154 ectx.ec_iidx = trycmd->tcd_catch_idx;
1155 }
1156 else
1157 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001158 // Not inside try or need to return from current functions.
1159 // Push a dummy return value.
1160 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1161 goto failed;
1162 tv = STACK_TV_BOT(0);
1163 tv->v_type = VAR_NUMBER;
1164 tv->vval.v_number = 0;
1165 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001166 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001167 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001168 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001169 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001170 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1171 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 goto done;
1173 }
1174
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001175 if (func_return(&ectx) == FAIL)
1176 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 }
1178 continue;
1179 }
1180
1181 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1182 switch (iptr->isn_type)
1183 {
1184 // execute Ex command line
1185 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001186 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001187 SOURCING_LNUM = iptr->isn_lnum;
1188 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001189 if (did_emsg)
1190 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001191 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001192 break;
1193
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001194 // execute Ex command from pieces on the stack
1195 case ISN_EXECCONCAT:
1196 {
1197 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001198 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001199 int pass;
1200 int i;
1201 char_u *cmd = NULL;
1202 char_u *str;
1203
1204 for (pass = 1; pass <= 2; ++pass)
1205 {
1206 for (i = 0; i < count; ++i)
1207 {
1208 tv = STACK_TV_BOT(i - count);
1209 str = tv->vval.v_string;
1210 if (str != NULL && *str != NUL)
1211 {
1212 if (pass == 2)
1213 STRCPY(cmd + len, str);
1214 len += STRLEN(str);
1215 }
1216 if (pass == 2)
1217 clear_tv(tv);
1218 }
1219 if (pass == 1)
1220 {
1221 cmd = alloc(len + 1);
1222 if (cmd == NULL)
1223 goto failed;
1224 len = 0;
1225 }
1226 }
1227
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001228 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001229 do_cmdline_cmd(cmd);
1230 vim_free(cmd);
1231 }
1232 break;
1233
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001234 // execute :echo {string} ...
1235 case ISN_ECHO:
1236 {
1237 int count = iptr->isn_arg.echo.echo_count;
1238 int atstart = TRUE;
1239 int needclr = TRUE;
1240
1241 for (idx = 0; idx < count; ++idx)
1242 {
1243 tv = STACK_TV_BOT(idx - count);
1244 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1245 &atstart, &needclr);
1246 clear_tv(tv);
1247 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001248 if (needclr)
1249 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001250 ectx.ec_stack.ga_len -= count;
1251 }
1252 break;
1253
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001254 // :execute {string} ...
1255 // :echomsg {string} ...
1256 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001257 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001258 case ISN_ECHOMSG:
1259 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001260 {
1261 int count = iptr->isn_arg.number;
1262 garray_T ga;
1263 char_u buf[NUMBUFLEN];
1264 char_u *p;
1265 int len;
1266 int failed = FALSE;
1267
1268 ga_init2(&ga, 1, 80);
1269 for (idx = 0; idx < count; ++idx)
1270 {
1271 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001272 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001273 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001274 if (tv->v_type == VAR_CHANNEL
1275 || tv->v_type == VAR_JOB)
1276 {
1277 SOURCING_LNUM = iptr->isn_lnum;
1278 emsg(_(e_inval_string));
1279 break;
1280 }
1281 else
1282 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001283 }
1284 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001285 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001286
1287 len = (int)STRLEN(p);
1288 if (ga_grow(&ga, len + 2) == FAIL)
1289 failed = TRUE;
1290 else
1291 {
1292 if (ga.ga_len > 0)
1293 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1294 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1295 ga.ga_len += len;
1296 }
1297 clear_tv(tv);
1298 }
1299 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001300 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001301 {
1302 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001303 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001304 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001305
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001306 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001307 {
1308 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001309 {
1310 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001311 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001312 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001313 {
1314 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001315 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001316 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001317 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001318 else
1319 {
1320 msg_sb_eol();
1321 if (iptr->isn_type == ISN_ECHOMSG)
1322 {
1323 msg_attr(ga.ga_data, echo_attr);
1324 out_flush();
1325 }
1326 else
1327 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001328 SOURCING_LNUM = iptr->isn_lnum;
1329 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001330 }
1331 }
1332 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001333 ga_clear(&ga);
1334 }
1335 break;
1336
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337 // load local variable or argument
1338 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001339 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001340 goto failed;
1341 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1342 ++ectx.ec_stack.ga_len;
1343 break;
1344
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001345 // load variable or argument from outer scope
1346 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001347 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001348 goto failed;
1349 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1350 STACK_TV_BOT(0));
1351 ++ectx.ec_stack.ga_len;
1352 break;
1353
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001354 // load v: variable
1355 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001356 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357 goto failed;
1358 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1359 ++ectx.ec_stack.ga_len;
1360 break;
1361
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001362 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001363 case ISN_LOADSCRIPT:
1364 {
1365 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001366 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 svar_T *sv;
1368
1369 sv = ((svar_T *)si->sn_var_vals.ga_data)
1370 + iptr->isn_arg.script.script_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001371 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001372 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001373 goto failed;
1374 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1375 ++ectx.ec_stack.ga_len;
1376 }
1377 break;
1378
1379 // load s: variable in old script
1380 case ISN_LOADS:
1381 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001382 hashtab_T *ht = &SCRIPT_VARS(
1383 iptr->isn_arg.loadstore.ls_sid);
1384 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387 if (di == NULL)
1388 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001389 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001390 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001391 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001392 }
1393 else
1394 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001395 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 goto failed;
1397 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1398 ++ectx.ec_stack.ga_len;
1399 }
1400 }
1401 break;
1402
Bram Moolenaard3aac292020-04-19 14:32:17 +02001403 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001405 case ISN_LOADB:
1406 case ISN_LOADW:
1407 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001409 dictitem_T *di = NULL;
1410 hashtab_T *ht = NULL;
1411 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001412
Bram Moolenaard3aac292020-04-19 14:32:17 +02001413 switch (iptr->isn_type)
1414 {
1415 case ISN_LOADG:
1416 ht = get_globvar_ht();
1417 namespace = 'g';
1418 break;
1419 case ISN_LOADB:
1420 ht = &curbuf->b_vars->dv_hashtab;
1421 namespace = 'b';
1422 break;
1423 case ISN_LOADW:
1424 ht = &curwin->w_vars->dv_hashtab;
1425 namespace = 'w';
1426 break;
1427 case ISN_LOADT:
1428 ht = &curtab->tp_vars->dv_hashtab;
1429 namespace = 't';
1430 break;
1431 default: // Cannot reach here
1432 goto failed;
1433 }
1434 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001435
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001436 if (di == NULL)
1437 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001438 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001439 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001440 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001441 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001442 }
1443 else
1444 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001445 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446 goto failed;
1447 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1448 ++ectx.ec_stack.ga_len;
1449 }
1450 }
1451 break;
1452
Bram Moolenaar03290b82020-12-19 16:30:44 +01001453 // load autoload variable
1454 case ISN_LOADAUTO:
1455 {
1456 char_u *name = iptr->isn_arg.string;
1457
1458 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1459 goto failed;
1460 SOURCING_LNUM = iptr->isn_lnum;
1461 if (eval_variable(name, STRLEN(name),
1462 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1463 goto on_error;
1464 ++ectx.ec_stack.ga_len;
1465 }
1466 break;
1467
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001468 // load g:/b:/w:/t: namespace
1469 case ISN_LOADGDICT:
1470 case ISN_LOADBDICT:
1471 case ISN_LOADWDICT:
1472 case ISN_LOADTDICT:
1473 {
1474 dict_T *d = NULL;
1475
1476 switch (iptr->isn_type)
1477 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001478 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1479 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1480 case ISN_LOADWDICT: d = curwin->w_vars; break;
1481 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001482 default: // Cannot reach here
1483 goto failed;
1484 }
1485 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1486 goto failed;
1487 tv = STACK_TV_BOT(0);
1488 tv->v_type = VAR_DICT;
1489 tv->v_lock = 0;
1490 tv->vval.v_dict = d;
1491 ++ectx.ec_stack.ga_len;
1492 }
1493 break;
1494
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001495 // load &option
1496 case ISN_LOADOPT:
1497 {
1498 typval_T optval;
1499 char_u *name = iptr->isn_arg.string;
1500
Bram Moolenaara8c17702020-04-01 21:17:24 +02001501 // This is not expected to fail, name is checked during
1502 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001503 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001505 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001506 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001507 *STACK_TV_BOT(0) = optval;
1508 ++ectx.ec_stack.ga_len;
1509 }
1510 break;
1511
1512 // load $ENV
1513 case ISN_LOADENV:
1514 {
1515 typval_T optval;
1516 char_u *name = iptr->isn_arg.string;
1517
Bram Moolenaar270d0382020-05-15 21:42:53 +02001518 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001519 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001520 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001521 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001522 *STACK_TV_BOT(0) = optval;
1523 ++ectx.ec_stack.ga_len;
1524 }
1525 break;
1526
1527 // load @register
1528 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001529 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001530 goto failed;
1531 tv = STACK_TV_BOT(0);
1532 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001533 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001534 // This may result in NULL, which should be equivalent to an
1535 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001536 tv->vval.v_string = get_reg_contents(
1537 iptr->isn_arg.number, GREG_EXPR_SRC);
1538 ++ectx.ec_stack.ga_len;
1539 break;
1540
1541 // store local variable
1542 case ISN_STORE:
1543 --ectx.ec_stack.ga_len;
1544 tv = STACK_TV_VAR(iptr->isn_arg.number);
1545 clear_tv(tv);
1546 *tv = *STACK_TV_BOT(0);
1547 break;
1548
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001549 // store variable or argument in outer scope
1550 case ISN_STOREOUTER:
1551 --ectx.ec_stack.ga_len;
1552 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1553 clear_tv(tv);
1554 *tv = *STACK_TV_BOT(0);
1555 break;
1556
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001557 // store s: variable in old script
1558 case ISN_STORES:
1559 {
1560 hashtab_T *ht = &SCRIPT_VARS(
1561 iptr->isn_arg.loadstore.ls_sid);
1562 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001563 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001564
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001565 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001566 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001567 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001568 else
1569 {
1570 clear_tv(&di->di_tv);
1571 di->di_tv = *STACK_TV_BOT(0);
1572 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001573 }
1574 break;
1575
1576 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 case ISN_STORESCRIPT:
1578 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001579 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 iptr->isn_arg.script.script_sid);
1581 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1582 + iptr->isn_arg.script.script_idx;
1583
1584 --ectx.ec_stack.ga_len;
1585 clear_tv(sv->sv_tv);
1586 *sv->sv_tv = *STACK_TV_BOT(0);
1587 }
1588 break;
1589
1590 // store option
1591 case ISN_STOREOPT:
1592 {
1593 long n = 0;
1594 char_u *s = NULL;
1595 char *msg;
1596
1597 --ectx.ec_stack.ga_len;
1598 tv = STACK_TV_BOT(0);
1599 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001600 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001601 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001602 if (s == NULL)
1603 s = (char_u *)"";
1604 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001606 // must be VAR_NUMBER, CHECKTYPE makes sure
1607 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1609 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001610 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 if (msg != NULL)
1612 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001613 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001614 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001615 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 }
1618 break;
1619
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001620 // store $ENV
1621 case ISN_STOREENV:
1622 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001623 tv = STACK_TV_BOT(0);
1624 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1625 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001626 break;
1627
1628 // store @r
1629 case ISN_STOREREG:
1630 {
1631 int reg = iptr->isn_arg.number;
1632
1633 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001634 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001635 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001636 tv_get_string(tv), -1, FALSE);
1637 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001638 }
1639 break;
1640
1641 // store v: variable
1642 case ISN_STOREV:
1643 --ectx.ec_stack.ga_len;
1644 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1645 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001646 // should not happen, type is checked when compiling
1647 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001648 break;
1649
Bram Moolenaard3aac292020-04-19 14:32:17 +02001650 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001651 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001652 case ISN_STOREB:
1653 case ISN_STOREW:
1654 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001655 {
1656 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001657 hashtab_T *ht;
1658 switch (iptr->isn_type)
1659 {
1660 case ISN_STOREG:
1661 ht = get_globvar_ht();
1662 break;
1663 case ISN_STOREB:
1664 ht = &curbuf->b_vars->dv_hashtab;
1665 break;
1666 case ISN_STOREW:
1667 ht = &curwin->w_vars->dv_hashtab;
1668 break;
1669 case ISN_STORET:
1670 ht = &curtab->tp_vars->dv_hashtab;
1671 break;
1672 default: // Cannot reach here
1673 goto failed;
1674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675
1676 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001677 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001678 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001679 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 else
1681 {
1682 clear_tv(&di->di_tv);
1683 di->di_tv = *STACK_TV_BOT(0);
1684 }
1685 }
1686 break;
1687
Bram Moolenaar03290b82020-12-19 16:30:44 +01001688 // store an autoload variable
1689 case ISN_STOREAUTO:
1690 SOURCING_LNUM = iptr->isn_lnum;
1691 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1692 clear_tv(STACK_TV_BOT(-1));
1693 --ectx.ec_stack.ga_len;
1694 break;
1695
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001696 // store number in local variable
1697 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001698 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 clear_tv(tv);
1700 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001701 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001702 break;
1703
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001704 // store value in list variable
1705 case ISN_STORELIST:
1706 {
1707 typval_T *tv_idx = STACK_TV_BOT(-2);
1708 varnumber_T lidx = tv_idx->vval.v_number;
1709 typval_T *tv_list = STACK_TV_BOT(-1);
1710 list_T *list = tv_list->vval.v_list;
1711
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001712 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001713 if (lidx < 0 && list->lv_len + lidx >= 0)
1714 // negative index is relative to the end
1715 lidx = list->lv_len + lidx;
1716 if (lidx < 0 || lidx > list->lv_len)
1717 {
1718 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001719 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001720 }
1721 tv = STACK_TV_BOT(-3);
1722 if (lidx < list->lv_len)
1723 {
1724 listitem_T *li = list_find(list, lidx);
1725
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001726 if (error_if_locked(li->li_tv.v_lock,
1727 e_cannot_change_list_item))
1728 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001729 // overwrite existing list item
1730 clear_tv(&li->li_tv);
1731 li->li_tv = *tv;
1732 }
1733 else
1734 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001735 if (error_if_locked(list->lv_lock,
1736 e_cannot_change_list))
1737 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001738 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001739 if (list_append_tv(list, tv) == FAIL)
1740 goto failed;
1741 clear_tv(tv);
1742 }
1743 clear_tv(tv_idx);
1744 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001745 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001746 }
1747 break;
1748
1749 // store value in dict variable
1750 case ISN_STOREDICT:
1751 {
1752 typval_T *tv_key = STACK_TV_BOT(-2);
1753 char_u *key = tv_key->vval.v_string;
1754 typval_T *tv_dict = STACK_TV_BOT(-1);
1755 dict_T *dict = tv_dict->vval.v_dict;
1756 dictitem_T *di;
1757
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001758 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001759 if (dict == NULL)
1760 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001761 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001762 goto on_error;
1763 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001764 if (key == NULL)
1765 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001766 tv = STACK_TV_BOT(-3);
1767 di = dict_find(dict, key, -1);
1768 if (di != NULL)
1769 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001770 if (error_if_locked(di->di_tv.v_lock,
1771 e_cannot_change_dict_item))
1772 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001773 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001774 clear_tv(&di->di_tv);
1775 di->di_tv = *tv;
1776 }
1777 else
1778 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001779 if (error_if_locked(dict->dv_lock,
1780 e_cannot_change_dict))
1781 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001782 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001783 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1784 goto failed;
1785 clear_tv(tv);
1786 }
1787 clear_tv(tv_key);
1788 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001789 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001790 }
1791 break;
1792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 // push constant
1794 case ISN_PUSHNR:
1795 case ISN_PUSHBOOL:
1796 case ISN_PUSHSPEC:
1797 case ISN_PUSHF:
1798 case ISN_PUSHS:
1799 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001800 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001801 case ISN_PUSHCHANNEL:
1802 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001803 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 goto failed;
1805 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001806 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 ++ectx.ec_stack.ga_len;
1808 switch (iptr->isn_type)
1809 {
1810 case ISN_PUSHNR:
1811 tv->v_type = VAR_NUMBER;
1812 tv->vval.v_number = iptr->isn_arg.number;
1813 break;
1814 case ISN_PUSHBOOL:
1815 tv->v_type = VAR_BOOL;
1816 tv->vval.v_number = iptr->isn_arg.number;
1817 break;
1818 case ISN_PUSHSPEC:
1819 tv->v_type = VAR_SPECIAL;
1820 tv->vval.v_number = iptr->isn_arg.number;
1821 break;
1822#ifdef FEAT_FLOAT
1823 case ISN_PUSHF:
1824 tv->v_type = VAR_FLOAT;
1825 tv->vval.v_float = iptr->isn_arg.fnumber;
1826 break;
1827#endif
1828 case ISN_PUSHBLOB:
1829 blob_copy(iptr->isn_arg.blob, tv);
1830 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001831 case ISN_PUSHFUNC:
1832 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001833 if (iptr->isn_arg.string == NULL)
1834 tv->vval.v_string = NULL;
1835 else
1836 tv->vval.v_string =
1837 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001838 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001839 case ISN_PUSHCHANNEL:
1840#ifdef FEAT_JOB_CHANNEL
1841 tv->v_type = VAR_CHANNEL;
1842 tv->vval.v_channel = iptr->isn_arg.channel;
1843 if (tv->vval.v_channel != NULL)
1844 ++tv->vval.v_channel->ch_refcount;
1845#endif
1846 break;
1847 case ISN_PUSHJOB:
1848#ifdef FEAT_JOB_CHANNEL
1849 tv->v_type = VAR_JOB;
1850 tv->vval.v_job = iptr->isn_arg.job;
1851 if (tv->vval.v_job != NULL)
1852 ++tv->vval.v_job->jv_refcount;
1853#endif
1854 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001855 default:
1856 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001857 tv->vval.v_string = vim_strsave(
1858 iptr->isn_arg.string == NULL
1859 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 }
1861 break;
1862
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001863 case ISN_UNLET:
1864 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1865 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001866 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001867 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001868 case ISN_UNLETENV:
1869 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1870 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001871
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001872 case ISN_LOCKCONST:
1873 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1874 break;
1875
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001876 // create a list from items on the stack; uses a single allocation
1877 // for the list header and the items
1878 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001879 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1880 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 break;
1882
1883 // create a dict from items on the stack
1884 case ISN_NEWDICT:
1885 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001886 int count = iptr->isn_arg.number;
1887 dict_T *dict = dict_alloc();
1888 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001889 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890
1891 if (dict == NULL)
1892 goto failed;
1893 for (idx = 0; idx < count; ++idx)
1894 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001895 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001897 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001898 key = tv->vval.v_string == NULL
1899 ? (char_u *)"" : tv->vval.v_string;
1900 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001901 if (item != NULL)
1902 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001904 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001905 dict_unref(dict);
1906 goto on_error;
1907 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001908 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 clear_tv(tv);
1910 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001911 {
1912 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1916 item->di_tv.v_lock = 0;
1917 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001918 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001919 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001920 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001922 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 }
1924
1925 if (count > 0)
1926 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001927 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 goto failed;
1929 else
1930 ++ectx.ec_stack.ga_len;
1931 tv = STACK_TV_BOT(-1);
1932 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001933 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001934 tv->vval.v_dict = dict;
1935 ++dict->dv_refcount;
1936 }
1937 break;
1938
1939 // call a :def function
1940 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001941 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001942 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1943 iptr->isn_arg.dfunc.cdf_argcount,
1944 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001945 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 break;
1947
1948 // call a builtin function
1949 case ISN_BCALL:
1950 SOURCING_LNUM = iptr->isn_lnum;
1951 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1952 iptr->isn_arg.bfunc.cbf_argcount,
1953 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001954 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955 break;
1956
1957 // call a funcref or partial
1958 case ISN_PCALL:
1959 {
1960 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1961 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001962 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963
1964 SOURCING_LNUM = iptr->isn_lnum;
1965 if (pfunc->cpf_top)
1966 {
1967 // funcref is above the arguments
1968 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1969 }
1970 else
1971 {
1972 // Get the funcref from the stack.
1973 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001974 partial_tv = *STACK_TV_BOT(0);
1975 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001976 }
1977 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001978 if (tv == &partial_tv)
1979 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001981 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 }
1983 break;
1984
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001985 case ISN_PCALL_END:
1986 // PCALL finished, arguments have been consumed and replaced by
1987 // the return value. Now clear the funcref from the stack,
1988 // and move the return value in its place.
1989 --ectx.ec_stack.ga_len;
1990 clear_tv(STACK_TV_BOT(-1));
1991 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1992 break;
1993
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001994 // call a user defined function or funcref/partial
1995 case ISN_UCALL:
1996 {
1997 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1998
1999 SOURCING_LNUM = iptr->isn_lnum;
2000 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002001 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002002 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 }
2004 break;
2005
2006 // return from a :def function call
2007 case ISN_RETURN:
2008 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002009 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002010 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002011
2012 if (trystack->ga_len > 0)
2013 trycmd = ((trycmd_T *)trystack->ga_data)
2014 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002015 if (trycmd != NULL
2016 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 && trycmd->tcd_finally_idx != 0)
2018 {
2019 // jump to ":finally"
2020 ectx.ec_iidx = trycmd->tcd_finally_idx;
2021 trycmd->tcd_return = TRUE;
2022 }
2023 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002024 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 }
2026 break;
2027
2028 // push a function reference to a compiled function
2029 case ISN_FUNCREF:
2030 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002031 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2032 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2033 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 if (pt == NULL)
2036 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002037 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002038 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002039 vim_free(pt);
2040 goto failed;
2041 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002042 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2043 &ectx) == FAIL)
2044 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002045
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002046 tv = STACK_TV_BOT(0);
2047 ++ectx.ec_stack.ga_len;
2048 tv->vval.v_partial = pt;
2049 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002050 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002051 }
2052 break;
2053
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002054 // Create a global function from a lambda.
2055 case ISN_NEWFUNC:
2056 {
2057 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002058 ufunc_T *new_ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002059
Bram Moolenaarf112f302020-12-20 17:47:52 +01002060 new_ufunc = copy_func(
2061 newfunc->nf_lambda, newfunc->nf_global);
2062 if (new_ufunc != NULL
2063 && (new_ufunc->uf_flags & FC_CLOSURE))
2064 {
2065 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2066
2067 // Need to create a partial to store the context of the
2068 // function.
2069 if (pt == NULL)
2070 goto failed;
2071 if (fill_partial_and_closure(pt, new_ufunc,
2072 &ectx) == FAIL)
2073 goto failed;
2074 new_ufunc->uf_partial = pt;
2075 --pt->pt_refcount; // not referenced here
2076 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002077 }
2078 break;
2079
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002080 // List functions
2081 case ISN_DEF:
2082 if (iptr->isn_arg.string == NULL)
2083 list_functions(NULL);
2084 else
2085 {
2086 exarg_T ea;
2087
2088 CLEAR_FIELD(ea);
2089 ea.cmd = ea.arg = iptr->isn_arg.string;
2090 define_function(&ea, NULL);
2091 }
2092 break;
2093
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002094 // jump if a condition is met
2095 case ISN_JUMP:
2096 {
2097 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002098 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099 int jump = TRUE;
2100
2101 if (when != JUMP_ALWAYS)
2102 {
2103 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002104 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002105 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002106 || when == JUMP_IF_COND_TRUE)
2107 {
2108 SOURCING_LNUM = iptr->isn_lnum;
2109 jump = tv_get_bool_chk(tv, &error);
2110 if (error)
2111 goto on_error;
2112 }
2113 else
2114 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002115 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002116 || when == JUMP_AND_KEEP_IF_FALSE
2117 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002118 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002119 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002120 {
2121 // drop the value from the stack
2122 clear_tv(tv);
2123 --ectx.ec_stack.ga_len;
2124 }
2125 }
2126 if (jump)
2127 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2128 }
2129 break;
2130
2131 // top of a for loop
2132 case ISN_FOR:
2133 {
2134 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2135 typval_T *idxtv =
2136 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2137
2138 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002139 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002141 ++idxtv->vval.v_number;
2142 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002143 // past the end of the list, jump to "endfor"
2144 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2145 else if (list->lv_first == &range_list_item)
2146 {
2147 // non-materialized range() list
2148 tv = STACK_TV_BOT(0);
2149 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002150 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002151 tv->vval.v_number = list_find_nr(
2152 list, idxtv->vval.v_number, NULL);
2153 ++ectx.ec_stack.ga_len;
2154 }
2155 else
2156 {
2157 listitem_T *li = list_find(list, idxtv->vval.v_number);
2158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2160 ++ectx.ec_stack.ga_len;
2161 }
2162 }
2163 break;
2164
2165 // start of ":try" block
2166 case ISN_TRY:
2167 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002168 trycmd_T *trycmd = NULL;
2169
Bram Moolenaar270d0382020-05-15 21:42:53 +02002170 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002171 goto failed;
2172 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2173 + ectx.ec_trystack.ga_len;
2174 ++ectx.ec_trystack.ga_len;
2175 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002176 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2178 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002179 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002180 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 }
2182 break;
2183
2184 case ISN_PUSHEXC:
2185 if (current_exception == NULL)
2186 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002187 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002188 iemsg("Evaluating catch while current_exception is NULL");
2189 goto failed;
2190 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002191 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 goto failed;
2193 tv = STACK_TV_BOT(0);
2194 ++ectx.ec_stack.ga_len;
2195 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002196 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 tv->vval.v_string = vim_strsave(
2198 (char_u *)current_exception->value);
2199 break;
2200
2201 case ISN_CATCH:
2202 {
2203 garray_T *trystack = &ectx.ec_trystack;
2204
2205 if (trystack->ga_len > 0)
2206 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002207 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 + trystack->ga_len - 1;
2209 trycmd->tcd_caught = TRUE;
2210 }
2211 did_emsg = got_int = did_throw = FALSE;
2212 catch_exception(current_exception);
2213 }
2214 break;
2215
2216 // end of ":try" block
2217 case ISN_ENDTRY:
2218 {
2219 garray_T *trystack = &ectx.ec_trystack;
2220
2221 if (trystack->ga_len > 0)
2222 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002223 trycmd_T *trycmd = NULL;
2224
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002225 --trystack->ga_len;
2226 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002227 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002228 trycmd = ((trycmd_T *)trystack->ga_data)
2229 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002230 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002231 {
2232 // discard the exception
2233 if (caught_stack == current_exception)
2234 caught_stack = caught_stack->caught;
2235 discard_current_exception();
2236 }
2237
2238 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002239 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002240 }
2241 }
2242 break;
2243
2244 case ISN_THROW:
2245 --ectx.ec_stack.ga_len;
2246 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002247 if (tv->vval.v_string == NULL
2248 || *skipwhite(tv->vval.v_string) == NUL)
2249 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002250 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002251 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002252 emsg(_(e_throw_with_empty_string));
2253 goto failed;
2254 }
2255
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002256 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2257 {
2258 vim_free(tv->vval.v_string);
2259 goto failed;
2260 }
2261 did_throw = TRUE;
2262 break;
2263
2264 // compare with special values
2265 case ISN_COMPAREBOOL:
2266 case ISN_COMPARESPECIAL:
2267 {
2268 typval_T *tv1 = STACK_TV_BOT(-2);
2269 typval_T *tv2 = STACK_TV_BOT(-1);
2270 varnumber_T arg1 = tv1->vval.v_number;
2271 varnumber_T arg2 = tv2->vval.v_number;
2272 int res;
2273
2274 switch (iptr->isn_arg.op.op_type)
2275 {
2276 case EXPR_EQUAL: res = arg1 == arg2; break;
2277 case EXPR_NEQUAL: res = arg1 != arg2; break;
2278 default: res = 0; break;
2279 }
2280
2281 --ectx.ec_stack.ga_len;
2282 tv1->v_type = VAR_BOOL;
2283 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2284 }
2285 break;
2286
2287 // Operation with two number arguments
2288 case ISN_OPNR:
2289 case ISN_COMPARENR:
2290 {
2291 typval_T *tv1 = STACK_TV_BOT(-2);
2292 typval_T *tv2 = STACK_TV_BOT(-1);
2293 varnumber_T arg1 = tv1->vval.v_number;
2294 varnumber_T arg2 = tv2->vval.v_number;
2295 varnumber_T res;
2296
2297 switch (iptr->isn_arg.op.op_type)
2298 {
2299 case EXPR_MULT: res = arg1 * arg2; break;
2300 case EXPR_DIV: res = arg1 / arg2; break;
2301 case EXPR_REM: res = arg1 % arg2; break;
2302 case EXPR_SUB: res = arg1 - arg2; break;
2303 case EXPR_ADD: res = arg1 + arg2; break;
2304
2305 case EXPR_EQUAL: res = arg1 == arg2; break;
2306 case EXPR_NEQUAL: res = arg1 != arg2; break;
2307 case EXPR_GREATER: res = arg1 > arg2; break;
2308 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2309 case EXPR_SMALLER: res = arg1 < arg2; break;
2310 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2311 default: res = 0; break;
2312 }
2313
2314 --ectx.ec_stack.ga_len;
2315 if (iptr->isn_type == ISN_COMPARENR)
2316 {
2317 tv1->v_type = VAR_BOOL;
2318 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2319 }
2320 else
2321 tv1->vval.v_number = res;
2322 }
2323 break;
2324
2325 // Computation with two float arguments
2326 case ISN_OPFLOAT:
2327 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002328#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002329 {
2330 typval_T *tv1 = STACK_TV_BOT(-2);
2331 typval_T *tv2 = STACK_TV_BOT(-1);
2332 float_T arg1 = tv1->vval.v_float;
2333 float_T arg2 = tv2->vval.v_float;
2334 float_T res = 0;
2335 int cmp = FALSE;
2336
2337 switch (iptr->isn_arg.op.op_type)
2338 {
2339 case EXPR_MULT: res = arg1 * arg2; break;
2340 case EXPR_DIV: res = arg1 / arg2; break;
2341 case EXPR_SUB: res = arg1 - arg2; break;
2342 case EXPR_ADD: res = arg1 + arg2; break;
2343
2344 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2345 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2346 case EXPR_GREATER: cmp = arg1 > arg2; break;
2347 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2348 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2349 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2350 default: cmp = 0; break;
2351 }
2352 --ectx.ec_stack.ga_len;
2353 if (iptr->isn_type == ISN_COMPAREFLOAT)
2354 {
2355 tv1->v_type = VAR_BOOL;
2356 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2357 }
2358 else
2359 tv1->vval.v_float = res;
2360 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002361#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002362 break;
2363
2364 case ISN_COMPARELIST:
2365 {
2366 typval_T *tv1 = STACK_TV_BOT(-2);
2367 typval_T *tv2 = STACK_TV_BOT(-1);
2368 list_T *arg1 = tv1->vval.v_list;
2369 list_T *arg2 = tv2->vval.v_list;
2370 int cmp = FALSE;
2371 int ic = iptr->isn_arg.op.op_ic;
2372
2373 switch (iptr->isn_arg.op.op_type)
2374 {
2375 case EXPR_EQUAL: cmp =
2376 list_equal(arg1, arg2, ic, FALSE); break;
2377 case EXPR_NEQUAL: cmp =
2378 !list_equal(arg1, arg2, ic, FALSE); break;
2379 case EXPR_IS: cmp = arg1 == arg2; break;
2380 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2381 default: cmp = 0; break;
2382 }
2383 --ectx.ec_stack.ga_len;
2384 clear_tv(tv1);
2385 clear_tv(tv2);
2386 tv1->v_type = VAR_BOOL;
2387 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2388 }
2389 break;
2390
2391 case ISN_COMPAREBLOB:
2392 {
2393 typval_T *tv1 = STACK_TV_BOT(-2);
2394 typval_T *tv2 = STACK_TV_BOT(-1);
2395 blob_T *arg1 = tv1->vval.v_blob;
2396 blob_T *arg2 = tv2->vval.v_blob;
2397 int cmp = FALSE;
2398
2399 switch (iptr->isn_arg.op.op_type)
2400 {
2401 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2402 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2403 case EXPR_IS: cmp = arg1 == arg2; break;
2404 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2405 default: cmp = 0; break;
2406 }
2407 --ectx.ec_stack.ga_len;
2408 clear_tv(tv1);
2409 clear_tv(tv2);
2410 tv1->v_type = VAR_BOOL;
2411 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2412 }
2413 break;
2414
2415 // TODO: handle separately
2416 case ISN_COMPARESTRING:
2417 case ISN_COMPAREDICT:
2418 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 case ISN_COMPAREANY:
2420 {
2421 typval_T *tv1 = STACK_TV_BOT(-2);
2422 typval_T *tv2 = STACK_TV_BOT(-1);
2423 exptype_T exptype = iptr->isn_arg.op.op_type;
2424 int ic = iptr->isn_arg.op.op_ic;
2425
Bram Moolenaareb26f432020-09-14 16:50:05 +02002426 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 typval_compare(tv1, tv2, exptype, ic);
2428 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 --ectx.ec_stack.ga_len;
2430 }
2431 break;
2432
2433 case ISN_ADDLIST:
2434 case ISN_ADDBLOB:
2435 {
2436 typval_T *tv1 = STACK_TV_BOT(-2);
2437 typval_T *tv2 = STACK_TV_BOT(-1);
2438
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002439 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 if (iptr->isn_type == ISN_ADDLIST)
2441 eval_addlist(tv1, tv2);
2442 else
2443 eval_addblob(tv1, tv2);
2444 clear_tv(tv2);
2445 --ectx.ec_stack.ga_len;
2446 }
2447 break;
2448
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002449 case ISN_LISTAPPEND:
2450 {
2451 typval_T *tv1 = STACK_TV_BOT(-2);
2452 typval_T *tv2 = STACK_TV_BOT(-1);
2453 list_T *l = tv1->vval.v_list;
2454
2455 // add an item to a list
2456 if (l == NULL)
2457 {
2458 SOURCING_LNUM = iptr->isn_lnum;
2459 emsg(_(e_cannot_add_to_null_list));
2460 goto on_error;
2461 }
2462 if (list_append_tv(l, tv2) == FAIL)
2463 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002464 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002465 --ectx.ec_stack.ga_len;
2466 }
2467 break;
2468
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002469 case ISN_BLOBAPPEND:
2470 {
2471 typval_T *tv1 = STACK_TV_BOT(-2);
2472 typval_T *tv2 = STACK_TV_BOT(-1);
2473 blob_T *b = tv1->vval.v_blob;
2474 int error = FALSE;
2475 varnumber_T n;
2476
2477 // add a number to a blob
2478 if (b == NULL)
2479 {
2480 SOURCING_LNUM = iptr->isn_lnum;
2481 emsg(_(e_cannot_add_to_null_blob));
2482 goto on_error;
2483 }
2484 n = tv_get_number_chk(tv2, &error);
2485 if (error)
2486 goto on_error;
2487 ga_append(&b->bv_ga, (int)n);
2488 --ectx.ec_stack.ga_len;
2489 }
2490 break;
2491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 // Computation with two arguments of unknown type
2493 case ISN_OPANY:
2494 {
2495 typval_T *tv1 = STACK_TV_BOT(-2);
2496 typval_T *tv2 = STACK_TV_BOT(-1);
2497 varnumber_T n1, n2;
2498#ifdef FEAT_FLOAT
2499 float_T f1 = 0, f2 = 0;
2500#endif
2501 int error = FALSE;
2502
2503 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2504 {
2505 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2506 {
2507 eval_addlist(tv1, tv2);
2508 clear_tv(tv2);
2509 --ectx.ec_stack.ga_len;
2510 break;
2511 }
2512 else if (tv1->v_type == VAR_BLOB
2513 && tv2->v_type == VAR_BLOB)
2514 {
2515 eval_addblob(tv1, tv2);
2516 clear_tv(tv2);
2517 --ectx.ec_stack.ga_len;
2518 break;
2519 }
2520 }
2521#ifdef FEAT_FLOAT
2522 if (tv1->v_type == VAR_FLOAT)
2523 {
2524 f1 = tv1->vval.v_float;
2525 n1 = 0;
2526 }
2527 else
2528#endif
2529 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002530 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 n1 = tv_get_number_chk(tv1, &error);
2532 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002533 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534#ifdef FEAT_FLOAT
2535 if (tv2->v_type == VAR_FLOAT)
2536 f1 = n1;
2537#endif
2538 }
2539#ifdef FEAT_FLOAT
2540 if (tv2->v_type == VAR_FLOAT)
2541 {
2542 f2 = tv2->vval.v_float;
2543 n2 = 0;
2544 }
2545 else
2546#endif
2547 {
2548 n2 = tv_get_number_chk(tv2, &error);
2549 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002550 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002551#ifdef FEAT_FLOAT
2552 if (tv1->v_type == VAR_FLOAT)
2553 f2 = n2;
2554#endif
2555 }
2556#ifdef FEAT_FLOAT
2557 // if there is a float on either side the result is a float
2558 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2559 {
2560 switch (iptr->isn_arg.op.op_type)
2561 {
2562 case EXPR_MULT: f1 = f1 * f2; break;
2563 case EXPR_DIV: f1 = f1 / f2; break;
2564 case EXPR_SUB: f1 = f1 - f2; break;
2565 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002566 default: SOURCING_LNUM = iptr->isn_lnum;
2567 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002568 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 }
2570 clear_tv(tv1);
2571 clear_tv(tv2);
2572 tv1->v_type = VAR_FLOAT;
2573 tv1->vval.v_float = f1;
2574 --ectx.ec_stack.ga_len;
2575 }
2576 else
2577#endif
2578 {
2579 switch (iptr->isn_arg.op.op_type)
2580 {
2581 case EXPR_MULT: n1 = n1 * n2; break;
2582 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2583 case EXPR_SUB: n1 = n1 - n2; break;
2584 case EXPR_ADD: n1 = n1 + n2; break;
2585 default: n1 = num_modulus(n1, n2); break;
2586 }
2587 clear_tv(tv1);
2588 clear_tv(tv2);
2589 tv1->v_type = VAR_NUMBER;
2590 tv1->vval.v_number = n1;
2591 --ectx.ec_stack.ga_len;
2592 }
2593 }
2594 break;
2595
2596 case ISN_CONCAT:
2597 {
2598 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2599 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2600 char_u *res;
2601
2602 res = concat_str(str1, str2);
2603 clear_tv(STACK_TV_BOT(-2));
2604 clear_tv(STACK_TV_BOT(-1));
2605 --ectx.ec_stack.ga_len;
2606 STACK_TV_BOT(-1)->vval.v_string = res;
2607 }
2608 break;
2609
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002610 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002611 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002612 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002613 int is_slice = iptr->isn_type == ISN_STRSLICE;
2614 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002615 char_u *res;
2616
2617 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002618 // string slice: string is at stack-3, first index at
2619 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002620 if (is_slice)
2621 {
2622 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002623 n1 = tv->vval.v_number;
2624 }
2625
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002626 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002627 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002628
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002629 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002630 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002631 if (is_slice)
2632 // Slice: Select the characters from the string
2633 res = string_slice(tv->vval.v_string, n1, n2);
2634 else
2635 // Index: The resulting variable is a string of a
2636 // single character. If the index is too big or
2637 // negative the result is empty.
2638 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002639 vim_free(tv->vval.v_string);
2640 tv->vval.v_string = res;
2641 }
2642 break;
2643
2644 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002645 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002646 {
Bram Moolenaared591872020-08-15 22:14:53 +02002647 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002649 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002650
2651 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002652 // list slice: list is at stack-3, indexes at stack-2 and
2653 // stack-1
2654 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 list = tv->vval.v_list;
2656
2657 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002658 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002659 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002660
2661 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002662 {
Bram Moolenaared591872020-08-15 22:14:53 +02002663 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002664 n1 = tv->vval.v_number;
2665 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002666 }
Bram Moolenaared591872020-08-15 22:14:53 +02002667
2668 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002669 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002670 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002671 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2672 == FAIL)
2673 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002674 }
2675 break;
2676
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002677 case ISN_ANYINDEX:
2678 case ISN_ANYSLICE:
2679 {
2680 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2681 typval_T *var1, *var2;
2682 int res;
2683
2684 // index: composite is at stack-2, index at stack-1
2685 // slice: composite is at stack-3, indexes at stack-2 and
2686 // stack-1
2687 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002688 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002689 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2690 goto on_error;
2691 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2692 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2693 res = eval_index_inner(tv, is_slice,
2694 var1, var2, NULL, -1, TRUE);
2695 clear_tv(var1);
2696 if (is_slice)
2697 clear_tv(var2);
2698 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2699 if (res == FAIL)
2700 goto on_error;
2701 }
2702 break;
2703
Bram Moolenaar9af78762020-06-16 11:34:42 +02002704 case ISN_SLICE:
2705 {
2706 list_T *list;
2707 int count = iptr->isn_arg.number;
2708
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002709 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002710 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002711 list = tv->vval.v_list;
2712
2713 // no error for short list, expect it to be checked earlier
2714 if (list != NULL && list->lv_len >= count)
2715 {
2716 list_T *newlist = list_slice(list,
2717 count, list->lv_len - 1);
2718
2719 if (newlist != NULL)
2720 {
2721 list_unref(list);
2722 tv->vval.v_list = newlist;
2723 ++newlist->lv_refcount;
2724 }
2725 }
2726 }
2727 break;
2728
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002729 case ISN_GETITEM:
2730 {
2731 listitem_T *li;
2732 int index = iptr->isn_arg.number;
2733
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002734 // Get list item: list is at stack-1, push item.
2735 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002736 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002737 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002738
2739 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2740 goto failed;
2741 ++ectx.ec_stack.ga_len;
2742 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2743 }
2744 break;
2745
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 case ISN_MEMBER:
2747 {
2748 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002749 char_u *key;
2750 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002751 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002752
2753 // dict member: dict is at stack-2, key at stack-1
2754 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002755 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002756 dict = tv->vval.v_dict;
2757
2758 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002759 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002760 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002761 if (key == NULL)
2762 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002763
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002764 if ((di = dict_find(dict, key, -1)) == NULL)
2765 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002766 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002767 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002768
2769 // If :silent! is used we will continue, make sure the
2770 // stack contents makes sense.
2771 clear_tv(tv);
2772 --ectx.ec_stack.ga_len;
2773 tv = STACK_TV_BOT(-1);
2774 clear_tv(tv);
2775 tv->v_type = VAR_NUMBER;
2776 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002777 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002778 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002779 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002780 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002781 // Clear the dict only after getting the item, to avoid
2782 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002783 tv = STACK_TV_BOT(-1);
2784 temp_tv = *tv;
2785 copy_tv(&di->di_tv, tv);
2786 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002787 }
2788 break;
2789
2790 // dict member with string key
2791 case ISN_STRINGMEMBER:
2792 {
2793 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002795 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796
2797 tv = STACK_TV_BOT(-1);
2798 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2799 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002800 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002802 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002803 }
2804 dict = tv->vval.v_dict;
2805
2806 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2807 == NULL)
2808 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002809 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002810 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002811 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002813 // Clear the dict after getting the item, to avoid that it
2814 // make the item invalid.
2815 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002817 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002818 }
2819 break;
2820
2821 case ISN_NEGATENR:
2822 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002823 if (tv->v_type != VAR_NUMBER
2824#ifdef FEAT_FLOAT
2825 && tv->v_type != VAR_FLOAT
2826#endif
2827 )
2828 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002829 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002830 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002831 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002832 }
2833#ifdef FEAT_FLOAT
2834 if (tv->v_type == VAR_FLOAT)
2835 tv->vval.v_float = -tv->vval.v_float;
2836 else
2837#endif
2838 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002839 break;
2840
2841 case ISN_CHECKNR:
2842 {
2843 int error = FALSE;
2844
2845 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002846 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002847 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002848 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002849 (void)tv_get_number_chk(tv, &error);
2850 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002851 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002852 }
2853 break;
2854
2855 case ISN_CHECKTYPE:
2856 {
2857 checktype_T *ct = &iptr->isn_arg.type;
2858
2859 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002860 SOURCING_LNUM = iptr->isn_lnum;
2861 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2862 goto on_error;
2863
2864 // number 0 is FALSE, number 1 is TRUE
2865 if (tv->v_type == VAR_NUMBER
2866 && ct->ct_type->tt_type == VAR_BOOL
2867 && (tv->vval.v_number == 0
2868 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002869 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002870 tv->v_type = VAR_BOOL;
2871 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002872 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002873 }
2874 }
2875 break;
2876
Bram Moolenaar9af78762020-06-16 11:34:42 +02002877 case ISN_CHECKLEN:
2878 {
2879 int min_len = iptr->isn_arg.checklen.cl_min_len;
2880 list_T *list = NULL;
2881
2882 tv = STACK_TV_BOT(-1);
2883 if (tv->v_type == VAR_LIST)
2884 list = tv->vval.v_list;
2885 if (list == NULL || list->lv_len < min_len
2886 || (list->lv_len > min_len
2887 && !iptr->isn_arg.checklen.cl_more_OK))
2888 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002890 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002891 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002892 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002893 }
2894 }
2895 break;
2896
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002898 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002899 {
2900 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002901 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002902
2903 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002904 if (iptr->isn_type == ISN_2BOOL)
2905 {
2906 n = tv2bool(tv);
2907 if (iptr->isn_arg.number) // invert
2908 n = !n;
2909 }
2910 else
2911 {
2912 SOURCING_LNUM = iptr->isn_lnum;
2913 n = tv_get_bool_chk(tv, &error);
2914 if (error)
2915 goto on_error;
2916 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 clear_tv(tv);
2918 tv->v_type = VAR_BOOL;
2919 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2920 }
2921 break;
2922
2923 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002924 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002925 {
2926 char_u *str;
2927
2928 tv = STACK_TV_BOT(iptr->isn_arg.number);
2929 if (tv->v_type != VAR_STRING)
2930 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002931 if (iptr->isn_type == ISN_2STRING_ANY)
2932 {
2933 switch (tv->v_type)
2934 {
2935 case VAR_SPECIAL:
2936 case VAR_BOOL:
2937 case VAR_NUMBER:
2938 case VAR_FLOAT:
2939 case VAR_BLOB: break;
2940 default: to_string_error(tv->v_type);
2941 goto on_error;
2942 }
2943 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002944 str = typval_tostring(tv);
2945 clear_tv(tv);
2946 tv->v_type = VAR_STRING;
2947 tv->vval.v_string = str;
2948 }
2949 }
2950 break;
2951
Bram Moolenaar08597872020-12-10 19:43:40 +01002952 case ISN_RANGE:
2953 {
2954 exarg_T ea;
2955 char *errormsg;
2956
2957 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2958 goto failed;
2959 ++ectx.ec_stack.ga_len;
2960 tv = STACK_TV_BOT(-1);
2961 ea.addr_type = ADDR_LINES;
2962 ea.cmd = iptr->isn_arg.string;
2963 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2964 goto failed;
2965 if (ea.addr_count == 0)
2966 tv->vval.v_number = curwin->w_cursor.lnum;
2967 else
2968 tv->vval.v_number = ea.line2;
2969 }
2970 break;
2971
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002972 case ISN_PUT:
2973 {
2974 int regname = iptr->isn_arg.put.put_regname;
2975 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2976 char_u *expr = NULL;
2977 int dir = FORWARD;
2978
2979 if (regname == '=')
2980 {
2981 tv = STACK_TV_BOT(-1);
2982 if (tv->v_type == VAR_STRING)
2983 expr = tv->vval.v_string;
2984 else
2985 {
2986 expr = typval_tostring(tv); // allocates value
2987 clear_tv(tv);
2988 }
2989 --ectx.ec_stack.ga_len;
2990 }
Bram Moolenaar08597872020-12-10 19:43:40 +01002991 if (lnum < -2)
2992 {
2993 // line number was put on the stack by ISN_RANGE
2994 tv = STACK_TV_BOT(-1);
2995 curwin->w_cursor.lnum = tv->vval.v_number;
2996 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
2997 dir = BACKWARD;
2998 --ectx.ec_stack.ga_len;
2999 }
3000 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003001 // :put! above cursor
3002 dir = BACKWARD;
3003 else if (lnum >= 0)
3004 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3005 check_cursor();
3006 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3007 vim_free(expr);
3008 }
3009 break;
3010
Bram Moolenaar02194d22020-10-24 23:08:38 +02003011 case ISN_CMDMOD:
3012 save_cmdmod = cmdmod;
3013 restore_cmdmod = TRUE;
3014 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3015 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003016 break;
3017
Bram Moolenaar02194d22020-10-24 23:08:38 +02003018 case ISN_CMDMOD_REV:
3019 // filter regprog is owned by the instruction, don't free it
3020 cmdmod.cmod_filter_regmatch.regprog = NULL;
3021 undo_cmdmod(&cmdmod);
3022 cmdmod = save_cmdmod;
3023 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003024 break;
3025
Bram Moolenaar792f7862020-11-23 08:31:18 +01003026 case ISN_UNPACK:
3027 {
3028 int count = iptr->isn_arg.unpack.unp_count;
3029 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3030 list_T *l;
3031 listitem_T *li;
3032 int i;
3033
3034 // Check there is a valid list to unpack.
3035 tv = STACK_TV_BOT(-1);
3036 if (tv->v_type != VAR_LIST)
3037 {
3038 SOURCING_LNUM = iptr->isn_lnum;
3039 emsg(_(e_for_argument_must_be_sequence_of_lists));
3040 goto on_error;
3041 }
3042 l = tv->vval.v_list;
3043 if (l == NULL
3044 || l->lv_len < (semicolon ? count - 1 : count))
3045 {
3046 SOURCING_LNUM = iptr->isn_lnum;
3047 emsg(_(e_list_value_does_not_have_enough_items));
3048 goto on_error;
3049 }
3050 else if (!semicolon && l->lv_len > count)
3051 {
3052 SOURCING_LNUM = iptr->isn_lnum;
3053 emsg(_(e_list_value_has_more_items_than_targets));
3054 goto on_error;
3055 }
3056
3057 CHECK_LIST_MATERIALIZE(l);
3058 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3059 goto failed;
3060 ectx.ec_stack.ga_len += count - 1;
3061
3062 // Variable after semicolon gets a list with the remaining
3063 // items.
3064 if (semicolon)
3065 {
3066 list_T *rem_list =
3067 list_alloc_with_items(l->lv_len - count + 1);
3068
3069 if (rem_list == NULL)
3070 goto failed;
3071 tv = STACK_TV_BOT(-count);
3072 tv->vval.v_list = rem_list;
3073 ++rem_list->lv_refcount;
3074 tv->v_lock = 0;
3075 li = l->lv_first;
3076 for (i = 0; i < count - 1; ++i)
3077 li = li->li_next;
3078 for (i = 0; li != NULL; ++i)
3079 {
3080 list_set_item(rem_list, i, &li->li_tv);
3081 li = li->li_next;
3082 }
3083 --count;
3084 }
3085
3086 // Produce the values in reverse order, first item last.
3087 li = l->lv_first;
3088 for (i = 0; i < count; ++i)
3089 {
3090 tv = STACK_TV_BOT(-i - 1);
3091 copy_tv(&li->li_tv, tv);
3092 li = li->li_next;
3093 }
3094
3095 list_unref(l);
3096 }
3097 break;
3098
Bram Moolenaar389df252020-07-09 21:20:47 +02003099 case ISN_SHUFFLE:
3100 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003101 typval_T tmp_tv;
3102 int item = iptr->isn_arg.shuffle.shfl_item;
3103 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003104
3105 tmp_tv = *STACK_TV_BOT(-item);
3106 for ( ; up > 0 && item > 1; --up)
3107 {
3108 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3109 --item;
3110 }
3111 *STACK_TV_BOT(-item) = tmp_tv;
3112 }
3113 break;
3114
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003115 case ISN_DROP:
3116 --ectx.ec_stack.ga_len;
3117 clear_tv(STACK_TV_BOT(0));
3118 break;
3119 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003120 continue;
3121
Bram Moolenaard032f342020-07-18 18:13:02 +02003122func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003123 // Restore previous function. If the frame pointer is where we started
3124 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003125 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003126 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003127
Bram Moolenaard032f342020-07-18 18:13:02 +02003128 if (func_return(&ectx) == FAIL)
3129 // only fails when out of memory
3130 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003131 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003132
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003133on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003134 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003135 // If "emsg_silent" is set then ignore the error, unless it was set
3136 // when calling the function.
3137 if (did_emsg_cumul + did_emsg == did_emsg_before
3138 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003139 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003140on_fatal_error:
3141 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003142 // If we are not inside a try-catch started here, abort execution.
3143 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003144 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003145 }
3146
3147done:
3148 // function finished, get result from the stack.
3149 tv = STACK_TV_BOT(-1);
3150 *rettv = *tv;
3151 tv->v_type = VAR_UNKNOWN;
3152 ret = OK;
3153
3154failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003155 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003156 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003157 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003158
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003159 // Deal with any remaining closures, they may be in use somewhere.
3160 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003161 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003162 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003163 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3164 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003165
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003166 estack_pop();
3167 current_sctx = save_current_sctx;
3168
Bram Moolenaar352134b2020-10-17 22:04:08 +02003169 if (*msg_list != NULL && saved_msg_list != NULL)
3170 {
3171 msglist_T **plist = saved_msg_list;
3172
3173 // Append entries from the current msg_list (uncaught exceptions) to
3174 // the saved msg_list.
3175 while (*plist != NULL)
3176 plist = &(*plist)->next;
3177
3178 *plist = *msg_list;
3179 }
3180 msg_list = saved_msg_list;
3181
Bram Moolenaar02194d22020-10-24 23:08:38 +02003182 if (restore_cmdmod)
3183 {
3184 cmdmod.cmod_filter_regmatch.regprog = NULL;
3185 undo_cmdmod(&cmdmod);
3186 cmdmod = save_cmdmod;
3187 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003188 emsg_silent_def = save_emsg_silent_def;
3189 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003190
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003191failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003192 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3194 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003195
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003197 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003198
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003199 // Not sure if this is necessary.
3200 suppress_errthrow = save_suppress_errthrow;
3201
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003202 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003203 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003204 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003205 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 return ret;
3207}
3208
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003210 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003211 * We don't really need this at runtime, but we do have tests that require it,
3212 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003213 */
3214 void
3215ex_disassemble(exarg_T *eap)
3216{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003217 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003218 char_u *fname;
3219 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003220 dfunc_T *dfunc;
3221 isn_T *instr;
3222 int current;
3223 int line_idx = 0;
3224 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003225 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003227 if (STRNCMP(arg, "<lambda>", 8) == 0)
3228 {
3229 arg += 8;
3230 (void)getdigits(&arg);
3231 fname = vim_strnsave(eap->arg, arg - eap->arg);
3232 }
3233 else
3234 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003235 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003236 if (fname == NULL)
3237 {
3238 semsg(_(e_invarg2), eap->arg);
3239 return;
3240 }
3241
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003242 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003243 if (ufunc == NULL)
3244 {
3245 char_u *p = untrans_function_name(fname);
3246
3247 if (p != NULL)
3248 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003249 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003250 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003251 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 if (ufunc == NULL)
3253 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003254 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 return;
3256 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003257 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003258 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3259 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003260 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003262 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 return;
3264 }
3265 if (ufunc->uf_name_exp != NULL)
3266 msg((char *)ufunc->uf_name_exp);
3267 else
3268 msg((char *)ufunc->uf_name);
3269
3270 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3271 instr = dfunc->df_instr;
3272 for (current = 0; current < dfunc->df_instr_count; ++current)
3273 {
3274 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003275 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276
3277 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3278 {
3279 if (current > prev_current)
3280 {
3281 msg_puts("\n\n");
3282 prev_current = current;
3283 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003284 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3285 if (line != NULL)
3286 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 }
3288
3289 switch (iptr->isn_type)
3290 {
3291 case ISN_EXEC:
3292 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3293 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003294 case ISN_EXECCONCAT:
3295 smsg("%4d EXECCONCAT %lld", current,
3296 (long long)iptr->isn_arg.number);
3297 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 case ISN_ECHO:
3299 {
3300 echo_T *echo = &iptr->isn_arg.echo;
3301
3302 smsg("%4d %s %d", current,
3303 echo->echo_with_white ? "ECHO" : "ECHON",
3304 echo->echo_count);
3305 }
3306 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003307 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003308 smsg("%4d EXECUTE %lld", current,
3309 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003310 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003311 case ISN_ECHOMSG:
3312 smsg("%4d ECHOMSG %lld", current,
3313 (long long)(iptr->isn_arg.number));
3314 break;
3315 case ISN_ECHOERR:
3316 smsg("%4d ECHOERR %lld", current,
3317 (long long)(iptr->isn_arg.number));
3318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003320 case ISN_LOADOUTER:
3321 {
3322 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3323
3324 if (iptr->isn_arg.number < 0)
3325 smsg("%4d LOAD%s arg[%lld]", current, add,
3326 (long long)(iptr->isn_arg.number
3327 + STACK_FRAME_SIZE));
3328 else
3329 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003330 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003331 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 break;
3333 case ISN_LOADV:
3334 smsg("%4d LOADV v:%s", current,
3335 get_vim_var_name(iptr->isn_arg.number));
3336 break;
3337 case ISN_LOADSCRIPT:
3338 {
3339 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003340 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3342 + iptr->isn_arg.script.script_idx;
3343
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003344 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3345 sv->sv_name,
3346 iptr->isn_arg.script.script_idx,
3347 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 }
3349 break;
3350 case ISN_LOADS:
3351 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003352 scriptitem_T *si = SCRIPT_ITEM(
3353 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003354
3355 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003356 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003357 }
3358 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003359 case ISN_LOADAUTO:
3360 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3361 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 case ISN_LOADG:
3363 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3364 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003365 case ISN_LOADB:
3366 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3367 break;
3368 case ISN_LOADW:
3369 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3370 break;
3371 case ISN_LOADT:
3372 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3373 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003374 case ISN_LOADGDICT:
3375 smsg("%4d LOAD g:", current);
3376 break;
3377 case ISN_LOADBDICT:
3378 smsg("%4d LOAD b:", current);
3379 break;
3380 case ISN_LOADWDICT:
3381 smsg("%4d LOAD w:", current);
3382 break;
3383 case ISN_LOADTDICT:
3384 smsg("%4d LOAD t:", current);
3385 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 case ISN_LOADOPT:
3387 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3388 break;
3389 case ISN_LOADENV:
3390 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3391 break;
3392 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003393 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 break;
3395
3396 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003397 case ISN_STOREOUTER:
3398 {
3399 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3400
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003401 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003402 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003403 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003404 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003405 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003406 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003407 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003408 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003409 case ISN_STOREV:
3410 smsg("%4d STOREV v:%s", current,
3411 get_vim_var_name(iptr->isn_arg.number));
3412 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003413 case ISN_STOREAUTO:
3414 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3415 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003416 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003417 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3418 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003419 case ISN_STOREB:
3420 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3421 break;
3422 case ISN_STOREW:
3423 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3424 break;
3425 case ISN_STORET:
3426 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3427 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003428 case ISN_STORES:
3429 {
3430 scriptitem_T *si = SCRIPT_ITEM(
3431 iptr->isn_arg.loadstore.ls_sid);
3432
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003433 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003434 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003435 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 break;
3437 case ISN_STORESCRIPT:
3438 {
3439 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003440 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003441 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3442 + iptr->isn_arg.script.script_idx;
3443
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003444 smsg("%4d STORESCRIPT %s-%d in %s", current,
3445 sv->sv_name,
3446 iptr->isn_arg.script.script_idx,
3447 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 }
3449 break;
3450 case ISN_STOREOPT:
3451 smsg("%4d STOREOPT &%s", current,
3452 iptr->isn_arg.storeopt.so_name);
3453 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003454 case ISN_STOREENV:
3455 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3456 break;
3457 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003458 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003459 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003460 case ISN_STORENR:
3461 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003462 iptr->isn_arg.storenr.stnr_val,
3463 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003464 break;
3465
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003466 case ISN_STORELIST:
3467 smsg("%4d STORELIST", current);
3468 break;
3469
3470 case ISN_STOREDICT:
3471 smsg("%4d STOREDICT", current);
3472 break;
3473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003474 // constants
3475 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003476 smsg("%4d PUSHNR %lld", current,
3477 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003478 break;
3479 case ISN_PUSHBOOL:
3480 case ISN_PUSHSPEC:
3481 smsg("%4d PUSH %s", current,
3482 get_var_special_name(iptr->isn_arg.number));
3483 break;
3484 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003485#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003486 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003487#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 break;
3489 case ISN_PUSHS:
3490 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3491 break;
3492 case ISN_PUSHBLOB:
3493 {
3494 char_u *r;
3495 char_u numbuf[NUMBUFLEN];
3496 char_u *tofree;
3497
3498 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003499 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003500 vim_free(tofree);
3501 }
3502 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003503 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003504 {
3505 char *name = (char *)iptr->isn_arg.string;
3506
3507 smsg("%4d PUSHFUNC \"%s\"", current,
3508 name == NULL ? "[none]" : name);
3509 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003510 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003511 case ISN_PUSHCHANNEL:
3512#ifdef FEAT_JOB_CHANNEL
3513 {
3514 channel_T *channel = iptr->isn_arg.channel;
3515
3516 smsg("%4d PUSHCHANNEL %d", current,
3517 channel == NULL ? 0 : channel->ch_id);
3518 }
3519#endif
3520 break;
3521 case ISN_PUSHJOB:
3522#ifdef FEAT_JOB_CHANNEL
3523 {
3524 typval_T tv;
3525 char_u *name;
3526
3527 tv.v_type = VAR_JOB;
3528 tv.vval.v_job = iptr->isn_arg.job;
3529 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003530 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003531 }
3532#endif
3533 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003534 case ISN_PUSHEXC:
3535 smsg("%4d PUSH v:exception", current);
3536 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003537 case ISN_UNLET:
3538 smsg("%4d UNLET%s %s", current,
3539 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3540 iptr->isn_arg.unlet.ul_name);
3541 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003542 case ISN_UNLETENV:
3543 smsg("%4d UNLETENV%s $%s", current,
3544 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3545 iptr->isn_arg.unlet.ul_name);
3546 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003547 case ISN_LOCKCONST:
3548 smsg("%4d LOCKCONST", current);
3549 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003551 smsg("%4d NEWLIST size %lld", current,
3552 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 break;
3554 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003555 smsg("%4d NEWDICT size %lld", current,
3556 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003557 break;
3558
3559 // function call
3560 case ISN_BCALL:
3561 {
3562 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3563
3564 smsg("%4d BCALL %s(argc %d)", current,
3565 internal_func_name(cbfunc->cbf_idx),
3566 cbfunc->cbf_argcount);
3567 }
3568 break;
3569 case ISN_DCALL:
3570 {
3571 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3572 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3573 + cdfunc->cdf_idx;
3574
3575 smsg("%4d DCALL %s(argc %d)", current,
3576 df->df_ufunc->uf_name_exp != NULL
3577 ? df->df_ufunc->uf_name_exp
3578 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3579 }
3580 break;
3581 case ISN_UCALL:
3582 {
3583 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3584
3585 smsg("%4d UCALL %s(argc %d)", current,
3586 cufunc->cuf_name, cufunc->cuf_argcount);
3587 }
3588 break;
3589 case ISN_PCALL:
3590 {
3591 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3592
3593 smsg("%4d PCALL%s (argc %d)", current,
3594 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3595 }
3596 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003597 case ISN_PCALL_END:
3598 smsg("%4d PCALL end", current);
3599 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003600 case ISN_RETURN:
3601 smsg("%4d RETURN", current);
3602 break;
3603 case ISN_FUNCREF:
3604 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003605 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003606 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003607 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003609 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003610 }
3611 break;
3612
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003613 case ISN_NEWFUNC:
3614 {
3615 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3616
3617 smsg("%4d NEWFUNC %s %s", current,
3618 newfunc->nf_lambda, newfunc->nf_global);
3619 }
3620 break;
3621
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003622 case ISN_DEF:
3623 {
3624 char_u *name = iptr->isn_arg.string;
3625
3626 smsg("%4d DEF %s", current,
3627 name == NULL ? (char_u *)"" : name);
3628 }
3629 break;
3630
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003631 case ISN_JUMP:
3632 {
3633 char *when = "?";
3634
3635 switch (iptr->isn_arg.jump.jump_when)
3636 {
3637 case JUMP_ALWAYS:
3638 when = "JUMP";
3639 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 case JUMP_AND_KEEP_IF_TRUE:
3641 when = "JUMP_AND_KEEP_IF_TRUE";
3642 break;
3643 case JUMP_IF_FALSE:
3644 when = "JUMP_IF_FALSE";
3645 break;
3646 case JUMP_AND_KEEP_IF_FALSE:
3647 when = "JUMP_AND_KEEP_IF_FALSE";
3648 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003649 case JUMP_IF_COND_FALSE:
3650 when = "JUMP_IF_COND_FALSE";
3651 break;
3652 case JUMP_IF_COND_TRUE:
3653 when = "JUMP_IF_COND_TRUE";
3654 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003655 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003656 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 iptr->isn_arg.jump.jump_where);
3658 }
3659 break;
3660
3661 case ISN_FOR:
3662 {
3663 forloop_T *forloop = &iptr->isn_arg.forloop;
3664
3665 smsg("%4d FOR $%d -> %d", current,
3666 forloop->for_idx, forloop->for_end);
3667 }
3668 break;
3669
3670 case ISN_TRY:
3671 {
3672 try_T *try = &iptr->isn_arg.try;
3673
3674 smsg("%4d TRY catch -> %d, finally -> %d", current,
3675 try->try_catch, try->try_finally);
3676 }
3677 break;
3678 case ISN_CATCH:
3679 // TODO
3680 smsg("%4d CATCH", current);
3681 break;
3682 case ISN_ENDTRY:
3683 smsg("%4d ENDTRY", current);
3684 break;
3685 case ISN_THROW:
3686 smsg("%4d THROW", current);
3687 break;
3688
3689 // expression operations on number
3690 case ISN_OPNR:
3691 case ISN_OPFLOAT:
3692 case ISN_OPANY:
3693 {
3694 char *what;
3695 char *ins;
3696
3697 switch (iptr->isn_arg.op.op_type)
3698 {
3699 case EXPR_MULT: what = "*"; break;
3700 case EXPR_DIV: what = "/"; break;
3701 case EXPR_REM: what = "%"; break;
3702 case EXPR_SUB: what = "-"; break;
3703 case EXPR_ADD: what = "+"; break;
3704 default: what = "???"; break;
3705 }
3706 switch (iptr->isn_type)
3707 {
3708 case ISN_OPNR: ins = "OPNR"; break;
3709 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3710 case ISN_OPANY: ins = "OPANY"; break;
3711 default: ins = "???"; break;
3712 }
3713 smsg("%4d %s %s", current, ins, what);
3714 }
3715 break;
3716
3717 case ISN_COMPAREBOOL:
3718 case ISN_COMPARESPECIAL:
3719 case ISN_COMPARENR:
3720 case ISN_COMPAREFLOAT:
3721 case ISN_COMPARESTRING:
3722 case ISN_COMPAREBLOB:
3723 case ISN_COMPARELIST:
3724 case ISN_COMPAREDICT:
3725 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003726 case ISN_COMPAREANY:
3727 {
3728 char *p;
3729 char buf[10];
3730 char *type;
3731
3732 switch (iptr->isn_arg.op.op_type)
3733 {
3734 case EXPR_EQUAL: p = "=="; break;
3735 case EXPR_NEQUAL: p = "!="; break;
3736 case EXPR_GREATER: p = ">"; break;
3737 case EXPR_GEQUAL: p = ">="; break;
3738 case EXPR_SMALLER: p = "<"; break;
3739 case EXPR_SEQUAL: p = "<="; break;
3740 case EXPR_MATCH: p = "=~"; break;
3741 case EXPR_IS: p = "is"; break;
3742 case EXPR_ISNOT: p = "isnot"; break;
3743 case EXPR_NOMATCH: p = "!~"; break;
3744 default: p = "???"; break;
3745 }
3746 STRCPY(buf, p);
3747 if (iptr->isn_arg.op.op_ic == TRUE)
3748 strcat(buf, "?");
3749 switch(iptr->isn_type)
3750 {
3751 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3752 case ISN_COMPARESPECIAL:
3753 type = "COMPARESPECIAL"; break;
3754 case ISN_COMPARENR: type = "COMPARENR"; break;
3755 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3756 case ISN_COMPARESTRING:
3757 type = "COMPARESTRING"; break;
3758 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3759 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3760 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3761 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3763 default: type = "???"; break;
3764 }
3765
3766 smsg("%4d %s %s", current, type, buf);
3767 }
3768 break;
3769
3770 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3771 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3772
3773 // expression operations
3774 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003775 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003776 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003777 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003778 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003779 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003780 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003781 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3782 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003783 case ISN_SLICE: smsg("%4d SLICE %lld",
3784 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003785 case ISN_GETITEM: smsg("%4d ITEM %lld",
3786 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003787 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3788 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003789 iptr->isn_arg.string); break;
3790 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3791
3792 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003793 case ISN_CHECKTYPE:
3794 {
3795 char *tofree;
3796
3797 smsg("%4d CHECKTYPE %s stack[%d]", current,
3798 type_name(iptr->isn_arg.type.ct_type, &tofree),
3799 iptr->isn_arg.type.ct_off);
3800 vim_free(tofree);
3801 break;
3802 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003803 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3804 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3805 iptr->isn_arg.checklen.cl_min_len);
3806 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003807 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003808 case ISN_2BOOL: if (iptr->isn_arg.number)
3809 smsg("%4d INVERT (!val)", current);
3810 else
3811 smsg("%4d 2BOOL (!!val)", current);
3812 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003813 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3814 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003815 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003816 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3817 (long long)(iptr->isn_arg.number));
3818 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003819 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3820 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003821 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003822 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3823 smsg("%4d PUT %c above range",
3824 current, iptr->isn_arg.put.put_regname);
3825 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3826 smsg("%4d PUT %c range",
3827 current, iptr->isn_arg.put.put_regname);
3828 else
3829 smsg("%4d PUT %c %ld", current,
3830 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003831 (long)iptr->isn_arg.put.put_lnum);
3832 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003833
Bram Moolenaar02194d22020-10-24 23:08:38 +02003834 // TODO: summarize modifiers
3835 case ISN_CMDMOD:
3836 {
3837 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003838 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003839 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3840
3841 buf = alloc(len + 1);
3842 if (buf != NULL)
3843 {
3844 (void)produce_cmdmods(
3845 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3846 smsg("%4d CMDMOD %s", current, buf);
3847 vim_free(buf);
3848 }
3849 break;
3850 }
3851 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003852
Bram Moolenaar792f7862020-11-23 08:31:18 +01003853 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3854 iptr->isn_arg.unpack.unp_count,
3855 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3856 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003857 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3858 iptr->isn_arg.shuffle.shfl_item,
3859 iptr->isn_arg.shuffle.shfl_up);
3860 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003861 case ISN_DROP: smsg("%4d DROP", current); break;
3862 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003863
3864 out_flush(); // output one line at a time
3865 ui_breakcheck();
3866 if (got_int)
3867 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869}
3870
3871/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003872 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 * list, etc. Mostly like what JavaScript does, except that empty list and
3874 * empty dictionary are FALSE.
3875 */
3876 int
3877tv2bool(typval_T *tv)
3878{
3879 switch (tv->v_type)
3880 {
3881 case VAR_NUMBER:
3882 return tv->vval.v_number != 0;
3883 case VAR_FLOAT:
3884#ifdef FEAT_FLOAT
3885 return tv->vval.v_float != 0.0;
3886#else
3887 break;
3888#endif
3889 case VAR_PARTIAL:
3890 return tv->vval.v_partial != NULL;
3891 case VAR_FUNC:
3892 case VAR_STRING:
3893 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3894 case VAR_LIST:
3895 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3896 case VAR_DICT:
3897 return tv->vval.v_dict != NULL
3898 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3899 case VAR_BOOL:
3900 case VAR_SPECIAL:
3901 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3902 case VAR_JOB:
3903#ifdef FEAT_JOB_CHANNEL
3904 return tv->vval.v_job != NULL;
3905#else
3906 break;
3907#endif
3908 case VAR_CHANNEL:
3909#ifdef FEAT_JOB_CHANNEL
3910 return tv->vval.v_channel != NULL;
3911#else
3912 break;
3913#endif
3914 case VAR_BLOB:
3915 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3916 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003917 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003918 case VAR_VOID:
3919 break;
3920 }
3921 return FALSE;
3922}
3923
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003924 void
3925emsg_using_string_as(typval_T *tv, int as_number)
3926{
3927 semsg(_(as_number ? e_using_string_as_number_str
3928 : e_using_string_as_bool_str),
3929 tv->vval.v_string == NULL
3930 ? (char_u *)"" : tv->vval.v_string);
3931}
3932
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003933/*
3934 * If "tv" is a string give an error and return FAIL.
3935 */
3936 int
3937check_not_string(typval_T *tv)
3938{
3939 if (tv->v_type == VAR_STRING)
3940 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003941 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 clear_tv(tv);
3943 return FAIL;
3944 }
3945 return OK;
3946}
3947
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948
3949#endif // FEAT_EVAL