blob: db15cea4cda0401fad339cb10bc952708999920b [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/*
805 * When the value of "sv" is a null list of dict, allocate it.
806 */
807 static void
808allocate_if_null(typval_T *tv)
809{
810 switch (tv->v_type)
811 {
812 case VAR_LIST:
813 if (tv->vval.v_list == NULL)
814 rettv_list_alloc(tv);
815 break;
816 case VAR_DICT:
817 if (tv->vval.v_dict == NULL)
818 rettv_dict_alloc(tv);
819 break;
820 default:
821 break;
822 }
823}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200824
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100825/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100826 * Execute a function by "name".
827 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100828 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829 */
830 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100831call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832{
Bram Moolenaared677f52020-08-12 16:38:10 +0200833 int called_emsg_before = called_emsg;
834 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
Bram Moolenaared677f52020-08-12 16:38:10 +0200836 res = call_by_name(name, argcount, ectx, iptr);
837 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200839 dictitem_T *v;
840
841 v = find_var(name, NULL, FALSE);
842 if (v == NULL)
843 {
844 semsg(_(e_unknownfunc), name);
845 return FAIL;
846 }
847 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
848 {
849 semsg(_(e_unknownfunc), name);
850 return FAIL;
851 }
852 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200854 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100855}
856
857/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100858 * When a function reference is used, fill a partial with the information
859 * needed, especially when it is used as a closure.
860 */
861 static int
862fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
863{
864 pt->pt_func = ufunc;
865 pt->pt_refcount = 1;
866
867 if (pt->pt_func->uf_flags & FC_CLOSURE)
868 {
869 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
870 + ectx->ec_dfunc_idx;
871
872 // The closure needs to find arguments and local
873 // variables in the current stack.
874 pt->pt_ectx_stack = &ectx->ec_stack;
875 pt->pt_ectx_frame = ectx->ec_frame_idx;
876
877 // If this function returns and the closure is still
878 // being used, we need to make a copy of the context
879 // (arguments and local variables). Store a reference
880 // to the partial so we can handle that.
881 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
882 {
883 vim_free(pt);
884 return FAIL;
885 }
886 // Extra variable keeps the count of closures created
887 // in the current function call.
888 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
889 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
890
891 ((partial_T **)ectx->ec_funcrefs.ga_data)
892 [ectx->ec_funcrefs.ga_len] = pt;
893 ++pt->pt_refcount;
894 ++ectx->ec_funcrefs.ga_len;
895 }
896 ++pt->pt_func->uf_refcount;
897 return OK;
898}
899
900/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100901 * Call a "def" function from old Vim script.
902 * Return OK or FAIL.
903 */
904 int
905call_def_function(
906 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200907 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100908 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200909 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100910 typval_T *rettv) // return value
911{
912 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200913 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200914 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100915 typval_T *tv;
916 int idx;
917 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100918 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200919 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200920 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100921 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200922 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200923 msglist_T **saved_msg_list = NULL;
924 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200925 cmdmod_T save_cmdmod;
926 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100927 int save_emsg_silent_def = emsg_silent_def;
928 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100929 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100930 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100931
932// Get pointer to item in the stack.
933#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
934
935// Get pointer to item at the bottom of the stack, -1 is the bottom.
936#undef STACK_TV_BOT
937#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
938
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200939// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200940#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 +0100941
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200942// Like STACK_TV_VAR but use the outer scope
943#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
944
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200945 if (ufunc->uf_def_status == UF_NOT_COMPILED
946 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200947 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200948 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100949 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200950 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200951 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200952 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200953 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200954
Bram Moolenaar09689a02020-05-09 22:50:08 +0200955 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200956 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200957 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
958 + ufunc->uf_dfunc_idx;
959 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200960 {
961 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +0200962 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +0200963 }
Bram Moolenaar09689a02020-05-09 22:50:08 +0200964 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100965
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100966 // If depth of calling is getting too high, don't execute the function.
967 orig_funcdepth = funcdepth_get();
968 if (funcdepth_increment() == FAIL)
969 return FAIL;
970
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200971 CLEAR_FIELD(ectx);
972 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
973 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
974 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100975 {
976 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +0200977 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100979 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200980 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100981
982 // Put arguments on the stack.
983 for (idx = 0; idx < argc; ++idx)
984 {
Bram Moolenaar65b95452020-07-19 14:03:09 +0200985 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +0200986 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
987 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +0200988 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100989 copy_tv(&argv[idx], STACK_TV_BOT(0));
990 ++ectx.ec_stack.ga_len;
991 }
Bram Moolenaar23e03252020-04-12 22:22:31 +0200992
993 // Turn varargs into a list. Empty list if no args.
994 if (ufunc->uf_va_name != NULL)
995 {
996 int vararg_count = argc - ufunc->uf_args.ga_len;
997
998 if (vararg_count < 0)
999 vararg_count = 0;
1000 else
1001 argc -= vararg_count;
1002 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001003 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001004
1005 // Check the type of the list items.
1006 tv = STACK_TV_BOT(-1);
1007 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001008 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001009 && ufunc->uf_va_type->tt_member != &t_any
1010 && tv->vval.v_list != NULL)
1011 {
1012 type_T *expected = ufunc->uf_va_type->tt_member;
1013 listitem_T *li = tv->vval.v_list->lv_first;
1014
1015 for (idx = 0; idx < vararg_count; ++idx)
1016 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001017 if (check_typval_type(expected, &li->li_tv,
1018 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001019 goto failed_early;
1020 li = li->li_next;
1021 }
1022 }
1023
Bram Moolenaar23e03252020-04-12 22:22:31 +02001024 if (defcount > 0)
1025 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001026 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001027 --ectx.ec_stack.ga_len;
1028 }
1029
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001030 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001031 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001032 if (defcount > 0)
1033 for (idx = 0; idx < defcount; ++idx)
1034 {
1035 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1036 ++ectx.ec_stack.ga_len;
1037 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001038 if (ufunc->uf_va_name != NULL)
1039 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040
1041 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001042 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1043 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001045 if (partial != NULL)
1046 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001047 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1048 {
1049 // TODO: is this always the right way?
1050 ectx.ec_outer_stack = &current_ectx->ec_stack;
1051 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1052 }
1053 else
1054 {
1055 ectx.ec_outer_stack = partial->pt_ectx_stack;
1056 ectx.ec_outer_frame = partial->pt_ectx_frame;
1057 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001058 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001059 else if (ufunc->uf_partial != NULL)
1060 {
1061 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1062 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1063 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001064
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001065 // dummy frame entries
1066 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1067 {
1068 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1069 ++ectx.ec_stack.ga_len;
1070 }
1071
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001072 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001073 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001074 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1075 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001076
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001077 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001078 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001079 ectx.ec_stack.ga_len += dfunc->df_varcount;
1080 if (dfunc->df_has_closure)
1081 {
1082 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1083 STACK_TV_VAR(idx)->vval.v_number = 0;
1084 ++ectx.ec_stack.ga_len;
1085 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001086
1087 ectx.ec_instr = dfunc->df_instr;
1088 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001089
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001090 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001091 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001092 estack_push_ufunc(ufunc, 1);
1093 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001094 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1095
Bram Moolenaar352134b2020-10-17 22:04:08 +02001096 // Use a specific location for storing error messages to be converted to an
1097 // exception.
1098 saved_msg_list = msg_list;
1099 msg_list = &private_msg_list;
1100
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001101 // Do turn errors into exceptions.
1102 suppress_errthrow = FALSE;
1103
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001104 // When ":silent!" was used before calling then we still abort the
1105 // function. If ":silent!" is used in the function then we don't.
1106 emsg_silent_def = emsg_silent;
1107 did_emsg_def = 0;
1108
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001109 // Decide where to start execution, handles optional arguments.
1110 init_instr_idx(ufunc, argc, &ectx);
1111
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001112 for (;;)
1113 {
1114 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001115
Bram Moolenaar270d0382020-05-15 21:42:53 +02001116 if (++breakcheck_count >= 100)
1117 {
1118 line_breakcheck();
1119 breakcheck_count = 0;
1120 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001121 if (got_int)
1122 {
1123 // Turn CTRL-C into an exception.
1124 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001125 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001126 goto failed;
1127 did_throw = TRUE;
1128 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129
Bram Moolenaara26b9702020-04-18 19:53:28 +02001130 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1131 {
1132 // Turn an error message into an exception.
1133 did_emsg = FALSE;
1134 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1135 goto failed;
1136 did_throw = TRUE;
1137 *msg_list = NULL;
1138 }
1139
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 if (did_throw && !ectx.ec_in_catch)
1141 {
1142 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001143 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001144
1145 // An exception jumps to the first catch, finally, or returns from
1146 // the current function.
1147 if (trystack->ga_len > 0)
1148 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001149 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 {
1151 // jump to ":catch" or ":finally"
1152 ectx.ec_in_catch = TRUE;
1153 ectx.ec_iidx = trycmd->tcd_catch_idx;
1154 }
1155 else
1156 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001157 // Not inside try or need to return from current functions.
1158 // Push a dummy return value.
1159 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1160 goto failed;
1161 tv = STACK_TV_BOT(0);
1162 tv->v_type = VAR_NUMBER;
1163 tv->vval.v_number = 0;
1164 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001165 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001166 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001167 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001168 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001169 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1170 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 goto done;
1172 }
1173
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001174 if (func_return(&ectx) == FAIL)
1175 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001176 }
1177 continue;
1178 }
1179
1180 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1181 switch (iptr->isn_type)
1182 {
1183 // execute Ex command line
1184 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001185 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001186 SOURCING_LNUM = iptr->isn_lnum;
1187 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001188 if (did_emsg)
1189 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001190 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 break;
1192
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001193 // execute Ex command from pieces on the stack
1194 case ISN_EXECCONCAT:
1195 {
1196 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001197 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001198 int pass;
1199 int i;
1200 char_u *cmd = NULL;
1201 char_u *str;
1202
1203 for (pass = 1; pass <= 2; ++pass)
1204 {
1205 for (i = 0; i < count; ++i)
1206 {
1207 tv = STACK_TV_BOT(i - count);
1208 str = tv->vval.v_string;
1209 if (str != NULL && *str != NUL)
1210 {
1211 if (pass == 2)
1212 STRCPY(cmd + len, str);
1213 len += STRLEN(str);
1214 }
1215 if (pass == 2)
1216 clear_tv(tv);
1217 }
1218 if (pass == 1)
1219 {
1220 cmd = alloc(len + 1);
1221 if (cmd == NULL)
1222 goto failed;
1223 len = 0;
1224 }
1225 }
1226
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001227 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001228 do_cmdline_cmd(cmd);
1229 vim_free(cmd);
1230 }
1231 break;
1232
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233 // execute :echo {string} ...
1234 case ISN_ECHO:
1235 {
1236 int count = iptr->isn_arg.echo.echo_count;
1237 int atstart = TRUE;
1238 int needclr = TRUE;
1239
1240 for (idx = 0; idx < count; ++idx)
1241 {
1242 tv = STACK_TV_BOT(idx - count);
1243 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1244 &atstart, &needclr);
1245 clear_tv(tv);
1246 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001247 if (needclr)
1248 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 ectx.ec_stack.ga_len -= count;
1250 }
1251 break;
1252
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001253 // :execute {string} ...
1254 // :echomsg {string} ...
1255 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001256 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001257 case ISN_ECHOMSG:
1258 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001259 {
1260 int count = iptr->isn_arg.number;
1261 garray_T ga;
1262 char_u buf[NUMBUFLEN];
1263 char_u *p;
1264 int len;
1265 int failed = FALSE;
1266
1267 ga_init2(&ga, 1, 80);
1268 for (idx = 0; idx < count; ++idx)
1269 {
1270 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001271 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001272 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001273 if (tv->v_type == VAR_CHANNEL
1274 || tv->v_type == VAR_JOB)
1275 {
1276 SOURCING_LNUM = iptr->isn_lnum;
1277 emsg(_(e_inval_string));
1278 break;
1279 }
1280 else
1281 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001282 }
1283 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001284 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001285
1286 len = (int)STRLEN(p);
1287 if (ga_grow(&ga, len + 2) == FAIL)
1288 failed = TRUE;
1289 else
1290 {
1291 if (ga.ga_len > 0)
1292 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1293 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1294 ga.ga_len += len;
1295 }
1296 clear_tv(tv);
1297 }
1298 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001299 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001300 {
1301 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001302 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001303 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001304
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001305 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001306 {
1307 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001308 {
1309 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001310 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001311 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001312 {
1313 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001314 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001315 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001316 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001317 else
1318 {
1319 msg_sb_eol();
1320 if (iptr->isn_type == ISN_ECHOMSG)
1321 {
1322 msg_attr(ga.ga_data, echo_attr);
1323 out_flush();
1324 }
1325 else
1326 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001327 SOURCING_LNUM = iptr->isn_lnum;
1328 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001329 }
1330 }
1331 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001332 ga_clear(&ga);
1333 }
1334 break;
1335
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001336 // load local variable or argument
1337 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001338 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 goto failed;
1340 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1341 ++ectx.ec_stack.ga_len;
1342 break;
1343
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001344 // load variable or argument from outer scope
1345 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001346 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001347 goto failed;
1348 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1349 STACK_TV_BOT(0));
1350 ++ectx.ec_stack.ga_len;
1351 break;
1352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001353 // load v: variable
1354 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001355 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356 goto failed;
1357 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1358 ++ectx.ec_stack.ga_len;
1359 break;
1360
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001361 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001362 case ISN_LOADSCRIPT:
1363 {
1364 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001365 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 svar_T *sv;
1367
1368 sv = ((svar_T *)si->sn_var_vals.ga_data)
1369 + iptr->isn_arg.script.script_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001370 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001371 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 goto failed;
1373 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1374 ++ectx.ec_stack.ga_len;
1375 }
1376 break;
1377
1378 // load s: variable in old script
1379 case ISN_LOADS:
1380 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001381 hashtab_T *ht = &SCRIPT_VARS(
1382 iptr->isn_arg.loadstore.ls_sid);
1383 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001384 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001386 if (di == NULL)
1387 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001388 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001389 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001391 }
1392 else
1393 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001394 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001395 goto failed;
1396 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1397 ++ectx.ec_stack.ga_len;
1398 }
1399 }
1400 break;
1401
Bram Moolenaard3aac292020-04-19 14:32:17 +02001402 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001404 case ISN_LOADB:
1405 case ISN_LOADW:
1406 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001408 dictitem_T *di = NULL;
1409 hashtab_T *ht = NULL;
1410 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001411
Bram Moolenaard3aac292020-04-19 14:32:17 +02001412 switch (iptr->isn_type)
1413 {
1414 case ISN_LOADG:
1415 ht = get_globvar_ht();
1416 namespace = 'g';
1417 break;
1418 case ISN_LOADB:
1419 ht = &curbuf->b_vars->dv_hashtab;
1420 namespace = 'b';
1421 break;
1422 case ISN_LOADW:
1423 ht = &curwin->w_vars->dv_hashtab;
1424 namespace = 'w';
1425 break;
1426 case ISN_LOADT:
1427 ht = &curtab->tp_vars->dv_hashtab;
1428 namespace = 't';
1429 break;
1430 default: // Cannot reach here
1431 goto failed;
1432 }
1433 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001434
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001435 if (di == NULL)
1436 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001438 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001439 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001440 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001441 }
1442 else
1443 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001444 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001445 goto failed;
1446 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1447 ++ectx.ec_stack.ga_len;
1448 }
1449 }
1450 break;
1451
Bram Moolenaar03290b82020-12-19 16:30:44 +01001452 // load autoload variable
1453 case ISN_LOADAUTO:
1454 {
1455 char_u *name = iptr->isn_arg.string;
1456
1457 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1458 goto failed;
1459 SOURCING_LNUM = iptr->isn_lnum;
1460 if (eval_variable(name, STRLEN(name),
1461 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1462 goto on_error;
1463 ++ectx.ec_stack.ga_len;
1464 }
1465 break;
1466
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001467 // load g:/b:/w:/t: namespace
1468 case ISN_LOADGDICT:
1469 case ISN_LOADBDICT:
1470 case ISN_LOADWDICT:
1471 case ISN_LOADTDICT:
1472 {
1473 dict_T *d = NULL;
1474
1475 switch (iptr->isn_type)
1476 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001477 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1478 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1479 case ISN_LOADWDICT: d = curwin->w_vars; break;
1480 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001481 default: // Cannot reach here
1482 goto failed;
1483 }
1484 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1485 goto failed;
1486 tv = STACK_TV_BOT(0);
1487 tv->v_type = VAR_DICT;
1488 tv->v_lock = 0;
1489 tv->vval.v_dict = d;
1490 ++ectx.ec_stack.ga_len;
1491 }
1492 break;
1493
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 // load &option
1495 case ISN_LOADOPT:
1496 {
1497 typval_T optval;
1498 char_u *name = iptr->isn_arg.string;
1499
Bram Moolenaara8c17702020-04-01 21:17:24 +02001500 // This is not expected to fail, name is checked during
1501 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001502 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001503 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001504 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001505 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001506 *STACK_TV_BOT(0) = optval;
1507 ++ectx.ec_stack.ga_len;
1508 }
1509 break;
1510
1511 // load $ENV
1512 case ISN_LOADENV:
1513 {
1514 typval_T optval;
1515 char_u *name = iptr->isn_arg.string;
1516
Bram Moolenaar270d0382020-05-15 21:42:53 +02001517 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001518 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001519 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001520 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001521 *STACK_TV_BOT(0) = optval;
1522 ++ectx.ec_stack.ga_len;
1523 }
1524 break;
1525
1526 // load @register
1527 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001528 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001529 goto failed;
1530 tv = STACK_TV_BOT(0);
1531 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001532 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001533 // This may result in NULL, which should be equivalent to an
1534 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001535 tv->vval.v_string = get_reg_contents(
1536 iptr->isn_arg.number, GREG_EXPR_SRC);
1537 ++ectx.ec_stack.ga_len;
1538 break;
1539
1540 // store local variable
1541 case ISN_STORE:
1542 --ectx.ec_stack.ga_len;
1543 tv = STACK_TV_VAR(iptr->isn_arg.number);
1544 clear_tv(tv);
1545 *tv = *STACK_TV_BOT(0);
1546 break;
1547
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001548 // store variable or argument in outer scope
1549 case ISN_STOREOUTER:
1550 --ectx.ec_stack.ga_len;
1551 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1552 clear_tv(tv);
1553 *tv = *STACK_TV_BOT(0);
1554 break;
1555
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001556 // store s: variable in old script
1557 case ISN_STORES:
1558 {
1559 hashtab_T *ht = &SCRIPT_VARS(
1560 iptr->isn_arg.loadstore.ls_sid);
1561 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001562 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001563
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001564 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001565 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001566 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001567 else
1568 {
1569 clear_tv(&di->di_tv);
1570 di->di_tv = *STACK_TV_BOT(0);
1571 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001572 }
1573 break;
1574
1575 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 case ISN_STORESCRIPT:
1577 {
Bram Moolenaar21b9e972020-01-26 19:26:46 +01001578 scriptitem_T *si = SCRIPT_ITEM(
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 iptr->isn_arg.script.script_sid);
1580 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
1581 + iptr->isn_arg.script.script_idx;
1582
1583 --ectx.ec_stack.ga_len;
1584 clear_tv(sv->sv_tv);
1585 *sv->sv_tv = *STACK_TV_BOT(0);
1586 }
1587 break;
1588
1589 // store option
1590 case ISN_STOREOPT:
1591 {
1592 long n = 0;
1593 char_u *s = NULL;
1594 char *msg;
1595
1596 --ectx.ec_stack.ga_len;
1597 tv = STACK_TV_BOT(0);
1598 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001599 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001600 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001601 if (s == NULL)
1602 s = (char_u *)"";
1603 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001605 // must be VAR_NUMBER, CHECKTYPE makes sure
1606 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001607 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1608 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001609 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001610 if (msg != NULL)
1611 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001612 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001614 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 }
1617 break;
1618
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001619 // store $ENV
1620 case ISN_STOREENV:
1621 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001622 tv = STACK_TV_BOT(0);
1623 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1624 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001625 break;
1626
1627 // store @r
1628 case ISN_STOREREG:
1629 {
1630 int reg = iptr->isn_arg.number;
1631
1632 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001633 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001634 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001635 tv_get_string(tv), -1, FALSE);
1636 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001637 }
1638 break;
1639
1640 // store v: variable
1641 case ISN_STOREV:
1642 --ectx.ec_stack.ga_len;
1643 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1644 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001645 // should not happen, type is checked when compiling
1646 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001647 break;
1648
Bram Moolenaard3aac292020-04-19 14:32:17 +02001649 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001650 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001651 case ISN_STOREB:
1652 case ISN_STOREW:
1653 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 {
1655 dictitem_T *di;
Bram Moolenaard3aac292020-04-19 14:32:17 +02001656 hashtab_T *ht;
1657 switch (iptr->isn_type)
1658 {
1659 case ISN_STOREG:
1660 ht = get_globvar_ht();
1661 break;
1662 case ISN_STOREB:
1663 ht = &curbuf->b_vars->dv_hashtab;
1664 break;
1665 case ISN_STOREW:
1666 ht = &curwin->w_vars->dv_hashtab;
1667 break;
1668 case ISN_STORET:
1669 ht = &curtab->tp_vars->dv_hashtab;
1670 break;
1671 default: // Cannot reach here
1672 goto failed;
1673 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674
1675 --ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001676 di = find_var_in_ht(ht, 0, iptr->isn_arg.string + 2, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001678 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 else
1680 {
1681 clear_tv(&di->di_tv);
1682 di->di_tv = *STACK_TV_BOT(0);
1683 }
1684 }
1685 break;
1686
Bram Moolenaar03290b82020-12-19 16:30:44 +01001687 // store an autoload variable
1688 case ISN_STOREAUTO:
1689 SOURCING_LNUM = iptr->isn_lnum;
1690 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1691 clear_tv(STACK_TV_BOT(-1));
1692 --ectx.ec_stack.ga_len;
1693 break;
1694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 // store number in local variable
1696 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001697 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001698 clear_tv(tv);
1699 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001700 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 break;
1702
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001703 // store value in list variable
1704 case ISN_STORELIST:
1705 {
1706 typval_T *tv_idx = STACK_TV_BOT(-2);
1707 varnumber_T lidx = tv_idx->vval.v_number;
1708 typval_T *tv_list = STACK_TV_BOT(-1);
1709 list_T *list = tv_list->vval.v_list;
1710
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001711 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001712 if (lidx < 0 && list->lv_len + lidx >= 0)
1713 // negative index is relative to the end
1714 lidx = list->lv_len + lidx;
1715 if (lidx < 0 || lidx > list->lv_len)
1716 {
1717 semsg(_(e_listidx), lidx);
Bram Moolenaare8593122020-07-18 15:17:02 +02001718 goto on_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001719 }
1720 tv = STACK_TV_BOT(-3);
1721 if (lidx < list->lv_len)
1722 {
1723 listitem_T *li = list_find(list, lidx);
1724
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001725 if (error_if_locked(li->li_tv.v_lock,
1726 e_cannot_change_list_item))
1727 goto failed;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001728 // overwrite existing list item
1729 clear_tv(&li->li_tv);
1730 li->li_tv = *tv;
1731 }
1732 else
1733 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001734 if (error_if_locked(list->lv_lock,
1735 e_cannot_change_list))
1736 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001737 // append to list, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001738 if (list_append_tv(list, tv) == FAIL)
1739 goto failed;
1740 clear_tv(tv);
1741 }
1742 clear_tv(tv_idx);
1743 clear_tv(tv_list);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001744 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001745 }
1746 break;
1747
1748 // store value in dict variable
1749 case ISN_STOREDICT:
1750 {
1751 typval_T *tv_key = STACK_TV_BOT(-2);
1752 char_u *key = tv_key->vval.v_string;
1753 typval_T *tv_dict = STACK_TV_BOT(-1);
1754 dict_T *dict = tv_dict->vval.v_dict;
1755 dictitem_T *di;
1756
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001757 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001758 if (dict == NULL)
1759 {
Bram Moolenaarbc4c5052020-08-13 22:47:35 +02001760 emsg(_(e_dictionary_not_set));
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001761 goto on_error;
1762 }
Bram Moolenaar58626872020-08-01 14:06:38 +02001763 if (key == NULL)
1764 key = (char_u *)"";
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001765 tv = STACK_TV_BOT(-3);
1766 di = dict_find(dict, key, -1);
1767 if (di != NULL)
1768 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001769 if (error_if_locked(di->di_tv.v_lock,
1770 e_cannot_change_dict_item))
1771 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001772 // overwrite existing value
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001773 clear_tv(&di->di_tv);
1774 di->di_tv = *tv;
1775 }
1776 else
1777 {
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001778 if (error_if_locked(dict->dv_lock,
1779 e_cannot_change_dict))
1780 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001781 // add to dict, only fails when out of memory
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001782 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1783 goto failed;
1784 clear_tv(tv);
1785 }
1786 clear_tv(tv_key);
1787 clear_tv(tv_dict);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001788 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001789 }
1790 break;
1791
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001792 // push constant
1793 case ISN_PUSHNR:
1794 case ISN_PUSHBOOL:
1795 case ISN_PUSHSPEC:
1796 case ISN_PUSHF:
1797 case ISN_PUSHS:
1798 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001799 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001800 case ISN_PUSHCHANNEL:
1801 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001802 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 goto failed;
1804 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001805 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001806 ++ectx.ec_stack.ga_len;
1807 switch (iptr->isn_type)
1808 {
1809 case ISN_PUSHNR:
1810 tv->v_type = VAR_NUMBER;
1811 tv->vval.v_number = iptr->isn_arg.number;
1812 break;
1813 case ISN_PUSHBOOL:
1814 tv->v_type = VAR_BOOL;
1815 tv->vval.v_number = iptr->isn_arg.number;
1816 break;
1817 case ISN_PUSHSPEC:
1818 tv->v_type = VAR_SPECIAL;
1819 tv->vval.v_number = iptr->isn_arg.number;
1820 break;
1821#ifdef FEAT_FLOAT
1822 case ISN_PUSHF:
1823 tv->v_type = VAR_FLOAT;
1824 tv->vval.v_float = iptr->isn_arg.fnumber;
1825 break;
1826#endif
1827 case ISN_PUSHBLOB:
1828 blob_copy(iptr->isn_arg.blob, tv);
1829 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001830 case ISN_PUSHFUNC:
1831 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001832 if (iptr->isn_arg.string == NULL)
1833 tv->vval.v_string = NULL;
1834 else
1835 tv->vval.v_string =
1836 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001837 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001838 case ISN_PUSHCHANNEL:
1839#ifdef FEAT_JOB_CHANNEL
1840 tv->v_type = VAR_CHANNEL;
1841 tv->vval.v_channel = iptr->isn_arg.channel;
1842 if (tv->vval.v_channel != NULL)
1843 ++tv->vval.v_channel->ch_refcount;
1844#endif
1845 break;
1846 case ISN_PUSHJOB:
1847#ifdef FEAT_JOB_CHANNEL
1848 tv->v_type = VAR_JOB;
1849 tv->vval.v_job = iptr->isn_arg.job;
1850 if (tv->vval.v_job != NULL)
1851 ++tv->vval.v_job->jv_refcount;
1852#endif
1853 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001854 default:
1855 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001856 tv->vval.v_string = vim_strsave(
1857 iptr->isn_arg.string == NULL
1858 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001859 }
1860 break;
1861
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001862 case ISN_UNLET:
1863 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1864 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001865 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001866 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001867 case ISN_UNLETENV:
1868 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1869 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001870
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001871 case ISN_LOCKCONST:
1872 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1873 break;
1874
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 // create a list from items on the stack; uses a single allocation
1876 // for the list header and the items
1877 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001878 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1879 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 break;
1881
1882 // create a dict from items on the stack
1883 case ISN_NEWDICT:
1884 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001885 int count = iptr->isn_arg.number;
1886 dict_T *dict = dict_alloc();
1887 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001888 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001889
1890 if (dict == NULL)
1891 goto failed;
1892 for (idx = 0; idx < count; ++idx)
1893 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001894 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001896 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001897 key = tv->vval.v_string == NULL
1898 ? (char_u *)"" : tv->vval.v_string;
1899 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02001900 if (item != NULL)
1901 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001902 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001903 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02001904 dict_unref(dict);
1905 goto on_error;
1906 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001907 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 clear_tv(tv);
1909 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001910 {
1911 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001912 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
1915 item->di_tv.v_lock = 0;
1916 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001917 {
Bram Moolenaard032f342020-07-18 18:13:02 +02001918 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02001919 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02001921 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001922 }
1923
1924 if (count > 0)
1925 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001926 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 goto failed;
1928 else
1929 ++ectx.ec_stack.ga_len;
1930 tv = STACK_TV_BOT(-1);
1931 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001932 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001933 tv->vval.v_dict = dict;
1934 ++dict->dv_refcount;
1935 }
1936 break;
1937
1938 // call a :def function
1939 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02001940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001941 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
1942 iptr->isn_arg.dfunc.cdf_argcount,
1943 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001944 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001945 break;
1946
1947 // call a builtin function
1948 case ISN_BCALL:
1949 SOURCING_LNUM = iptr->isn_lnum;
1950 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
1951 iptr->isn_arg.bfunc.cbf_argcount,
1952 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001953 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001954 break;
1955
1956 // call a funcref or partial
1957 case ISN_PCALL:
1958 {
1959 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
1960 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001961 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962
1963 SOURCING_LNUM = iptr->isn_lnum;
1964 if (pfunc->cpf_top)
1965 {
1966 // funcref is above the arguments
1967 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
1968 }
1969 else
1970 {
1971 // Get the funcref from the stack.
1972 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001973 partial_tv = *STACK_TV_BOT(0);
1974 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001975 }
1976 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001977 if (tv == &partial_tv)
1978 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02001980 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001981 }
1982 break;
1983
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001984 case ISN_PCALL_END:
1985 // PCALL finished, arguments have been consumed and replaced by
1986 // the return value. Now clear the funcref from the stack,
1987 // and move the return value in its place.
1988 --ectx.ec_stack.ga_len;
1989 clear_tv(STACK_TV_BOT(-1));
1990 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
1991 break;
1992
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001993 // call a user defined function or funcref/partial
1994 case ISN_UCALL:
1995 {
1996 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
1997
1998 SOURCING_LNUM = iptr->isn_lnum;
1999 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002000 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002001 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002002 }
2003 break;
2004
2005 // return from a :def function call
2006 case ISN_RETURN:
2007 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002008 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002009 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002010
2011 if (trystack->ga_len > 0)
2012 trycmd = ((trycmd_T *)trystack->ga_data)
2013 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002014 if (trycmd != NULL
2015 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002016 && trycmd->tcd_finally_idx != 0)
2017 {
2018 // jump to ":finally"
2019 ectx.ec_iidx = trycmd->tcd_finally_idx;
2020 trycmd->tcd_return = TRUE;
2021 }
2022 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002023 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002024 }
2025 break;
2026
2027 // push a function reference to a compiled function
2028 case ISN_FUNCREF:
2029 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002030 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2031 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2032 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002033
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002034 if (pt == NULL)
2035 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002036 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002037 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002038 vim_free(pt);
2039 goto failed;
2040 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002041 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2042 &ectx) == FAIL)
2043 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002044
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002045 tv = STACK_TV_BOT(0);
2046 ++ectx.ec_stack.ga_len;
2047 tv->vval.v_partial = pt;
2048 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002049 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002050 }
2051 break;
2052
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002053 // Create a global function from a lambda.
2054 case ISN_NEWFUNC:
2055 {
2056 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
Bram Moolenaarf112f302020-12-20 17:47:52 +01002057 ufunc_T *new_ufunc;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002058
Bram Moolenaarf112f302020-12-20 17:47:52 +01002059 new_ufunc = copy_func(
2060 newfunc->nf_lambda, newfunc->nf_global);
2061 if (new_ufunc != NULL
2062 && (new_ufunc->uf_flags & FC_CLOSURE))
2063 {
2064 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2065
2066 // Need to create a partial to store the context of the
2067 // function.
2068 if (pt == NULL)
2069 goto failed;
2070 if (fill_partial_and_closure(pt, new_ufunc,
2071 &ectx) == FAIL)
2072 goto failed;
2073 new_ufunc->uf_partial = pt;
2074 --pt->pt_refcount; // not referenced here
2075 }
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002076 }
2077 break;
2078
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002079 // List functions
2080 case ISN_DEF:
2081 if (iptr->isn_arg.string == NULL)
2082 list_functions(NULL);
2083 else
2084 {
2085 exarg_T ea;
2086
2087 CLEAR_FIELD(ea);
2088 ea.cmd = ea.arg = iptr->isn_arg.string;
2089 define_function(&ea, NULL);
2090 }
2091 break;
2092
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002093 // jump if a condition is met
2094 case ISN_JUMP:
2095 {
2096 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002097 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002098 int jump = TRUE;
2099
2100 if (when != JUMP_ALWAYS)
2101 {
2102 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002103 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002104 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002105 || when == JUMP_IF_COND_TRUE)
2106 {
2107 SOURCING_LNUM = iptr->isn_lnum;
2108 jump = tv_get_bool_chk(tv, &error);
2109 if (error)
2110 goto on_error;
2111 }
2112 else
2113 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002114 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002115 || when == JUMP_AND_KEEP_IF_FALSE
2116 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002117 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002118 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 {
2120 // drop the value from the stack
2121 clear_tv(tv);
2122 --ectx.ec_stack.ga_len;
2123 }
2124 }
2125 if (jump)
2126 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2127 }
2128 break;
2129
2130 // top of a for loop
2131 case ISN_FOR:
2132 {
2133 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2134 typval_T *idxtv =
2135 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2136
2137 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002138 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002139 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002140 ++idxtv->vval.v_number;
2141 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002142 // past the end of the list, jump to "endfor"
2143 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2144 else if (list->lv_first == &range_list_item)
2145 {
2146 // non-materialized range() list
2147 tv = STACK_TV_BOT(0);
2148 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002149 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002150 tv->vval.v_number = list_find_nr(
2151 list, idxtv->vval.v_number, NULL);
2152 ++ectx.ec_stack.ga_len;
2153 }
2154 else
2155 {
2156 listitem_T *li = list_find(list, idxtv->vval.v_number);
2157
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002158 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2159 ++ectx.ec_stack.ga_len;
2160 }
2161 }
2162 break;
2163
2164 // start of ":try" block
2165 case ISN_TRY:
2166 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002167 trycmd_T *trycmd = NULL;
2168
Bram Moolenaar270d0382020-05-15 21:42:53 +02002169 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002170 goto failed;
2171 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2172 + ectx.ec_trystack.ga_len;
2173 ++ectx.ec_trystack.ga_len;
2174 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002175 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002176 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2177 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002178 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002179 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002180 }
2181 break;
2182
2183 case ISN_PUSHEXC:
2184 if (current_exception == NULL)
2185 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002186 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002187 iemsg("Evaluating catch while current_exception is NULL");
2188 goto failed;
2189 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002190 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002191 goto failed;
2192 tv = STACK_TV_BOT(0);
2193 ++ectx.ec_stack.ga_len;
2194 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002195 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 tv->vval.v_string = vim_strsave(
2197 (char_u *)current_exception->value);
2198 break;
2199
2200 case ISN_CATCH:
2201 {
2202 garray_T *trystack = &ectx.ec_trystack;
2203
2204 if (trystack->ga_len > 0)
2205 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002206 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 + trystack->ga_len - 1;
2208 trycmd->tcd_caught = TRUE;
2209 }
2210 did_emsg = got_int = did_throw = FALSE;
2211 catch_exception(current_exception);
2212 }
2213 break;
2214
2215 // end of ":try" block
2216 case ISN_ENDTRY:
2217 {
2218 garray_T *trystack = &ectx.ec_trystack;
2219
2220 if (trystack->ga_len > 0)
2221 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002222 trycmd_T *trycmd = NULL;
2223
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002224 --trystack->ga_len;
2225 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002226 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 trycmd = ((trycmd_T *)trystack->ga_data)
2228 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002229 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 {
2231 // discard the exception
2232 if (caught_stack == current_exception)
2233 caught_stack = caught_stack->caught;
2234 discard_current_exception();
2235 }
2236
2237 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002238 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002239 }
2240 }
2241 break;
2242
2243 case ISN_THROW:
2244 --ectx.ec_stack.ga_len;
2245 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002246 if (tv->vval.v_string == NULL
2247 || *skipwhite(tv->vval.v_string) == NUL)
2248 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002249 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002250 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002251 emsg(_(e_throw_with_empty_string));
2252 goto failed;
2253 }
2254
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002255 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2256 {
2257 vim_free(tv->vval.v_string);
2258 goto failed;
2259 }
2260 did_throw = TRUE;
2261 break;
2262
2263 // compare with special values
2264 case ISN_COMPAREBOOL:
2265 case ISN_COMPARESPECIAL:
2266 {
2267 typval_T *tv1 = STACK_TV_BOT(-2);
2268 typval_T *tv2 = STACK_TV_BOT(-1);
2269 varnumber_T arg1 = tv1->vval.v_number;
2270 varnumber_T arg2 = tv2->vval.v_number;
2271 int res;
2272
2273 switch (iptr->isn_arg.op.op_type)
2274 {
2275 case EXPR_EQUAL: res = arg1 == arg2; break;
2276 case EXPR_NEQUAL: res = arg1 != arg2; break;
2277 default: res = 0; break;
2278 }
2279
2280 --ectx.ec_stack.ga_len;
2281 tv1->v_type = VAR_BOOL;
2282 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2283 }
2284 break;
2285
2286 // Operation with two number arguments
2287 case ISN_OPNR:
2288 case ISN_COMPARENR:
2289 {
2290 typval_T *tv1 = STACK_TV_BOT(-2);
2291 typval_T *tv2 = STACK_TV_BOT(-1);
2292 varnumber_T arg1 = tv1->vval.v_number;
2293 varnumber_T arg2 = tv2->vval.v_number;
2294 varnumber_T res;
2295
2296 switch (iptr->isn_arg.op.op_type)
2297 {
2298 case EXPR_MULT: res = arg1 * arg2; break;
2299 case EXPR_DIV: res = arg1 / arg2; break;
2300 case EXPR_REM: res = arg1 % arg2; break;
2301 case EXPR_SUB: res = arg1 - arg2; break;
2302 case EXPR_ADD: res = arg1 + arg2; break;
2303
2304 case EXPR_EQUAL: res = arg1 == arg2; break;
2305 case EXPR_NEQUAL: res = arg1 != arg2; break;
2306 case EXPR_GREATER: res = arg1 > arg2; break;
2307 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2308 case EXPR_SMALLER: res = arg1 < arg2; break;
2309 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2310 default: res = 0; break;
2311 }
2312
2313 --ectx.ec_stack.ga_len;
2314 if (iptr->isn_type == ISN_COMPARENR)
2315 {
2316 tv1->v_type = VAR_BOOL;
2317 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2318 }
2319 else
2320 tv1->vval.v_number = res;
2321 }
2322 break;
2323
2324 // Computation with two float arguments
2325 case ISN_OPFLOAT:
2326 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002327#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002328 {
2329 typval_T *tv1 = STACK_TV_BOT(-2);
2330 typval_T *tv2 = STACK_TV_BOT(-1);
2331 float_T arg1 = tv1->vval.v_float;
2332 float_T arg2 = tv2->vval.v_float;
2333 float_T res = 0;
2334 int cmp = FALSE;
2335
2336 switch (iptr->isn_arg.op.op_type)
2337 {
2338 case EXPR_MULT: res = arg1 * arg2; break;
2339 case EXPR_DIV: res = arg1 / arg2; break;
2340 case EXPR_SUB: res = arg1 - arg2; break;
2341 case EXPR_ADD: res = arg1 + arg2; break;
2342
2343 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2344 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2345 case EXPR_GREATER: cmp = arg1 > arg2; break;
2346 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2347 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2348 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2349 default: cmp = 0; break;
2350 }
2351 --ectx.ec_stack.ga_len;
2352 if (iptr->isn_type == ISN_COMPAREFLOAT)
2353 {
2354 tv1->v_type = VAR_BOOL;
2355 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2356 }
2357 else
2358 tv1->vval.v_float = res;
2359 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002360#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002361 break;
2362
2363 case ISN_COMPARELIST:
2364 {
2365 typval_T *tv1 = STACK_TV_BOT(-2);
2366 typval_T *tv2 = STACK_TV_BOT(-1);
2367 list_T *arg1 = tv1->vval.v_list;
2368 list_T *arg2 = tv2->vval.v_list;
2369 int cmp = FALSE;
2370 int ic = iptr->isn_arg.op.op_ic;
2371
2372 switch (iptr->isn_arg.op.op_type)
2373 {
2374 case EXPR_EQUAL: cmp =
2375 list_equal(arg1, arg2, ic, FALSE); break;
2376 case EXPR_NEQUAL: cmp =
2377 !list_equal(arg1, arg2, ic, FALSE); break;
2378 case EXPR_IS: cmp = arg1 == arg2; break;
2379 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2380 default: cmp = 0; break;
2381 }
2382 --ectx.ec_stack.ga_len;
2383 clear_tv(tv1);
2384 clear_tv(tv2);
2385 tv1->v_type = VAR_BOOL;
2386 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2387 }
2388 break;
2389
2390 case ISN_COMPAREBLOB:
2391 {
2392 typval_T *tv1 = STACK_TV_BOT(-2);
2393 typval_T *tv2 = STACK_TV_BOT(-1);
2394 blob_T *arg1 = tv1->vval.v_blob;
2395 blob_T *arg2 = tv2->vval.v_blob;
2396 int cmp = FALSE;
2397
2398 switch (iptr->isn_arg.op.op_type)
2399 {
2400 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2401 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2402 case EXPR_IS: cmp = arg1 == arg2; break;
2403 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2404 default: cmp = 0; break;
2405 }
2406 --ectx.ec_stack.ga_len;
2407 clear_tv(tv1);
2408 clear_tv(tv2);
2409 tv1->v_type = VAR_BOOL;
2410 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2411 }
2412 break;
2413
2414 // TODO: handle separately
2415 case ISN_COMPARESTRING:
2416 case ISN_COMPAREDICT:
2417 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 case ISN_COMPAREANY:
2419 {
2420 typval_T *tv1 = STACK_TV_BOT(-2);
2421 typval_T *tv2 = STACK_TV_BOT(-1);
2422 exptype_T exptype = iptr->isn_arg.op.op_type;
2423 int ic = iptr->isn_arg.op.op_ic;
2424
Bram Moolenaareb26f432020-09-14 16:50:05 +02002425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002426 typval_compare(tv1, tv2, exptype, ic);
2427 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 --ectx.ec_stack.ga_len;
2429 }
2430 break;
2431
2432 case ISN_ADDLIST:
2433 case ISN_ADDBLOB:
2434 {
2435 typval_T *tv1 = STACK_TV_BOT(-2);
2436 typval_T *tv2 = STACK_TV_BOT(-1);
2437
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002438 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002439 if (iptr->isn_type == ISN_ADDLIST)
2440 eval_addlist(tv1, tv2);
2441 else
2442 eval_addblob(tv1, tv2);
2443 clear_tv(tv2);
2444 --ectx.ec_stack.ga_len;
2445 }
2446 break;
2447
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002448 case ISN_LISTAPPEND:
2449 {
2450 typval_T *tv1 = STACK_TV_BOT(-2);
2451 typval_T *tv2 = STACK_TV_BOT(-1);
2452 list_T *l = tv1->vval.v_list;
2453
2454 // add an item to a list
2455 if (l == NULL)
2456 {
2457 SOURCING_LNUM = iptr->isn_lnum;
2458 emsg(_(e_cannot_add_to_null_list));
2459 goto on_error;
2460 }
2461 if (list_append_tv(l, tv2) == FAIL)
2462 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002463 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002464 --ectx.ec_stack.ga_len;
2465 }
2466 break;
2467
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002468 case ISN_BLOBAPPEND:
2469 {
2470 typval_T *tv1 = STACK_TV_BOT(-2);
2471 typval_T *tv2 = STACK_TV_BOT(-1);
2472 blob_T *b = tv1->vval.v_blob;
2473 int error = FALSE;
2474 varnumber_T n;
2475
2476 // add a number to a blob
2477 if (b == NULL)
2478 {
2479 SOURCING_LNUM = iptr->isn_lnum;
2480 emsg(_(e_cannot_add_to_null_blob));
2481 goto on_error;
2482 }
2483 n = tv_get_number_chk(tv2, &error);
2484 if (error)
2485 goto on_error;
2486 ga_append(&b->bv_ga, (int)n);
2487 --ectx.ec_stack.ga_len;
2488 }
2489 break;
2490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 // Computation with two arguments of unknown type
2492 case ISN_OPANY:
2493 {
2494 typval_T *tv1 = STACK_TV_BOT(-2);
2495 typval_T *tv2 = STACK_TV_BOT(-1);
2496 varnumber_T n1, n2;
2497#ifdef FEAT_FLOAT
2498 float_T f1 = 0, f2 = 0;
2499#endif
2500 int error = FALSE;
2501
2502 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2503 {
2504 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2505 {
2506 eval_addlist(tv1, tv2);
2507 clear_tv(tv2);
2508 --ectx.ec_stack.ga_len;
2509 break;
2510 }
2511 else if (tv1->v_type == VAR_BLOB
2512 && tv2->v_type == VAR_BLOB)
2513 {
2514 eval_addblob(tv1, tv2);
2515 clear_tv(tv2);
2516 --ectx.ec_stack.ga_len;
2517 break;
2518 }
2519 }
2520#ifdef FEAT_FLOAT
2521 if (tv1->v_type == VAR_FLOAT)
2522 {
2523 f1 = tv1->vval.v_float;
2524 n1 = 0;
2525 }
2526 else
2527#endif
2528 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002529 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 n1 = tv_get_number_chk(tv1, &error);
2531 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002532 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002533#ifdef FEAT_FLOAT
2534 if (tv2->v_type == VAR_FLOAT)
2535 f1 = n1;
2536#endif
2537 }
2538#ifdef FEAT_FLOAT
2539 if (tv2->v_type == VAR_FLOAT)
2540 {
2541 f2 = tv2->vval.v_float;
2542 n2 = 0;
2543 }
2544 else
2545#endif
2546 {
2547 n2 = tv_get_number_chk(tv2, &error);
2548 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002549 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550#ifdef FEAT_FLOAT
2551 if (tv1->v_type == VAR_FLOAT)
2552 f2 = n2;
2553#endif
2554 }
2555#ifdef FEAT_FLOAT
2556 // if there is a float on either side the result is a float
2557 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2558 {
2559 switch (iptr->isn_arg.op.op_type)
2560 {
2561 case EXPR_MULT: f1 = f1 * f2; break;
2562 case EXPR_DIV: f1 = f1 / f2; break;
2563 case EXPR_SUB: f1 = f1 - f2; break;
2564 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002565 default: SOURCING_LNUM = iptr->isn_lnum;
2566 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002567 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 }
2569 clear_tv(tv1);
2570 clear_tv(tv2);
2571 tv1->v_type = VAR_FLOAT;
2572 tv1->vval.v_float = f1;
2573 --ectx.ec_stack.ga_len;
2574 }
2575 else
2576#endif
2577 {
2578 switch (iptr->isn_arg.op.op_type)
2579 {
2580 case EXPR_MULT: n1 = n1 * n2; break;
2581 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2582 case EXPR_SUB: n1 = n1 - n2; break;
2583 case EXPR_ADD: n1 = n1 + n2; break;
2584 default: n1 = num_modulus(n1, n2); break;
2585 }
2586 clear_tv(tv1);
2587 clear_tv(tv2);
2588 tv1->v_type = VAR_NUMBER;
2589 tv1->vval.v_number = n1;
2590 --ectx.ec_stack.ga_len;
2591 }
2592 }
2593 break;
2594
2595 case ISN_CONCAT:
2596 {
2597 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2598 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2599 char_u *res;
2600
2601 res = concat_str(str1, str2);
2602 clear_tv(STACK_TV_BOT(-2));
2603 clear_tv(STACK_TV_BOT(-1));
2604 --ectx.ec_stack.ga_len;
2605 STACK_TV_BOT(-1)->vval.v_string = res;
2606 }
2607 break;
2608
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002609 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002610 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002611 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002612 int is_slice = iptr->isn_type == ISN_STRSLICE;
2613 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002614 char_u *res;
2615
2616 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002617 // string slice: string is at stack-3, first index at
2618 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002619 if (is_slice)
2620 {
2621 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002622 n1 = tv->vval.v_number;
2623 }
2624
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002625 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002626 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002627
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002628 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002629 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002630 if (is_slice)
2631 // Slice: Select the characters from the string
2632 res = string_slice(tv->vval.v_string, n1, n2);
2633 else
2634 // Index: The resulting variable is a string of a
2635 // single character. If the index is too big or
2636 // negative the result is empty.
2637 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002638 vim_free(tv->vval.v_string);
2639 tv->vval.v_string = res;
2640 }
2641 break;
2642
2643 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002644 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 {
Bram Moolenaared591872020-08-15 22:14:53 +02002646 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002647 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002648 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002649
2650 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002651 // list slice: list is at stack-3, indexes at stack-2 and
2652 // stack-1
2653 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002654 list = tv->vval.v_list;
2655
2656 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002657 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002658 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002659
2660 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002661 {
Bram Moolenaared591872020-08-15 22:14:53 +02002662 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002663 n1 = tv->vval.v_number;
2664 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 }
Bram Moolenaared591872020-08-15 22:14:53 +02002666
2667 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002668 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002670 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2671 == FAIL)
2672 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002673 }
2674 break;
2675
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002676 case ISN_ANYINDEX:
2677 case ISN_ANYSLICE:
2678 {
2679 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2680 typval_T *var1, *var2;
2681 int res;
2682
2683 // index: composite is at stack-2, index at stack-1
2684 // slice: composite is at stack-3, indexes at stack-2 and
2685 // stack-1
2686 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002687 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002688 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2689 goto on_error;
2690 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2691 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2692 res = eval_index_inner(tv, is_slice,
2693 var1, var2, NULL, -1, TRUE);
2694 clear_tv(var1);
2695 if (is_slice)
2696 clear_tv(var2);
2697 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2698 if (res == FAIL)
2699 goto on_error;
2700 }
2701 break;
2702
Bram Moolenaar9af78762020-06-16 11:34:42 +02002703 case ISN_SLICE:
2704 {
2705 list_T *list;
2706 int count = iptr->isn_arg.number;
2707
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002708 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002709 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002710 list = tv->vval.v_list;
2711
2712 // no error for short list, expect it to be checked earlier
2713 if (list != NULL && list->lv_len >= count)
2714 {
2715 list_T *newlist = list_slice(list,
2716 count, list->lv_len - 1);
2717
2718 if (newlist != NULL)
2719 {
2720 list_unref(list);
2721 tv->vval.v_list = newlist;
2722 ++newlist->lv_refcount;
2723 }
2724 }
2725 }
2726 break;
2727
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002728 case ISN_GETITEM:
2729 {
2730 listitem_T *li;
2731 int index = iptr->isn_arg.number;
2732
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002733 // Get list item: list is at stack-1, push item.
2734 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002735 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002736 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002737
2738 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2739 goto failed;
2740 ++ectx.ec_stack.ga_len;
2741 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2742 }
2743 break;
2744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002745 case ISN_MEMBER:
2746 {
2747 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002748 char_u *key;
2749 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002750 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002751
2752 // dict member: dict is at stack-2, key at stack-1
2753 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002754 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002755 dict = tv->vval.v_dict;
2756
2757 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002758 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002759 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002760 if (key == NULL)
2761 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002762
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002763 if ((di = dict_find(dict, key, -1)) == NULL)
2764 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002765 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002766 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002767
2768 // If :silent! is used we will continue, make sure the
2769 // stack contents makes sense.
2770 clear_tv(tv);
2771 --ectx.ec_stack.ga_len;
2772 tv = STACK_TV_BOT(-1);
2773 clear_tv(tv);
2774 tv->v_type = VAR_NUMBER;
2775 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002776 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002777 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002778 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002779 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002780 // Clear the dict only after getting the item, to avoid
2781 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002782 tv = STACK_TV_BOT(-1);
2783 temp_tv = *tv;
2784 copy_tv(&di->di_tv, tv);
2785 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002786 }
2787 break;
2788
2789 // dict member with string key
2790 case ISN_STRINGMEMBER:
2791 {
2792 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002794 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795
2796 tv = STACK_TV_BOT(-1);
2797 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2798 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002799 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002801 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 }
2803 dict = tv->vval.v_dict;
2804
2805 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2806 == NULL)
2807 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002808 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002809 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002810 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002811 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002812 // Clear the dict after getting the item, to avoid that it
2813 // make the item invalid.
2814 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002815 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002816 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002817 }
2818 break;
2819
2820 case ISN_NEGATENR:
2821 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002822 if (tv->v_type != VAR_NUMBER
2823#ifdef FEAT_FLOAT
2824 && tv->v_type != VAR_FLOAT
2825#endif
2826 )
2827 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002828 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002829 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002830 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002831 }
2832#ifdef FEAT_FLOAT
2833 if (tv->v_type == VAR_FLOAT)
2834 tv->vval.v_float = -tv->vval.v_float;
2835 else
2836#endif
2837 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002838 break;
2839
2840 case ISN_CHECKNR:
2841 {
2842 int error = FALSE;
2843
2844 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002845 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002847 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002848 (void)tv_get_number_chk(tv, &error);
2849 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002850 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 }
2852 break;
2853
2854 case ISN_CHECKTYPE:
2855 {
2856 checktype_T *ct = &iptr->isn_arg.type;
2857
2858 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002859 SOURCING_LNUM = iptr->isn_lnum;
2860 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2861 goto on_error;
2862
2863 // number 0 is FALSE, number 1 is TRUE
2864 if (tv->v_type == VAR_NUMBER
2865 && ct->ct_type->tt_type == VAR_BOOL
2866 && (tv->vval.v_number == 0
2867 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002869 tv->v_type = VAR_BOOL;
2870 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002871 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 }
2873 }
2874 break;
2875
Bram Moolenaar9af78762020-06-16 11:34:42 +02002876 case ISN_CHECKLEN:
2877 {
2878 int min_len = iptr->isn_arg.checklen.cl_min_len;
2879 list_T *list = NULL;
2880
2881 tv = STACK_TV_BOT(-1);
2882 if (tv->v_type == VAR_LIST)
2883 list = tv->vval.v_list;
2884 if (list == NULL || list->lv_len < min_len
2885 || (list->lv_len > min_len
2886 && !iptr->isn_arg.checklen.cl_more_OK))
2887 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002888 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002889 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002890 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002891 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002892 }
2893 }
2894 break;
2895
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002896 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002897 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002898 {
2899 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002900 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002901
2902 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002903 if (iptr->isn_type == ISN_2BOOL)
2904 {
2905 n = tv2bool(tv);
2906 if (iptr->isn_arg.number) // invert
2907 n = !n;
2908 }
2909 else
2910 {
2911 SOURCING_LNUM = iptr->isn_lnum;
2912 n = tv_get_bool_chk(tv, &error);
2913 if (error)
2914 goto on_error;
2915 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002916 clear_tv(tv);
2917 tv->v_type = VAR_BOOL;
2918 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
2919 }
2920 break;
2921
2922 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002923 case ISN_2STRING_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002924 {
2925 char_u *str;
2926
2927 tv = STACK_TV_BOT(iptr->isn_arg.number);
2928 if (tv->v_type != VAR_STRING)
2929 {
Bram Moolenaar418f1df2020-08-12 21:34:49 +02002930 if (iptr->isn_type == ISN_2STRING_ANY)
2931 {
2932 switch (tv->v_type)
2933 {
2934 case VAR_SPECIAL:
2935 case VAR_BOOL:
2936 case VAR_NUMBER:
2937 case VAR_FLOAT:
2938 case VAR_BLOB: break;
2939 default: to_string_error(tv->v_type);
2940 goto on_error;
2941 }
2942 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 str = typval_tostring(tv);
2944 clear_tv(tv);
2945 tv->v_type = VAR_STRING;
2946 tv->vval.v_string = str;
2947 }
2948 }
2949 break;
2950
Bram Moolenaar08597872020-12-10 19:43:40 +01002951 case ISN_RANGE:
2952 {
2953 exarg_T ea;
2954 char *errormsg;
2955
2956 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2957 goto failed;
2958 ++ectx.ec_stack.ga_len;
2959 tv = STACK_TV_BOT(-1);
2960 ea.addr_type = ADDR_LINES;
2961 ea.cmd = iptr->isn_arg.string;
2962 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
2963 goto failed;
2964 if (ea.addr_count == 0)
2965 tv->vval.v_number = curwin->w_cursor.lnum;
2966 else
2967 tv->vval.v_number = ea.line2;
2968 }
2969 break;
2970
Bram Moolenaarc3516f72020-09-08 22:45:35 +02002971 case ISN_PUT:
2972 {
2973 int regname = iptr->isn_arg.put.put_regname;
2974 linenr_T lnum = iptr->isn_arg.put.put_lnum;
2975 char_u *expr = NULL;
2976 int dir = FORWARD;
2977
2978 if (regname == '=')
2979 {
2980 tv = STACK_TV_BOT(-1);
2981 if (tv->v_type == VAR_STRING)
2982 expr = tv->vval.v_string;
2983 else
2984 {
2985 expr = typval_tostring(tv); // allocates value
2986 clear_tv(tv);
2987 }
2988 --ectx.ec_stack.ga_len;
2989 }
Bram Moolenaar08597872020-12-10 19:43:40 +01002990 if (lnum < -2)
2991 {
2992 // line number was put on the stack by ISN_RANGE
2993 tv = STACK_TV_BOT(-1);
2994 curwin->w_cursor.lnum = tv->vval.v_number;
2995 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
2996 dir = BACKWARD;
2997 --ectx.ec_stack.ga_len;
2998 }
2999 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003000 // :put! above cursor
3001 dir = BACKWARD;
3002 else if (lnum >= 0)
3003 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3004 check_cursor();
3005 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3006 vim_free(expr);
3007 }
3008 break;
3009
Bram Moolenaar02194d22020-10-24 23:08:38 +02003010 case ISN_CMDMOD:
3011 save_cmdmod = cmdmod;
3012 restore_cmdmod = TRUE;
3013 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3014 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003015 break;
3016
Bram Moolenaar02194d22020-10-24 23:08:38 +02003017 case ISN_CMDMOD_REV:
3018 // filter regprog is owned by the instruction, don't free it
3019 cmdmod.cmod_filter_regmatch.regprog = NULL;
3020 undo_cmdmod(&cmdmod);
3021 cmdmod = save_cmdmod;
3022 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003023 break;
3024
Bram Moolenaar792f7862020-11-23 08:31:18 +01003025 case ISN_UNPACK:
3026 {
3027 int count = iptr->isn_arg.unpack.unp_count;
3028 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3029 list_T *l;
3030 listitem_T *li;
3031 int i;
3032
3033 // Check there is a valid list to unpack.
3034 tv = STACK_TV_BOT(-1);
3035 if (tv->v_type != VAR_LIST)
3036 {
3037 SOURCING_LNUM = iptr->isn_lnum;
3038 emsg(_(e_for_argument_must_be_sequence_of_lists));
3039 goto on_error;
3040 }
3041 l = tv->vval.v_list;
3042 if (l == NULL
3043 || l->lv_len < (semicolon ? count - 1 : count))
3044 {
3045 SOURCING_LNUM = iptr->isn_lnum;
3046 emsg(_(e_list_value_does_not_have_enough_items));
3047 goto on_error;
3048 }
3049 else if (!semicolon && l->lv_len > count)
3050 {
3051 SOURCING_LNUM = iptr->isn_lnum;
3052 emsg(_(e_list_value_has_more_items_than_targets));
3053 goto on_error;
3054 }
3055
3056 CHECK_LIST_MATERIALIZE(l);
3057 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3058 goto failed;
3059 ectx.ec_stack.ga_len += count - 1;
3060
3061 // Variable after semicolon gets a list with the remaining
3062 // items.
3063 if (semicolon)
3064 {
3065 list_T *rem_list =
3066 list_alloc_with_items(l->lv_len - count + 1);
3067
3068 if (rem_list == NULL)
3069 goto failed;
3070 tv = STACK_TV_BOT(-count);
3071 tv->vval.v_list = rem_list;
3072 ++rem_list->lv_refcount;
3073 tv->v_lock = 0;
3074 li = l->lv_first;
3075 for (i = 0; i < count - 1; ++i)
3076 li = li->li_next;
3077 for (i = 0; li != NULL; ++i)
3078 {
3079 list_set_item(rem_list, i, &li->li_tv);
3080 li = li->li_next;
3081 }
3082 --count;
3083 }
3084
3085 // Produce the values in reverse order, first item last.
3086 li = l->lv_first;
3087 for (i = 0; i < count; ++i)
3088 {
3089 tv = STACK_TV_BOT(-i - 1);
3090 copy_tv(&li->li_tv, tv);
3091 li = li->li_next;
3092 }
3093
3094 list_unref(l);
3095 }
3096 break;
3097
Bram Moolenaar389df252020-07-09 21:20:47 +02003098 case ISN_SHUFFLE:
3099 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003100 typval_T tmp_tv;
3101 int item = iptr->isn_arg.shuffle.shfl_item;
3102 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003103
3104 tmp_tv = *STACK_TV_BOT(-item);
3105 for ( ; up > 0 && item > 1; --up)
3106 {
3107 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3108 --item;
3109 }
3110 *STACK_TV_BOT(-item) = tmp_tv;
3111 }
3112 break;
3113
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114 case ISN_DROP:
3115 --ectx.ec_stack.ga_len;
3116 clear_tv(STACK_TV_BOT(0));
3117 break;
3118 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003119 continue;
3120
Bram Moolenaard032f342020-07-18 18:13:02 +02003121func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003122 // Restore previous function. If the frame pointer is where we started
3123 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003124 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003125 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003126
Bram Moolenaard032f342020-07-18 18:13:02 +02003127 if (func_return(&ectx) == FAIL)
3128 // only fails when out of memory
3129 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003130 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003131
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003132on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003133 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003134 // If "emsg_silent" is set then ignore the error, unless it was set
3135 // when calling the function.
3136 if (did_emsg_cumul + did_emsg == did_emsg_before
3137 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003138 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003139on_fatal_error:
3140 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003141 // If we are not inside a try-catch started here, abort execution.
3142 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003143 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144 }
3145
3146done:
3147 // function finished, get result from the stack.
3148 tv = STACK_TV_BOT(-1);
3149 *rettv = *tv;
3150 tv->v_type = VAR_UNKNOWN;
3151 ret = OK;
3152
3153failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003154 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003155 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003156 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003157
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003158 // Deal with any remaining closures, they may be in use somewhere.
3159 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003160 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003161 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003162 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3163 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003164
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003165 estack_pop();
3166 current_sctx = save_current_sctx;
3167
Bram Moolenaar352134b2020-10-17 22:04:08 +02003168 if (*msg_list != NULL && saved_msg_list != NULL)
3169 {
3170 msglist_T **plist = saved_msg_list;
3171
3172 // Append entries from the current msg_list (uncaught exceptions) to
3173 // the saved msg_list.
3174 while (*plist != NULL)
3175 plist = &(*plist)->next;
3176
3177 *plist = *msg_list;
3178 }
3179 msg_list = saved_msg_list;
3180
Bram Moolenaar02194d22020-10-24 23:08:38 +02003181 if (restore_cmdmod)
3182 {
3183 cmdmod.cmod_filter_regmatch.regprog = NULL;
3184 undo_cmdmod(&cmdmod);
3185 cmdmod = save_cmdmod;
3186 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003187 emsg_silent_def = save_emsg_silent_def;
3188 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003189
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003190failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003191 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003192 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3193 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003194
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003195 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003196 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003197
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003198 // Not sure if this is necessary.
3199 suppress_errthrow = save_suppress_errthrow;
3200
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003201 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003202 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003203 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003204 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003205 return ret;
3206}
3207
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003208/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003209 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003210 * We don't really need this at runtime, but we do have tests that require it,
3211 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 */
3213 void
3214ex_disassemble(exarg_T *eap)
3215{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003216 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003217 char_u *fname;
3218 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003219 dfunc_T *dfunc;
3220 isn_T *instr;
3221 int current;
3222 int line_idx = 0;
3223 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003224 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003225
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003226 if (STRNCMP(arg, "<lambda>", 8) == 0)
3227 {
3228 arg += 8;
3229 (void)getdigits(&arg);
3230 fname = vim_strnsave(eap->arg, arg - eap->arg);
3231 }
3232 else
3233 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003234 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003235 if (fname == NULL)
3236 {
3237 semsg(_(e_invarg2), eap->arg);
3238 return;
3239 }
3240
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003241 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003242 if (ufunc == NULL)
3243 {
3244 char_u *p = untrans_function_name(fname);
3245
3246 if (p != NULL)
3247 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003248 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003249 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003250 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 if (ufunc == NULL)
3252 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003253 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 return;
3255 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003256 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003257 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3258 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003259 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003260 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003261 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 return;
3263 }
3264 if (ufunc->uf_name_exp != NULL)
3265 msg((char *)ufunc->uf_name_exp);
3266 else
3267 msg((char *)ufunc->uf_name);
3268
3269 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3270 instr = dfunc->df_instr;
3271 for (current = 0; current < dfunc->df_instr_count; ++current)
3272 {
3273 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003274 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275
3276 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3277 {
3278 if (current > prev_current)
3279 {
3280 msg_puts("\n\n");
3281 prev_current = current;
3282 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003283 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3284 if (line != NULL)
3285 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 }
3287
3288 switch (iptr->isn_type)
3289 {
3290 case ISN_EXEC:
3291 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3292 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003293 case ISN_EXECCONCAT:
3294 smsg("%4d EXECCONCAT %lld", current,
3295 (long long)iptr->isn_arg.number);
3296 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003297 case ISN_ECHO:
3298 {
3299 echo_T *echo = &iptr->isn_arg.echo;
3300
3301 smsg("%4d %s %d", current,
3302 echo->echo_with_white ? "ECHO" : "ECHON",
3303 echo->echo_count);
3304 }
3305 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003306 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003307 smsg("%4d EXECUTE %lld", current,
3308 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003309 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003310 case ISN_ECHOMSG:
3311 smsg("%4d ECHOMSG %lld", current,
3312 (long long)(iptr->isn_arg.number));
3313 break;
3314 case ISN_ECHOERR:
3315 smsg("%4d ECHOERR %lld", current,
3316 (long long)(iptr->isn_arg.number));
3317 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003318 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003319 case ISN_LOADOUTER:
3320 {
3321 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3322
3323 if (iptr->isn_arg.number < 0)
3324 smsg("%4d LOAD%s arg[%lld]", current, add,
3325 (long long)(iptr->isn_arg.number
3326 + STACK_FRAME_SIZE));
3327 else
3328 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003329 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003330 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003331 break;
3332 case ISN_LOADV:
3333 smsg("%4d LOADV v:%s", current,
3334 get_vim_var_name(iptr->isn_arg.number));
3335 break;
3336 case ISN_LOADSCRIPT:
3337 {
3338 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003339 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003340 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3341 + iptr->isn_arg.script.script_idx;
3342
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003343 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3344 sv->sv_name,
3345 iptr->isn_arg.script.script_idx,
3346 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003347 }
3348 break;
3349 case ISN_LOADS:
3350 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003351 scriptitem_T *si = SCRIPT_ITEM(
3352 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003353
3354 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003355 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003356 }
3357 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003358 case ISN_LOADAUTO:
3359 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3360 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003361 case ISN_LOADG:
3362 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3363 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003364 case ISN_LOADB:
3365 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3366 break;
3367 case ISN_LOADW:
3368 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3369 break;
3370 case ISN_LOADT:
3371 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3372 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003373 case ISN_LOADGDICT:
3374 smsg("%4d LOAD g:", current);
3375 break;
3376 case ISN_LOADBDICT:
3377 smsg("%4d LOAD b:", current);
3378 break;
3379 case ISN_LOADWDICT:
3380 smsg("%4d LOAD w:", current);
3381 break;
3382 case ISN_LOADTDICT:
3383 smsg("%4d LOAD t:", current);
3384 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003385 case ISN_LOADOPT:
3386 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3387 break;
3388 case ISN_LOADENV:
3389 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3390 break;
3391 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003392 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003393 break;
3394
3395 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003396 case ISN_STOREOUTER:
3397 {
3398 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3399
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003400 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003401 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003402 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003403 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003404 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003405 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003406 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003407 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003408 case ISN_STOREV:
3409 smsg("%4d STOREV v:%s", current,
3410 get_vim_var_name(iptr->isn_arg.number));
3411 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003412 case ISN_STOREAUTO:
3413 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3414 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003416 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3417 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003418 case ISN_STOREB:
3419 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3420 break;
3421 case ISN_STOREW:
3422 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3423 break;
3424 case ISN_STORET:
3425 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3426 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003427 case ISN_STORES:
3428 {
3429 scriptitem_T *si = SCRIPT_ITEM(
3430 iptr->isn_arg.loadstore.ls_sid);
3431
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003432 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003433 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 break;
3436 case ISN_STORESCRIPT:
3437 {
3438 scriptitem_T *si =
Bram Moolenaar21b9e972020-01-26 19:26:46 +01003439 SCRIPT_ITEM(iptr->isn_arg.script.script_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
3441 + iptr->isn_arg.script.script_idx;
3442
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003443 smsg("%4d STORESCRIPT %s-%d in %s", current,
3444 sv->sv_name,
3445 iptr->isn_arg.script.script_idx,
3446 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003447 }
3448 break;
3449 case ISN_STOREOPT:
3450 smsg("%4d STOREOPT &%s", current,
3451 iptr->isn_arg.storeopt.so_name);
3452 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003453 case ISN_STOREENV:
3454 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3455 break;
3456 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003457 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003458 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003459 case ISN_STORENR:
3460 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003461 iptr->isn_arg.storenr.stnr_val,
3462 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 break;
3464
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003465 case ISN_STORELIST:
3466 smsg("%4d STORELIST", current);
3467 break;
3468
3469 case ISN_STOREDICT:
3470 smsg("%4d STOREDICT", current);
3471 break;
3472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 // constants
3474 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003475 smsg("%4d PUSHNR %lld", current,
3476 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003477 break;
3478 case ISN_PUSHBOOL:
3479 case ISN_PUSHSPEC:
3480 smsg("%4d PUSH %s", current,
3481 get_var_special_name(iptr->isn_arg.number));
3482 break;
3483 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003484#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003485 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003486#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003487 break;
3488 case ISN_PUSHS:
3489 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3490 break;
3491 case ISN_PUSHBLOB:
3492 {
3493 char_u *r;
3494 char_u numbuf[NUMBUFLEN];
3495 char_u *tofree;
3496
3497 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003498 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003499 vim_free(tofree);
3500 }
3501 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003502 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003503 {
3504 char *name = (char *)iptr->isn_arg.string;
3505
3506 smsg("%4d PUSHFUNC \"%s\"", current,
3507 name == NULL ? "[none]" : name);
3508 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003509 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003510 case ISN_PUSHCHANNEL:
3511#ifdef FEAT_JOB_CHANNEL
3512 {
3513 channel_T *channel = iptr->isn_arg.channel;
3514
3515 smsg("%4d PUSHCHANNEL %d", current,
3516 channel == NULL ? 0 : channel->ch_id);
3517 }
3518#endif
3519 break;
3520 case ISN_PUSHJOB:
3521#ifdef FEAT_JOB_CHANNEL
3522 {
3523 typval_T tv;
3524 char_u *name;
3525
3526 tv.v_type = VAR_JOB;
3527 tv.vval.v_job = iptr->isn_arg.job;
3528 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003529 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003530 }
3531#endif
3532 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003533 case ISN_PUSHEXC:
3534 smsg("%4d PUSH v:exception", current);
3535 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003536 case ISN_UNLET:
3537 smsg("%4d UNLET%s %s", current,
3538 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3539 iptr->isn_arg.unlet.ul_name);
3540 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003541 case ISN_UNLETENV:
3542 smsg("%4d UNLETENV%s $%s", current,
3543 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3544 iptr->isn_arg.unlet.ul_name);
3545 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003546 case ISN_LOCKCONST:
3547 smsg("%4d LOCKCONST", current);
3548 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003549 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003550 smsg("%4d NEWLIST size %lld", current,
3551 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003552 break;
3553 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003554 smsg("%4d NEWDICT size %lld", current,
3555 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556 break;
3557
3558 // function call
3559 case ISN_BCALL:
3560 {
3561 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3562
3563 smsg("%4d BCALL %s(argc %d)", current,
3564 internal_func_name(cbfunc->cbf_idx),
3565 cbfunc->cbf_argcount);
3566 }
3567 break;
3568 case ISN_DCALL:
3569 {
3570 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3571 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3572 + cdfunc->cdf_idx;
3573
3574 smsg("%4d DCALL %s(argc %d)", current,
3575 df->df_ufunc->uf_name_exp != NULL
3576 ? df->df_ufunc->uf_name_exp
3577 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3578 }
3579 break;
3580 case ISN_UCALL:
3581 {
3582 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3583
3584 smsg("%4d UCALL %s(argc %d)", current,
3585 cufunc->cuf_name, cufunc->cuf_argcount);
3586 }
3587 break;
3588 case ISN_PCALL:
3589 {
3590 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3591
3592 smsg("%4d PCALL%s (argc %d)", current,
3593 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3594 }
3595 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003596 case ISN_PCALL_END:
3597 smsg("%4d PCALL end", current);
3598 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003599 case ISN_RETURN:
3600 smsg("%4d RETURN", current);
3601 break;
3602 case ISN_FUNCREF:
3603 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003604 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003605 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003606 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003607
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003608 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003609 }
3610 break;
3611
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003612 case ISN_NEWFUNC:
3613 {
3614 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3615
3616 smsg("%4d NEWFUNC %s %s", current,
3617 newfunc->nf_lambda, newfunc->nf_global);
3618 }
3619 break;
3620
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003621 case ISN_DEF:
3622 {
3623 char_u *name = iptr->isn_arg.string;
3624
3625 smsg("%4d DEF %s", current,
3626 name == NULL ? (char_u *)"" : name);
3627 }
3628 break;
3629
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 case ISN_JUMP:
3631 {
3632 char *when = "?";
3633
3634 switch (iptr->isn_arg.jump.jump_when)
3635 {
3636 case JUMP_ALWAYS:
3637 when = "JUMP";
3638 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003639 case JUMP_AND_KEEP_IF_TRUE:
3640 when = "JUMP_AND_KEEP_IF_TRUE";
3641 break;
3642 case JUMP_IF_FALSE:
3643 when = "JUMP_IF_FALSE";
3644 break;
3645 case JUMP_AND_KEEP_IF_FALSE:
3646 when = "JUMP_AND_KEEP_IF_FALSE";
3647 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003648 case JUMP_IF_COND_FALSE:
3649 when = "JUMP_IF_COND_FALSE";
3650 break;
3651 case JUMP_IF_COND_TRUE:
3652 when = "JUMP_IF_COND_TRUE";
3653 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003654 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003655 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003656 iptr->isn_arg.jump.jump_where);
3657 }
3658 break;
3659
3660 case ISN_FOR:
3661 {
3662 forloop_T *forloop = &iptr->isn_arg.forloop;
3663
3664 smsg("%4d FOR $%d -> %d", current,
3665 forloop->for_idx, forloop->for_end);
3666 }
3667 break;
3668
3669 case ISN_TRY:
3670 {
3671 try_T *try = &iptr->isn_arg.try;
3672
3673 smsg("%4d TRY catch -> %d, finally -> %d", current,
3674 try->try_catch, try->try_finally);
3675 }
3676 break;
3677 case ISN_CATCH:
3678 // TODO
3679 smsg("%4d CATCH", current);
3680 break;
3681 case ISN_ENDTRY:
3682 smsg("%4d ENDTRY", current);
3683 break;
3684 case ISN_THROW:
3685 smsg("%4d THROW", current);
3686 break;
3687
3688 // expression operations on number
3689 case ISN_OPNR:
3690 case ISN_OPFLOAT:
3691 case ISN_OPANY:
3692 {
3693 char *what;
3694 char *ins;
3695
3696 switch (iptr->isn_arg.op.op_type)
3697 {
3698 case EXPR_MULT: what = "*"; break;
3699 case EXPR_DIV: what = "/"; break;
3700 case EXPR_REM: what = "%"; break;
3701 case EXPR_SUB: what = "-"; break;
3702 case EXPR_ADD: what = "+"; break;
3703 default: what = "???"; break;
3704 }
3705 switch (iptr->isn_type)
3706 {
3707 case ISN_OPNR: ins = "OPNR"; break;
3708 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3709 case ISN_OPANY: ins = "OPANY"; break;
3710 default: ins = "???"; break;
3711 }
3712 smsg("%4d %s %s", current, ins, what);
3713 }
3714 break;
3715
3716 case ISN_COMPAREBOOL:
3717 case ISN_COMPARESPECIAL:
3718 case ISN_COMPARENR:
3719 case ISN_COMPAREFLOAT:
3720 case ISN_COMPARESTRING:
3721 case ISN_COMPAREBLOB:
3722 case ISN_COMPARELIST:
3723 case ISN_COMPAREDICT:
3724 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003725 case ISN_COMPAREANY:
3726 {
3727 char *p;
3728 char buf[10];
3729 char *type;
3730
3731 switch (iptr->isn_arg.op.op_type)
3732 {
3733 case EXPR_EQUAL: p = "=="; break;
3734 case EXPR_NEQUAL: p = "!="; break;
3735 case EXPR_GREATER: p = ">"; break;
3736 case EXPR_GEQUAL: p = ">="; break;
3737 case EXPR_SMALLER: p = "<"; break;
3738 case EXPR_SEQUAL: p = "<="; break;
3739 case EXPR_MATCH: p = "=~"; break;
3740 case EXPR_IS: p = "is"; break;
3741 case EXPR_ISNOT: p = "isnot"; break;
3742 case EXPR_NOMATCH: p = "!~"; break;
3743 default: p = "???"; break;
3744 }
3745 STRCPY(buf, p);
3746 if (iptr->isn_arg.op.op_ic == TRUE)
3747 strcat(buf, "?");
3748 switch(iptr->isn_type)
3749 {
3750 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3751 case ISN_COMPARESPECIAL:
3752 type = "COMPARESPECIAL"; break;
3753 case ISN_COMPARENR: type = "COMPARENR"; break;
3754 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3755 case ISN_COMPARESTRING:
3756 type = "COMPARESTRING"; break;
3757 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3758 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3759 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3760 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003761 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3762 default: type = "???"; break;
3763 }
3764
3765 smsg("%4d %s %s", current, type, buf);
3766 }
3767 break;
3768
3769 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3770 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3771
3772 // expression operations
3773 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003774 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003775 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003776 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003777 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003778 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003779 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003780 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3781 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003782 case ISN_SLICE: smsg("%4d SLICE %lld",
3783 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003784 case ISN_GETITEM: smsg("%4d ITEM %lld",
3785 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003786 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3787 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 iptr->isn_arg.string); break;
3789 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3790
3791 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003792 case ISN_CHECKTYPE:
3793 {
3794 char *tofree;
3795
3796 smsg("%4d CHECKTYPE %s stack[%d]", current,
3797 type_name(iptr->isn_arg.type.ct_type, &tofree),
3798 iptr->isn_arg.type.ct_off);
3799 vim_free(tofree);
3800 break;
3801 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003802 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3803 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3804 iptr->isn_arg.checklen.cl_min_len);
3805 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003806 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003807 case ISN_2BOOL: if (iptr->isn_arg.number)
3808 smsg("%4d INVERT (!val)", current);
3809 else
3810 smsg("%4d 2BOOL (!!val)", current);
3811 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003812 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3813 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003814 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003815 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3816 (long long)(iptr->isn_arg.number));
3817 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003818 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3819 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003820 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003821 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3822 smsg("%4d PUT %c above range",
3823 current, iptr->isn_arg.put.put_regname);
3824 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3825 smsg("%4d PUT %c range",
3826 current, iptr->isn_arg.put.put_regname);
3827 else
3828 smsg("%4d PUT %c %ld", current,
3829 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003830 (long)iptr->isn_arg.put.put_lnum);
3831 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832
Bram Moolenaar02194d22020-10-24 23:08:38 +02003833 // TODO: summarize modifiers
3834 case ISN_CMDMOD:
3835 {
3836 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003837 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003838 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3839
3840 buf = alloc(len + 1);
3841 if (buf != NULL)
3842 {
3843 (void)produce_cmdmods(
3844 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3845 smsg("%4d CMDMOD %s", current, buf);
3846 vim_free(buf);
3847 }
3848 break;
3849 }
3850 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003851
Bram Moolenaar792f7862020-11-23 08:31:18 +01003852 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3853 iptr->isn_arg.unpack.unp_count,
3854 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3855 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003856 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3857 iptr->isn_arg.shuffle.shfl_item,
3858 iptr->isn_arg.shuffle.shfl_up);
3859 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 case ISN_DROP: smsg("%4d DROP", current); break;
3861 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003862
3863 out_flush(); // output one line at a time
3864 ui_breakcheck();
3865 if (got_int)
3866 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003867 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868}
3869
3870/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003871 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003872 * list, etc. Mostly like what JavaScript does, except that empty list and
3873 * empty dictionary are FALSE.
3874 */
3875 int
3876tv2bool(typval_T *tv)
3877{
3878 switch (tv->v_type)
3879 {
3880 case VAR_NUMBER:
3881 return tv->vval.v_number != 0;
3882 case VAR_FLOAT:
3883#ifdef FEAT_FLOAT
3884 return tv->vval.v_float != 0.0;
3885#else
3886 break;
3887#endif
3888 case VAR_PARTIAL:
3889 return tv->vval.v_partial != NULL;
3890 case VAR_FUNC:
3891 case VAR_STRING:
3892 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3893 case VAR_LIST:
3894 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3895 case VAR_DICT:
3896 return tv->vval.v_dict != NULL
3897 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3898 case VAR_BOOL:
3899 case VAR_SPECIAL:
3900 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3901 case VAR_JOB:
3902#ifdef FEAT_JOB_CHANNEL
3903 return tv->vval.v_job != NULL;
3904#else
3905 break;
3906#endif
3907 case VAR_CHANNEL:
3908#ifdef FEAT_JOB_CHANNEL
3909 return tv->vval.v_channel != NULL;
3910#else
3911 break;
3912#endif
3913 case VAR_BLOB:
3914 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3915 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003916 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003917 case VAR_VOID:
3918 break;
3919 }
3920 return FALSE;
3921}
3922
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003923 void
3924emsg_using_string_as(typval_T *tv, int as_number)
3925{
3926 semsg(_(as_number ? e_using_string_as_number_str
3927 : e_using_string_as_bool_str),
3928 tv->vval.v_string == NULL
3929 ? (char_u *)"" : tv->vval.v_string);
3930}
3931
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003932/*
3933 * If "tv" is a string give an error and return FAIL.
3934 */
3935 int
3936check_not_string(typval_T *tv)
3937{
3938 if (tv->v_type == VAR_STRING)
3939 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01003940 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 clear_tv(tv);
3942 return FAIL;
3943 }
3944 return OK;
3945}
3946
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947
3948#endif // FEAT_EVAL