blob: 6409d88f1bc10cf2330e6a489d55cd33c89c21c9 [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 {
609 // The function has been compiled, can call it quickly. For a function
610 // that was defined later: we can call it directly next time.
611 if (iptr != NULL)
612 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100613 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100614 iptr->isn_type = ISN_DCALL;
615 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
616 iptr->isn_arg.dfunc.cdf_argcount = argcount;
617 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100619 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100620
621 if (call_prepare(argcount, argvars, ectx) == FAIL)
622 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200623 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100624 funcexe.evaluate = TRUE;
625
626 // Call the user function. Result goes in last position on the stack.
627 // TODO: add selfdict if there is one
628 error = call_user_func_check(ufunc, argcount, argvars,
629 STACK_TV_BOT(-1), &funcexe, NULL);
630
631 // Clear the arguments.
632 for (idx = 0; idx < argcount; ++idx)
633 clear_tv(&argvars[idx]);
634
635 if (error != FCERR_NONE)
636 {
637 user_func_error(error, ufunc->uf_name);
638 return FAIL;
639 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100640 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200641 // Error other than from calling the function itself.
642 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 return OK;
644}
645
646/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200647 * Return TRUE if an error was given or CTRL-C was pressed.
648 */
649 static int
650vim9_aborting(int prev_called_emsg)
651{
652 return called_emsg > prev_called_emsg || got_int || did_throw;
653}
654
655/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656 * Execute a function by "name".
657 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100658 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100659 * Returns FAIL if not found without an error message.
660 */
661 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100662call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663{
664 ufunc_T *ufunc;
665
666 if (builtin_function(name, -1))
667 {
668 int func_idx = find_internal_func(name);
669
670 if (func_idx < 0)
671 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200672 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673 return FAIL;
674 return call_bfunc(func_idx, argcount, ectx);
675 }
676
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200677 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200678
679 if (ufunc == NULL)
680 {
681 int called_emsg_before = called_emsg;
682
683 if (script_autoload(name, TRUE))
684 // loaded a package, search for the function again
685 ufunc = find_func(name, FALSE, NULL);
686 if (vim9_aborting(called_emsg_before))
687 return FAIL; // bail out if loading the script caused an error
688 }
689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100691 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
693 return FAIL;
694}
695
696 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200697call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200699 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200700 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200702 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100703
704 if (tv->v_type == VAR_PARTIAL)
705 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200706 partial_T *pt = tv->vval.v_partial;
707 int i;
708
709 if (pt->pt_argc > 0)
710 {
711 // Make space for arguments from the partial, shift the "argcount"
712 // arguments up.
713 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
714 return FAIL;
715 for (i = 1; i <= argcount; ++i)
716 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
717 ectx->ec_stack.ga_len += pt->pt_argc;
718 argcount += pt->pt_argc;
719
720 // copy the arguments from the partial onto the stack
721 for (i = 0; i < pt->pt_argc; ++i)
722 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
723 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100724
725 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200726 {
727 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
728
729 // closure may need the function context where it was defined
730 ectx->ec_outer_stack = pt->pt_ectx_stack;
731 ectx->ec_outer_frame = pt->pt_ectx_frame;
732
733 return ret;
734 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735 name = pt->pt_name;
736 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200737 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200739 if (name != NULL)
740 {
741 char_u fname_buf[FLEN_FIXED + 1];
742 char_u *tofree = NULL;
743 int error = FCERR_NONE;
744 char_u *fname;
745
746 // May need to translate <SNR>123_ to K_SNR.
747 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
748 if (error != FCERR_NONE)
749 res = FAIL;
750 else
751 res = call_by_name(fname, argcount, ectx, NULL);
752 vim_free(tofree);
753 }
754
755 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 {
757 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200758 semsg(_(e_unknownfunc),
759 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100760 return FAIL;
761 }
762 return OK;
763}
764
765/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200766 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
767 * TRUE.
768 */
769 static int
770error_if_locked(int lock, char *error)
771{
772 if (lock & (VAR_LOCKED | VAR_FIXED))
773 {
774 emsg(_(error));
775 return TRUE;
776 }
777 return FALSE;
778}
779
780/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100781 * Store "tv" in variable "name".
782 * This is for s: and g: variables.
783 */
784 static void
785store_var(char_u *name, typval_T *tv)
786{
787 funccal_entry_T entry;
788
789 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200790 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100791 restore_funccal();
792}
793
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100794/*
795 * When the value of "sv" is a null list of dict, allocate it.
796 */
797 static void
798allocate_if_null(typval_T *tv)
799{
800 switch (tv->v_type)
801 {
802 case VAR_LIST:
803 if (tv->vval.v_list == NULL)
804 rettv_list_alloc(tv);
805 break;
806 case VAR_DICT:
807 if (tv->vval.v_dict == NULL)
808 rettv_dict_alloc(tv);
809 break;
810 default:
811 break;
812 }
813}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200814
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100815/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100816 * Execute a function by "name".
817 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100818 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100819 */
820 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100821call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100822{
Bram Moolenaared677f52020-08-12 16:38:10 +0200823 int called_emsg_before = called_emsg;
824 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825
Bram Moolenaared677f52020-08-12 16:38:10 +0200826 res = call_by_name(name, argcount, ectx, iptr);
827 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100828 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200829 dictitem_T *v;
830
831 v = find_var(name, NULL, FALSE);
832 if (v == NULL)
833 {
834 semsg(_(e_unknownfunc), name);
835 return FAIL;
836 }
837 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
838 {
839 semsg(_(e_unknownfunc), name);
840 return FAIL;
841 }
842 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100843 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200844 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100845}
846
847/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100848 * When a function reference is used, fill a partial with the information
849 * needed, especially when it is used as a closure.
850 */
851 static int
852fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
853{
854 pt->pt_func = ufunc;
855 pt->pt_refcount = 1;
856
857 if (pt->pt_func->uf_flags & FC_CLOSURE)
858 {
859 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
860 + ectx->ec_dfunc_idx;
861
862 // The closure needs to find arguments and local
863 // variables in the current stack.
864 pt->pt_ectx_stack = &ectx->ec_stack;
865 pt->pt_ectx_frame = ectx->ec_frame_idx;
866
867 // If this function returns and the closure is still
868 // being used, we need to make a copy of the context
869 // (arguments and local variables). Store a reference
870 // to the partial so we can handle that.
871 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
872 {
873 vim_free(pt);
874 return FAIL;
875 }
876 // Extra variable keeps the count of closures created
877 // in the current function call.
878 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
879 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
880
881 ((partial_T **)ectx->ec_funcrefs.ga_data)
882 [ectx->ec_funcrefs.ga_len] = pt;
883 ++pt->pt_refcount;
884 ++ectx->ec_funcrefs.ga_len;
885 }
886 ++pt->pt_func->uf_refcount;
887 return OK;
888}
889
890/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100891 * Call a "def" function from old Vim script.
892 * Return OK or FAIL.
893 */
894 int
895call_def_function(
896 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200897 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100898 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200899 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100900 typval_T *rettv) // return value
901{
902 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200903 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200904 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100905 typval_T *tv;
906 int idx;
907 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100908 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200909 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200910 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100911 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200912 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200913 msglist_T **saved_msg_list = NULL;
914 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200915 cmdmod_T save_cmdmod;
916 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100917 int save_emsg_silent_def = emsg_silent_def;
918 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100919 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100920 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100921
922// Get pointer to item in the stack.
923#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
924
925// Get pointer to item at the bottom of the stack, -1 is the bottom.
926#undef STACK_TV_BOT
927#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
928
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200929// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200930#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 +0100931
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200932// Like STACK_TV_VAR but use the outer scope
933#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
934
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200935 if (ufunc->uf_def_status == UF_NOT_COMPILED
936 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200937 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200938 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100939 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200940 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200941 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200942 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200943 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200944
Bram Moolenaar09689a02020-05-09 22:50:08 +0200945 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200946 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200947 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
948 + ufunc->uf_dfunc_idx;
949 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200950 {
951 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200952 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200953 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200954 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100955
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100956 // If depth of calling is getting too high, don't execute the function.
957 orig_funcdepth = funcdepth_get();
958 if (funcdepth_increment() == FAIL)
959 return FAIL;
960
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200961 CLEAR_FIELD(ectx);
962 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
963 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
964 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100965 {
966 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200967 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100968 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100969 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200970 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100971
972 // Put arguments on the stack.
973 for (idx = 0; idx < argc; ++idx)
974 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200975 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200976 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
977 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200978 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 copy_tv(&argv[idx], STACK_TV_BOT(0));
980 ++ectx.ec_stack.ga_len;
981 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200982
983 // Turn varargs into a list. Empty list if no args.
984 if (ufunc->uf_va_name != NULL)
985 {
986 int vararg_count = argc - ufunc->uf_args.ga_len;
987
988 if (vararg_count < 0)
989 vararg_count = 0;
990 else
991 argc -= vararg_count;
992 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +0200993 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200994
995 // Check the type of the list items.
996 tv = STACK_TV_BOT(-1);
997 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +0200998 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +0200999 && ufunc->uf_va_type->tt_member != &t_any
1000 && tv->vval.v_list != NULL)
1001 {
1002 type_T *expected = ufunc->uf_va_type->tt_member;
1003 listitem_T *li = tv->vval.v_list->lv_first;
1004
1005 for (idx = 0; idx < vararg_count; ++idx)
1006 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001007 if (check_typval_type(expected, &li->li_tv,
1008 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001009 goto failed_early;
1010 li = li->li_next;
1011 }
1012 }
1013
Bram Moolenaar23e03252020-04-12 22:22:31 +02001014 if (defcount > 0)
1015 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001016 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001017 --ectx.ec_stack.ga_len;
1018 }
1019
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001020 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001021 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001022 if (defcount > 0)
1023 for (idx = 0; idx < defcount; ++idx)
1024 {
1025 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1026 ++ectx.ec_stack.ga_len;
1027 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001028 if (ufunc->uf_va_name != NULL)
1029 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030
1031 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001032 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1033 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001034
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001035 if (partial != NULL)
1036 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001037 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1038 {
1039 // TODO: is this always the right way?
1040 ectx.ec_outer_stack = &current_ectx->ec_stack;
1041 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1042 }
1043 else
1044 {
1045 ectx.ec_outer_stack = partial->pt_ectx_stack;
1046 ectx.ec_outer_frame = partial->pt_ectx_frame;
1047 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001048 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001049 else if (ufunc->uf_partial != NULL)
1050 {
1051 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1052 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1053 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001054
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001055 // dummy frame entries
1056 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1057 {
1058 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1059 ++ectx.ec_stack.ga_len;
1060 }
1061
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001062 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001063 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001064 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1065 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001066
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001067 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001068 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001069 ectx.ec_stack.ga_len += dfunc->df_varcount;
1070 if (dfunc->df_has_closure)
1071 {
1072 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1073 STACK_TV_VAR(idx)->vval.v_number = 0;
1074 ++ectx.ec_stack.ga_len;
1075 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001076
1077 ectx.ec_instr = dfunc->df_instr;
1078 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001079
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001080 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001081 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001082 estack_push_ufunc(ufunc, 1);
1083 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001084 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1085
Bram Moolenaar352134b2020-10-17 22:04:08 +02001086 // Use a specific location for storing error messages to be converted to an
1087 // exception.
1088 saved_msg_list = msg_list;
1089 msg_list = &private_msg_list;
1090
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001091 // Do turn errors into exceptions.
1092 suppress_errthrow = FALSE;
1093
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001094 // When ":silent!" was used before calling then we still abort the
1095 // function. If ":silent!" is used in the function then we don't.
1096 emsg_silent_def = emsg_silent;
1097 did_emsg_def = 0;
1098
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001099 // Decide where to start execution, handles optional arguments.
1100 init_instr_idx(ufunc, argc, &ectx);
1101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102 for (;;)
1103 {
1104 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001105
Bram Moolenaar270d0382020-05-15 21:42:53 +02001106 if (++breakcheck_count >= 100)
1107 {
1108 line_breakcheck();
1109 breakcheck_count = 0;
1110 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001111 if (got_int)
1112 {
1113 // Turn CTRL-C into an exception.
1114 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001115 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001116 goto failed;
1117 did_throw = TRUE;
1118 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119
Bram Moolenaara26b9702020-04-18 19:53:28 +02001120 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1121 {
1122 // Turn an error message into an exception.
1123 did_emsg = FALSE;
1124 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1125 goto failed;
1126 did_throw = TRUE;
1127 *msg_list = NULL;
1128 }
1129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 if (did_throw && !ectx.ec_in_catch)
1131 {
1132 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001133 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001134
1135 // An exception jumps to the first catch, finally, or returns from
1136 // the current function.
1137 if (trystack->ga_len > 0)
1138 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001139 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 {
1141 // jump to ":catch" or ":finally"
1142 ectx.ec_in_catch = TRUE;
1143 ectx.ec_iidx = trycmd->tcd_catch_idx;
1144 }
1145 else
1146 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001147 // Not inside try or need to return from current functions.
1148 // Push a dummy return value.
1149 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1150 goto failed;
1151 tv = STACK_TV_BOT(0);
1152 tv->v_type = VAR_NUMBER;
1153 tv->vval.v_number = 0;
1154 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001155 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001157 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001158 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001159 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1160 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161 goto done;
1162 }
1163
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001164 if (func_return(&ectx) == FAIL)
1165 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 }
1167 continue;
1168 }
1169
1170 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1171 switch (iptr->isn_type)
1172 {
1173 // execute Ex command line
1174 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001175 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001176 SOURCING_LNUM = iptr->isn_lnum;
1177 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001178 if (did_emsg)
1179 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181 break;
1182
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001183 // execute Ex command from pieces on the stack
1184 case ISN_EXECCONCAT:
1185 {
1186 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001187 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001188 int pass;
1189 int i;
1190 char_u *cmd = NULL;
1191 char_u *str;
1192
1193 for (pass = 1; pass <= 2; ++pass)
1194 {
1195 for (i = 0; i < count; ++i)
1196 {
1197 tv = STACK_TV_BOT(i - count);
1198 str = tv->vval.v_string;
1199 if (str != NULL && *str != NUL)
1200 {
1201 if (pass == 2)
1202 STRCPY(cmd + len, str);
1203 len += STRLEN(str);
1204 }
1205 if (pass == 2)
1206 clear_tv(tv);
1207 }
1208 if (pass == 1)
1209 {
1210 cmd = alloc(len + 1);
1211 if (cmd == NULL)
1212 goto failed;
1213 len = 0;
1214 }
1215 }
1216
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001217 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001218 do_cmdline_cmd(cmd);
1219 vim_free(cmd);
1220 }
1221 break;
1222
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 // execute :echo {string} ...
1224 case ISN_ECHO:
1225 {
1226 int count = iptr->isn_arg.echo.echo_count;
1227 int atstart = TRUE;
1228 int needclr = TRUE;
1229
1230 for (idx = 0; idx < count; ++idx)
1231 {
1232 tv = STACK_TV_BOT(idx - count);
1233 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1234 &atstart, &needclr);
1235 clear_tv(tv);
1236 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001237 if (needclr)
1238 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001239 ectx.ec_stack.ga_len -= count;
1240 }
1241 break;
1242
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001243 // :execute {string} ...
1244 // :echomsg {string} ...
1245 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001246 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001247 case ISN_ECHOMSG:
1248 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001249 {
1250 int count = iptr->isn_arg.number;
1251 garray_T ga;
1252 char_u buf[NUMBUFLEN];
1253 char_u *p;
1254 int len;
1255 int failed = FALSE;
1256
1257 ga_init2(&ga, 1, 80);
1258 for (idx = 0; idx < count; ++idx)
1259 {
1260 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001261 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001262 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001263 if (tv->v_type == VAR_CHANNEL
1264 || tv->v_type == VAR_JOB)
1265 {
1266 SOURCING_LNUM = iptr->isn_lnum;
1267 emsg(_(e_inval_string));
1268 break;
1269 }
1270 else
1271 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001272 }
1273 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001274 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001275
1276 len = (int)STRLEN(p);
1277 if (ga_grow(&ga, len + 2) == FAIL)
1278 failed = TRUE;
1279 else
1280 {
1281 if (ga.ga_len > 0)
1282 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1283 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1284 ga.ga_len += len;
1285 }
1286 clear_tv(tv);
1287 }
1288 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001289 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001290 {
1291 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001292 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001293 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001294
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001295 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001296 {
1297 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001298 {
1299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001300 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001301 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001302 {
1303 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001304 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001305 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001306 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001307 else
1308 {
1309 msg_sb_eol();
1310 if (iptr->isn_type == ISN_ECHOMSG)
1311 {
1312 msg_attr(ga.ga_data, echo_attr);
1313 out_flush();
1314 }
1315 else
1316 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001317 SOURCING_LNUM = iptr->isn_lnum;
1318 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001319 }
1320 }
1321 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001322 ga_clear(&ga);
1323 }
1324 break;
1325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001326 // load local variable or argument
1327 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001328 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001329 goto failed;
1330 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1331 ++ectx.ec_stack.ga_len;
1332 break;
1333
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001334 // load variable or argument from outer scope
1335 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001336 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001337 goto failed;
1338 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1339 STACK_TV_BOT(0));
1340 ++ectx.ec_stack.ga_len;
1341 break;
1342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 // load v: variable
1344 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001345 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 goto failed;
1347 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1348 ++ectx.ec_stack.ga_len;
1349 break;
1350
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001351 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001352 case ISN_LOADSCRIPT:
1353 {
1354 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001355 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 svar_T *sv;
1357
1358 sv = ((svar_T *)si->sn_var_vals.ga_data)
1359 + iptr->isn_arg.script.script_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001360 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001361 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 goto failed;
1363 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1364 ++ectx.ec_stack.ga_len;
1365 }
1366 break;
1367
1368 // load s: variable in old script
1369 case ISN_LOADS:
1370 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001371 hashtab_T *ht = &SCRIPT_VARS(
1372 iptr->isn_arg.loadstore.ls_sid);
1373 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001374 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 if (di == NULL)
1377 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001378 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001379 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001380 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 }
1382 else
1383 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001384 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 goto failed;
1386 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1387 ++ectx.ec_stack.ga_len;
1388 }
1389 }
1390 break;
1391
Bram Moolenaard3aac292020-04-19 14:32:17 +02001392 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001394 case ISN_LOADB:
1395 case ISN_LOADW:
1396 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001398 dictitem_T *di = NULL;
1399 hashtab_T *ht = NULL;
1400 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001401
Bram Moolenaard3aac292020-04-19 14:32:17 +02001402 switch (iptr->isn_type)
1403 {
1404 case ISN_LOADG:
1405 ht = get_globvar_ht();
1406 namespace = 'g';
1407 break;
1408 case ISN_LOADB:
1409 ht = &curbuf->b_vars->dv_hashtab;
1410 namespace = 'b';
1411 break;
1412 case ISN_LOADW:
1413 ht = &curwin->w_vars->dv_hashtab;
1414 namespace = 'w';
1415 break;
1416 case ISN_LOADT:
1417 ht = &curtab->tp_vars->dv_hashtab;
1418 namespace = 't';
1419 break;
1420 default: // Cannot reach here
1421 goto failed;
1422 }
1423 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001424
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 if (di == NULL)
1426 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001427 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001428 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001429 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001430 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 }
1432 else
1433 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001434 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 goto failed;
1436 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1437 ++ectx.ec_stack.ga_len;
1438 }
1439 }
1440 break;
1441
Bram Moolenaar03290b82020-12-19 16:30:44 +01001442 // load autoload variable
1443 case ISN_LOADAUTO:
1444 {
1445 char_u *name = iptr->isn_arg.string;
1446
1447 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1448 goto failed;
1449 SOURCING_LNUM = iptr->isn_lnum;
1450 if (eval_variable(name, STRLEN(name),
1451 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1452 goto on_error;
1453 ++ectx.ec_stack.ga_len;
1454 }
1455 break;
1456
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001457 // load g:/b:/w:/t: namespace
1458 case ISN_LOADGDICT:
1459 case ISN_LOADBDICT:
1460 case ISN_LOADWDICT:
1461 case ISN_LOADTDICT:
1462 {
1463 dict_T *d = NULL;
1464
1465 switch (iptr->isn_type)
1466 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001467 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1468 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1469 case ISN_LOADWDICT: d = curwin->w_vars; break;
1470 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001471 default: // Cannot reach here
1472 goto failed;
1473 }
1474 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1475 goto failed;
1476 tv = STACK_TV_BOT(0);
1477 tv->v_type = VAR_DICT;
1478 tv->v_lock = 0;
1479 tv->vval.v_dict = d;
1480 ++ectx.ec_stack.ga_len;
1481 }
1482 break;
1483
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 // load &option
1485 case ISN_LOADOPT:
1486 {
1487 typval_T optval;
1488 char_u *name = iptr->isn_arg.string;
1489
Bram Moolenaara8c17702020-04-01 21:17:24 +02001490 // This is not expected to fail, name is checked during
1491 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001492 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001494 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001495 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001496 *STACK_TV_BOT(0) = optval;
1497 ++ectx.ec_stack.ga_len;
1498 }
1499 break;
1500
1501 // load $ENV
1502 case ISN_LOADENV:
1503 {
1504 typval_T optval;
1505 char_u *name = iptr->isn_arg.string;
1506
Bram Moolenaar270d0382020-05-15 21:42:53 +02001507 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001508 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001509 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001510 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001511 *STACK_TV_BOT(0) = optval;
1512 ++ectx.ec_stack.ga_len;
1513 }
1514 break;
1515
1516 // load @register
1517 case ISN_LOADREG:
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;
1520 tv = STACK_TV_BOT(0);
1521 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001522 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001523 // This may result in NULL, which should be equivalent to an
1524 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001525 tv->vval.v_string = get_reg_contents(
1526 iptr->isn_arg.number, GREG_EXPR_SRC);
1527 ++ectx.ec_stack.ga_len;
1528 break;
1529
1530 // store local variable
1531 case ISN_STORE:
1532 --ectx.ec_stack.ga_len;
1533 tv = STACK_TV_VAR(iptr->isn_arg.number);
1534 clear_tv(tv);
1535 *tv = *STACK_TV_BOT(0);
1536 break;
1537
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001538 // store variable or argument in outer scope
1539 case ISN_STOREOUTER:
1540 --ectx.ec_stack.ga_len;
1541 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1542 clear_tv(tv);
1543 *tv = *STACK_TV_BOT(0);
1544 break;
1545
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001546 // store s: variable in old script
1547 case ISN_STORES:
1548 {
1549 hashtab_T *ht = &SCRIPT_VARS(
1550 iptr->isn_arg.loadstore.ls_sid);
1551 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001552 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001553
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001554 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001555 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001556 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001557 else
1558 {
1559 clear_tv(&di->di_tv);
1560 di->di_tv = *STACK_TV_BOT(0);
1561 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001562 }
1563 break;
1564
1565 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 case ISN_STORESCRIPT:
1567 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001568 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001569 iptr->isn_arg.script.script_sid);
1570 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1571 + iptr->isn_arg.script.script_idx;
1572
1573 --ectx.ec_stack.ga_len;
1574 clear_tv(sv->sv_tv);
1575 *sv->sv_tv = *STACK_TV_BOT(0);
1576 }
1577 break;
1578
1579 // store option
1580 case ISN_STOREOPT:
1581 {
1582 long n = 0;
1583 char_u *s = NULL;
1584 char *msg;
1585
1586 --ectx.ec_stack.ga_len;
1587 tv = STACK_TV_BOT(0);
1588 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001589 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001590 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001591 if (s == NULL)
1592 s = (char_u *)"";
1593 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001595 // must be VAR_NUMBER, CHECKTYPE makes sure
1596 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1598 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001599 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 if (msg != NULL)
1601 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001602 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001604 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001606 }
1607 break;
1608
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001609 // store $ENV
1610 case ISN_STOREENV:
1611 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001612 tv = STACK_TV_BOT(0);
1613 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1614 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001615 break;
1616
1617 // store @r
1618 case ISN_STOREREG:
1619 {
1620 int reg = iptr->isn_arg.number;
1621
1622 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001623 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001624 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001625 tv_get_string(tv), -1, FALSE);
1626 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001627 }
1628 break;
1629
1630 // store v: variable
1631 case ISN_STOREV:
1632 --ectx.ec_stack.ga_len;
1633 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1634 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001635 // should not happen, type is checked when compiling
1636 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001637 break;
1638
Bram Moolenaard3aac292020-04-19 14:32:17 +02001639 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001641 case ISN_STOREB:
1642 case ISN_STOREW:
1643 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001644 {
1645 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001646 hashtab_T *ht;
1647 switch (iptr->isn_type)
1648 {
1649 case ISN_STOREG:
1650 ht = get_globvar_ht();
1651 break;
1652 case ISN_STOREB:
1653 ht = &curbuf->b_vars->dv_hashtab;
1654 break;
1655 case ISN_STOREW:
1656 ht = &curwin->w_vars->dv_hashtab;
1657 break;
1658 case ISN_STORET:
1659 ht = &curtab->tp_vars->dv_hashtab;
1660 break;
1661 default: // Cannot reach here
1662 goto failed;
1663 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664
1665 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001666 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001668 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 else
1670 {
1671 clear_tv(&di->di_tv);
1672 di->di_tv = *STACK_TV_BOT(0);
1673 }
1674 }
1675 break;
1676
Bram Moolenaar03290b82020-12-19 16:30:44 +01001677 // store an autoload variable
1678 case ISN_STOREAUTO:
1679 SOURCING_LNUM = iptr->isn_lnum;
1680 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1681 clear_tv(STACK_TV_BOT(-1));
1682 --ectx.ec_stack.ga_len;
1683 break;
1684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001685 // store number in local variable
1686 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001687 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 clear_tv(tv);
1689 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001690 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 break;
1692
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001693 // store value in list variable
1694 case ISN_STORELIST:
1695 {
1696 typval_T *tv_idx = STACK_TV_BOT(-2);
1697 varnumber_T lidx = tv_idx->vval.v_number;
1698 typval_T *tv_list = STACK_TV_BOT(-1);
1699 list_T *list = tv_list->vval.v_list;
1700
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001701 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001702 if (lidx < 0 && list->lv_len + lidx >= 0)
1703 // negative index is relative to the end
1704 lidx = list->lv_len + lidx;
1705 if (lidx < 0 || lidx > list->lv_len)
1706 {
1707 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001708 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001709 }
1710 tv = STACK_TV_BOT(-3);
1711 if (lidx < list->lv_len)
1712 {
1713 listitem_T *li = list_find(list, lidx);
1714
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001715 if (error_if_locked(li->li_tv.v_lock,
1716 e_cannot_change_list_item))
1717 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001718 // overwrite existing list item
1719 clear_tv(&li->li_tv);
1720 li->li_tv = *tv;
1721 }
1722 else
1723 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001724 if (error_if_locked(list->lv_lock,
1725 e_cannot_change_list))
1726 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001727 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001728 if (list_append_tv(list, tv) == FAIL)
1729 goto failed;
1730 clear_tv(tv);
1731 }
1732 clear_tv(tv_idx);
1733 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001734 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001735 }
1736 break;
1737
1738 // store value in dict variable
1739 case ISN_STOREDICT:
1740 {
1741 typval_T *tv_key = STACK_TV_BOT(-2);
1742 char_u *key = tv_key->vval.v_string;
1743 typval_T *tv_dict = STACK_TV_BOT(-1);
1744 dict_T *dict = tv_dict->vval.v_dict;
1745 dictitem_T *di;
1746
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001747 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001748 if (dict == NULL)
1749 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001750 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001751 goto on_error;
1752 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001753 if (key == NULL)
1754 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001755 tv = STACK_TV_BOT(-3);
1756 di = dict_find(dict, key, -1);
1757 if (di != NULL)
1758 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001759 if (error_if_locked(di->di_tv.v_lock,
1760 e_cannot_change_dict_item))
1761 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001762 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001763 clear_tv(&di->di_tv);
1764 di->di_tv = *tv;
1765 }
1766 else
1767 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001768 if (error_if_locked(dict->dv_lock,
1769 e_cannot_change_dict))
1770 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001771 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001772 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1773 goto failed;
1774 clear_tv(tv);
1775 }
1776 clear_tv(tv_key);
1777 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001778 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001779 }
1780 break;
1781
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001782 // push constant
1783 case ISN_PUSHNR:
1784 case ISN_PUSHBOOL:
1785 case ISN_PUSHSPEC:
1786 case ISN_PUSHF:
1787 case ISN_PUSHS:
1788 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001789 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001790 case ISN_PUSHCHANNEL:
1791 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001792 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001793 goto failed;
1794 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001795 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001796 ++ectx.ec_stack.ga_len;
1797 switch (iptr->isn_type)
1798 {
1799 case ISN_PUSHNR:
1800 tv->v_type = VAR_NUMBER;
1801 tv->vval.v_number = iptr->isn_arg.number;
1802 break;
1803 case ISN_PUSHBOOL:
1804 tv->v_type = VAR_BOOL;
1805 tv->vval.v_number = iptr->isn_arg.number;
1806 break;
1807 case ISN_PUSHSPEC:
1808 tv->v_type = VAR_SPECIAL;
1809 tv->vval.v_number = iptr->isn_arg.number;
1810 break;
1811#ifdef FEAT_FLOAT
1812 case ISN_PUSHF:
1813 tv->v_type = VAR_FLOAT;
1814 tv->vval.v_float = iptr->isn_arg.fnumber;
1815 break;
1816#endif
1817 case ISN_PUSHBLOB:
1818 blob_copy(iptr->isn_arg.blob, tv);
1819 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001820 case ISN_PUSHFUNC:
1821 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001822 if (iptr->isn_arg.string == NULL)
1823 tv->vval.v_string = NULL;
1824 else
1825 tv->vval.v_string =
1826 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001827 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001828 case ISN_PUSHCHANNEL:
1829#ifdef FEAT_JOB_CHANNEL
1830 tv->v_type = VAR_CHANNEL;
1831 tv->vval.v_channel = iptr->isn_arg.channel;
1832 if (tv->vval.v_channel != NULL)
1833 ++tv->vval.v_channel->ch_refcount;
1834#endif
1835 break;
1836 case ISN_PUSHJOB:
1837#ifdef FEAT_JOB_CHANNEL
1838 tv->v_type = VAR_JOB;
1839 tv->vval.v_job = iptr->isn_arg.job;
1840 if (tv->vval.v_job != NULL)
1841 ++tv->vval.v_job->jv_refcount;
1842#endif
1843 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 default:
1845 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001846 tv->vval.v_string = vim_strsave(
1847 iptr->isn_arg.string == NULL
1848 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001849 }
1850 break;
1851
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001852 case ISN_UNLET:
1853 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1854 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001855 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001856 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001857 case ISN_UNLETENV:
1858 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1859 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001860
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001861 case ISN_LOCKCONST:
1862 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1863 break;
1864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001865 // create a list from items on the stack; uses a single allocation
1866 // for the list header and the items
1867 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001868 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1869 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 break;
1871
1872 // create a dict from items on the stack
1873 case ISN_NEWDICT:
1874 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001875 int count = iptr->isn_arg.number;
1876 dict_T *dict = dict_alloc();
1877 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001878 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879
1880 if (dict == NULL)
1881 goto failed;
1882 for (idx = 0; idx < count; ++idx)
1883 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001884 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001885 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001886 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001887 key = tv->vval.v_string == NULL
1888 ? (char_u *)"" : tv->vval.v_string;
1889 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001890 if (item != NULL)
1891 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001892 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001893 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001894 dict_unref(dict);
1895 goto on_error;
1896 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001897 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 clear_tv(tv);
1899 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001900 {
1901 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001903 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1905 item->di_tv.v_lock = 0;
1906 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001907 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001908 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001909 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001911 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 }
1913
1914 if (count > 0)
1915 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001916 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001917 goto failed;
1918 else
1919 ++ectx.ec_stack.ga_len;
1920 tv = STACK_TV_BOT(-1);
1921 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001922 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 tv->vval.v_dict = dict;
1924 ++dict->dv_refcount;
1925 }
1926 break;
1927
1928 // call a :def function
1929 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001930 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1932 iptr->isn_arg.dfunc.cdf_argcount,
1933 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001934 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001935 break;
1936
1937 // call a builtin function
1938 case ISN_BCALL:
1939 SOURCING_LNUM = iptr->isn_lnum;
1940 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1941 iptr->isn_arg.bfunc.cbf_argcount,
1942 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001943 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001944 break;
1945
1946 // call a funcref or partial
1947 case ISN_PCALL:
1948 {
1949 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1950 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001951 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952
1953 SOURCING_LNUM = iptr->isn_lnum;
1954 if (pfunc->cpf_top)
1955 {
1956 // funcref is above the arguments
1957 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1958 }
1959 else
1960 {
1961 // Get the funcref from the stack.
1962 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001963 partial_tv = *STACK_TV_BOT(0);
1964 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 }
1966 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001967 if (tv == &partial_tv)
1968 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001969 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001970 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001971 }
1972 break;
1973
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001974 case ISN_PCALL_END:
1975 // PCALL finished, arguments have been consumed and replaced by
1976 // the return value. Now clear the funcref from the stack,
1977 // and move the return value in its place.
1978 --ectx.ec_stack.ga_len;
1979 clear_tv(STACK_TV_BOT(-1));
1980 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1981 break;
1982
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 // call a user defined function or funcref/partial
1984 case ISN_UCALL:
1985 {
1986 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1987
1988 SOURCING_LNUM = iptr->isn_lnum;
1989 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001990 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001991 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 }
1993 break;
1994
1995 // return from a :def function call
1996 case ISN_RETURN:
1997 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01001998 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001999 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002000
2001 if (trystack->ga_len > 0)
2002 trycmd = ((trycmd_T *)trystack->ga_data)
2003 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002004 if (trycmd != NULL
2005 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 && trycmd->tcd_finally_idx != 0)
2007 {
2008 // jump to ":finally"
2009 ectx.ec_iidx = trycmd->tcd_finally_idx;
2010 trycmd->tcd_return = TRUE;
2011 }
2012 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002013 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 }
2015 break;
2016
2017 // push a function reference to a compiled function
2018 case ISN_FUNCREF:
2019 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002020 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2021 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2022 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 if (pt == NULL)
2025 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002026 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002027 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002028 vim_free(pt);
2029 goto failed;
2030 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002031 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2032 &ectx) == FAIL)
2033 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002034
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002035 tv = STACK_TV_BOT(0);
2036 ++ectx.ec_stack.ga_len;
2037 tv->vval.v_partial = pt;
2038 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002039 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002040 }
2041 break;
2042
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002043 // Create a global function from a lambda.
2044 case ISN_NEWFUNC:
2045 {
2046 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002047 ufunc_T *new_ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002048
Bram Moolenaarf112f302020-12-20 17:47:52 +01002049 new_ufunc = copy_func(
2050 newfunc->nf_lambda, newfunc->nf_global);
2051 if (new_ufunc != NULL
2052 && (new_ufunc->uf_flags & FC_CLOSURE))
2053 {
2054 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2055
2056 // Need to create a partial to store the context of the
2057 // function.
2058 if (pt == NULL)
2059 goto failed;
2060 if (fill_partial_and_closure(pt, new_ufunc,
2061 &ectx) == FAIL)
2062 goto failed;
2063 new_ufunc->uf_partial = pt;
2064 --pt->pt_refcount; // not referenced here
2065 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002066 }
2067 break;
2068
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002069 // List functions
2070 case ISN_DEF:
2071 if (iptr->isn_arg.string == NULL)
2072 list_functions(NULL);
2073 else
2074 {
2075 exarg_T ea;
2076
2077 CLEAR_FIELD(ea);
2078 ea.cmd = ea.arg = iptr->isn_arg.string;
2079 define_function(&ea, NULL);
2080 }
2081 break;
2082
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002083 // jump if a condition is met
2084 case ISN_JUMP:
2085 {
2086 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002087 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002088 int jump = TRUE;
2089
2090 if (when != JUMP_ALWAYS)
2091 {
2092 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002093 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002094 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002095 || when == JUMP_IF_COND_TRUE)
2096 {
2097 SOURCING_LNUM = iptr->isn_lnum;
2098 jump = tv_get_bool_chk(tv, &error);
2099 if (error)
2100 goto on_error;
2101 }
2102 else
2103 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002104 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002105 || when == JUMP_AND_KEEP_IF_FALSE
2106 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002107 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002108 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002109 {
2110 // drop the value from the stack
2111 clear_tv(tv);
2112 --ectx.ec_stack.ga_len;
2113 }
2114 }
2115 if (jump)
2116 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2117 }
2118 break;
2119
2120 // top of a for loop
2121 case ISN_FOR:
2122 {
2123 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2124 typval_T *idxtv =
2125 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2126
2127 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002128 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002129 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002130 ++idxtv->vval.v_number;
2131 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002132 // past the end of the list, jump to "endfor"
2133 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2134 else if (list->lv_first == &range_list_item)
2135 {
2136 // non-materialized range() list
2137 tv = STACK_TV_BOT(0);
2138 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002139 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002140 tv->vval.v_number = list_find_nr(
2141 list, idxtv->vval.v_number, NULL);
2142 ++ectx.ec_stack.ga_len;
2143 }
2144 else
2145 {
2146 listitem_T *li = list_find(list, idxtv->vval.v_number);
2147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2149 ++ectx.ec_stack.ga_len;
2150 }
2151 }
2152 break;
2153
2154 // start of ":try" block
2155 case ISN_TRY:
2156 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002157 trycmd_T *trycmd = NULL;
2158
Bram Moolenaar270d0382020-05-15 21:42:53 +02002159 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002160 goto failed;
2161 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2162 + ectx.ec_trystack.ga_len;
2163 ++ectx.ec_trystack.ga_len;
2164 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002165 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2167 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002168 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002169 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 }
2171 break;
2172
2173 case ISN_PUSHEXC:
2174 if (current_exception == NULL)
2175 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002176 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002177 iemsg("Evaluating catch while current_exception is NULL");
2178 goto failed;
2179 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002180 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 goto failed;
2182 tv = STACK_TV_BOT(0);
2183 ++ectx.ec_stack.ga_len;
2184 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002185 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 tv->vval.v_string = vim_strsave(
2187 (char_u *)current_exception->value);
2188 break;
2189
2190 case ISN_CATCH:
2191 {
2192 garray_T *trystack = &ectx.ec_trystack;
2193
2194 if (trystack->ga_len > 0)
2195 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002196 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 + trystack->ga_len - 1;
2198 trycmd->tcd_caught = TRUE;
2199 }
2200 did_emsg = got_int = did_throw = FALSE;
2201 catch_exception(current_exception);
2202 }
2203 break;
2204
2205 // end of ":try" block
2206 case ISN_ENDTRY:
2207 {
2208 garray_T *trystack = &ectx.ec_trystack;
2209
2210 if (trystack->ga_len > 0)
2211 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002212 trycmd_T *trycmd = NULL;
2213
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002214 --trystack->ga_len;
2215 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002216 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 trycmd = ((trycmd_T *)trystack->ga_data)
2218 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002219 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002220 {
2221 // discard the exception
2222 if (caught_stack == current_exception)
2223 caught_stack = caught_stack->caught;
2224 discard_current_exception();
2225 }
2226
2227 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002228 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002229 }
2230 }
2231 break;
2232
2233 case ISN_THROW:
2234 --ectx.ec_stack.ga_len;
2235 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002236 if (tv->vval.v_string == NULL
2237 || *skipwhite(tv->vval.v_string) == NUL)
2238 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002239 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002240 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002241 emsg(_(e_throw_with_empty_string));
2242 goto failed;
2243 }
2244
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002245 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2246 {
2247 vim_free(tv->vval.v_string);
2248 goto failed;
2249 }
2250 did_throw = TRUE;
2251 break;
2252
2253 // compare with special values
2254 case ISN_COMPAREBOOL:
2255 case ISN_COMPARESPECIAL:
2256 {
2257 typval_T *tv1 = STACK_TV_BOT(-2);
2258 typval_T *tv2 = STACK_TV_BOT(-1);
2259 varnumber_T arg1 = tv1->vval.v_number;
2260 varnumber_T arg2 = tv2->vval.v_number;
2261 int res;
2262
2263 switch (iptr->isn_arg.op.op_type)
2264 {
2265 case EXPR_EQUAL: res = arg1 == arg2; break;
2266 case EXPR_NEQUAL: res = arg1 != arg2; break;
2267 default: res = 0; break;
2268 }
2269
2270 --ectx.ec_stack.ga_len;
2271 tv1->v_type = VAR_BOOL;
2272 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2273 }
2274 break;
2275
2276 // Operation with two number arguments
2277 case ISN_OPNR:
2278 case ISN_COMPARENR:
2279 {
2280 typval_T *tv1 = STACK_TV_BOT(-2);
2281 typval_T *tv2 = STACK_TV_BOT(-1);
2282 varnumber_T arg1 = tv1->vval.v_number;
2283 varnumber_T arg2 = tv2->vval.v_number;
2284 varnumber_T res;
2285
2286 switch (iptr->isn_arg.op.op_type)
2287 {
2288 case EXPR_MULT: res = arg1 * arg2; break;
2289 case EXPR_DIV: res = arg1 / arg2; break;
2290 case EXPR_REM: res = arg1 % arg2; break;
2291 case EXPR_SUB: res = arg1 - arg2; break;
2292 case EXPR_ADD: res = arg1 + arg2; break;
2293
2294 case EXPR_EQUAL: res = arg1 == arg2; break;
2295 case EXPR_NEQUAL: res = arg1 != arg2; break;
2296 case EXPR_GREATER: res = arg1 > arg2; break;
2297 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2298 case EXPR_SMALLER: res = arg1 < arg2; break;
2299 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2300 default: res = 0; break;
2301 }
2302
2303 --ectx.ec_stack.ga_len;
2304 if (iptr->isn_type == ISN_COMPARENR)
2305 {
2306 tv1->v_type = VAR_BOOL;
2307 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2308 }
2309 else
2310 tv1->vval.v_number = res;
2311 }
2312 break;
2313
2314 // Computation with two float arguments
2315 case ISN_OPFLOAT:
2316 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002317#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002318 {
2319 typval_T *tv1 = STACK_TV_BOT(-2);
2320 typval_T *tv2 = STACK_TV_BOT(-1);
2321 float_T arg1 = tv1->vval.v_float;
2322 float_T arg2 = tv2->vval.v_float;
2323 float_T res = 0;
2324 int cmp = FALSE;
2325
2326 switch (iptr->isn_arg.op.op_type)
2327 {
2328 case EXPR_MULT: res = arg1 * arg2; break;
2329 case EXPR_DIV: res = arg1 / arg2; break;
2330 case EXPR_SUB: res = arg1 - arg2; break;
2331 case EXPR_ADD: res = arg1 + arg2; break;
2332
2333 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2334 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2335 case EXPR_GREATER: cmp = arg1 > arg2; break;
2336 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2337 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2338 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2339 default: cmp = 0; break;
2340 }
2341 --ectx.ec_stack.ga_len;
2342 if (iptr->isn_type == ISN_COMPAREFLOAT)
2343 {
2344 tv1->v_type = VAR_BOOL;
2345 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2346 }
2347 else
2348 tv1->vval.v_float = res;
2349 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002350#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351 break;
2352
2353 case ISN_COMPARELIST:
2354 {
2355 typval_T *tv1 = STACK_TV_BOT(-2);
2356 typval_T *tv2 = STACK_TV_BOT(-1);
2357 list_T *arg1 = tv1->vval.v_list;
2358 list_T *arg2 = tv2->vval.v_list;
2359 int cmp = FALSE;
2360 int ic = iptr->isn_arg.op.op_ic;
2361
2362 switch (iptr->isn_arg.op.op_type)
2363 {
2364 case EXPR_EQUAL: cmp =
2365 list_equal(arg1, arg2, ic, FALSE); break;
2366 case EXPR_NEQUAL: cmp =
2367 !list_equal(arg1, arg2, ic, FALSE); break;
2368 case EXPR_IS: cmp = arg1 == arg2; break;
2369 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2370 default: cmp = 0; break;
2371 }
2372 --ectx.ec_stack.ga_len;
2373 clear_tv(tv1);
2374 clear_tv(tv2);
2375 tv1->v_type = VAR_BOOL;
2376 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2377 }
2378 break;
2379
2380 case ISN_COMPAREBLOB:
2381 {
2382 typval_T *tv1 = STACK_TV_BOT(-2);
2383 typval_T *tv2 = STACK_TV_BOT(-1);
2384 blob_T *arg1 = tv1->vval.v_blob;
2385 blob_T *arg2 = tv2->vval.v_blob;
2386 int cmp = FALSE;
2387
2388 switch (iptr->isn_arg.op.op_type)
2389 {
2390 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2391 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2392 case EXPR_IS: cmp = arg1 == arg2; break;
2393 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2394 default: cmp = 0; break;
2395 }
2396 --ectx.ec_stack.ga_len;
2397 clear_tv(tv1);
2398 clear_tv(tv2);
2399 tv1->v_type = VAR_BOOL;
2400 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2401 }
2402 break;
2403
2404 // TODO: handle separately
2405 case ISN_COMPARESTRING:
2406 case ISN_COMPAREDICT:
2407 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002408 case ISN_COMPAREANY:
2409 {
2410 typval_T *tv1 = STACK_TV_BOT(-2);
2411 typval_T *tv2 = STACK_TV_BOT(-1);
2412 exptype_T exptype = iptr->isn_arg.op.op_type;
2413 int ic = iptr->isn_arg.op.op_ic;
2414
Bram Moolenaareb26f432020-09-14 16:50:05 +02002415 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 typval_compare(tv1, tv2, exptype, ic);
2417 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 --ectx.ec_stack.ga_len;
2419 }
2420 break;
2421
2422 case ISN_ADDLIST:
2423 case ISN_ADDBLOB:
2424 {
2425 typval_T *tv1 = STACK_TV_BOT(-2);
2426 typval_T *tv2 = STACK_TV_BOT(-1);
2427
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002428 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 if (iptr->isn_type == ISN_ADDLIST)
2430 eval_addlist(tv1, tv2);
2431 else
2432 eval_addblob(tv1, tv2);
2433 clear_tv(tv2);
2434 --ectx.ec_stack.ga_len;
2435 }
2436 break;
2437
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002438 case ISN_LISTAPPEND:
2439 {
2440 typval_T *tv1 = STACK_TV_BOT(-2);
2441 typval_T *tv2 = STACK_TV_BOT(-1);
2442 list_T *l = tv1->vval.v_list;
2443
2444 // add an item to a list
2445 if (l == NULL)
2446 {
2447 SOURCING_LNUM = iptr->isn_lnum;
2448 emsg(_(e_cannot_add_to_null_list));
2449 goto on_error;
2450 }
2451 if (list_append_tv(l, tv2) == FAIL)
2452 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002453 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002454 --ectx.ec_stack.ga_len;
2455 }
2456 break;
2457
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002458 case ISN_BLOBAPPEND:
2459 {
2460 typval_T *tv1 = STACK_TV_BOT(-2);
2461 typval_T *tv2 = STACK_TV_BOT(-1);
2462 blob_T *b = tv1->vval.v_blob;
2463 int error = FALSE;
2464 varnumber_T n;
2465
2466 // add a number to a blob
2467 if (b == NULL)
2468 {
2469 SOURCING_LNUM = iptr->isn_lnum;
2470 emsg(_(e_cannot_add_to_null_blob));
2471 goto on_error;
2472 }
2473 n = tv_get_number_chk(tv2, &error);
2474 if (error)
2475 goto on_error;
2476 ga_append(&b->bv_ga, (int)n);
2477 --ectx.ec_stack.ga_len;
2478 }
2479 break;
2480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 // Computation with two arguments of unknown type
2482 case ISN_OPANY:
2483 {
2484 typval_T *tv1 = STACK_TV_BOT(-2);
2485 typval_T *tv2 = STACK_TV_BOT(-1);
2486 varnumber_T n1, n2;
2487#ifdef FEAT_FLOAT
2488 float_T f1 = 0, f2 = 0;
2489#endif
2490 int error = FALSE;
2491
2492 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2493 {
2494 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2495 {
2496 eval_addlist(tv1, tv2);
2497 clear_tv(tv2);
2498 --ectx.ec_stack.ga_len;
2499 break;
2500 }
2501 else if (tv1->v_type == VAR_BLOB
2502 && tv2->v_type == VAR_BLOB)
2503 {
2504 eval_addblob(tv1, tv2);
2505 clear_tv(tv2);
2506 --ectx.ec_stack.ga_len;
2507 break;
2508 }
2509 }
2510#ifdef FEAT_FLOAT
2511 if (tv1->v_type == VAR_FLOAT)
2512 {
2513 f1 = tv1->vval.v_float;
2514 n1 = 0;
2515 }
2516 else
2517#endif
2518 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002520 n1 = tv_get_number_chk(tv1, &error);
2521 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523#ifdef FEAT_FLOAT
2524 if (tv2->v_type == VAR_FLOAT)
2525 f1 = n1;
2526#endif
2527 }
2528#ifdef FEAT_FLOAT
2529 if (tv2->v_type == VAR_FLOAT)
2530 {
2531 f2 = tv2->vval.v_float;
2532 n2 = 0;
2533 }
2534 else
2535#endif
2536 {
2537 n2 = tv_get_number_chk(tv2, &error);
2538 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002539 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002540#ifdef FEAT_FLOAT
2541 if (tv1->v_type == VAR_FLOAT)
2542 f2 = n2;
2543#endif
2544 }
2545#ifdef FEAT_FLOAT
2546 // if there is a float on either side the result is a float
2547 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2548 {
2549 switch (iptr->isn_arg.op.op_type)
2550 {
2551 case EXPR_MULT: f1 = f1 * f2; break;
2552 case EXPR_DIV: f1 = f1 / f2; break;
2553 case EXPR_SUB: f1 = f1 - f2; break;
2554 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002555 default: SOURCING_LNUM = iptr->isn_lnum;
2556 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002557 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 }
2559 clear_tv(tv1);
2560 clear_tv(tv2);
2561 tv1->v_type = VAR_FLOAT;
2562 tv1->vval.v_float = f1;
2563 --ectx.ec_stack.ga_len;
2564 }
2565 else
2566#endif
2567 {
2568 switch (iptr->isn_arg.op.op_type)
2569 {
2570 case EXPR_MULT: n1 = n1 * n2; break;
2571 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2572 case EXPR_SUB: n1 = n1 - n2; break;
2573 case EXPR_ADD: n1 = n1 + n2; break;
2574 default: n1 = num_modulus(n1, n2); break;
2575 }
2576 clear_tv(tv1);
2577 clear_tv(tv2);
2578 tv1->v_type = VAR_NUMBER;
2579 tv1->vval.v_number = n1;
2580 --ectx.ec_stack.ga_len;
2581 }
2582 }
2583 break;
2584
2585 case ISN_CONCAT:
2586 {
2587 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2588 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2589 char_u *res;
2590
2591 res = concat_str(str1, str2);
2592 clear_tv(STACK_TV_BOT(-2));
2593 clear_tv(STACK_TV_BOT(-1));
2594 --ectx.ec_stack.ga_len;
2595 STACK_TV_BOT(-1)->vval.v_string = res;
2596 }
2597 break;
2598
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002599 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002600 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002601 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002602 int is_slice = iptr->isn_type == ISN_STRSLICE;
2603 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002604 char_u *res;
2605
2606 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002607 // string slice: string is at stack-3, first index at
2608 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002609 if (is_slice)
2610 {
2611 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002612 n1 = tv->vval.v_number;
2613 }
2614
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002615 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002616 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002617
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002618 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002619 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002620 if (is_slice)
2621 // Slice: Select the characters from the string
2622 res = string_slice(tv->vval.v_string, n1, n2);
2623 else
2624 // Index: The resulting variable is a string of a
2625 // single character. If the index is too big or
2626 // negative the result is empty.
2627 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002628 vim_free(tv->vval.v_string);
2629 tv->vval.v_string = res;
2630 }
2631 break;
2632
2633 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002634 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 {
Bram Moolenaared591872020-08-15 22:14:53 +02002636 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002638 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002639
2640 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002641 // list slice: list is at stack-3, indexes at stack-2 and
2642 // stack-1
2643 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002644 list = tv->vval.v_list;
2645
2646 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002647 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002649
2650 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002651 {
Bram Moolenaared591872020-08-15 22:14:53 +02002652 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002653 n1 = tv->vval.v_number;
2654 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 }
Bram Moolenaared591872020-08-15 22:14:53 +02002656
2657 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002658 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002659 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002660 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2661 == FAIL)
2662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 }
2664 break;
2665
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002666 case ISN_ANYINDEX:
2667 case ISN_ANYSLICE:
2668 {
2669 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2670 typval_T *var1, *var2;
2671 int res;
2672
2673 // index: composite is at stack-2, index at stack-1
2674 // slice: composite is at stack-3, indexes at stack-2 and
2675 // stack-1
2676 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002677 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002678 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2679 goto on_error;
2680 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2681 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2682 res = eval_index_inner(tv, is_slice,
2683 var1, var2, NULL, -1, TRUE);
2684 clear_tv(var1);
2685 if (is_slice)
2686 clear_tv(var2);
2687 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2688 if (res == FAIL)
2689 goto on_error;
2690 }
2691 break;
2692
Bram Moolenaar9af78762020-06-16 11:34:42 +02002693 case ISN_SLICE:
2694 {
2695 list_T *list;
2696 int count = iptr->isn_arg.number;
2697
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002698 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002699 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002700 list = tv->vval.v_list;
2701
2702 // no error for short list, expect it to be checked earlier
2703 if (list != NULL && list->lv_len >= count)
2704 {
2705 list_T *newlist = list_slice(list,
2706 count, list->lv_len - 1);
2707
2708 if (newlist != NULL)
2709 {
2710 list_unref(list);
2711 tv->vval.v_list = newlist;
2712 ++newlist->lv_refcount;
2713 }
2714 }
2715 }
2716 break;
2717
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002718 case ISN_GETITEM:
2719 {
2720 listitem_T *li;
2721 int index = iptr->isn_arg.number;
2722
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002723 // Get list item: list is at stack-1, push item.
2724 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002725 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002726 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002727
2728 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2729 goto failed;
2730 ++ectx.ec_stack.ga_len;
2731 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2732 }
2733 break;
2734
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002735 case ISN_MEMBER:
2736 {
2737 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002738 char_u *key;
2739 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002740 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002741
2742 // dict member: dict is at stack-2, key at stack-1
2743 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002744 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002745 dict = tv->vval.v_dict;
2746
2747 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002748 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002749 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002750 if (key == NULL)
2751 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002752
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002753 if ((di = dict_find(dict, key, -1)) == NULL)
2754 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002755 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002756 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002757
2758 // If :silent! is used we will continue, make sure the
2759 // stack contents makes sense.
2760 clear_tv(tv);
2761 --ectx.ec_stack.ga_len;
2762 tv = STACK_TV_BOT(-1);
2763 clear_tv(tv);
2764 tv->v_type = VAR_NUMBER;
2765 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002766 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002767 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002768 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002769 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002770 // Clear the dict only after getting the item, to avoid
2771 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002772 tv = STACK_TV_BOT(-1);
2773 temp_tv = *tv;
2774 copy_tv(&di->di_tv, tv);
2775 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002776 }
2777 break;
2778
2779 // dict member with string key
2780 case ISN_STRINGMEMBER:
2781 {
2782 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002783 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002784 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785
2786 tv = STACK_TV_BOT(-1);
2787 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2788 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002789 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002791 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002792 }
2793 dict = tv->vval.v_dict;
2794
2795 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2796 == NULL)
2797 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002798 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002799 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002800 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002801 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002802 // Clear the dict after getting the item, to avoid that it
2803 // make the item invalid.
2804 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002806 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002807 }
2808 break;
2809
2810 case ISN_NEGATENR:
2811 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002812 if (tv->v_type != VAR_NUMBER
2813#ifdef FEAT_FLOAT
2814 && tv->v_type != VAR_FLOAT
2815#endif
2816 )
2817 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002818 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002819 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002820 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002821 }
2822#ifdef FEAT_FLOAT
2823 if (tv->v_type == VAR_FLOAT)
2824 tv->vval.v_float = -tv->vval.v_float;
2825 else
2826#endif
2827 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002828 break;
2829
2830 case ISN_CHECKNR:
2831 {
2832 int error = FALSE;
2833
2834 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002835 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002836 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002837 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 (void)tv_get_number_chk(tv, &error);
2839 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002840 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 }
2842 break;
2843
2844 case ISN_CHECKTYPE:
2845 {
2846 checktype_T *ct = &iptr->isn_arg.type;
2847
2848 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002849 SOURCING_LNUM = iptr->isn_lnum;
2850 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2851 goto on_error;
2852
2853 // number 0 is FALSE, number 1 is TRUE
2854 if (tv->v_type == VAR_NUMBER
2855 && ct->ct_type->tt_type == VAR_BOOL
2856 && (tv->vval.v_number == 0
2857 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002859 tv->v_type = VAR_BOOL;
2860 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002861 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 }
2863 }
2864 break;
2865
Bram Moolenaar9af78762020-06-16 11:34:42 +02002866 case ISN_CHECKLEN:
2867 {
2868 int min_len = iptr->isn_arg.checklen.cl_min_len;
2869 list_T *list = NULL;
2870
2871 tv = STACK_TV_BOT(-1);
2872 if (tv->v_type == VAR_LIST)
2873 list = tv->vval.v_list;
2874 if (list == NULL || list->lv_len < min_len
2875 || (list->lv_len > min_len
2876 && !iptr->isn_arg.checklen.cl_more_OK))
2877 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002878 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002879 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002880 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002881 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002882 }
2883 }
2884 break;
2885
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002886 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002887 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 {
2889 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002890 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002891
2892 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002893 if (iptr->isn_type == ISN_2BOOL)
2894 {
2895 n = tv2bool(tv);
2896 if (iptr->isn_arg.number) // invert
2897 n = !n;
2898 }
2899 else
2900 {
2901 SOURCING_LNUM = iptr->isn_lnum;
2902 n = tv_get_bool_chk(tv, &error);
2903 if (error)
2904 goto on_error;
2905 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 clear_tv(tv);
2907 tv->v_type = VAR_BOOL;
2908 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2909 }
2910 break;
2911
2912 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002913 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 {
2915 char_u *str;
2916
2917 tv = STACK_TV_BOT(iptr->isn_arg.number);
2918 if (tv->v_type != VAR_STRING)
2919 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002920 if (iptr->isn_type == ISN_2STRING_ANY)
2921 {
2922 switch (tv->v_type)
2923 {
2924 case VAR_SPECIAL:
2925 case VAR_BOOL:
2926 case VAR_NUMBER:
2927 case VAR_FLOAT:
2928 case VAR_BLOB: break;
2929 default: to_string_error(tv->v_type);
2930 goto on_error;
2931 }
2932 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 str = typval_tostring(tv);
2934 clear_tv(tv);
2935 tv->v_type = VAR_STRING;
2936 tv->vval.v_string = str;
2937 }
2938 }
2939 break;
2940
Bram Moolenaar08597872020-12-10 19:43:40 +01002941 case ISN_RANGE:
2942 {
2943 exarg_T ea;
2944 char *errormsg;
2945
2946 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2947 goto failed;
2948 ++ectx.ec_stack.ga_len;
2949 tv = STACK_TV_BOT(-1);
2950 ea.addr_type = ADDR_LINES;
2951 ea.cmd = iptr->isn_arg.string;
2952 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2953 goto failed;
2954 if (ea.addr_count == 0)
2955 tv->vval.v_number = curwin->w_cursor.lnum;
2956 else
2957 tv->vval.v_number = ea.line2;
2958 }
2959 break;
2960
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002961 case ISN_PUT:
2962 {
2963 int regname = iptr->isn_arg.put.put_regname;
2964 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2965 char_u *expr = NULL;
2966 int dir = FORWARD;
2967
2968 if (regname == '=')
2969 {
2970 tv = STACK_TV_BOT(-1);
2971 if (tv->v_type == VAR_STRING)
2972 expr = tv->vval.v_string;
2973 else
2974 {
2975 expr = typval_tostring(tv); // allocates value
2976 clear_tv(tv);
2977 }
2978 --ectx.ec_stack.ga_len;
2979 }
Bram Moolenaar08597872020-12-10 19:43:40 +01002980 if (lnum < -2)
2981 {
2982 // line number was put on the stack by ISN_RANGE
2983 tv = STACK_TV_BOT(-1);
2984 curwin->w_cursor.lnum = tv->vval.v_number;
2985 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
2986 dir = BACKWARD;
2987 --ectx.ec_stack.ga_len;
2988 }
2989 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002990 // :put! above cursor
2991 dir = BACKWARD;
2992 else if (lnum >= 0)
2993 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
2994 check_cursor();
2995 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
2996 vim_free(expr);
2997 }
2998 break;
2999
Bram Moolenaar02194d22020-10-24 23:08:38 +02003000 case ISN_CMDMOD:
3001 save_cmdmod = cmdmod;
3002 restore_cmdmod = TRUE;
3003 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3004 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003005 break;
3006
Bram Moolenaar02194d22020-10-24 23:08:38 +02003007 case ISN_CMDMOD_REV:
3008 // filter regprog is owned by the instruction, don't free it
3009 cmdmod.cmod_filter_regmatch.regprog = NULL;
3010 undo_cmdmod(&cmdmod);
3011 cmdmod = save_cmdmod;
3012 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003013 break;
3014
Bram Moolenaar792f7862020-11-23 08:31:18 +01003015 case ISN_UNPACK:
3016 {
3017 int count = iptr->isn_arg.unpack.unp_count;
3018 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3019 list_T *l;
3020 listitem_T *li;
3021 int i;
3022
3023 // Check there is a valid list to unpack.
3024 tv = STACK_TV_BOT(-1);
3025 if (tv->v_type != VAR_LIST)
3026 {
3027 SOURCING_LNUM = iptr->isn_lnum;
3028 emsg(_(e_for_argument_must_be_sequence_of_lists));
3029 goto on_error;
3030 }
3031 l = tv->vval.v_list;
3032 if (l == NULL
3033 || l->lv_len < (semicolon ? count - 1 : count))
3034 {
3035 SOURCING_LNUM = iptr->isn_lnum;
3036 emsg(_(e_list_value_does_not_have_enough_items));
3037 goto on_error;
3038 }
3039 else if (!semicolon && l->lv_len > count)
3040 {
3041 SOURCING_LNUM = iptr->isn_lnum;
3042 emsg(_(e_list_value_has_more_items_than_targets));
3043 goto on_error;
3044 }
3045
3046 CHECK_LIST_MATERIALIZE(l);
3047 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3048 goto failed;
3049 ectx.ec_stack.ga_len += count - 1;
3050
3051 // Variable after semicolon gets a list with the remaining
3052 // items.
3053 if (semicolon)
3054 {
3055 list_T *rem_list =
3056 list_alloc_with_items(l->lv_len - count + 1);
3057
3058 if (rem_list == NULL)
3059 goto failed;
3060 tv = STACK_TV_BOT(-count);
3061 tv->vval.v_list = rem_list;
3062 ++rem_list->lv_refcount;
3063 tv->v_lock = 0;
3064 li = l->lv_first;
3065 for (i = 0; i < count - 1; ++i)
3066 li = li->li_next;
3067 for (i = 0; li != NULL; ++i)
3068 {
3069 list_set_item(rem_list, i, &li->li_tv);
3070 li = li->li_next;
3071 }
3072 --count;
3073 }
3074
3075 // Produce the values in reverse order, first item last.
3076 li = l->lv_first;
3077 for (i = 0; i < count; ++i)
3078 {
3079 tv = STACK_TV_BOT(-i - 1);
3080 copy_tv(&li->li_tv, tv);
3081 li = li->li_next;
3082 }
3083
3084 list_unref(l);
3085 }
3086 break;
3087
Bram Moolenaar389df252020-07-09 21:20:47 +02003088 case ISN_SHUFFLE:
3089 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003090 typval_T tmp_tv;
3091 int item = iptr->isn_arg.shuffle.shfl_item;
3092 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003093
3094 tmp_tv = *STACK_TV_BOT(-item);
3095 for ( ; up > 0 && item > 1; --up)
3096 {
3097 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3098 --item;
3099 }
3100 *STACK_TV_BOT(-item) = tmp_tv;
3101 }
3102 break;
3103
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 case ISN_DROP:
3105 --ectx.ec_stack.ga_len;
3106 clear_tv(STACK_TV_BOT(0));
3107 break;
3108 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003109 continue;
3110
Bram Moolenaard032f342020-07-18 18:13:02 +02003111func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003112 // Restore previous function. If the frame pointer is where we started
3113 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003114 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003115 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003116
Bram Moolenaard032f342020-07-18 18:13:02 +02003117 if (func_return(&ectx) == FAIL)
3118 // only fails when out of memory
3119 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003120 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003121
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003122on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003123 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003124 // If "emsg_silent" is set then ignore the error, unless it was set
3125 // when calling the function.
3126 if (did_emsg_cumul + did_emsg == did_emsg_before
3127 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003128 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003129on_fatal_error:
3130 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003131 // If we are not inside a try-catch started here, abort execution.
3132 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003133 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 }
3135
3136done:
3137 // function finished, get result from the stack.
3138 tv = STACK_TV_BOT(-1);
3139 *rettv = *tv;
3140 tv->v_type = VAR_UNKNOWN;
3141 ret = OK;
3142
3143failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003144 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003145 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003146 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003147
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003148 // Deal with any remaining closures, they may be in use somewhere.
3149 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003150 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003151 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003152 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3153 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003154
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003155 estack_pop();
3156 current_sctx = save_current_sctx;
3157
Bram Moolenaar352134b2020-10-17 22:04:08 +02003158 if (*msg_list != NULL && saved_msg_list != NULL)
3159 {
3160 msglist_T **plist = saved_msg_list;
3161
3162 // Append entries from the current msg_list (uncaught exceptions) to
3163 // the saved msg_list.
3164 while (*plist != NULL)
3165 plist = &(*plist)->next;
3166
3167 *plist = *msg_list;
3168 }
3169 msg_list = saved_msg_list;
3170
Bram Moolenaar02194d22020-10-24 23:08:38 +02003171 if (restore_cmdmod)
3172 {
3173 cmdmod.cmod_filter_regmatch.regprog = NULL;
3174 undo_cmdmod(&cmdmod);
3175 cmdmod = save_cmdmod;
3176 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003177 emsg_silent_def = save_emsg_silent_def;
3178 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003179
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003180failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003181 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003182 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3183 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003186 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003187
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003188 // Not sure if this is necessary.
3189 suppress_errthrow = save_suppress_errthrow;
3190
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003191 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003192 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003193 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003194 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 return ret;
3196}
3197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003199 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003200 * We don't really need this at runtime, but we do have tests that require it,
3201 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 */
3203 void
3204ex_disassemble(exarg_T *eap)
3205{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003206 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003207 char_u *fname;
3208 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003209 dfunc_T *dfunc;
3210 isn_T *instr;
3211 int current;
3212 int line_idx = 0;
3213 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003214 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003215
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003216 if (STRNCMP(arg, "<lambda>", 8) == 0)
3217 {
3218 arg += 8;
3219 (void)getdigits(&arg);
3220 fname = vim_strnsave(eap->arg, arg - eap->arg);
3221 }
3222 else
3223 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003224 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003225 if (fname == NULL)
3226 {
3227 semsg(_(e_invarg2), eap->arg);
3228 return;
3229 }
3230
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003231 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003232 if (ufunc == NULL)
3233 {
3234 char_u *p = untrans_function_name(fname);
3235
3236 if (p != NULL)
3237 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003238 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003239 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003240 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 if (ufunc == NULL)
3242 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003243 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003244 return;
3245 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003246 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003247 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3248 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003249 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003251 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252 return;
3253 }
3254 if (ufunc->uf_name_exp != NULL)
3255 msg((char *)ufunc->uf_name_exp);
3256 else
3257 msg((char *)ufunc->uf_name);
3258
3259 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3260 instr = dfunc->df_instr;
3261 for (current = 0; current < dfunc->df_instr_count; ++current)
3262 {
3263 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003264 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265
3266 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3267 {
3268 if (current > prev_current)
3269 {
3270 msg_puts("\n\n");
3271 prev_current = current;
3272 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003273 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3274 if (line != NULL)
3275 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003276 }
3277
3278 switch (iptr->isn_type)
3279 {
3280 case ISN_EXEC:
3281 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3282 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003283 case ISN_EXECCONCAT:
3284 smsg("%4d EXECCONCAT %lld", current,
3285 (long long)iptr->isn_arg.number);
3286 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003287 case ISN_ECHO:
3288 {
3289 echo_T *echo = &iptr->isn_arg.echo;
3290
3291 smsg("%4d %s %d", current,
3292 echo->echo_with_white ? "ECHO" : "ECHON",
3293 echo->echo_count);
3294 }
3295 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003296 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003297 smsg("%4d EXECUTE %lld", current,
3298 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003299 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003300 case ISN_ECHOMSG:
3301 smsg("%4d ECHOMSG %lld", current,
3302 (long long)(iptr->isn_arg.number));
3303 break;
3304 case ISN_ECHOERR:
3305 smsg("%4d ECHOERR %lld", current,
3306 (long long)(iptr->isn_arg.number));
3307 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003308 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003309 case ISN_LOADOUTER:
3310 {
3311 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3312
3313 if (iptr->isn_arg.number < 0)
3314 smsg("%4d LOAD%s arg[%lld]", current, add,
3315 (long long)(iptr->isn_arg.number
3316 + STACK_FRAME_SIZE));
3317 else
3318 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003319 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003320 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003321 break;
3322 case ISN_LOADV:
3323 smsg("%4d LOADV v:%s", current,
3324 get_vim_var_name(iptr->isn_arg.number));
3325 break;
3326 case ISN_LOADSCRIPT:
3327 {
3328 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003329 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3331 + iptr->isn_arg.script.script_idx;
3332
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003333 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3334 sv->sv_name,
3335 iptr->isn_arg.script.script_idx,
3336 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003337 }
3338 break;
3339 case ISN_LOADS:
3340 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003341 scriptitem_T *si = SCRIPT_ITEM(
3342 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003343
3344 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003345 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 }
3347 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003348 case ISN_LOADAUTO:
3349 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3350 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351 case ISN_LOADG:
3352 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3353 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003354 case ISN_LOADB:
3355 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3356 break;
3357 case ISN_LOADW:
3358 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3359 break;
3360 case ISN_LOADT:
3361 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3362 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003363 case ISN_LOADGDICT:
3364 smsg("%4d LOAD g:", current);
3365 break;
3366 case ISN_LOADBDICT:
3367 smsg("%4d LOAD b:", current);
3368 break;
3369 case ISN_LOADWDICT:
3370 smsg("%4d LOAD w:", current);
3371 break;
3372 case ISN_LOADTDICT:
3373 smsg("%4d LOAD t:", current);
3374 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 case ISN_LOADOPT:
3376 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3377 break;
3378 case ISN_LOADENV:
3379 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3380 break;
3381 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003382 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003383 break;
3384
3385 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003386 case ISN_STOREOUTER:
3387 {
3388 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3389
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003390 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003391 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003392 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003393 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003394 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003395 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003396 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003398 case ISN_STOREV:
3399 smsg("%4d STOREV v:%s", current,
3400 get_vim_var_name(iptr->isn_arg.number));
3401 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003402 case ISN_STOREAUTO:
3403 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003406 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3407 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003408 case ISN_STOREB:
3409 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3410 break;
3411 case ISN_STOREW:
3412 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3413 break;
3414 case ISN_STORET:
3415 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3416 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003417 case ISN_STORES:
3418 {
3419 scriptitem_T *si = SCRIPT_ITEM(
3420 iptr->isn_arg.loadstore.ls_sid);
3421
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003422 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003423 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003424 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 break;
3426 case ISN_STORESCRIPT:
3427 {
3428 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003429 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003430 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3431 + iptr->isn_arg.script.script_idx;
3432
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003433 smsg("%4d STORESCRIPT %s-%d in %s", current,
3434 sv->sv_name,
3435 iptr->isn_arg.script.script_idx,
3436 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 }
3438 break;
3439 case ISN_STOREOPT:
3440 smsg("%4d STOREOPT &%s", current,
3441 iptr->isn_arg.storeopt.so_name);
3442 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003443 case ISN_STOREENV:
3444 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3445 break;
3446 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003447 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003448 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003449 case ISN_STORENR:
3450 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003451 iptr->isn_arg.storenr.stnr_val,
3452 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003453 break;
3454
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003455 case ISN_STORELIST:
3456 smsg("%4d STORELIST", current);
3457 break;
3458
3459 case ISN_STOREDICT:
3460 smsg("%4d STOREDICT", current);
3461 break;
3462
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 // constants
3464 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003465 smsg("%4d PUSHNR %lld", current,
3466 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003467 break;
3468 case ISN_PUSHBOOL:
3469 case ISN_PUSHSPEC:
3470 smsg("%4d PUSH %s", current,
3471 get_var_special_name(iptr->isn_arg.number));
3472 break;
3473 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003474#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003475 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003476#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 break;
3478 case ISN_PUSHS:
3479 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3480 break;
3481 case ISN_PUSHBLOB:
3482 {
3483 char_u *r;
3484 char_u numbuf[NUMBUFLEN];
3485 char_u *tofree;
3486
3487 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003488 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 vim_free(tofree);
3490 }
3491 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003492 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003493 {
3494 char *name = (char *)iptr->isn_arg.string;
3495
3496 smsg("%4d PUSHFUNC \"%s\"", current,
3497 name == NULL ? "[none]" : name);
3498 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003499 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003500 case ISN_PUSHCHANNEL:
3501#ifdef FEAT_JOB_CHANNEL
3502 {
3503 channel_T *channel = iptr->isn_arg.channel;
3504
3505 smsg("%4d PUSHCHANNEL %d", current,
3506 channel == NULL ? 0 : channel->ch_id);
3507 }
3508#endif
3509 break;
3510 case ISN_PUSHJOB:
3511#ifdef FEAT_JOB_CHANNEL
3512 {
3513 typval_T tv;
3514 char_u *name;
3515
3516 tv.v_type = VAR_JOB;
3517 tv.vval.v_job = iptr->isn_arg.job;
3518 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003519 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003520 }
3521#endif
3522 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003523 case ISN_PUSHEXC:
3524 smsg("%4d PUSH v:exception", current);
3525 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003526 case ISN_UNLET:
3527 smsg("%4d UNLET%s %s", current,
3528 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3529 iptr->isn_arg.unlet.ul_name);
3530 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003531 case ISN_UNLETENV:
3532 smsg("%4d UNLETENV%s $%s", current,
3533 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3534 iptr->isn_arg.unlet.ul_name);
3535 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003536 case ISN_LOCKCONST:
3537 smsg("%4d LOCKCONST", current);
3538 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003540 smsg("%4d NEWLIST size %lld", current,
3541 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003542 break;
3543 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003544 smsg("%4d NEWDICT size %lld", current,
3545 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003546 break;
3547
3548 // function call
3549 case ISN_BCALL:
3550 {
3551 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3552
3553 smsg("%4d BCALL %s(argc %d)", current,
3554 internal_func_name(cbfunc->cbf_idx),
3555 cbfunc->cbf_argcount);
3556 }
3557 break;
3558 case ISN_DCALL:
3559 {
3560 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3561 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3562 + cdfunc->cdf_idx;
3563
3564 smsg("%4d DCALL %s(argc %d)", current,
3565 df->df_ufunc->uf_name_exp != NULL
3566 ? df->df_ufunc->uf_name_exp
3567 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3568 }
3569 break;
3570 case ISN_UCALL:
3571 {
3572 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3573
3574 smsg("%4d UCALL %s(argc %d)", current,
3575 cufunc->cuf_name, cufunc->cuf_argcount);
3576 }
3577 break;
3578 case ISN_PCALL:
3579 {
3580 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3581
3582 smsg("%4d PCALL%s (argc %d)", current,
3583 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3584 }
3585 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003586 case ISN_PCALL_END:
3587 smsg("%4d PCALL end", current);
3588 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003589 case ISN_RETURN:
3590 smsg("%4d RETURN", current);
3591 break;
3592 case ISN_FUNCREF:
3593 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003594 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003595 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003596 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003597
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003598 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 }
3600 break;
3601
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003602 case ISN_NEWFUNC:
3603 {
3604 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3605
3606 smsg("%4d NEWFUNC %s %s", current,
3607 newfunc->nf_lambda, newfunc->nf_global);
3608 }
3609 break;
3610
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003611 case ISN_DEF:
3612 {
3613 char_u *name = iptr->isn_arg.string;
3614
3615 smsg("%4d DEF %s", current,
3616 name == NULL ? (char_u *)"" : name);
3617 }
3618 break;
3619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 case ISN_JUMP:
3621 {
3622 char *when = "?";
3623
3624 switch (iptr->isn_arg.jump.jump_when)
3625 {
3626 case JUMP_ALWAYS:
3627 when = "JUMP";
3628 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003629 case JUMP_AND_KEEP_IF_TRUE:
3630 when = "JUMP_AND_KEEP_IF_TRUE";
3631 break;
3632 case JUMP_IF_FALSE:
3633 when = "JUMP_IF_FALSE";
3634 break;
3635 case JUMP_AND_KEEP_IF_FALSE:
3636 when = "JUMP_AND_KEEP_IF_FALSE";
3637 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003638 case JUMP_IF_COND_FALSE:
3639 when = "JUMP_IF_COND_FALSE";
3640 break;
3641 case JUMP_IF_COND_TRUE:
3642 when = "JUMP_IF_COND_TRUE";
3643 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003644 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003645 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003646 iptr->isn_arg.jump.jump_where);
3647 }
3648 break;
3649
3650 case ISN_FOR:
3651 {
3652 forloop_T *forloop = &iptr->isn_arg.forloop;
3653
3654 smsg("%4d FOR $%d -> %d", current,
3655 forloop->for_idx, forloop->for_end);
3656 }
3657 break;
3658
3659 case ISN_TRY:
3660 {
3661 try_T *try = &iptr->isn_arg.try;
3662
3663 smsg("%4d TRY catch -> %d, finally -> %d", current,
3664 try->try_catch, try->try_finally);
3665 }
3666 break;
3667 case ISN_CATCH:
3668 // TODO
3669 smsg("%4d CATCH", current);
3670 break;
3671 case ISN_ENDTRY:
3672 smsg("%4d ENDTRY", current);
3673 break;
3674 case ISN_THROW:
3675 smsg("%4d THROW", current);
3676 break;
3677
3678 // expression operations on number
3679 case ISN_OPNR:
3680 case ISN_OPFLOAT:
3681 case ISN_OPANY:
3682 {
3683 char *what;
3684 char *ins;
3685
3686 switch (iptr->isn_arg.op.op_type)
3687 {
3688 case EXPR_MULT: what = "*"; break;
3689 case EXPR_DIV: what = "/"; break;
3690 case EXPR_REM: what = "%"; break;
3691 case EXPR_SUB: what = "-"; break;
3692 case EXPR_ADD: what = "+"; break;
3693 default: what = "???"; break;
3694 }
3695 switch (iptr->isn_type)
3696 {
3697 case ISN_OPNR: ins = "OPNR"; break;
3698 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3699 case ISN_OPANY: ins = "OPANY"; break;
3700 default: ins = "???"; break;
3701 }
3702 smsg("%4d %s %s", current, ins, what);
3703 }
3704 break;
3705
3706 case ISN_COMPAREBOOL:
3707 case ISN_COMPARESPECIAL:
3708 case ISN_COMPARENR:
3709 case ISN_COMPAREFLOAT:
3710 case ISN_COMPARESTRING:
3711 case ISN_COMPAREBLOB:
3712 case ISN_COMPARELIST:
3713 case ISN_COMPAREDICT:
3714 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003715 case ISN_COMPAREANY:
3716 {
3717 char *p;
3718 char buf[10];
3719 char *type;
3720
3721 switch (iptr->isn_arg.op.op_type)
3722 {
3723 case EXPR_EQUAL: p = "=="; break;
3724 case EXPR_NEQUAL: p = "!="; break;
3725 case EXPR_GREATER: p = ">"; break;
3726 case EXPR_GEQUAL: p = ">="; break;
3727 case EXPR_SMALLER: p = "<"; break;
3728 case EXPR_SEQUAL: p = "<="; break;
3729 case EXPR_MATCH: p = "=~"; break;
3730 case EXPR_IS: p = "is"; break;
3731 case EXPR_ISNOT: p = "isnot"; break;
3732 case EXPR_NOMATCH: p = "!~"; break;
3733 default: p = "???"; break;
3734 }
3735 STRCPY(buf, p);
3736 if (iptr->isn_arg.op.op_ic == TRUE)
3737 strcat(buf, "?");
3738 switch(iptr->isn_type)
3739 {
3740 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3741 case ISN_COMPARESPECIAL:
3742 type = "COMPARESPECIAL"; break;
3743 case ISN_COMPARENR: type = "COMPARENR"; break;
3744 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3745 case ISN_COMPARESTRING:
3746 type = "COMPARESTRING"; break;
3747 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3748 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3749 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3750 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003751 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3752 default: type = "???"; break;
3753 }
3754
3755 smsg("%4d %s %s", current, type, buf);
3756 }
3757 break;
3758
3759 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3760 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3761
3762 // expression operations
3763 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003764 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003765 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003766 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003767 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003768 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003769 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003770 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3771 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003772 case ISN_SLICE: smsg("%4d SLICE %lld",
3773 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003774 case ISN_GETITEM: smsg("%4d ITEM %lld",
3775 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003776 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3777 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 iptr->isn_arg.string); break;
3779 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3780
3781 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003782 case ISN_CHECKTYPE:
3783 {
3784 char *tofree;
3785
3786 smsg("%4d CHECKTYPE %s stack[%d]", current,
3787 type_name(iptr->isn_arg.type.ct_type, &tofree),
3788 iptr->isn_arg.type.ct_off);
3789 vim_free(tofree);
3790 break;
3791 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003792 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3793 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3794 iptr->isn_arg.checklen.cl_min_len);
3795 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003796 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003797 case ISN_2BOOL: if (iptr->isn_arg.number)
3798 smsg("%4d INVERT (!val)", current);
3799 else
3800 smsg("%4d 2BOOL (!!val)", current);
3801 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003802 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3803 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003804 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003805 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3806 (long long)(iptr->isn_arg.number));
3807 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003808 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3809 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003810 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003811 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3812 smsg("%4d PUT %c above range",
3813 current, iptr->isn_arg.put.put_regname);
3814 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3815 smsg("%4d PUT %c range",
3816 current, iptr->isn_arg.put.put_regname);
3817 else
3818 smsg("%4d PUT %c %ld", current,
3819 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003820 (long)iptr->isn_arg.put.put_lnum);
3821 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003822
Bram Moolenaar02194d22020-10-24 23:08:38 +02003823 // TODO: summarize modifiers
3824 case ISN_CMDMOD:
3825 {
3826 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003827 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003828 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3829
3830 buf = alloc(len + 1);
3831 if (buf != NULL)
3832 {
3833 (void)produce_cmdmods(
3834 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3835 smsg("%4d CMDMOD %s", current, buf);
3836 vim_free(buf);
3837 }
3838 break;
3839 }
3840 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003841
Bram Moolenaar792f7862020-11-23 08:31:18 +01003842 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3843 iptr->isn_arg.unpack.unp_count,
3844 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3845 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003846 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3847 iptr->isn_arg.shuffle.shfl_item,
3848 iptr->isn_arg.shuffle.shfl_up);
3849 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003850 case ISN_DROP: smsg("%4d DROP", current); break;
3851 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003852
3853 out_flush(); // output one line at a time
3854 ui_breakcheck();
3855 if (got_int)
3856 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003858}
3859
3860/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003861 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 * list, etc. Mostly like what JavaScript does, except that empty list and
3863 * empty dictionary are FALSE.
3864 */
3865 int
3866tv2bool(typval_T *tv)
3867{
3868 switch (tv->v_type)
3869 {
3870 case VAR_NUMBER:
3871 return tv->vval.v_number != 0;
3872 case VAR_FLOAT:
3873#ifdef FEAT_FLOAT
3874 return tv->vval.v_float != 0.0;
3875#else
3876 break;
3877#endif
3878 case VAR_PARTIAL:
3879 return tv->vval.v_partial != NULL;
3880 case VAR_FUNC:
3881 case VAR_STRING:
3882 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3883 case VAR_LIST:
3884 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3885 case VAR_DICT:
3886 return tv->vval.v_dict != NULL
3887 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3888 case VAR_BOOL:
3889 case VAR_SPECIAL:
3890 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3891 case VAR_JOB:
3892#ifdef FEAT_JOB_CHANNEL
3893 return tv->vval.v_job != NULL;
3894#else
3895 break;
3896#endif
3897 case VAR_CHANNEL:
3898#ifdef FEAT_JOB_CHANNEL
3899 return tv->vval.v_channel != NULL;
3900#else
3901 break;
3902#endif
3903 case VAR_BLOB:
3904 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3905 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003906 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003907 case VAR_VOID:
3908 break;
3909 }
3910 return FALSE;
3911}
3912
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003913 void
3914emsg_using_string_as(typval_T *tv, int as_number)
3915{
3916 semsg(_(as_number ? e_using_string_as_number_str
3917 : e_using_string_as_bool_str),
3918 tv->vval.v_string == NULL
3919 ? (char_u *)"" : tv->vval.v_string);
3920}
3921
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922/*
3923 * If "tv" is a string give an error and return FAIL.
3924 */
3925 int
3926check_not_string(typval_T *tv)
3927{
3928 if (tv->v_type == VAR_STRING)
3929 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003930 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 clear_tv(tv);
3932 return FAIL;
3933 }
3934 return OK;
3935}
3936
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937
3938#endif // FEAT_EVAL