blob: 7d56e2143ec1b69b2ae6f771aba002351ef13d56 [file] [log] [blame]
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * vim9execute.c: execute Vim9 script instructions
12 */
13
14#define USING_FLOAT_STUFF
15#include "vim.h"
16
17#if defined(FEAT_EVAL) || defined(PROTO)
18
19#ifdef VMS
20# include <float.h>
21#endif
22
23#include "vim9.h"
24
25// Structure put on ec_trystack when ISN_TRY is encountered.
26typedef struct {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020027 int tcd_frame_idx; // ec_frame_idx when ISN_TRY was encountered
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010028 int tcd_catch_idx; // instruction of the first catch
29 int tcd_finally_idx; // instruction of the finally block
30 int tcd_caught; // catch block entered
31 int tcd_return; // when TRUE return from end of :finally
32} trycmd_T;
33
34
35// A stack is used to store:
36// - arguments passed to a :def function
37// - info about the calling function, to use when returning
38// - local variables
39// - temporary values
40//
41// In detail (FP == Frame Pointer):
42// arg1 first argument from caller (if present)
43// arg2 second argument from caller (if present)
44// extra_arg1 any missing optional argument default value
45// FP -> cur_func calling function
46// current previous instruction pointer
47// frame_ptr previous Frame Pointer
48// var1 space for local variable
49// var2 space for local variable
50// .... fixed space for max. number of local variables
51// temp temporary values
52// .... flexible space for temporary values (can grow big)
53
54/*
55 * Execution context.
56 */
57typedef struct {
58 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020059 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010060
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020061 garray_T *ec_outer_stack; // stack used for closures
62 int ec_outer_frame; // stack frame in ec_outer_stack
63
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072} ectx_T;
73
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
152 *
153 * Stack has:
154 * - current arguments (already there)
155 * - omitted optional argument (default values) added here
156 * - stack frame:
157 * - pointer to calling function
158 * - Index of next instruction in calling function
159 * - previous frame pointer
160 * - reserved space for local variables
161 */
162 static int
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200163call_dfunc(int cdf_idx, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100164{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200165 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
167 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int arg_to_add;
169 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200170 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100171 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200172 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173
174 if (dfunc->df_deleted)
175 {
176 emsg_funcname(e_func_deleted, ufunc->uf_name);
177 return FAIL;
178 }
179
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200180 if (ufunc->uf_va_name != NULL)
181 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200182 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200183 // Stack at time of call with 2 varargs:
184 // normal_arg
185 // optional_arg
186 // vararg_1
187 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200188 // After creating the list:
189 // normal_arg
190 // optional_arg
191 // vararg-list
192 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200193 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200194 // After creating the list
195 // normal_arg
196 // (space for optional_arg)
197 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 vararg_count = argcount - ufunc->uf_args.ga_len;
199 if (vararg_count < 0)
200 vararg_count = 0;
201 else
202 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200204 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205
206 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 }
208
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200210 if (arg_to_add < 0)
211 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200212 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200213 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 return FAIL;
217 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200218
219 // Reserve space for:
220 // - missing arguments
221 // - stack frame
222 // - local variables
223 // - if needed: a counter for number of closures created in
224 // ectx->ec_funcrefs.
225 varcount = dfunc->df_varcount + dfunc->df_has_closure;
226 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
227 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100228 return FAIL;
229
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100230 // If depth of calling is getting too high, don't execute the function.
231 if (funcdepth_increment() == FAIL)
232 return FAIL;
233
Bram Moolenaarfe270812020-04-11 22:31:27 +0200234 // Move the vararg-list to below the missing optional arguments.
235 if (vararg_count > 0 && arg_to_add > 0)
236 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100237
238 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200239 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200240 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242
243 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
245 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200246 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
247 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
248 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200249 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100250
251 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200252 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 if (dfunc->df_has_closure)
255 {
256 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
257
258 tv->v_type = VAR_NUMBER;
259 tv->vval.v_number = 0;
260 }
261 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100262
263 // Set execution state to the start of the called function.
264 ectx->ec_dfunc_idx = cdf_idx;
265 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200266 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
267 if (entry != NULL)
268 {
269 // Set the script context to the script where the function was defined.
270 // TODO: save more than the SID?
271 entry->es_save_sid = current_sctx.sc_sid;
272 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
273 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100274
275 // Decide where to start execution, handles optional arguments.
276 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100277
278 return OK;
279}
280
281// Get pointer to item in the stack.
282#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
283
284/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200285 * Used when returning from a function: Check if any closure is still
286 * referenced. If so then move the arguments and variables to a separate piece
287 * of stack to be used when the closure is called.
288 * When "free_arguments" is TRUE the arguments are to be freed.
289 * Returns FAIL when out of memory.
290 */
291 static int
292handle_closure_in_use(ectx_T *ectx, int free_arguments)
293{
294 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
295 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200296 int argcount;
297 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200298 int idx;
299 typval_T *tv;
300 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200301 garray_T *gap = &ectx->ec_funcrefs;
302 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200303
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200305 return OK; // function was freed
306 if (dfunc->df_has_closure == 0)
307 return OK; // no closures
308 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
309 closure_count = tv->vval.v_number;
310 if (closure_count == 0)
311 return OK; // no funcrefs created
312
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200313 argcount = ufunc_argcount(dfunc->df_ufunc);
314 top = ectx->ec_frame_idx - argcount;
315
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200316 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200317 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200318 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200319 partial_T *pt;
320 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200321
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200322 if (off < 0)
323 continue; // count is off or already done
324 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200326 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200327 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200328 int i;
329
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200330 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200331 // unreferenced on return.
332 for (i = 0; i < dfunc->df_varcount; ++i)
333 {
334 typval_T *stv = STACK_TV(ectx->ec_frame_idx
335 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200336 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200337 --refcount;
338 }
339 if (refcount > 1)
340 {
341 closure_in_use = TRUE;
342 break;
343 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200344 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345 }
346
347 if (closure_in_use)
348 {
349 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
350 typval_T *stack;
351
352 // A closure is using the arguments and/or local variables.
353 // Move them to the called function.
354 if (funcstack == NULL)
355 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200356 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
357 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200358 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
359 funcstack->fs_ga.ga_data = stack;
360 if (stack == NULL)
361 {
362 vim_free(funcstack);
363 return FAIL;
364 }
365
366 // Move or copy the arguments.
367 for (idx = 0; idx < argcount; ++idx)
368 {
369 tv = STACK_TV(top + idx);
370 if (free_arguments)
371 {
372 *(stack + idx) = *tv;
373 tv->v_type = VAR_UNKNOWN;
374 }
375 else
376 copy_tv(tv, stack + idx);
377 }
378 // Move the local variables.
379 for (idx = 0; idx < dfunc->df_varcount; ++idx)
380 {
381 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200382
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200383 // A partial created for a local function, that is also used as a
384 // local variable, has a reference count for the variable, thus
385 // will never go down to zero. When all these refcounts are one
386 // then the funcstack is unused. We need to count how many we have
387 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200388 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
389 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200390 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200391
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200393 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
394 gap->ga_len - closure_count + i])
395 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 }
397
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 tv->v_type = VAR_UNKNOWN;
400 }
401
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200402 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200403 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200404 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
405 - closure_count + idx];
406 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200408 ++funcstack->fs_refcount;
409 pt->pt_funcstack = funcstack;
410 pt->pt_ectx_stack = &funcstack->fs_ga;
411 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200412 }
413 }
414 }
415
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 for (idx = 0; idx < closure_count; ++idx)
417 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
418 - closure_count + idx]);
419 gap->ga_len -= closure_count;
420 if (gap->ga_len == 0)
421 ga_clear(gap);
422
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 return OK;
424}
425
426/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200427 * Called when a partial is freed or its reference count goes down to one. The
428 * funcstack may be the only reference to the partials in the local variables.
429 * Go over all of them, the funcref and can be freed if all partials
430 * referencing the funcstack have a reference count of one.
431 */
432 void
433funcstack_check_refcount(funcstack_T *funcstack)
434{
435 int i;
436 garray_T *gap = &funcstack->fs_ga;
437 int done = 0;
438
439 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
440 return;
441 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
442 {
443 typval_T *tv = ((typval_T *)gap->ga_data) + i;
444
445 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
446 && tv->vval.v_partial->pt_funcstack == funcstack
447 && tv->vval.v_partial->pt_refcount == 1)
448 ++done;
449 }
450 if (done == funcstack->fs_min_refcount)
451 {
452 typval_T *stack = gap->ga_data;
453
454 // All partials referencing the funcstack have a reference count of
455 // one, thus the funcstack is no longer of use.
456 for (i = 0; i < gap->ga_len; ++i)
457 clear_tv(stack + i);
458 vim_free(stack);
459 vim_free(funcstack);
460 }
461}
462
463/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100464 * Return from the current function.
465 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100467func_return(ectx_T *ectx)
468{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100469 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100470 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200471 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
472 + ectx->ec_dfunc_idx;
473 int argcount = ufunc_argcount(dfunc->df_ufunc);
474 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200475 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100476
477 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200478 entry = estack_pop();
479 if (entry != NULL)
480 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100481
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200482 if (handle_closure_in_use(ectx, TRUE) == FAIL)
483 return FAIL;
484
485 // Clear the arguments.
486 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
487 clear_tv(STACK_TV(idx));
488
489 // Clear local variables and temp values, but not the return value.
490 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100491 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100492 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100493
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100494 // The return value should be on top of the stack. However, when aborting
495 // it may not be there and ec_frame_idx is the top of the stack.
496 ret_idx = ectx->ec_stack.ga_len - 1;
497 if (ret_idx == ectx->ec_frame_idx + 4)
498 ret_idx = 0;
499
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100500 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200501 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
502 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200503 ectx->ec_outer_stack =
504 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
505 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
506 // restoring ec_frame_idx must be last
507 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100508 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
509 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100510
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100511 if (ret_idx > 0)
512 {
513 // Reset the stack to the position before the call, with a spot for the
514 // return value, moved there from above the frame.
515 ectx->ec_stack.ga_len = top + 1;
516 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
517 }
518 else
519 // Reset the stack to the position before the call.
520 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200521
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100522 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200523 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524}
525
526#undef STACK_TV
527
528/*
529 * Prepare arguments and rettv for calling a builtin or user function.
530 */
531 static int
532call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
533{
534 int idx;
535 typval_T *tv;
536
537 // Move arguments from bottom of the stack to argvars[] and add terminator.
538 for (idx = 0; idx < argcount; ++idx)
539 argvars[idx] = *STACK_TV_BOT(idx - argcount);
540 argvars[argcount].v_type = VAR_UNKNOWN;
541
542 // Result replaces the arguments on the stack.
543 if (argcount > 0)
544 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200545 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546 return FAIL;
547 else
548 ++ectx->ec_stack.ga_len;
549
550 // Default return value is zero.
551 tv = STACK_TV_BOT(-1);
552 tv->v_type = VAR_NUMBER;
553 tv->vval.v_number = 0;
554
555 return OK;
556}
557
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200558// Ugly global to avoid passing the execution context around through many
559// layers.
560static ectx_T *current_ectx = NULL;
561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100562/*
563 * Call a builtin function by index.
564 */
565 static int
566call_bfunc(int func_idx, int argcount, ectx_T *ectx)
567{
568 typval_T argvars[MAX_FUNC_ARGS];
569 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100570 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200571 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100572
573 if (call_prepare(argcount, argvars, ectx) == FAIL)
574 return FAIL;
575
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200576 // Call the builtin function. Set "current_ectx" so that when it
577 // recursively invokes call_def_function() a closure context can be set.
578 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200580 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581
582 // Clear the arguments.
583 for (idx = 0; idx < argcount; ++idx)
584 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200585
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100586 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200587 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100588 return OK;
589}
590
591/*
592 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100593 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100594 */
595 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100596call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597{
598 typval_T argvars[MAX_FUNC_ARGS];
599 funcexe_T funcexe;
600 int error;
601 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100602 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100603
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200604 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200605 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
606 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200607 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100608 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100609 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100610 if (error != FCERR_UNKNOWN)
611 {
612 if (error == FCERR_TOOMANY)
613 semsg(_(e_toomanyarg), ufunc->uf_name);
614 else
615 semsg(_(e_toofewarg), ufunc->uf_name);
616 return FAIL;
617 }
618
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100619 // The function has been compiled, can call it quickly. For a function
620 // that was defined later: we can call it directly next time.
621 if (iptr != NULL)
622 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100623 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100624 iptr->isn_type = ISN_DCALL;
625 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
626 iptr->isn_arg.dfunc.cdf_argcount = argcount;
627 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100629 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630
631 if (call_prepare(argcount, argvars, ectx) == FAIL)
632 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200633 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 funcexe.evaluate = TRUE;
635
636 // Call the user function. Result goes in last position on the stack.
637 // TODO: add selfdict if there is one
638 error = call_user_func_check(ufunc, argcount, argvars,
639 STACK_TV_BOT(-1), &funcexe, NULL);
640
641 // Clear the arguments.
642 for (idx = 0; idx < argcount; ++idx)
643 clear_tv(&argvars[idx]);
644
645 if (error != FCERR_NONE)
646 {
647 user_func_error(error, ufunc->uf_name);
648 return FAIL;
649 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100650 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200651 // Error other than from calling the function itself.
652 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653 return OK;
654}
655
656/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200657 * Return TRUE if an error was given or CTRL-C was pressed.
658 */
659 static int
660vim9_aborting(int prev_called_emsg)
661{
662 return called_emsg > prev_called_emsg || got_int || did_throw;
663}
664
665/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100666 * Execute a function by "name".
667 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100668 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100669 * Returns FAIL if not found without an error message.
670 */
671 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100672call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100673{
674 ufunc_T *ufunc;
675
676 if (builtin_function(name, -1))
677 {
678 int func_idx = find_internal_func(name);
679
680 if (func_idx < 0)
681 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200682 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100683 return FAIL;
684 return call_bfunc(func_idx, argcount, ectx);
685 }
686
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200687 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200688
689 if (ufunc == NULL)
690 {
691 int called_emsg_before = called_emsg;
692
693 if (script_autoload(name, TRUE))
694 // loaded a package, search for the function again
695 ufunc = find_func(name, FALSE, NULL);
696 if (vim9_aborting(called_emsg_before))
697 return FAIL; // bail out if loading the script caused an error
698 }
699
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100700 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100701 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100702
703 return FAIL;
704}
705
706 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200707call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100708{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200709 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200710 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200712 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100713
714 if (tv->v_type == VAR_PARTIAL)
715 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200716 partial_T *pt = tv->vval.v_partial;
717 int i;
718
719 if (pt->pt_argc > 0)
720 {
721 // Make space for arguments from the partial, shift the "argcount"
722 // arguments up.
723 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
724 return FAIL;
725 for (i = 1; i <= argcount; ++i)
726 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
727 ectx->ec_stack.ga_len += pt->pt_argc;
728 argcount += pt->pt_argc;
729
730 // copy the arguments from the partial onto the stack
731 for (i = 0; i < pt->pt_argc; ++i)
732 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
735 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200736 {
737 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
738
739 // closure may need the function context where it was defined
740 ectx->ec_outer_stack = pt->pt_ectx_stack;
741 ectx->ec_outer_frame = pt->pt_ectx_frame;
742
743 return ret;
744 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 name = pt->pt_name;
746 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200747 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100748 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200749 if (name != NULL)
750 {
751 char_u fname_buf[FLEN_FIXED + 1];
752 char_u *tofree = NULL;
753 int error = FCERR_NONE;
754 char_u *fname;
755
756 // May need to translate <SNR>123_ to K_SNR.
757 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
758 if (error != FCERR_NONE)
759 res = FAIL;
760 else
761 res = call_by_name(fname, argcount, ectx, NULL);
762 vim_free(tofree);
763 }
764
765 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100766 {
767 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200768 semsg(_(e_unknownfunc),
769 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 return FAIL;
771 }
772 return OK;
773}
774
775/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200776 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
777 * TRUE.
778 */
779 static int
780error_if_locked(int lock, char *error)
781{
782 if (lock & (VAR_LOCKED | VAR_FIXED))
783 {
784 emsg(_(error));
785 return TRUE;
786 }
787 return FALSE;
788}
789
790/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100791 * Store "tv" in variable "name".
792 * This is for s: and g: variables.
793 */
794 static void
795store_var(char_u *name, typval_T *tv)
796{
797 funccal_entry_T entry;
798
799 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200800 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100801 restore_funccal();
802}
803
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100804/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100805 * Convert "tv" to a string.
806 * Return FAIL if not allowed.
807 */
808 static int
809do_2string(typval_T *tv, int is_2string_any)
810{
811 if (tv->v_type != VAR_STRING)
812 {
813 char_u *str;
814
815 if (is_2string_any)
816 {
817 switch (tv->v_type)
818 {
819 case VAR_SPECIAL:
820 case VAR_BOOL:
821 case VAR_NUMBER:
822 case VAR_FLOAT:
823 case VAR_BLOB: break;
824 default: to_string_error(tv->v_type);
825 return FAIL;
826 }
827 }
828 str = typval_tostring(tv);
829 clear_tv(tv);
830 tv->v_type = VAR_STRING;
831 tv->vval.v_string = str;
832 }
833 return OK;
834}
835
836/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100837 * When the value of "sv" is a null list of dict, allocate it.
838 */
839 static void
840allocate_if_null(typval_T *tv)
841{
842 switch (tv->v_type)
843 {
844 case VAR_LIST:
845 if (tv->vval.v_list == NULL)
846 rettv_list_alloc(tv);
847 break;
848 case VAR_DICT:
849 if (tv->vval.v_dict == NULL)
850 rettv_dict_alloc(tv);
851 break;
852 default:
853 break;
854 }
855}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200856
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100857/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 * Execute a function by "name".
859 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100860 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861 */
862 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100863call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100864{
Bram Moolenaared677f52020-08-12 16:38:10 +0200865 int called_emsg_before = called_emsg;
866 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867
Bram Moolenaared677f52020-08-12 16:38:10 +0200868 res = call_by_name(name, argcount, ectx, iptr);
869 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200871 dictitem_T *v;
872
873 v = find_var(name, NULL, FALSE);
874 if (v == NULL)
875 {
876 semsg(_(e_unknownfunc), name);
877 return FAIL;
878 }
879 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
880 {
881 semsg(_(e_unknownfunc), name);
882 return FAIL;
883 }
884 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200886 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100887}
888
889/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100890 * When a function reference is used, fill a partial with the information
891 * needed, especially when it is used as a closure.
892 */
893 static int
894fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
895{
896 pt->pt_func = ufunc;
897 pt->pt_refcount = 1;
898
899 if (pt->pt_func->uf_flags & FC_CLOSURE)
900 {
901 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
902 + ectx->ec_dfunc_idx;
903
904 // The closure needs to find arguments and local
905 // variables in the current stack.
906 pt->pt_ectx_stack = &ectx->ec_stack;
907 pt->pt_ectx_frame = ectx->ec_frame_idx;
908
909 // If this function returns and the closure is still
910 // being used, we need to make a copy of the context
911 // (arguments and local variables). Store a reference
912 // to the partial so we can handle that.
913 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
914 {
915 vim_free(pt);
916 return FAIL;
917 }
918 // Extra variable keeps the count of closures created
919 // in the current function call.
920 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
921 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
922
923 ((partial_T **)ectx->ec_funcrefs.ga_data)
924 [ectx->ec_funcrefs.ga_len] = pt;
925 ++pt->pt_refcount;
926 ++ectx->ec_funcrefs.ga_len;
927 }
928 ++pt->pt_func->uf_refcount;
929 return OK;
930}
931
932/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100933 * Call a "def" function from old Vim script.
934 * Return OK or FAIL.
935 */
936 int
937call_def_function(
938 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200939 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100940 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200941 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 typval_T *rettv) // return value
943{
944 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200945 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200946 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100947 typval_T *tv;
948 int idx;
949 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100950 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200951 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200952 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100953 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200954 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200955 msglist_T **saved_msg_list = NULL;
956 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200957 cmdmod_T save_cmdmod;
958 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100959 int save_emsg_silent_def = emsg_silent_def;
960 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100961 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100962 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100963
964// Get pointer to item in the stack.
965#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
966
967// Get pointer to item at the bottom of the stack, -1 is the bottom.
968#undef STACK_TV_BOT
969#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
970
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200971// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200972#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 +0100973
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200974// Like STACK_TV_VAR but use the outer scope
975#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
976
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200977 if (ufunc->uf_def_status == UF_NOT_COMPILED
978 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200979 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200980 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100981 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200982 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200983 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200984 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200985 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200986
Bram Moolenaar09689a02020-05-09 22:50:08 +0200987 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200988 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200989 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
990 + ufunc->uf_dfunc_idx;
991 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200992 {
993 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200994 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200995 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200996 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100997
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100998 // If depth of calling is getting too high, don't execute the function.
999 orig_funcdepth = funcdepth_get();
1000 if (funcdepth_increment() == FAIL)
1001 return FAIL;
1002
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001003 CLEAR_FIELD(ectx);
1004 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1005 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1006 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001007 {
1008 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001009 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001011 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001012 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001013
1014 // Put arguments on the stack.
1015 for (idx = 0; idx < argc; ++idx)
1016 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001017 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001018 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1019 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001020 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001021 copy_tv(&argv[idx], STACK_TV_BOT(0));
1022 ++ectx.ec_stack.ga_len;
1023 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001024
1025 // Turn varargs into a list. Empty list if no args.
1026 if (ufunc->uf_va_name != NULL)
1027 {
1028 int vararg_count = argc - ufunc->uf_args.ga_len;
1029
1030 if (vararg_count < 0)
1031 vararg_count = 0;
1032 else
1033 argc -= vararg_count;
1034 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001035 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001036
1037 // Check the type of the list items.
1038 tv = STACK_TV_BOT(-1);
1039 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001040 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001041 && ufunc->uf_va_type->tt_member != &t_any
1042 && tv->vval.v_list != NULL)
1043 {
1044 type_T *expected = ufunc->uf_va_type->tt_member;
1045 listitem_T *li = tv->vval.v_list->lv_first;
1046
1047 for (idx = 0; idx < vararg_count; ++idx)
1048 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001049 if (check_typval_type(expected, &li->li_tv,
1050 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001051 goto failed_early;
1052 li = li->li_next;
1053 }
1054 }
1055
Bram Moolenaar23e03252020-04-12 22:22:31 +02001056 if (defcount > 0)
1057 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001058 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001059 --ectx.ec_stack.ga_len;
1060 }
1061
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001062 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001063 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001064 if (defcount > 0)
1065 for (idx = 0; idx < defcount; ++idx)
1066 {
1067 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1068 ++ectx.ec_stack.ga_len;
1069 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001070 if (ufunc->uf_va_name != NULL)
1071 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001072
1073 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001074 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1075 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001077 if (partial != NULL)
1078 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001079 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1080 {
1081 // TODO: is this always the right way?
1082 ectx.ec_outer_stack = &current_ectx->ec_stack;
1083 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1084 }
1085 else
1086 {
1087 ectx.ec_outer_stack = partial->pt_ectx_stack;
1088 ectx.ec_outer_frame = partial->pt_ectx_frame;
1089 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001090 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001091 else if (ufunc->uf_partial != NULL)
1092 {
1093 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1094 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1095 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001096
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001097 // dummy frame entries
1098 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1099 {
1100 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1101 ++ectx.ec_stack.ga_len;
1102 }
1103
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001104 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001105 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001106 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1107 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001109 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001110 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001111 ectx.ec_stack.ga_len += dfunc->df_varcount;
1112 if (dfunc->df_has_closure)
1113 {
1114 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1115 STACK_TV_VAR(idx)->vval.v_number = 0;
1116 ++ectx.ec_stack.ga_len;
1117 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001118
1119 ectx.ec_instr = dfunc->df_instr;
1120 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001121
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001122 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001123 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001124 estack_push_ufunc(ufunc, 1);
1125 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001126 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1127
Bram Moolenaar352134b2020-10-17 22:04:08 +02001128 // Use a specific location for storing error messages to be converted to an
1129 // exception.
1130 saved_msg_list = msg_list;
1131 msg_list = &private_msg_list;
1132
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001133 // Do turn errors into exceptions.
1134 suppress_errthrow = FALSE;
1135
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001136 // When ":silent!" was used before calling then we still abort the
1137 // function. If ":silent!" is used in the function then we don't.
1138 emsg_silent_def = emsg_silent;
1139 did_emsg_def = 0;
1140
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001141 // Decide where to start execution, handles optional arguments.
1142 init_instr_idx(ufunc, argc, &ectx);
1143
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144 for (;;)
1145 {
1146 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001147
Bram Moolenaar270d0382020-05-15 21:42:53 +02001148 if (++breakcheck_count >= 100)
1149 {
1150 line_breakcheck();
1151 breakcheck_count = 0;
1152 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001153 if (got_int)
1154 {
1155 // Turn CTRL-C into an exception.
1156 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001157 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001158 goto failed;
1159 did_throw = TRUE;
1160 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001161
Bram Moolenaara26b9702020-04-18 19:53:28 +02001162 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1163 {
1164 // Turn an error message into an exception.
1165 did_emsg = FALSE;
1166 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1167 goto failed;
1168 did_throw = TRUE;
1169 *msg_list = NULL;
1170 }
1171
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001172 if (did_throw && !ectx.ec_in_catch)
1173 {
1174 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001175 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176
1177 // An exception jumps to the first catch, finally, or returns from
1178 // the current function.
1179 if (trystack->ga_len > 0)
1180 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001181 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182 {
1183 // jump to ":catch" or ":finally"
1184 ectx.ec_in_catch = TRUE;
1185 ectx.ec_iidx = trycmd->tcd_catch_idx;
1186 }
1187 else
1188 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001189 // Not inside try or need to return from current functions.
1190 // Push a dummy return value.
1191 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1192 goto failed;
1193 tv = STACK_TV_BOT(0);
1194 tv->v_type = VAR_NUMBER;
1195 tv->vval.v_number = 0;
1196 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001197 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001199 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001200 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001201 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1202 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 goto done;
1204 }
1205
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001206 if (func_return(&ectx) == FAIL)
1207 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 }
1209 continue;
1210 }
1211
1212 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1213 switch (iptr->isn_type)
1214 {
1215 // execute Ex command line
1216 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001217 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001218 SOURCING_LNUM = iptr->isn_lnum;
1219 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001220 if (did_emsg)
1221 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001222 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223 break;
1224
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001225 // execute Ex command from pieces on the stack
1226 case ISN_EXECCONCAT:
1227 {
1228 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001229 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001230 int pass;
1231 int i;
1232 char_u *cmd = NULL;
1233 char_u *str;
1234
1235 for (pass = 1; pass <= 2; ++pass)
1236 {
1237 for (i = 0; i < count; ++i)
1238 {
1239 tv = STACK_TV_BOT(i - count);
1240 str = tv->vval.v_string;
1241 if (str != NULL && *str != NUL)
1242 {
1243 if (pass == 2)
1244 STRCPY(cmd + len, str);
1245 len += STRLEN(str);
1246 }
1247 if (pass == 2)
1248 clear_tv(tv);
1249 }
1250 if (pass == 1)
1251 {
1252 cmd = alloc(len + 1);
1253 if (cmd == NULL)
1254 goto failed;
1255 len = 0;
1256 }
1257 }
1258
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001259 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001260 do_cmdline_cmd(cmd);
1261 vim_free(cmd);
1262 }
1263 break;
1264
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265 // execute :echo {string} ...
1266 case ISN_ECHO:
1267 {
1268 int count = iptr->isn_arg.echo.echo_count;
1269 int atstart = TRUE;
1270 int needclr = TRUE;
1271
1272 for (idx = 0; idx < count; ++idx)
1273 {
1274 tv = STACK_TV_BOT(idx - count);
1275 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1276 &atstart, &needclr);
1277 clear_tv(tv);
1278 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001279 if (needclr)
1280 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281 ectx.ec_stack.ga_len -= count;
1282 }
1283 break;
1284
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001285 // :execute {string} ...
1286 // :echomsg {string} ...
1287 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001288 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001289 case ISN_ECHOMSG:
1290 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001291 {
1292 int count = iptr->isn_arg.number;
1293 garray_T ga;
1294 char_u buf[NUMBUFLEN];
1295 char_u *p;
1296 int len;
1297 int failed = FALSE;
1298
1299 ga_init2(&ga, 1, 80);
1300 for (idx = 0; idx < count; ++idx)
1301 {
1302 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001303 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001304 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001305 if (tv->v_type == VAR_CHANNEL
1306 || tv->v_type == VAR_JOB)
1307 {
1308 SOURCING_LNUM = iptr->isn_lnum;
1309 emsg(_(e_inval_string));
1310 break;
1311 }
1312 else
1313 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001314 }
1315 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001316 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001317
1318 len = (int)STRLEN(p);
1319 if (ga_grow(&ga, len + 2) == FAIL)
1320 failed = TRUE;
1321 else
1322 {
1323 if (ga.ga_len > 0)
1324 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1325 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1326 ga.ga_len += len;
1327 }
1328 clear_tv(tv);
1329 }
1330 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001331 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001332 {
1333 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001334 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001335 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001336
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001337 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001338 {
1339 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001340 {
1341 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001342 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001343 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001344 {
1345 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001346 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001347 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001348 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001349 else
1350 {
1351 msg_sb_eol();
1352 if (iptr->isn_type == ISN_ECHOMSG)
1353 {
1354 msg_attr(ga.ga_data, echo_attr);
1355 out_flush();
1356 }
1357 else
1358 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001359 SOURCING_LNUM = iptr->isn_lnum;
1360 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001361 }
1362 }
1363 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001364 ga_clear(&ga);
1365 }
1366 break;
1367
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001368 // load local variable or argument
1369 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001370 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 goto failed;
1372 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1373 ++ectx.ec_stack.ga_len;
1374 break;
1375
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001376 // load variable or argument from outer scope
1377 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001378 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001379 goto failed;
1380 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1381 STACK_TV_BOT(0));
1382 ++ectx.ec_stack.ga_len;
1383 break;
1384
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001385 // load v: variable
1386 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001387 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001388 goto failed;
1389 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1390 ++ectx.ec_stack.ga_len;
1391 break;
1392
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001393 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001394 case ISN_LOADSCRIPT:
1395 {
1396 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001397 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 svar_T *sv;
1399
1400 sv = ((svar_T *)si->sn_var_vals.ga_data)
1401 + iptr->isn_arg.script.script_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001402 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001403 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001404 goto failed;
1405 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1406 ++ectx.ec_stack.ga_len;
1407 }
1408 break;
1409
1410 // load s: variable in old script
1411 case ISN_LOADS:
1412 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001413 hashtab_T *ht = &SCRIPT_VARS(
1414 iptr->isn_arg.loadstore.ls_sid);
1415 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001418 if (di == NULL)
1419 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001420 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001421 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001422 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001423 }
1424 else
1425 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001426 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 goto failed;
1428 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1429 ++ectx.ec_stack.ga_len;
1430 }
1431 }
1432 break;
1433
Bram Moolenaard3aac292020-04-19 14:32:17 +02001434 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001436 case ISN_LOADB:
1437 case ISN_LOADW:
1438 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001440 dictitem_T *di = NULL;
1441 hashtab_T *ht = NULL;
1442 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001443
Bram Moolenaard3aac292020-04-19 14:32:17 +02001444 switch (iptr->isn_type)
1445 {
1446 case ISN_LOADG:
1447 ht = get_globvar_ht();
1448 namespace = 'g';
1449 break;
1450 case ISN_LOADB:
1451 ht = &curbuf->b_vars->dv_hashtab;
1452 namespace = 'b';
1453 break;
1454 case ISN_LOADW:
1455 ht = &curwin->w_vars->dv_hashtab;
1456 namespace = 'w';
1457 break;
1458 case ISN_LOADT:
1459 ht = &curtab->tp_vars->dv_hashtab;
1460 namespace = 't';
1461 break;
1462 default: // Cannot reach here
1463 goto failed;
1464 }
1465 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001466
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 if (di == NULL)
1468 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001469 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001470 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001471 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 }
1474 else
1475 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001476 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001477 goto failed;
1478 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1479 ++ectx.ec_stack.ga_len;
1480 }
1481 }
1482 break;
1483
Bram Moolenaar03290b82020-12-19 16:30:44 +01001484 // load autoload variable
1485 case ISN_LOADAUTO:
1486 {
1487 char_u *name = iptr->isn_arg.string;
1488
1489 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1490 goto failed;
1491 SOURCING_LNUM = iptr->isn_lnum;
1492 if (eval_variable(name, STRLEN(name),
1493 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1494 goto on_error;
1495 ++ectx.ec_stack.ga_len;
1496 }
1497 break;
1498
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001499 // load g:/b:/w:/t: namespace
1500 case ISN_LOADGDICT:
1501 case ISN_LOADBDICT:
1502 case ISN_LOADWDICT:
1503 case ISN_LOADTDICT:
1504 {
1505 dict_T *d = NULL;
1506
1507 switch (iptr->isn_type)
1508 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001509 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1510 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1511 case ISN_LOADWDICT: d = curwin->w_vars; break;
1512 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001513 default: // Cannot reach here
1514 goto failed;
1515 }
1516 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1517 goto failed;
1518 tv = STACK_TV_BOT(0);
1519 tv->v_type = VAR_DICT;
1520 tv->v_lock = 0;
1521 tv->vval.v_dict = d;
1522 ++ectx.ec_stack.ga_len;
1523 }
1524 break;
1525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 // load &option
1527 case ISN_LOADOPT:
1528 {
1529 typval_T optval;
1530 char_u *name = iptr->isn_arg.string;
1531
Bram Moolenaara8c17702020-04-01 21:17:24 +02001532 // This is not expected to fail, name is checked during
1533 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001534 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001536 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001537 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001538 *STACK_TV_BOT(0) = optval;
1539 ++ectx.ec_stack.ga_len;
1540 }
1541 break;
1542
1543 // load $ENV
1544 case ISN_LOADENV:
1545 {
1546 typval_T optval;
1547 char_u *name = iptr->isn_arg.string;
1548
Bram Moolenaar270d0382020-05-15 21:42:53 +02001549 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001550 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001551 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001552 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001553 *STACK_TV_BOT(0) = optval;
1554 ++ectx.ec_stack.ga_len;
1555 }
1556 break;
1557
1558 // load @register
1559 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001560 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001561 goto failed;
1562 tv = STACK_TV_BOT(0);
1563 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001564 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001565 // This may result in NULL, which should be equivalent to an
1566 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 tv->vval.v_string = get_reg_contents(
1568 iptr->isn_arg.number, GREG_EXPR_SRC);
1569 ++ectx.ec_stack.ga_len;
1570 break;
1571
1572 // store local variable
1573 case ISN_STORE:
1574 --ectx.ec_stack.ga_len;
1575 tv = STACK_TV_VAR(iptr->isn_arg.number);
1576 clear_tv(tv);
1577 *tv = *STACK_TV_BOT(0);
1578 break;
1579
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001580 // store variable or argument in outer scope
1581 case ISN_STOREOUTER:
1582 --ectx.ec_stack.ga_len;
1583 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1584 clear_tv(tv);
1585 *tv = *STACK_TV_BOT(0);
1586 break;
1587
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001588 // store s: variable in old script
1589 case ISN_STORES:
1590 {
1591 hashtab_T *ht = &SCRIPT_VARS(
1592 iptr->isn_arg.loadstore.ls_sid);
1593 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001594 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001595
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001596 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001597 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001598 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001599 else
1600 {
1601 clear_tv(&di->di_tv);
1602 di->di_tv = *STACK_TV_BOT(0);
1603 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001604 }
1605 break;
1606
1607 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 case ISN_STORESCRIPT:
1609 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001610 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 iptr->isn_arg.script.script_sid);
1612 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1613 + iptr->isn_arg.script.script_idx;
1614
1615 --ectx.ec_stack.ga_len;
1616 clear_tv(sv->sv_tv);
1617 *sv->sv_tv = *STACK_TV_BOT(0);
1618 }
1619 break;
1620
1621 // store option
1622 case ISN_STOREOPT:
1623 {
1624 long n = 0;
1625 char_u *s = NULL;
1626 char *msg;
1627
1628 --ectx.ec_stack.ga_len;
1629 tv = STACK_TV_BOT(0);
1630 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001631 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001633 if (s == NULL)
1634 s = (char_u *)"";
1635 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001637 // must be VAR_NUMBER, CHECKTYPE makes sure
1638 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1640 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001641 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 if (msg != NULL)
1643 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001644 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001645 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001646 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001648 }
1649 break;
1650
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001651 // store $ENV
1652 case ISN_STOREENV:
1653 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001654 tv = STACK_TV_BOT(0);
1655 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1656 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001657 break;
1658
1659 // store @r
1660 case ISN_STOREREG:
1661 {
1662 int reg = iptr->isn_arg.number;
1663
1664 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001665 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001666 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001667 tv_get_string(tv), -1, FALSE);
1668 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001669 }
1670 break;
1671
1672 // store v: variable
1673 case ISN_STOREV:
1674 --ectx.ec_stack.ga_len;
1675 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1676 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001677 // should not happen, type is checked when compiling
1678 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001679 break;
1680
Bram Moolenaard3aac292020-04-19 14:32:17 +02001681 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001683 case ISN_STOREB:
1684 case ISN_STOREW:
1685 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001686 {
1687 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001688 hashtab_T *ht;
1689 switch (iptr->isn_type)
1690 {
1691 case ISN_STOREG:
1692 ht = get_globvar_ht();
1693 break;
1694 case ISN_STOREB:
1695 ht = &curbuf->b_vars->dv_hashtab;
1696 break;
1697 case ISN_STOREW:
1698 ht = &curwin->w_vars->dv_hashtab;
1699 break;
1700 case ISN_STORET:
1701 ht = &curtab->tp_vars->dv_hashtab;
1702 break;
1703 default: // Cannot reach here
1704 goto failed;
1705 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001706
1707 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001708 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001709 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001710 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 else
1712 {
1713 clear_tv(&di->di_tv);
1714 di->di_tv = *STACK_TV_BOT(0);
1715 }
1716 }
1717 break;
1718
Bram Moolenaar03290b82020-12-19 16:30:44 +01001719 // store an autoload variable
1720 case ISN_STOREAUTO:
1721 SOURCING_LNUM = iptr->isn_lnum;
1722 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1723 clear_tv(STACK_TV_BOT(-1));
1724 --ectx.ec_stack.ga_len;
1725 break;
1726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001727 // store number in local variable
1728 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001729 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 clear_tv(tv);
1731 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001732 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 break;
1734
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001735 // store value in list or dict variable
1736 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001737 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001738 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001739 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001740 typval_T *tv_dest = STACK_TV_BOT(-1);
1741 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001742
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001743 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001744 SOURCING_LNUM = iptr->isn_lnum;
1745 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001746 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001747 dest_type = tv_dest->v_type;
1748 if (dest_type == VAR_DICT)
1749 status = do_2string(tv_idx, TRUE);
1750 else if (dest_type == VAR_LIST
1751 && tv_idx->v_type != VAR_NUMBER)
1752 {
1753 emsg(_(e_number_exp));
1754 status = FAIL;
1755 }
1756 }
1757 else if (dest_type != tv_dest->v_type)
1758 {
1759 // just in case, should be OK
1760 semsg(_(e_expected_str_but_got_str),
1761 vartype_name(dest_type),
1762 vartype_name(tv_dest->v_type));
1763 status = FAIL;
1764 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001765
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001766 if (status == OK && dest_type == VAR_LIST)
1767 {
1768 varnumber_T lidx = tv_idx->vval.v_number;
1769 list_T *list = tv_dest->vval.v_list;
1770
1771 if (list == NULL)
1772 {
1773 emsg(_(e_list_not_set));
1774 goto on_error;
1775 }
1776 if (lidx < 0 && list->lv_len + lidx >= 0)
1777 // negative index is relative to the end
1778 lidx = list->lv_len + lidx;
1779 if (lidx < 0 || lidx > list->lv_len)
1780 {
1781 semsg(_(e_listidx), lidx);
1782 goto on_error;
1783 }
1784 if (lidx < list->lv_len)
1785 {
1786 listitem_T *li = list_find(list, lidx);
1787
1788 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001789 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001790 goto on_error;
1791 // overwrite existing list item
1792 clear_tv(&li->li_tv);
1793 li->li_tv = *tv;
1794 }
1795 else
1796 {
1797 if (error_if_locked(list->lv_lock,
1798 e_cannot_change_list))
1799 goto on_error;
1800 // append to list, only fails when out of memory
1801 if (list_append_tv(list, tv) == FAIL)
1802 goto failed;
1803 clear_tv(tv);
1804 }
1805 }
1806 else if (status == OK && dest_type == VAR_DICT)
1807 {
1808 char_u *key = tv_idx->vval.v_string;
1809 dict_T *dict = tv_dest->vval.v_dict;
1810 dictitem_T *di;
1811
1812 SOURCING_LNUM = iptr->isn_lnum;
1813 if (dict == NULL)
1814 {
1815 emsg(_(e_dictionary_not_set));
1816 goto on_error;
1817 }
1818 if (key == NULL)
1819 key = (char_u *)"";
1820 di = dict_find(dict, key, -1);
1821 if (di != NULL)
1822 {
1823 if (error_if_locked(di->di_tv.v_lock,
1824 e_cannot_change_dict_item))
1825 goto on_error;
1826 // overwrite existing value
1827 clear_tv(&di->di_tv);
1828 di->di_tv = *tv;
1829 }
1830 else
1831 {
1832 if (error_if_locked(dict->dv_lock,
1833 e_cannot_change_dict))
1834 goto on_error;
1835 // add to dict, only fails when out of memory
1836 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1837 goto failed;
1838 clear_tv(tv);
1839 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001840 }
1841 else
1842 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001843 status = FAIL;
1844 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001845 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001846
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001847 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001848 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001849 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001850 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001851 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001852 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001853 goto on_error;
1854 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001855 }
1856 break;
1857
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001858 // push constant
1859 case ISN_PUSHNR:
1860 case ISN_PUSHBOOL:
1861 case ISN_PUSHSPEC:
1862 case ISN_PUSHF:
1863 case ISN_PUSHS:
1864 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001865 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001866 case ISN_PUSHCHANNEL:
1867 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001868 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001869 goto failed;
1870 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001871 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001872 ++ectx.ec_stack.ga_len;
1873 switch (iptr->isn_type)
1874 {
1875 case ISN_PUSHNR:
1876 tv->v_type = VAR_NUMBER;
1877 tv->vval.v_number = iptr->isn_arg.number;
1878 break;
1879 case ISN_PUSHBOOL:
1880 tv->v_type = VAR_BOOL;
1881 tv->vval.v_number = iptr->isn_arg.number;
1882 break;
1883 case ISN_PUSHSPEC:
1884 tv->v_type = VAR_SPECIAL;
1885 tv->vval.v_number = iptr->isn_arg.number;
1886 break;
1887#ifdef FEAT_FLOAT
1888 case ISN_PUSHF:
1889 tv->v_type = VAR_FLOAT;
1890 tv->vval.v_float = iptr->isn_arg.fnumber;
1891 break;
1892#endif
1893 case ISN_PUSHBLOB:
1894 blob_copy(iptr->isn_arg.blob, tv);
1895 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001896 case ISN_PUSHFUNC:
1897 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001898 if (iptr->isn_arg.string == NULL)
1899 tv->vval.v_string = NULL;
1900 else
1901 tv->vval.v_string =
1902 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001903 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001904 case ISN_PUSHCHANNEL:
1905#ifdef FEAT_JOB_CHANNEL
1906 tv->v_type = VAR_CHANNEL;
1907 tv->vval.v_channel = iptr->isn_arg.channel;
1908 if (tv->vval.v_channel != NULL)
1909 ++tv->vval.v_channel->ch_refcount;
1910#endif
1911 break;
1912 case ISN_PUSHJOB:
1913#ifdef FEAT_JOB_CHANNEL
1914 tv->v_type = VAR_JOB;
1915 tv->vval.v_job = iptr->isn_arg.job;
1916 if (tv->vval.v_job != NULL)
1917 ++tv->vval.v_job->jv_refcount;
1918#endif
1919 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 default:
1921 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001922 tv->vval.v_string = vim_strsave(
1923 iptr->isn_arg.string == NULL
1924 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 }
1926 break;
1927
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001928 case ISN_UNLET:
1929 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1930 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001931 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001932 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001933 case ISN_UNLETENV:
1934 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1935 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001936
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001937 case ISN_LOCKCONST:
1938 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1939 break;
1940
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 // create a list from items on the stack; uses a single allocation
1942 // for the list header and the items
1943 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001944 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1945 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001946 break;
1947
1948 // create a dict from items on the stack
1949 case ISN_NEWDICT:
1950 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001951 int count = iptr->isn_arg.number;
1952 dict_T *dict = dict_alloc();
1953 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001954 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001955
1956 if (dict == NULL)
1957 goto failed;
1958 for (idx = 0; idx < count; ++idx)
1959 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001960 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001962 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001963 key = tv->vval.v_string == NULL
1964 ? (char_u *)"" : tv->vval.v_string;
1965 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001966 if (item != NULL)
1967 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001968 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001969 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001970 dict_unref(dict);
1971 goto on_error;
1972 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001973 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974 clear_tv(tv);
1975 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001976 {
1977 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001979 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1981 item->di_tv.v_lock = 0;
1982 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001983 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001984 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001985 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001986 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001987 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001988 }
1989
1990 if (count > 0)
1991 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001992 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 goto failed;
1994 else
1995 ++ectx.ec_stack.ga_len;
1996 tv = STACK_TV_BOT(-1);
1997 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001998 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001999 tv->vval.v_dict = dict;
2000 ++dict->dv_refcount;
2001 }
2002 break;
2003
2004 // call a :def function
2005 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002007 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2008 iptr->isn_arg.dfunc.cdf_argcount,
2009 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002010 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 break;
2012
2013 // call a builtin function
2014 case ISN_BCALL:
2015 SOURCING_LNUM = iptr->isn_lnum;
2016 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2017 iptr->isn_arg.bfunc.cbf_argcount,
2018 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002019 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002020 break;
2021
2022 // call a funcref or partial
2023 case ISN_PCALL:
2024 {
2025 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2026 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002027 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002028
2029 SOURCING_LNUM = iptr->isn_lnum;
2030 if (pfunc->cpf_top)
2031 {
2032 // funcref is above the arguments
2033 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2034 }
2035 else
2036 {
2037 // Get the funcref from the stack.
2038 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002039 partial_tv = *STACK_TV_BOT(0);
2040 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002041 }
2042 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002043 if (tv == &partial_tv)
2044 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002046 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002047 }
2048 break;
2049
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002050 case ISN_PCALL_END:
2051 // PCALL finished, arguments have been consumed and replaced by
2052 // the return value. Now clear the funcref from the stack,
2053 // and move the return value in its place.
2054 --ectx.ec_stack.ga_len;
2055 clear_tv(STACK_TV_BOT(-1));
2056 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2057 break;
2058
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002059 // call a user defined function or funcref/partial
2060 case ISN_UCALL:
2061 {
2062 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2063
2064 SOURCING_LNUM = iptr->isn_lnum;
2065 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002066 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002067 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002068 }
2069 break;
2070
2071 // return from a :def function call
2072 case ISN_RETURN:
2073 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002074 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002075 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002076
2077 if (trystack->ga_len > 0)
2078 trycmd = ((trycmd_T *)trystack->ga_data)
2079 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002080 if (trycmd != NULL
2081 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 && trycmd->tcd_finally_idx != 0)
2083 {
2084 // jump to ":finally"
2085 ectx.ec_iidx = trycmd->tcd_finally_idx;
2086 trycmd->tcd_return = TRUE;
2087 }
2088 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002089 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002090 }
2091 break;
2092
2093 // push a function reference to a compiled function
2094 case ISN_FUNCREF:
2095 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002096 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2097 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2098 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002099
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002100 if (pt == NULL)
2101 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002102 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002103 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002104 vim_free(pt);
2105 goto failed;
2106 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002107 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2108 &ectx) == FAIL)
2109 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002110
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002111 tv = STACK_TV_BOT(0);
2112 ++ectx.ec_stack.ga_len;
2113 tv->vval.v_partial = pt;
2114 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002115 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002116 }
2117 break;
2118
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002119 // Create a global function from a lambda.
2120 case ISN_NEWFUNC:
2121 {
2122 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002123 ufunc_T *new_ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002124
Bram Moolenaarf112f302020-12-20 17:47:52 +01002125 new_ufunc = copy_func(
2126 newfunc->nf_lambda, newfunc->nf_global);
2127 if (new_ufunc != NULL
2128 && (new_ufunc->uf_flags & FC_CLOSURE))
2129 {
2130 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2131
2132 // Need to create a partial to store the context of the
2133 // function.
2134 if (pt == NULL)
2135 goto failed;
2136 if (fill_partial_and_closure(pt, new_ufunc,
2137 &ectx) == FAIL)
2138 goto failed;
2139 new_ufunc->uf_partial = pt;
2140 --pt->pt_refcount; // not referenced here
2141 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002142 }
2143 break;
2144
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002145 // List functions
2146 case ISN_DEF:
2147 if (iptr->isn_arg.string == NULL)
2148 list_functions(NULL);
2149 else
2150 {
2151 exarg_T ea;
2152
2153 CLEAR_FIELD(ea);
2154 ea.cmd = ea.arg = iptr->isn_arg.string;
2155 define_function(&ea, NULL);
2156 }
2157 break;
2158
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002159 // jump if a condition is met
2160 case ISN_JUMP:
2161 {
2162 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002163 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002164 int jump = TRUE;
2165
2166 if (when != JUMP_ALWAYS)
2167 {
2168 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002169 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002170 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002171 || when == JUMP_IF_COND_TRUE)
2172 {
2173 SOURCING_LNUM = iptr->isn_lnum;
2174 jump = tv_get_bool_chk(tv, &error);
2175 if (error)
2176 goto on_error;
2177 }
2178 else
2179 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002181 || when == JUMP_AND_KEEP_IF_FALSE
2182 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002183 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002184 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 {
2186 // drop the value from the stack
2187 clear_tv(tv);
2188 --ectx.ec_stack.ga_len;
2189 }
2190 }
2191 if (jump)
2192 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2193 }
2194 break;
2195
2196 // top of a for loop
2197 case ISN_FOR:
2198 {
2199 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2200 typval_T *idxtv =
2201 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2202
2203 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002204 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002206 ++idxtv->vval.v_number;
2207 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002208 // past the end of the list, jump to "endfor"
2209 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2210 else if (list->lv_first == &range_list_item)
2211 {
2212 // non-materialized range() list
2213 tv = STACK_TV_BOT(0);
2214 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002215 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002216 tv->vval.v_number = list_find_nr(
2217 list, idxtv->vval.v_number, NULL);
2218 ++ectx.ec_stack.ga_len;
2219 }
2220 else
2221 {
2222 listitem_T *li = list_find(list, idxtv->vval.v_number);
2223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2225 ++ectx.ec_stack.ga_len;
2226 }
2227 }
2228 break;
2229
2230 // start of ":try" block
2231 case ISN_TRY:
2232 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002233 trycmd_T *trycmd = NULL;
2234
Bram Moolenaar270d0382020-05-15 21:42:53 +02002235 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002236 goto failed;
2237 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2238 + ectx.ec_trystack.ga_len;
2239 ++ectx.ec_trystack.ga_len;
2240 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002241 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002242 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2243 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002244 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002245 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 }
2247 break;
2248
2249 case ISN_PUSHEXC:
2250 if (current_exception == NULL)
2251 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002252 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 iemsg("Evaluating catch while current_exception is NULL");
2254 goto failed;
2255 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002256 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002257 goto failed;
2258 tv = STACK_TV_BOT(0);
2259 ++ectx.ec_stack.ga_len;
2260 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002261 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002262 tv->vval.v_string = vim_strsave(
2263 (char_u *)current_exception->value);
2264 break;
2265
2266 case ISN_CATCH:
2267 {
2268 garray_T *trystack = &ectx.ec_trystack;
2269
2270 if (trystack->ga_len > 0)
2271 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002272 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 + trystack->ga_len - 1;
2274 trycmd->tcd_caught = TRUE;
2275 }
2276 did_emsg = got_int = did_throw = FALSE;
2277 catch_exception(current_exception);
2278 }
2279 break;
2280
2281 // end of ":try" block
2282 case ISN_ENDTRY:
2283 {
2284 garray_T *trystack = &ectx.ec_trystack;
2285
2286 if (trystack->ga_len > 0)
2287 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002288 trycmd_T *trycmd = NULL;
2289
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 --trystack->ga_len;
2291 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002292 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002293 trycmd = ((trycmd_T *)trystack->ga_data)
2294 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002295 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 {
2297 // discard the exception
2298 if (caught_stack == current_exception)
2299 caught_stack = caught_stack->caught;
2300 discard_current_exception();
2301 }
2302
2303 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002304 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 }
2306 }
2307 break;
2308
2309 case ISN_THROW:
2310 --ectx.ec_stack.ga_len;
2311 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002312 if (tv->vval.v_string == NULL
2313 || *skipwhite(tv->vval.v_string) == NUL)
2314 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002315 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002316 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002317 emsg(_(e_throw_with_empty_string));
2318 goto failed;
2319 }
2320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2322 {
2323 vim_free(tv->vval.v_string);
2324 goto failed;
2325 }
2326 did_throw = TRUE;
2327 break;
2328
2329 // compare with special values
2330 case ISN_COMPAREBOOL:
2331 case ISN_COMPARESPECIAL:
2332 {
2333 typval_T *tv1 = STACK_TV_BOT(-2);
2334 typval_T *tv2 = STACK_TV_BOT(-1);
2335 varnumber_T arg1 = tv1->vval.v_number;
2336 varnumber_T arg2 = tv2->vval.v_number;
2337 int res;
2338
2339 switch (iptr->isn_arg.op.op_type)
2340 {
2341 case EXPR_EQUAL: res = arg1 == arg2; break;
2342 case EXPR_NEQUAL: res = arg1 != arg2; break;
2343 default: res = 0; break;
2344 }
2345
2346 --ectx.ec_stack.ga_len;
2347 tv1->v_type = VAR_BOOL;
2348 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2349 }
2350 break;
2351
2352 // Operation with two number arguments
2353 case ISN_OPNR:
2354 case ISN_COMPARENR:
2355 {
2356 typval_T *tv1 = STACK_TV_BOT(-2);
2357 typval_T *tv2 = STACK_TV_BOT(-1);
2358 varnumber_T arg1 = tv1->vval.v_number;
2359 varnumber_T arg2 = tv2->vval.v_number;
2360 varnumber_T res;
2361
2362 switch (iptr->isn_arg.op.op_type)
2363 {
2364 case EXPR_MULT: res = arg1 * arg2; break;
2365 case EXPR_DIV: res = arg1 / arg2; break;
2366 case EXPR_REM: res = arg1 % arg2; break;
2367 case EXPR_SUB: res = arg1 - arg2; break;
2368 case EXPR_ADD: res = arg1 + arg2; break;
2369
2370 case EXPR_EQUAL: res = arg1 == arg2; break;
2371 case EXPR_NEQUAL: res = arg1 != arg2; break;
2372 case EXPR_GREATER: res = arg1 > arg2; break;
2373 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2374 case EXPR_SMALLER: res = arg1 < arg2; break;
2375 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2376 default: res = 0; break;
2377 }
2378
2379 --ectx.ec_stack.ga_len;
2380 if (iptr->isn_type == ISN_COMPARENR)
2381 {
2382 tv1->v_type = VAR_BOOL;
2383 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2384 }
2385 else
2386 tv1->vval.v_number = res;
2387 }
2388 break;
2389
2390 // Computation with two float arguments
2391 case ISN_OPFLOAT:
2392 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002393#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002394 {
2395 typval_T *tv1 = STACK_TV_BOT(-2);
2396 typval_T *tv2 = STACK_TV_BOT(-1);
2397 float_T arg1 = tv1->vval.v_float;
2398 float_T arg2 = tv2->vval.v_float;
2399 float_T res = 0;
2400 int cmp = FALSE;
2401
2402 switch (iptr->isn_arg.op.op_type)
2403 {
2404 case EXPR_MULT: res = arg1 * arg2; break;
2405 case EXPR_DIV: res = arg1 / arg2; break;
2406 case EXPR_SUB: res = arg1 - arg2; break;
2407 case EXPR_ADD: res = arg1 + arg2; break;
2408
2409 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2410 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2411 case EXPR_GREATER: cmp = arg1 > arg2; break;
2412 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2413 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2414 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2415 default: cmp = 0; break;
2416 }
2417 --ectx.ec_stack.ga_len;
2418 if (iptr->isn_type == ISN_COMPAREFLOAT)
2419 {
2420 tv1->v_type = VAR_BOOL;
2421 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2422 }
2423 else
2424 tv1->vval.v_float = res;
2425 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002426#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 break;
2428
2429 case ISN_COMPARELIST:
2430 {
2431 typval_T *tv1 = STACK_TV_BOT(-2);
2432 typval_T *tv2 = STACK_TV_BOT(-1);
2433 list_T *arg1 = tv1->vval.v_list;
2434 list_T *arg2 = tv2->vval.v_list;
2435 int cmp = FALSE;
2436 int ic = iptr->isn_arg.op.op_ic;
2437
2438 switch (iptr->isn_arg.op.op_type)
2439 {
2440 case EXPR_EQUAL: cmp =
2441 list_equal(arg1, arg2, ic, FALSE); break;
2442 case EXPR_NEQUAL: cmp =
2443 !list_equal(arg1, arg2, ic, FALSE); break;
2444 case EXPR_IS: cmp = arg1 == arg2; break;
2445 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2446 default: cmp = 0; break;
2447 }
2448 --ectx.ec_stack.ga_len;
2449 clear_tv(tv1);
2450 clear_tv(tv2);
2451 tv1->v_type = VAR_BOOL;
2452 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2453 }
2454 break;
2455
2456 case ISN_COMPAREBLOB:
2457 {
2458 typval_T *tv1 = STACK_TV_BOT(-2);
2459 typval_T *tv2 = STACK_TV_BOT(-1);
2460 blob_T *arg1 = tv1->vval.v_blob;
2461 blob_T *arg2 = tv2->vval.v_blob;
2462 int cmp = FALSE;
2463
2464 switch (iptr->isn_arg.op.op_type)
2465 {
2466 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2467 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2468 case EXPR_IS: cmp = arg1 == arg2; break;
2469 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2470 default: cmp = 0; break;
2471 }
2472 --ectx.ec_stack.ga_len;
2473 clear_tv(tv1);
2474 clear_tv(tv2);
2475 tv1->v_type = VAR_BOOL;
2476 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2477 }
2478 break;
2479
2480 // TODO: handle separately
2481 case ISN_COMPARESTRING:
2482 case ISN_COMPAREDICT:
2483 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002484 case ISN_COMPAREANY:
2485 {
2486 typval_T *tv1 = STACK_TV_BOT(-2);
2487 typval_T *tv2 = STACK_TV_BOT(-1);
2488 exptype_T exptype = iptr->isn_arg.op.op_type;
2489 int ic = iptr->isn_arg.op.op_ic;
2490
Bram Moolenaareb26f432020-09-14 16:50:05 +02002491 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002492 typval_compare(tv1, tv2, exptype, ic);
2493 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002494 --ectx.ec_stack.ga_len;
2495 }
2496 break;
2497
2498 case ISN_ADDLIST:
2499 case ISN_ADDBLOB:
2500 {
2501 typval_T *tv1 = STACK_TV_BOT(-2);
2502 typval_T *tv2 = STACK_TV_BOT(-1);
2503
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002504 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 if (iptr->isn_type == ISN_ADDLIST)
2506 eval_addlist(tv1, tv2);
2507 else
2508 eval_addblob(tv1, tv2);
2509 clear_tv(tv2);
2510 --ectx.ec_stack.ga_len;
2511 }
2512 break;
2513
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002514 case ISN_LISTAPPEND:
2515 {
2516 typval_T *tv1 = STACK_TV_BOT(-2);
2517 typval_T *tv2 = STACK_TV_BOT(-1);
2518 list_T *l = tv1->vval.v_list;
2519
2520 // add an item to a list
2521 if (l == NULL)
2522 {
2523 SOURCING_LNUM = iptr->isn_lnum;
2524 emsg(_(e_cannot_add_to_null_list));
2525 goto on_error;
2526 }
2527 if (list_append_tv(l, tv2) == FAIL)
2528 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002529 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002530 --ectx.ec_stack.ga_len;
2531 }
2532 break;
2533
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002534 case ISN_BLOBAPPEND:
2535 {
2536 typval_T *tv1 = STACK_TV_BOT(-2);
2537 typval_T *tv2 = STACK_TV_BOT(-1);
2538 blob_T *b = tv1->vval.v_blob;
2539 int error = FALSE;
2540 varnumber_T n;
2541
2542 // add a number to a blob
2543 if (b == NULL)
2544 {
2545 SOURCING_LNUM = iptr->isn_lnum;
2546 emsg(_(e_cannot_add_to_null_blob));
2547 goto on_error;
2548 }
2549 n = tv_get_number_chk(tv2, &error);
2550 if (error)
2551 goto on_error;
2552 ga_append(&b->bv_ga, (int)n);
2553 --ectx.ec_stack.ga_len;
2554 }
2555 break;
2556
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 // Computation with two arguments of unknown type
2558 case ISN_OPANY:
2559 {
2560 typval_T *tv1 = STACK_TV_BOT(-2);
2561 typval_T *tv2 = STACK_TV_BOT(-1);
2562 varnumber_T n1, n2;
2563#ifdef FEAT_FLOAT
2564 float_T f1 = 0, f2 = 0;
2565#endif
2566 int error = FALSE;
2567
2568 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2569 {
2570 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2571 {
2572 eval_addlist(tv1, tv2);
2573 clear_tv(tv2);
2574 --ectx.ec_stack.ga_len;
2575 break;
2576 }
2577 else if (tv1->v_type == VAR_BLOB
2578 && tv2->v_type == VAR_BLOB)
2579 {
2580 eval_addblob(tv1, tv2);
2581 clear_tv(tv2);
2582 --ectx.ec_stack.ga_len;
2583 break;
2584 }
2585 }
2586#ifdef FEAT_FLOAT
2587 if (tv1->v_type == VAR_FLOAT)
2588 {
2589 f1 = tv1->vval.v_float;
2590 n1 = 0;
2591 }
2592 else
2593#endif
2594 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002595 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 n1 = tv_get_number_chk(tv1, &error);
2597 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002598 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599#ifdef FEAT_FLOAT
2600 if (tv2->v_type == VAR_FLOAT)
2601 f1 = n1;
2602#endif
2603 }
2604#ifdef FEAT_FLOAT
2605 if (tv2->v_type == VAR_FLOAT)
2606 {
2607 f2 = tv2->vval.v_float;
2608 n2 = 0;
2609 }
2610 else
2611#endif
2612 {
2613 n2 = tv_get_number_chk(tv2, &error);
2614 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002615 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616#ifdef FEAT_FLOAT
2617 if (tv1->v_type == VAR_FLOAT)
2618 f2 = n2;
2619#endif
2620 }
2621#ifdef FEAT_FLOAT
2622 // if there is a float on either side the result is a float
2623 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2624 {
2625 switch (iptr->isn_arg.op.op_type)
2626 {
2627 case EXPR_MULT: f1 = f1 * f2; break;
2628 case EXPR_DIV: f1 = f1 / f2; break;
2629 case EXPR_SUB: f1 = f1 - f2; break;
2630 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002631 default: SOURCING_LNUM = iptr->isn_lnum;
2632 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002633 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002634 }
2635 clear_tv(tv1);
2636 clear_tv(tv2);
2637 tv1->v_type = VAR_FLOAT;
2638 tv1->vval.v_float = f1;
2639 --ectx.ec_stack.ga_len;
2640 }
2641 else
2642#endif
2643 {
2644 switch (iptr->isn_arg.op.op_type)
2645 {
2646 case EXPR_MULT: n1 = n1 * n2; break;
2647 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2648 case EXPR_SUB: n1 = n1 - n2; break;
2649 case EXPR_ADD: n1 = n1 + n2; break;
2650 default: n1 = num_modulus(n1, n2); break;
2651 }
2652 clear_tv(tv1);
2653 clear_tv(tv2);
2654 tv1->v_type = VAR_NUMBER;
2655 tv1->vval.v_number = n1;
2656 --ectx.ec_stack.ga_len;
2657 }
2658 }
2659 break;
2660
2661 case ISN_CONCAT:
2662 {
2663 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2664 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2665 char_u *res;
2666
2667 res = concat_str(str1, str2);
2668 clear_tv(STACK_TV_BOT(-2));
2669 clear_tv(STACK_TV_BOT(-1));
2670 --ectx.ec_stack.ga_len;
2671 STACK_TV_BOT(-1)->vval.v_string = res;
2672 }
2673 break;
2674
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002675 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002676 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002677 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002678 int is_slice = iptr->isn_type == ISN_STRSLICE;
2679 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002680 char_u *res;
2681
2682 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002683 // string slice: string is at stack-3, first index at
2684 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002685 if (is_slice)
2686 {
2687 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002688 n1 = tv->vval.v_number;
2689 }
2690
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002691 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002692 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002693
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002694 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002695 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002696 if (is_slice)
2697 // Slice: Select the characters from the string
2698 res = string_slice(tv->vval.v_string, n1, n2);
2699 else
2700 // Index: The resulting variable is a string of a
2701 // single character. If the index is too big or
2702 // negative the result is empty.
2703 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002704 vim_free(tv->vval.v_string);
2705 tv->vval.v_string = res;
2706 }
2707 break;
2708
2709 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002710 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002711 {
Bram Moolenaared591872020-08-15 22:14:53 +02002712 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002713 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002714 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715
2716 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002717 // list slice: list is at stack-3, indexes at stack-2 and
2718 // stack-1
2719 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002720 list = tv->vval.v_list;
2721
2722 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002723 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002724 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002725
2726 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002727 {
Bram Moolenaared591872020-08-15 22:14:53 +02002728 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002729 n1 = tv->vval.v_number;
2730 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002731 }
Bram Moolenaared591872020-08-15 22:14:53 +02002732
2733 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002734 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002735 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002736 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2737 == FAIL)
2738 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002739 }
2740 break;
2741
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002742 case ISN_ANYINDEX:
2743 case ISN_ANYSLICE:
2744 {
2745 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2746 typval_T *var1, *var2;
2747 int res;
2748
2749 // index: composite is at stack-2, index at stack-1
2750 // slice: composite is at stack-3, indexes at stack-2 and
2751 // stack-1
2752 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002753 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002754 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2755 goto on_error;
2756 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2757 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2758 res = eval_index_inner(tv, is_slice,
2759 var1, var2, NULL, -1, TRUE);
2760 clear_tv(var1);
2761 if (is_slice)
2762 clear_tv(var2);
2763 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2764 if (res == FAIL)
2765 goto on_error;
2766 }
2767 break;
2768
Bram Moolenaar9af78762020-06-16 11:34:42 +02002769 case ISN_SLICE:
2770 {
2771 list_T *list;
2772 int count = iptr->isn_arg.number;
2773
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002774 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002775 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002776 list = tv->vval.v_list;
2777
2778 // no error for short list, expect it to be checked earlier
2779 if (list != NULL && list->lv_len >= count)
2780 {
2781 list_T *newlist = list_slice(list,
2782 count, list->lv_len - 1);
2783
2784 if (newlist != NULL)
2785 {
2786 list_unref(list);
2787 tv->vval.v_list = newlist;
2788 ++newlist->lv_refcount;
2789 }
2790 }
2791 }
2792 break;
2793
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002794 case ISN_GETITEM:
2795 {
2796 listitem_T *li;
2797 int index = iptr->isn_arg.number;
2798
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002799 // Get list item: list is at stack-1, push item.
2800 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002801 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002802 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002803
2804 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2805 goto failed;
2806 ++ectx.ec_stack.ga_len;
2807 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2808 }
2809 break;
2810
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 case ISN_MEMBER:
2812 {
2813 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002814 char_u *key;
2815 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002816 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002817
2818 // dict member: dict is at stack-2, key at stack-1
2819 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002820 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002821 dict = tv->vval.v_dict;
2822
2823 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002824 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002825 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002826 if (key == NULL)
2827 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002828
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002829 if ((di = dict_find(dict, key, -1)) == NULL)
2830 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002831 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002832 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002833
2834 // If :silent! is used we will continue, make sure the
2835 // stack contents makes sense.
2836 clear_tv(tv);
2837 --ectx.ec_stack.ga_len;
2838 tv = STACK_TV_BOT(-1);
2839 clear_tv(tv);
2840 tv->v_type = VAR_NUMBER;
2841 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002842 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002843 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002844 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002845 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002846 // Clear the dict only after getting the item, to avoid
2847 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002848 tv = STACK_TV_BOT(-1);
2849 temp_tv = *tv;
2850 copy_tv(&di->di_tv, tv);
2851 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002852 }
2853 break;
2854
2855 // dict member with string key
2856 case ISN_STRINGMEMBER:
2857 {
2858 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002859 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002860 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002861
2862 tv = STACK_TV_BOT(-1);
2863 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2864 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002865 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002867 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 }
2869 dict = tv->vval.v_dict;
2870
2871 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2872 == NULL)
2873 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002874 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002875 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002876 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002877 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002878 // Clear the dict after getting the item, to avoid that it
2879 // make the item invalid.
2880 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002881 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002882 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002883 }
2884 break;
2885
2886 case ISN_NEGATENR:
2887 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002888 if (tv->v_type != VAR_NUMBER
2889#ifdef FEAT_FLOAT
2890 && tv->v_type != VAR_FLOAT
2891#endif
2892 )
2893 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002894 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002895 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002896 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002897 }
2898#ifdef FEAT_FLOAT
2899 if (tv->v_type == VAR_FLOAT)
2900 tv->vval.v_float = -tv->vval.v_float;
2901 else
2902#endif
2903 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 break;
2905
2906 case ISN_CHECKNR:
2907 {
2908 int error = FALSE;
2909
2910 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002911 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002913 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 (void)tv_get_number_chk(tv, &error);
2915 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002916 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002917 }
2918 break;
2919
2920 case ISN_CHECKTYPE:
2921 {
2922 checktype_T *ct = &iptr->isn_arg.type;
2923
2924 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002925 SOURCING_LNUM = iptr->isn_lnum;
2926 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2927 goto on_error;
2928
2929 // number 0 is FALSE, number 1 is TRUE
2930 if (tv->v_type == VAR_NUMBER
2931 && ct->ct_type->tt_type == VAR_BOOL
2932 && (tv->vval.v_number == 0
2933 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002934 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002935 tv->v_type = VAR_BOOL;
2936 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002937 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002938 }
2939 }
2940 break;
2941
Bram Moolenaar9af78762020-06-16 11:34:42 +02002942 case ISN_CHECKLEN:
2943 {
2944 int min_len = iptr->isn_arg.checklen.cl_min_len;
2945 list_T *list = NULL;
2946
2947 tv = STACK_TV_BOT(-1);
2948 if (tv->v_type == VAR_LIST)
2949 list = tv->vval.v_list;
2950 if (list == NULL || list->lv_len < min_len
2951 || (list->lv_len > min_len
2952 && !iptr->isn_arg.checklen.cl_more_OK))
2953 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002954 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002955 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002956 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002957 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002958 }
2959 }
2960 break;
2961
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002962 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002963 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 {
2965 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002966 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967
2968 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002969 if (iptr->isn_type == ISN_2BOOL)
2970 {
2971 n = tv2bool(tv);
2972 if (iptr->isn_arg.number) // invert
2973 n = !n;
2974 }
2975 else
2976 {
2977 SOURCING_LNUM = iptr->isn_lnum;
2978 n = tv_get_bool_chk(tv, &error);
2979 if (error)
2980 goto on_error;
2981 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002982 clear_tv(tv);
2983 tv->v_type = VAR_BOOL;
2984 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2985 }
2986 break;
2987
2988 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002989 case ISN_2STRING_ANY:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002990 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
2991 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
2992 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 break;
2994
Bram Moolenaar08597872020-12-10 19:43:40 +01002995 case ISN_RANGE:
2996 {
2997 exarg_T ea;
2998 char *errormsg;
2999
3000 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3001 goto failed;
3002 ++ectx.ec_stack.ga_len;
3003 tv = STACK_TV_BOT(-1);
3004 ea.addr_type = ADDR_LINES;
3005 ea.cmd = iptr->isn_arg.string;
3006 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
3007 goto failed;
3008 if (ea.addr_count == 0)
3009 tv->vval.v_number = curwin->w_cursor.lnum;
3010 else
3011 tv->vval.v_number = ea.line2;
3012 }
3013 break;
3014
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003015 case ISN_PUT:
3016 {
3017 int regname = iptr->isn_arg.put.put_regname;
3018 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3019 char_u *expr = NULL;
3020 int dir = FORWARD;
3021
3022 if (regname == '=')
3023 {
3024 tv = STACK_TV_BOT(-1);
3025 if (tv->v_type == VAR_STRING)
3026 expr = tv->vval.v_string;
3027 else
3028 {
3029 expr = typval_tostring(tv); // allocates value
3030 clear_tv(tv);
3031 }
3032 --ectx.ec_stack.ga_len;
3033 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003034 if (lnum < -2)
3035 {
3036 // line number was put on the stack by ISN_RANGE
3037 tv = STACK_TV_BOT(-1);
3038 curwin->w_cursor.lnum = tv->vval.v_number;
3039 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3040 dir = BACKWARD;
3041 --ectx.ec_stack.ga_len;
3042 }
3043 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003044 // :put! above cursor
3045 dir = BACKWARD;
3046 else if (lnum >= 0)
3047 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3048 check_cursor();
3049 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3050 vim_free(expr);
3051 }
3052 break;
3053
Bram Moolenaar02194d22020-10-24 23:08:38 +02003054 case ISN_CMDMOD:
3055 save_cmdmod = cmdmod;
3056 restore_cmdmod = TRUE;
3057 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3058 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003059 break;
3060
Bram Moolenaar02194d22020-10-24 23:08:38 +02003061 case ISN_CMDMOD_REV:
3062 // filter regprog is owned by the instruction, don't free it
3063 cmdmod.cmod_filter_regmatch.regprog = NULL;
3064 undo_cmdmod(&cmdmod);
3065 cmdmod = save_cmdmod;
3066 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003067 break;
3068
Bram Moolenaar792f7862020-11-23 08:31:18 +01003069 case ISN_UNPACK:
3070 {
3071 int count = iptr->isn_arg.unpack.unp_count;
3072 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3073 list_T *l;
3074 listitem_T *li;
3075 int i;
3076
3077 // Check there is a valid list to unpack.
3078 tv = STACK_TV_BOT(-1);
3079 if (tv->v_type != VAR_LIST)
3080 {
3081 SOURCING_LNUM = iptr->isn_lnum;
3082 emsg(_(e_for_argument_must_be_sequence_of_lists));
3083 goto on_error;
3084 }
3085 l = tv->vval.v_list;
3086 if (l == NULL
3087 || l->lv_len < (semicolon ? count - 1 : count))
3088 {
3089 SOURCING_LNUM = iptr->isn_lnum;
3090 emsg(_(e_list_value_does_not_have_enough_items));
3091 goto on_error;
3092 }
3093 else if (!semicolon && l->lv_len > count)
3094 {
3095 SOURCING_LNUM = iptr->isn_lnum;
3096 emsg(_(e_list_value_has_more_items_than_targets));
3097 goto on_error;
3098 }
3099
3100 CHECK_LIST_MATERIALIZE(l);
3101 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3102 goto failed;
3103 ectx.ec_stack.ga_len += count - 1;
3104
3105 // Variable after semicolon gets a list with the remaining
3106 // items.
3107 if (semicolon)
3108 {
3109 list_T *rem_list =
3110 list_alloc_with_items(l->lv_len - count + 1);
3111
3112 if (rem_list == NULL)
3113 goto failed;
3114 tv = STACK_TV_BOT(-count);
3115 tv->vval.v_list = rem_list;
3116 ++rem_list->lv_refcount;
3117 tv->v_lock = 0;
3118 li = l->lv_first;
3119 for (i = 0; i < count - 1; ++i)
3120 li = li->li_next;
3121 for (i = 0; li != NULL; ++i)
3122 {
3123 list_set_item(rem_list, i, &li->li_tv);
3124 li = li->li_next;
3125 }
3126 --count;
3127 }
3128
3129 // Produce the values in reverse order, first item last.
3130 li = l->lv_first;
3131 for (i = 0; i < count; ++i)
3132 {
3133 tv = STACK_TV_BOT(-i - 1);
3134 copy_tv(&li->li_tv, tv);
3135 li = li->li_next;
3136 }
3137
3138 list_unref(l);
3139 }
3140 break;
3141
Bram Moolenaar389df252020-07-09 21:20:47 +02003142 case ISN_SHUFFLE:
3143 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003144 typval_T tmp_tv;
3145 int item = iptr->isn_arg.shuffle.shfl_item;
3146 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003147
3148 tmp_tv = *STACK_TV_BOT(-item);
3149 for ( ; up > 0 && item > 1; --up)
3150 {
3151 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3152 --item;
3153 }
3154 *STACK_TV_BOT(-item) = tmp_tv;
3155 }
3156 break;
3157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003158 case ISN_DROP:
3159 --ectx.ec_stack.ga_len;
3160 clear_tv(STACK_TV_BOT(0));
3161 break;
3162 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003163 continue;
3164
Bram Moolenaard032f342020-07-18 18:13:02 +02003165func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003166 // Restore previous function. If the frame pointer is where we started
3167 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003168 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003169 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003170
Bram Moolenaard032f342020-07-18 18:13:02 +02003171 if (func_return(&ectx) == FAIL)
3172 // only fails when out of memory
3173 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003174 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003175
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003176on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003177 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003178 // If "emsg_silent" is set then ignore the error, unless it was set
3179 // when calling the function.
3180 if (did_emsg_cumul + did_emsg == did_emsg_before
3181 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003182 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003183on_fatal_error:
3184 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003185 // If we are not inside a try-catch started here, abort execution.
3186 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003187 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003188 }
3189
3190done:
3191 // function finished, get result from the stack.
3192 tv = STACK_TV_BOT(-1);
3193 *rettv = *tv;
3194 tv->v_type = VAR_UNKNOWN;
3195 ret = OK;
3196
3197failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003198 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003199 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003200 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003201
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003202 // Deal with any remaining closures, they may be in use somewhere.
3203 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003204 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003205 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003206 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3207 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003208
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003209 estack_pop();
3210 current_sctx = save_current_sctx;
3211
Bram Moolenaar352134b2020-10-17 22:04:08 +02003212 if (*msg_list != NULL && saved_msg_list != NULL)
3213 {
3214 msglist_T **plist = saved_msg_list;
3215
3216 // Append entries from the current msg_list (uncaught exceptions) to
3217 // the saved msg_list.
3218 while (*plist != NULL)
3219 plist = &(*plist)->next;
3220
3221 *plist = *msg_list;
3222 }
3223 msg_list = saved_msg_list;
3224
Bram Moolenaar02194d22020-10-24 23:08:38 +02003225 if (restore_cmdmod)
3226 {
3227 cmdmod.cmod_filter_regmatch.regprog = NULL;
3228 undo_cmdmod(&cmdmod);
3229 cmdmod = save_cmdmod;
3230 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003231 emsg_silent_def = save_emsg_silent_def;
3232 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003233
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003234failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003235 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3237 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003238
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003239 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003240 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003241
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003242 // Not sure if this is necessary.
3243 suppress_errthrow = save_suppress_errthrow;
3244
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003245 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003246 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003247 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003248 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 return ret;
3250}
3251
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003252/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003253 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003254 * We don't really need this at runtime, but we do have tests that require it,
3255 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003256 */
3257 void
3258ex_disassemble(exarg_T *eap)
3259{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003260 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003261 char_u *fname;
3262 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 dfunc_T *dfunc;
3264 isn_T *instr;
3265 int current;
3266 int line_idx = 0;
3267 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003268 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003270 if (STRNCMP(arg, "<lambda>", 8) == 0)
3271 {
3272 arg += 8;
3273 (void)getdigits(&arg);
3274 fname = vim_strnsave(eap->arg, arg - eap->arg);
3275 }
3276 else
3277 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003278 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003279 if (fname == NULL)
3280 {
3281 semsg(_(e_invarg2), eap->arg);
3282 return;
3283 }
3284
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003285 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003286 if (ufunc == NULL)
3287 {
3288 char_u *p = untrans_function_name(fname);
3289
3290 if (p != NULL)
3291 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003292 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003293 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003294 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003295 if (ufunc == NULL)
3296 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003297 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298 return;
3299 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003300 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003301 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3302 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003303 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003305 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003306 return;
3307 }
3308 if (ufunc->uf_name_exp != NULL)
3309 msg((char *)ufunc->uf_name_exp);
3310 else
3311 msg((char *)ufunc->uf_name);
3312
3313 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3314 instr = dfunc->df_instr;
3315 for (current = 0; current < dfunc->df_instr_count; ++current)
3316 {
3317 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003318 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003319
3320 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3321 {
3322 if (current > prev_current)
3323 {
3324 msg_puts("\n\n");
3325 prev_current = current;
3326 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003327 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3328 if (line != NULL)
3329 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003330 }
3331
3332 switch (iptr->isn_type)
3333 {
3334 case ISN_EXEC:
3335 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3336 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003337 case ISN_EXECCONCAT:
3338 smsg("%4d EXECCONCAT %lld", current,
3339 (long long)iptr->isn_arg.number);
3340 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003341 case ISN_ECHO:
3342 {
3343 echo_T *echo = &iptr->isn_arg.echo;
3344
3345 smsg("%4d %s %d", current,
3346 echo->echo_with_white ? "ECHO" : "ECHON",
3347 echo->echo_count);
3348 }
3349 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003350 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003351 smsg("%4d EXECUTE %lld", current,
3352 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003353 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003354 case ISN_ECHOMSG:
3355 smsg("%4d ECHOMSG %lld", current,
3356 (long long)(iptr->isn_arg.number));
3357 break;
3358 case ISN_ECHOERR:
3359 smsg("%4d ECHOERR %lld", current,
3360 (long long)(iptr->isn_arg.number));
3361 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003363 case ISN_LOADOUTER:
3364 {
3365 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3366
3367 if (iptr->isn_arg.number < 0)
3368 smsg("%4d LOAD%s arg[%lld]", current, add,
3369 (long long)(iptr->isn_arg.number
3370 + STACK_FRAME_SIZE));
3371 else
3372 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003373 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003374 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003375 break;
3376 case ISN_LOADV:
3377 smsg("%4d LOADV v:%s", current,
3378 get_vim_var_name(iptr->isn_arg.number));
3379 break;
3380 case ISN_LOADSCRIPT:
3381 {
3382 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003383 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003384 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3385 + iptr->isn_arg.script.script_idx;
3386
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003387 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3388 sv->sv_name,
3389 iptr->isn_arg.script.script_idx,
3390 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 }
3392 break;
3393 case ISN_LOADS:
3394 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003395 scriptitem_T *si = SCRIPT_ITEM(
3396 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003397
3398 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003399 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003400 }
3401 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003402 case ISN_LOADAUTO:
3403 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 case ISN_LOADG:
3406 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3407 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003408 case ISN_LOADB:
3409 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3410 break;
3411 case ISN_LOADW:
3412 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3413 break;
3414 case ISN_LOADT:
3415 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3416 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003417 case ISN_LOADGDICT:
3418 smsg("%4d LOAD g:", current);
3419 break;
3420 case ISN_LOADBDICT:
3421 smsg("%4d LOAD b:", current);
3422 break;
3423 case ISN_LOADWDICT:
3424 smsg("%4d LOAD w:", current);
3425 break;
3426 case ISN_LOADTDICT:
3427 smsg("%4d LOAD t:", current);
3428 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 case ISN_LOADOPT:
3430 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3431 break;
3432 case ISN_LOADENV:
3433 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3434 break;
3435 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003436 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003437 break;
3438
3439 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003440 case ISN_STOREOUTER:
3441 {
3442 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3443
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003444 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003445 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003446 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003447 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003448 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003449 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003451 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003452 case ISN_STOREV:
3453 smsg("%4d STOREV v:%s", current,
3454 get_vim_var_name(iptr->isn_arg.number));
3455 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003456 case ISN_STOREAUTO:
3457 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003460 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3461 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003462 case ISN_STOREB:
3463 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3464 break;
3465 case ISN_STOREW:
3466 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3467 break;
3468 case ISN_STORET:
3469 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3470 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003471 case ISN_STORES:
3472 {
3473 scriptitem_T *si = SCRIPT_ITEM(
3474 iptr->isn_arg.loadstore.ls_sid);
3475
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003476 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003477 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003478 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 break;
3480 case ISN_STORESCRIPT:
3481 {
3482 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003483 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3485 + iptr->isn_arg.script.script_idx;
3486
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003487 smsg("%4d STORESCRIPT %s-%d in %s", current,
3488 sv->sv_name,
3489 iptr->isn_arg.script.script_idx,
3490 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003491 }
3492 break;
3493 case ISN_STOREOPT:
3494 smsg("%4d STOREOPT &%s", current,
3495 iptr->isn_arg.storeopt.so_name);
3496 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003497 case ISN_STOREENV:
3498 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3499 break;
3500 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003501 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003502 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003503 case ISN_STORENR:
3504 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003505 iptr->isn_arg.storenr.stnr_val,
3506 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003507 break;
3508
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003509 case ISN_STOREINDEX:
3510 switch (iptr->isn_arg.vartype)
3511 {
3512 case VAR_LIST:
3513 smsg("%4d STORELIST", current);
3514 break;
3515 case VAR_DICT:
3516 smsg("%4d STOREDICT", current);
3517 break;
3518 case VAR_ANY:
3519 smsg("%4d STOREINDEX", current);
3520 break;
3521 default: break;
3522 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003523 break;
3524
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003525 // constants
3526 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003527 smsg("%4d PUSHNR %lld", current,
3528 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003529 break;
3530 case ISN_PUSHBOOL:
3531 case ISN_PUSHSPEC:
3532 smsg("%4d PUSH %s", current,
3533 get_var_special_name(iptr->isn_arg.number));
3534 break;
3535 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003536#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003538#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003539 break;
3540 case ISN_PUSHS:
3541 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3542 break;
3543 case ISN_PUSHBLOB:
3544 {
3545 char_u *r;
3546 char_u numbuf[NUMBUFLEN];
3547 char_u *tofree;
3548
3549 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003550 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 vim_free(tofree);
3552 }
3553 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003554 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003555 {
3556 char *name = (char *)iptr->isn_arg.string;
3557
3558 smsg("%4d PUSHFUNC \"%s\"", current,
3559 name == NULL ? "[none]" : name);
3560 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003561 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003562 case ISN_PUSHCHANNEL:
3563#ifdef FEAT_JOB_CHANNEL
3564 {
3565 channel_T *channel = iptr->isn_arg.channel;
3566
3567 smsg("%4d PUSHCHANNEL %d", current,
3568 channel == NULL ? 0 : channel->ch_id);
3569 }
3570#endif
3571 break;
3572 case ISN_PUSHJOB:
3573#ifdef FEAT_JOB_CHANNEL
3574 {
3575 typval_T tv;
3576 char_u *name;
3577
3578 tv.v_type = VAR_JOB;
3579 tv.vval.v_job = iptr->isn_arg.job;
3580 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003581 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003582 }
3583#endif
3584 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 case ISN_PUSHEXC:
3586 smsg("%4d PUSH v:exception", current);
3587 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003588 case ISN_UNLET:
3589 smsg("%4d UNLET%s %s", current,
3590 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3591 iptr->isn_arg.unlet.ul_name);
3592 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003593 case ISN_UNLETENV:
3594 smsg("%4d UNLETENV%s $%s", current,
3595 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3596 iptr->isn_arg.unlet.ul_name);
3597 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003598 case ISN_LOCKCONST:
3599 smsg("%4d LOCKCONST", current);
3600 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003602 smsg("%4d NEWLIST size %lld", current,
3603 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003604 break;
3605 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003606 smsg("%4d NEWDICT size %lld", current,
3607 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003608 break;
3609
3610 // function call
3611 case ISN_BCALL:
3612 {
3613 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3614
3615 smsg("%4d BCALL %s(argc %d)", current,
3616 internal_func_name(cbfunc->cbf_idx),
3617 cbfunc->cbf_argcount);
3618 }
3619 break;
3620 case ISN_DCALL:
3621 {
3622 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3623 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3624 + cdfunc->cdf_idx;
3625
3626 smsg("%4d DCALL %s(argc %d)", current,
3627 df->df_ufunc->uf_name_exp != NULL
3628 ? df->df_ufunc->uf_name_exp
3629 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3630 }
3631 break;
3632 case ISN_UCALL:
3633 {
3634 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3635
3636 smsg("%4d UCALL %s(argc %d)", current,
3637 cufunc->cuf_name, cufunc->cuf_argcount);
3638 }
3639 break;
3640 case ISN_PCALL:
3641 {
3642 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3643
3644 smsg("%4d PCALL%s (argc %d)", current,
3645 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3646 }
3647 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003648 case ISN_PCALL_END:
3649 smsg("%4d PCALL end", current);
3650 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003651 case ISN_RETURN:
3652 smsg("%4d RETURN", current);
3653 break;
3654 case ISN_FUNCREF:
3655 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003656 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003657 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003658 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003659
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003660 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003661 }
3662 break;
3663
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003664 case ISN_NEWFUNC:
3665 {
3666 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3667
3668 smsg("%4d NEWFUNC %s %s", current,
3669 newfunc->nf_lambda, newfunc->nf_global);
3670 }
3671 break;
3672
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003673 case ISN_DEF:
3674 {
3675 char_u *name = iptr->isn_arg.string;
3676
3677 smsg("%4d DEF %s", current,
3678 name == NULL ? (char_u *)"" : name);
3679 }
3680 break;
3681
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003682 case ISN_JUMP:
3683 {
3684 char *when = "?";
3685
3686 switch (iptr->isn_arg.jump.jump_when)
3687 {
3688 case JUMP_ALWAYS:
3689 when = "JUMP";
3690 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 case JUMP_AND_KEEP_IF_TRUE:
3692 when = "JUMP_AND_KEEP_IF_TRUE";
3693 break;
3694 case JUMP_IF_FALSE:
3695 when = "JUMP_IF_FALSE";
3696 break;
3697 case JUMP_AND_KEEP_IF_FALSE:
3698 when = "JUMP_AND_KEEP_IF_FALSE";
3699 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003700 case JUMP_IF_COND_FALSE:
3701 when = "JUMP_IF_COND_FALSE";
3702 break;
3703 case JUMP_IF_COND_TRUE:
3704 when = "JUMP_IF_COND_TRUE";
3705 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003706 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003707 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003708 iptr->isn_arg.jump.jump_where);
3709 }
3710 break;
3711
3712 case ISN_FOR:
3713 {
3714 forloop_T *forloop = &iptr->isn_arg.forloop;
3715
3716 smsg("%4d FOR $%d -> %d", current,
3717 forloop->for_idx, forloop->for_end);
3718 }
3719 break;
3720
3721 case ISN_TRY:
3722 {
3723 try_T *try = &iptr->isn_arg.try;
3724
3725 smsg("%4d TRY catch -> %d, finally -> %d", current,
3726 try->try_catch, try->try_finally);
3727 }
3728 break;
3729 case ISN_CATCH:
3730 // TODO
3731 smsg("%4d CATCH", current);
3732 break;
3733 case ISN_ENDTRY:
3734 smsg("%4d ENDTRY", current);
3735 break;
3736 case ISN_THROW:
3737 smsg("%4d THROW", current);
3738 break;
3739
3740 // expression operations on number
3741 case ISN_OPNR:
3742 case ISN_OPFLOAT:
3743 case ISN_OPANY:
3744 {
3745 char *what;
3746 char *ins;
3747
3748 switch (iptr->isn_arg.op.op_type)
3749 {
3750 case EXPR_MULT: what = "*"; break;
3751 case EXPR_DIV: what = "/"; break;
3752 case EXPR_REM: what = "%"; break;
3753 case EXPR_SUB: what = "-"; break;
3754 case EXPR_ADD: what = "+"; break;
3755 default: what = "???"; break;
3756 }
3757 switch (iptr->isn_type)
3758 {
3759 case ISN_OPNR: ins = "OPNR"; break;
3760 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3761 case ISN_OPANY: ins = "OPANY"; break;
3762 default: ins = "???"; break;
3763 }
3764 smsg("%4d %s %s", current, ins, what);
3765 }
3766 break;
3767
3768 case ISN_COMPAREBOOL:
3769 case ISN_COMPARESPECIAL:
3770 case ISN_COMPARENR:
3771 case ISN_COMPAREFLOAT:
3772 case ISN_COMPARESTRING:
3773 case ISN_COMPAREBLOB:
3774 case ISN_COMPARELIST:
3775 case ISN_COMPAREDICT:
3776 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 case ISN_COMPAREANY:
3778 {
3779 char *p;
3780 char buf[10];
3781 char *type;
3782
3783 switch (iptr->isn_arg.op.op_type)
3784 {
3785 case EXPR_EQUAL: p = "=="; break;
3786 case EXPR_NEQUAL: p = "!="; break;
3787 case EXPR_GREATER: p = ">"; break;
3788 case EXPR_GEQUAL: p = ">="; break;
3789 case EXPR_SMALLER: p = "<"; break;
3790 case EXPR_SEQUAL: p = "<="; break;
3791 case EXPR_MATCH: p = "=~"; break;
3792 case EXPR_IS: p = "is"; break;
3793 case EXPR_ISNOT: p = "isnot"; break;
3794 case EXPR_NOMATCH: p = "!~"; break;
3795 default: p = "???"; break;
3796 }
3797 STRCPY(buf, p);
3798 if (iptr->isn_arg.op.op_ic == TRUE)
3799 strcat(buf, "?");
3800 switch(iptr->isn_type)
3801 {
3802 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3803 case ISN_COMPARESPECIAL:
3804 type = "COMPARESPECIAL"; break;
3805 case ISN_COMPARENR: type = "COMPARENR"; break;
3806 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3807 case ISN_COMPARESTRING:
3808 type = "COMPARESTRING"; break;
3809 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3810 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3811 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3812 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003813 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3814 default: type = "???"; break;
3815 }
3816
3817 smsg("%4d %s %s", current, type, buf);
3818 }
3819 break;
3820
3821 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3822 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3823
3824 // expression operations
3825 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003826 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003827 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003828 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003829 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003830 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003831 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003832 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3833 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003834 case ISN_SLICE: smsg("%4d SLICE %lld",
3835 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003836 case ISN_GETITEM: smsg("%4d ITEM %lld",
3837 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003838 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3839 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003840 iptr->isn_arg.string); break;
3841 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3842
3843 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003844 case ISN_CHECKTYPE:
3845 {
3846 char *tofree;
3847
3848 smsg("%4d CHECKTYPE %s stack[%d]", current,
3849 type_name(iptr->isn_arg.type.ct_type, &tofree),
3850 iptr->isn_arg.type.ct_off);
3851 vim_free(tofree);
3852 break;
3853 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003854 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3855 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3856 iptr->isn_arg.checklen.cl_min_len);
3857 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003858 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859 case ISN_2BOOL: if (iptr->isn_arg.number)
3860 smsg("%4d INVERT (!val)", current);
3861 else
3862 smsg("%4d 2BOOL (!!val)", current);
3863 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003864 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3865 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003866 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003867 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3868 (long long)(iptr->isn_arg.number));
3869 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003870 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3871 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003872 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003873 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3874 smsg("%4d PUT %c above range",
3875 current, iptr->isn_arg.put.put_regname);
3876 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3877 smsg("%4d PUT %c range",
3878 current, iptr->isn_arg.put.put_regname);
3879 else
3880 smsg("%4d PUT %c %ld", current,
3881 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003882 (long)iptr->isn_arg.put.put_lnum);
3883 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884
Bram Moolenaar02194d22020-10-24 23:08:38 +02003885 // TODO: summarize modifiers
3886 case ISN_CMDMOD:
3887 {
3888 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003889 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003890 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3891
3892 buf = alloc(len + 1);
3893 if (buf != NULL)
3894 {
3895 (void)produce_cmdmods(
3896 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3897 smsg("%4d CMDMOD %s", current, buf);
3898 vim_free(buf);
3899 }
3900 break;
3901 }
3902 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003903
Bram Moolenaar792f7862020-11-23 08:31:18 +01003904 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3905 iptr->isn_arg.unpack.unp_count,
3906 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3907 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003908 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3909 iptr->isn_arg.shuffle.shfl_item,
3910 iptr->isn_arg.shuffle.shfl_up);
3911 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 case ISN_DROP: smsg("%4d DROP", current); break;
3913 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003914
3915 out_flush(); // output one line at a time
3916 ui_breakcheck();
3917 if (got_int)
3918 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003920}
3921
3922/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003923 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 * list, etc. Mostly like what JavaScript does, except that empty list and
3925 * empty dictionary are FALSE.
3926 */
3927 int
3928tv2bool(typval_T *tv)
3929{
3930 switch (tv->v_type)
3931 {
3932 case VAR_NUMBER:
3933 return tv->vval.v_number != 0;
3934 case VAR_FLOAT:
3935#ifdef FEAT_FLOAT
3936 return tv->vval.v_float != 0.0;
3937#else
3938 break;
3939#endif
3940 case VAR_PARTIAL:
3941 return tv->vval.v_partial != NULL;
3942 case VAR_FUNC:
3943 case VAR_STRING:
3944 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3945 case VAR_LIST:
3946 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3947 case VAR_DICT:
3948 return tv->vval.v_dict != NULL
3949 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3950 case VAR_BOOL:
3951 case VAR_SPECIAL:
3952 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3953 case VAR_JOB:
3954#ifdef FEAT_JOB_CHANNEL
3955 return tv->vval.v_job != NULL;
3956#else
3957 break;
3958#endif
3959 case VAR_CHANNEL:
3960#ifdef FEAT_JOB_CHANNEL
3961 return tv->vval.v_channel != NULL;
3962#else
3963 break;
3964#endif
3965 case VAR_BLOB:
3966 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3967 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003968 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 case VAR_VOID:
3970 break;
3971 }
3972 return FALSE;
3973}
3974
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003975 void
3976emsg_using_string_as(typval_T *tv, int as_number)
3977{
3978 semsg(_(as_number ? e_using_string_as_number_str
3979 : e_using_string_as_bool_str),
3980 tv->vval.v_string == NULL
3981 ? (char_u *)"" : tv->vval.v_string);
3982}
3983
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984/*
3985 * If "tv" is a string give an error and return FAIL.
3986 */
3987 int
3988check_not_string(typval_T *tv)
3989{
3990 if (tv->v_type == VAR_STRING)
3991 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003992 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003993 clear_tv(tv);
3994 return FAIL;
3995 }
3996 return OK;
3997}
3998
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003999
4000#endif // FEAT_EVAL