blob: b3abee9ef8b396092c04f1bab5027e1dc9de408b [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 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010057struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010058 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 Moolenaarcd45ed02020-12-22 17:35:54 +010072};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
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 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100176 // don't use ufunc->uf_name, it may have been freed
177 emsg_funcname(e_func_deleted,
178 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100179 return FAIL;
180 }
181
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200182 if (ufunc->uf_va_name != NULL)
183 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200184 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200185 // Stack at time of call with 2 varargs:
186 // normal_arg
187 // optional_arg
188 // vararg_1
189 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200190 // After creating the list:
191 // normal_arg
192 // optional_arg
193 // vararg-list
194 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 // After creating the list
197 // normal_arg
198 // (space for optional_arg)
199 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200200 vararg_count = argcount - ufunc->uf_args.ga_len;
201 if (vararg_count < 0)
202 vararg_count = 0;
203 else
204 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200205 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200206 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200207
208 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200209 }
210
Bram Moolenaarfe270812020-04-11 22:31:27 +0200211 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200212 if (arg_to_add < 0)
213 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200214 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200215 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200216 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200217 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200218 return FAIL;
219 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200220
221 // Reserve space for:
222 // - missing arguments
223 // - stack frame
224 // - local variables
225 // - if needed: a counter for number of closures created in
226 // ectx->ec_funcrefs.
227 varcount = dfunc->df_varcount + dfunc->df_has_closure;
228 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
229 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100230 return FAIL;
231
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100232 // If depth of calling is getting too high, don't execute the function.
233 if (funcdepth_increment() == FAIL)
234 return FAIL;
235
Bram Moolenaarfe270812020-04-11 22:31:27 +0200236 // Move the vararg-list to below the missing optional arguments.
237 if (vararg_count > 0 && arg_to_add > 0)
238 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100239
240 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200241 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200242 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100244
245 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246 STACK_TV_BOT(0)->vval.v_number = ectx->ec_dfunc_idx;
247 STACK_TV_BOT(1)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200248 STACK_TV_BOT(2)->vval.v_string = (void *)ectx->ec_outer_stack;
249 STACK_TV_BOT(3)->vval.v_number = ectx->ec_outer_frame;
250 STACK_TV_BOT(4)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200251 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100252
253 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100255 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200256 if (dfunc->df_has_closure)
257 {
258 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
259
260 tv->v_type = VAR_NUMBER;
261 tv->vval.v_number = 0;
262 }
263 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100265 if (ufunc->uf_partial != NULL)
266 {
267 ectx->ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
268 ectx->ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
269 }
270
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100271 // Set execution state to the start of the called function.
272 ectx->ec_dfunc_idx = cdf_idx;
273 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200274 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
275 if (entry != NULL)
276 {
277 // Set the script context to the script where the function was defined.
278 // TODO: save more than the SID?
279 entry->es_save_sid = current_sctx.sc_sid;
280 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
281 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100282
283 // Decide where to start execution, handles optional arguments.
284 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
286 return OK;
287}
288
289// Get pointer to item in the stack.
290#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
291
292/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200293 * Used when returning from a function: Check if any closure is still
294 * referenced. If so then move the arguments and variables to a separate piece
295 * of stack to be used when the closure is called.
296 * When "free_arguments" is TRUE the arguments are to be freed.
297 * Returns FAIL when out of memory.
298 */
299 static int
300handle_closure_in_use(ectx_T *ectx, int free_arguments)
301{
302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
303 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200304 int argcount;
305 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200306 int idx;
307 typval_T *tv;
308 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200309 garray_T *gap = &ectx->ec_funcrefs;
310 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200311
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200312 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200313 return OK; // function was freed
314 if (dfunc->df_has_closure == 0)
315 return OK; // no closures
316 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
317 closure_count = tv->vval.v_number;
318 if (closure_count == 0)
319 return OK; // no funcrefs created
320
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200321 argcount = ufunc_argcount(dfunc->df_ufunc);
322 top = ectx->ec_frame_idx - argcount;
323
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200324 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200325 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200326 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200327 partial_T *pt;
328 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200329
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200330 if (off < 0)
331 continue; // count is off or already done
332 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200333 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200334 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200336 int i;
337
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200338 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200339 // unreferenced on return.
340 for (i = 0; i < dfunc->df_varcount; ++i)
341 {
342 typval_T *stv = STACK_TV(ectx->ec_frame_idx
343 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200344 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200345 --refcount;
346 }
347 if (refcount > 1)
348 {
349 closure_in_use = TRUE;
350 break;
351 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200352 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200353 }
354
355 if (closure_in_use)
356 {
357 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
358 typval_T *stack;
359
360 // A closure is using the arguments and/or local variables.
361 // Move them to the called function.
362 if (funcstack == NULL)
363 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200364 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
365 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
367 funcstack->fs_ga.ga_data = stack;
368 if (stack == NULL)
369 {
370 vim_free(funcstack);
371 return FAIL;
372 }
373
374 // Move or copy the arguments.
375 for (idx = 0; idx < argcount; ++idx)
376 {
377 tv = STACK_TV(top + idx);
378 if (free_arguments)
379 {
380 *(stack + idx) = *tv;
381 tv->v_type = VAR_UNKNOWN;
382 }
383 else
384 copy_tv(tv, stack + idx);
385 }
386 // Move the local variables.
387 for (idx = 0; idx < dfunc->df_varcount; ++idx)
388 {
389 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200390
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200391 // A partial created for a local function, that is also used as a
392 // local variable, has a reference count for the variable, thus
393 // will never go down to zero. When all these refcounts are one
394 // then the funcstack is unused. We need to count how many we have
395 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200396 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
397 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200399
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200400 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200401 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
402 gap->ga_len - closure_count + i])
403 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200404 }
405
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200406 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200407 tv->v_type = VAR_UNKNOWN;
408 }
409
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200410 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200411 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200412 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
413 - closure_count + idx];
414 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200415 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200416 ++funcstack->fs_refcount;
417 pt->pt_funcstack = funcstack;
418 pt->pt_ectx_stack = &funcstack->fs_ga;
419 pt->pt_ectx_frame = ectx->ec_frame_idx - top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200420 }
421 }
422 }
423
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200424 for (idx = 0; idx < closure_count; ++idx)
425 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
426 - closure_count + idx]);
427 gap->ga_len -= closure_count;
428 if (gap->ga_len == 0)
429 ga_clear(gap);
430
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200431 return OK;
432}
433
434/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200435 * Called when a partial is freed or its reference count goes down to one. The
436 * funcstack may be the only reference to the partials in the local variables.
437 * Go over all of them, the funcref and can be freed if all partials
438 * referencing the funcstack have a reference count of one.
439 */
440 void
441funcstack_check_refcount(funcstack_T *funcstack)
442{
443 int i;
444 garray_T *gap = &funcstack->fs_ga;
445 int done = 0;
446
447 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
448 return;
449 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
450 {
451 typval_T *tv = ((typval_T *)gap->ga_data) + i;
452
453 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
454 && tv->vval.v_partial->pt_funcstack == funcstack
455 && tv->vval.v_partial->pt_refcount == 1)
456 ++done;
457 }
458 if (done == funcstack->fs_min_refcount)
459 {
460 typval_T *stack = gap->ga_data;
461
462 // All partials referencing the funcstack have a reference count of
463 // one, thus the funcstack is no longer of use.
464 for (i = 0; i < gap->ga_len; ++i)
465 clear_tv(stack + i);
466 vim_free(stack);
467 vim_free(funcstack);
468 }
469}
470
471/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 * Return from the current function.
473 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200474 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100475func_return(ectx_T *ectx)
476{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100477 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100478 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200479 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
480 + ectx->ec_dfunc_idx;
481 int argcount = ufunc_argcount(dfunc->df_ufunc);
482 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200483 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100484
485 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200486 entry = estack_pop();
487 if (entry != NULL)
488 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100489
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200490 if (handle_closure_in_use(ectx, TRUE) == FAIL)
491 return FAIL;
492
493 // Clear the arguments.
494 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
495 clear_tv(STACK_TV(idx));
496
497 // Clear local variables and temp values, but not the return value.
498 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100499 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100501
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100502 // The return value should be on top of the stack. However, when aborting
503 // it may not be there and ec_frame_idx is the top of the stack.
504 ret_idx = ectx->ec_stack.ga_len - 1;
505 if (ret_idx == ectx->ec_frame_idx + 4)
506 ret_idx = 0;
507
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100508 // Restore the previous frame.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200509 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx)->vval.v_number;
510 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx + 1)->vval.v_number;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200511 ectx->ec_outer_stack =
512 (void *)STACK_TV(ectx->ec_frame_idx + 2)->vval.v_string;
513 ectx->ec_outer_frame = STACK_TV(ectx->ec_frame_idx + 3)->vval.v_number;
514 // restoring ec_frame_idx must be last
515 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx + 4)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100516 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
517 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100518
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100519 if (ret_idx > 0)
520 {
521 // Reset the stack to the position before the call, with a spot for the
522 // return value, moved there from above the frame.
523 ectx->ec_stack.ga_len = top + 1;
524 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
525 }
526 else
527 // Reset the stack to the position before the call.
528 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200529
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100530 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200531 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100532}
533
534#undef STACK_TV
535
536/*
537 * Prepare arguments and rettv for calling a builtin or user function.
538 */
539 static int
540call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
541{
542 int idx;
543 typval_T *tv;
544
545 // Move arguments from bottom of the stack to argvars[] and add terminator.
546 for (idx = 0; idx < argcount; ++idx)
547 argvars[idx] = *STACK_TV_BOT(idx - argcount);
548 argvars[argcount].v_type = VAR_UNKNOWN;
549
550 // Result replaces the arguments on the stack.
551 if (argcount > 0)
552 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200553 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 return FAIL;
555 else
556 ++ectx->ec_stack.ga_len;
557
558 // Default return value is zero.
559 tv = STACK_TV_BOT(-1);
560 tv->v_type = VAR_NUMBER;
561 tv->vval.v_number = 0;
562
563 return OK;
564}
565
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200566// Ugly global to avoid passing the execution context around through many
567// layers.
568static ectx_T *current_ectx = NULL;
569
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570/*
571 * Call a builtin function by index.
572 */
573 static int
574call_bfunc(int func_idx, int argcount, ectx_T *ectx)
575{
576 typval_T argvars[MAX_FUNC_ARGS];
577 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100578 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200579 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100580
581 if (call_prepare(argcount, argvars, ectx) == FAIL)
582 return FAIL;
583
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200584 // Call the builtin function. Set "current_ectx" so that when it
585 // recursively invokes call_def_function() a closure context can be set.
586 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100587 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200588 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100589
590 // Clear the arguments.
591 for (idx = 0; idx < argcount; ++idx)
592 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200593
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100594 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200595 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100596 return OK;
597}
598
599/*
600 * Execute a user defined function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100601 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100602 */
603 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100604call_ufunc(ufunc_T *ufunc, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100605{
606 typval_T argvars[MAX_FUNC_ARGS];
607 funcexe_T funcexe;
608 int error;
609 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100610 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100611
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200612 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200613 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
614 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200615 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100616 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100617 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100618 if (error != FCERR_UNKNOWN)
619 {
620 if (error == FCERR_TOOMANY)
621 semsg(_(e_toomanyarg), ufunc->uf_name);
622 else
623 semsg(_(e_toofewarg), ufunc->uf_name);
624 return FAIL;
625 }
626
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100627 // The function has been compiled, can call it quickly. For a function
628 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100629 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100630 if (iptr != NULL)
631 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100632 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100633 iptr->isn_type = ISN_DCALL;
634 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
635 iptr->isn_arg.dfunc.cdf_argcount = argcount;
636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100637 return call_dfunc(ufunc->uf_dfunc_idx, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100638 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100639
640 if (call_prepare(argcount, argvars, ectx) == FAIL)
641 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200642 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100643 funcexe.evaluate = TRUE;
644
645 // Call the user function. Result goes in last position on the stack.
646 // TODO: add selfdict if there is one
647 error = call_user_func_check(ufunc, argcount, argvars,
648 STACK_TV_BOT(-1), &funcexe, NULL);
649
650 // Clear the arguments.
651 for (idx = 0; idx < argcount; ++idx)
652 clear_tv(&argvars[idx]);
653
654 if (error != FCERR_NONE)
655 {
656 user_func_error(error, ufunc->uf_name);
657 return FAIL;
658 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100659 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200660 // Error other than from calling the function itself.
661 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100662 return OK;
663}
664
665/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200666 * Return TRUE if an error was given or CTRL-C was pressed.
667 */
668 static int
669vim9_aborting(int prev_called_emsg)
670{
671 return called_emsg > prev_called_emsg || got_int || did_throw;
672}
673
674/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675 * Execute a function by "name".
676 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100677 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100678 * Returns FAIL if not found without an error message.
679 */
680 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682{
683 ufunc_T *ufunc;
684
685 if (builtin_function(name, -1))
686 {
687 int func_idx = find_internal_func(name);
688
689 if (func_idx < 0)
690 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200691 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692 return FAIL;
693 return call_bfunc(func_idx, argcount, ectx);
694 }
695
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200696 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200697
698 if (ufunc == NULL)
699 {
700 int called_emsg_before = called_emsg;
701
702 if (script_autoload(name, TRUE))
703 // loaded a package, search for the function again
704 ufunc = find_func(name, FALSE, NULL);
705 if (vim9_aborting(called_emsg_before))
706 return FAIL; // bail out if loading the script caused an error
707 }
708
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100709 if (ufunc != NULL)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100710 return call_ufunc(ufunc, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711
712 return FAIL;
713}
714
715 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200716call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100717{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200718 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200719 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100720 int called_emsg_before = called_emsg;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200721 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100722
723 if (tv->v_type == VAR_PARTIAL)
724 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200725 partial_T *pt = tv->vval.v_partial;
726 int i;
727
728 if (pt->pt_argc > 0)
729 {
730 // Make space for arguments from the partial, shift the "argcount"
731 // arguments up.
732 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
733 return FAIL;
734 for (i = 1; i <= argcount; ++i)
735 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
736 ectx->ec_stack.ga_len += pt->pt_argc;
737 argcount += pt->pt_argc;
738
739 // copy the arguments from the partial onto the stack
740 for (i = 0; i < pt->pt_argc; ++i)
741 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
742 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100743
744 if (pt->pt_func != NULL)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200745 {
746 int ret = call_ufunc(pt->pt_func, argcount, ectx, NULL);
747
748 // closure may need the function context where it was defined
749 ectx->ec_outer_stack = pt->pt_ectx_stack;
750 ectx->ec_outer_frame = pt->pt_ectx_frame;
751
752 return ret;
753 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100754 name = pt->pt_name;
755 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200756 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200758 if (name != NULL)
759 {
760 char_u fname_buf[FLEN_FIXED + 1];
761 char_u *tofree = NULL;
762 int error = FCERR_NONE;
763 char_u *fname;
764
765 // May need to translate <SNR>123_ to K_SNR.
766 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
767 if (error != FCERR_NONE)
768 res = FAIL;
769 else
770 res = call_by_name(fname, argcount, ectx, NULL);
771 vim_free(tofree);
772 }
773
774 if (name == NULL || res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775 {
776 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200777 semsg(_(e_unknownfunc),
778 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779 return FAIL;
780 }
781 return OK;
782}
783
784/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200785 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
786 * TRUE.
787 */
788 static int
789error_if_locked(int lock, char *error)
790{
791 if (lock & (VAR_LOCKED | VAR_FIXED))
792 {
793 emsg(_(error));
794 return TRUE;
795 }
796 return FALSE;
797}
798
799/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100800 * Store "tv" in variable "name".
801 * This is for s: and g: variables.
802 */
803 static void
804store_var(char_u *name, typval_T *tv)
805{
806 funccal_entry_T entry;
807
808 save_funccal(&entry);
Bram Moolenaar30fd8202020-09-26 15:09:30 +0200809 set_var_const(name, NULL, tv, FALSE, ASSIGN_NO_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100810 restore_funccal();
811}
812
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100813/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100814 * Convert "tv" to a string.
815 * Return FAIL if not allowed.
816 */
817 static int
818do_2string(typval_T *tv, int is_2string_any)
819{
820 if (tv->v_type != VAR_STRING)
821 {
822 char_u *str;
823
824 if (is_2string_any)
825 {
826 switch (tv->v_type)
827 {
828 case VAR_SPECIAL:
829 case VAR_BOOL:
830 case VAR_NUMBER:
831 case VAR_FLOAT:
832 case VAR_BLOB: break;
833 default: to_string_error(tv->v_type);
834 return FAIL;
835 }
836 }
837 str = typval_tostring(tv);
838 clear_tv(tv);
839 tv->v_type = VAR_STRING;
840 tv->vval.v_string = str;
841 }
842 return OK;
843}
844
845/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100846 * When the value of "sv" is a null list of dict, allocate it.
847 */
848 static void
849allocate_if_null(typval_T *tv)
850{
851 switch (tv->v_type)
852 {
853 case VAR_LIST:
854 if (tv->vval.v_list == NULL)
855 rettv_list_alloc(tv);
856 break;
857 case VAR_DICT:
858 if (tv->vval.v_dict == NULL)
859 rettv_dict_alloc(tv);
860 break;
861 default:
862 break;
863 }
864}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200865
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100866/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100867 * Execute a function by "name".
868 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100869 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100870 */
871 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100872call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100873{
Bram Moolenaared677f52020-08-12 16:38:10 +0200874 int called_emsg_before = called_emsg;
875 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100876
Bram Moolenaared677f52020-08-12 16:38:10 +0200877 res = call_by_name(name, argcount, ectx, iptr);
878 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100879 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +0200880 dictitem_T *v;
881
882 v = find_var(name, NULL, FALSE);
883 if (v == NULL)
884 {
885 semsg(_(e_unknownfunc), name);
886 return FAIL;
887 }
888 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
889 {
890 semsg(_(e_unknownfunc), name);
891 return FAIL;
892 }
893 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100894 }
Bram Moolenaared677f52020-08-12 16:38:10 +0200895 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100896}
897
898/*
Bram Moolenaarf112f302020-12-20 17:47:52 +0100899 * When a function reference is used, fill a partial with the information
900 * needed, especially when it is used as a closure.
901 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100902 int
Bram Moolenaarf112f302020-12-20 17:47:52 +0100903fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
904{
905 pt->pt_func = ufunc;
906 pt->pt_refcount = 1;
907
908 if (pt->pt_func->uf_flags & FC_CLOSURE)
909 {
910 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
911 + ectx->ec_dfunc_idx;
912
913 // The closure needs to find arguments and local
914 // variables in the current stack.
915 pt->pt_ectx_stack = &ectx->ec_stack;
916 pt->pt_ectx_frame = ectx->ec_frame_idx;
917
918 // If this function returns and the closure is still
919 // being used, we need to make a copy of the context
920 // (arguments and local variables). Store a reference
921 // to the partial so we can handle that.
922 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
923 {
924 vim_free(pt);
925 return FAIL;
926 }
927 // Extra variable keeps the count of closures created
928 // in the current function call.
929 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
930 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
931
932 ((partial_T **)ectx->ec_funcrefs.ga_data)
933 [ectx->ec_funcrefs.ga_len] = pt;
934 ++pt->pt_refcount;
935 ++ectx->ec_funcrefs.ga_len;
936 }
937 ++pt->pt_func->uf_refcount;
938 return OK;
939}
940
941/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100942 * Call a "def" function from old Vim script.
943 * Return OK or FAIL.
944 */
945 int
946call_def_function(
947 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +0200948 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100949 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +0200950 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100951 typval_T *rettv) // return value
952{
953 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +0200954 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200955 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100956 typval_T *tv;
957 int idx;
958 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100959 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +0200960 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200961 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100962 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +0200963 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +0200964 msglist_T **saved_msg_list = NULL;
965 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +0200966 cmdmod_T save_cmdmod;
967 int restore_cmdmod = FALSE;
Bram Moolenaar56602ba2020-12-05 21:22:08 +0100968 int save_emsg_silent_def = emsg_silent_def;
969 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100970 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100971 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100972
973// Get pointer to item in the stack.
974#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
975
976// Get pointer to item at the bottom of the stack, -1 is the bottom.
977#undef STACK_TV_BOT
978#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
979
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200980// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200981#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 +0100982
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +0200983// Like STACK_TV_VAR but use the outer scope
984#define STACK_OUT_TV_VAR(idx) (((typval_T *)ectx.ec_outer_stack->ga_data) + ectx.ec_outer_frame + STACK_FRAME_SIZE + idx)
985
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200986 if (ufunc->uf_def_status == UF_NOT_COMPILED
987 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200988 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200989 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +0100990 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200991 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +0200992 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +0200993 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +0200994 }
Bram Moolenaar822ba242020-05-24 23:00:18 +0200995
Bram Moolenaar09689a02020-05-09 22:50:08 +0200996 {
Bram Moolenaar822ba242020-05-24 23:00:18 +0200997 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +0200998 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
999 + ufunc->uf_dfunc_idx;
1000 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001001 {
1002 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001003 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001004 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001005 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001006
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001007 // If depth of calling is getting too high, don't execute the function.
1008 orig_funcdepth = funcdepth_get();
1009 if (funcdepth_increment() == FAIL)
1010 return FAIL;
1011
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001012 CLEAR_FIELD(ectx);
1013 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1014 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1015 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001016 {
1017 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001018 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001019 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001020 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001021 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001022
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001023 // Put arguments on the stack, but no more than what the function expects.
1024 // A lambda can be called with more arguments than it uses.
1025 for (idx = 0; idx < argc
1026 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1027 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001028 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001029 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001030 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1031 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001032 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033 copy_tv(&argv[idx], STACK_TV_BOT(0));
1034 ++ectx.ec_stack.ga_len;
1035 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001036
1037 // Turn varargs into a list. Empty list if no args.
1038 if (ufunc->uf_va_name != NULL)
1039 {
1040 int vararg_count = argc - ufunc->uf_args.ga_len;
1041
1042 if (vararg_count < 0)
1043 vararg_count = 0;
1044 else
1045 argc -= vararg_count;
1046 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001047 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001048
1049 // Check the type of the list items.
1050 tv = STACK_TV_BOT(-1);
1051 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001052 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001053 && ufunc->uf_va_type->tt_member != &t_any
1054 && tv->vval.v_list != NULL)
1055 {
1056 type_T *expected = ufunc->uf_va_type->tt_member;
1057 listitem_T *li = tv->vval.v_list->lv_first;
1058
1059 for (idx = 0; idx < vararg_count; ++idx)
1060 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001061 if (check_typval_type(expected, &li->li_tv,
1062 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001063 goto failed_early;
1064 li = li->li_next;
1065 }
1066 }
1067
Bram Moolenaar23e03252020-04-12 22:22:31 +02001068 if (defcount > 0)
1069 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001070 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001071 --ectx.ec_stack.ga_len;
1072 }
1073
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001074 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001075 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001076 if (defcount > 0)
1077 for (idx = 0; idx < defcount; ++idx)
1078 {
1079 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1080 ++ectx.ec_stack.ga_len;
1081 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001082 if (ufunc->uf_va_name != NULL)
1083 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001084
1085 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001086 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1087 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001088
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001089 if (partial != NULL)
1090 {
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001091 if (partial->pt_ectx_stack == NULL && current_ectx != NULL)
1092 {
1093 // TODO: is this always the right way?
1094 ectx.ec_outer_stack = &current_ectx->ec_stack;
1095 ectx.ec_outer_frame = current_ectx->ec_frame_idx;
1096 }
1097 else
1098 {
1099 ectx.ec_outer_stack = partial->pt_ectx_stack;
1100 ectx.ec_outer_frame = partial->pt_ectx_frame;
1101 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001102 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001103 else if (ufunc->uf_partial != NULL)
1104 {
1105 ectx.ec_outer_stack = ufunc->uf_partial->pt_ectx_stack;
1106 ectx.ec_outer_frame = ufunc->uf_partial->pt_ectx_frame;
1107 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001108
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001109 // dummy frame entries
1110 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1111 {
1112 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1113 ++ectx.ec_stack.ga_len;
1114 }
1115
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001116 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001117 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001118 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1119 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001121 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001122 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001123 ectx.ec_stack.ga_len += dfunc->df_varcount;
1124 if (dfunc->df_has_closure)
1125 {
1126 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1127 STACK_TV_VAR(idx)->vval.v_number = 0;
1128 ++ectx.ec_stack.ga_len;
1129 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001130
1131 ectx.ec_instr = dfunc->df_instr;
1132 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001133
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001134 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001135 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001136 estack_push_ufunc(ufunc, 1);
1137 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001138 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1139
Bram Moolenaar352134b2020-10-17 22:04:08 +02001140 // Use a specific location for storing error messages to be converted to an
1141 // exception.
1142 saved_msg_list = msg_list;
1143 msg_list = &private_msg_list;
1144
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001145 // Do turn errors into exceptions.
1146 suppress_errthrow = FALSE;
1147
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001148 // When ":silent!" was used before calling then we still abort the
1149 // function. If ":silent!" is used in the function then we don't.
1150 emsg_silent_def = emsg_silent;
1151 did_emsg_def = 0;
1152
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001153 // Decide where to start execution, handles optional arguments.
1154 init_instr_idx(ufunc, argc, &ectx);
1155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001156 for (;;)
1157 {
1158 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001159
Bram Moolenaar270d0382020-05-15 21:42:53 +02001160 if (++breakcheck_count >= 100)
1161 {
1162 line_breakcheck();
1163 breakcheck_count = 0;
1164 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001165 if (got_int)
1166 {
1167 // Turn CTRL-C into an exception.
1168 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001169 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001170 goto failed;
1171 did_throw = TRUE;
1172 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001173
Bram Moolenaara26b9702020-04-18 19:53:28 +02001174 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1175 {
1176 // Turn an error message into an exception.
1177 did_emsg = FALSE;
1178 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1179 goto failed;
1180 did_throw = TRUE;
1181 *msg_list = NULL;
1182 }
1183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001184 if (did_throw && !ectx.ec_in_catch)
1185 {
1186 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001187 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001188
1189 // An exception jumps to the first catch, finally, or returns from
1190 // the current function.
1191 if (trystack->ga_len > 0)
1192 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001193 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001194 {
1195 // jump to ":catch" or ":finally"
1196 ectx.ec_in_catch = TRUE;
1197 ectx.ec_iidx = trycmd->tcd_catch_idx;
1198 }
1199 else
1200 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001201 // Not inside try or need to return from current functions.
1202 // Push a dummy return value.
1203 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1204 goto failed;
1205 tv = STACK_TV_BOT(0);
1206 tv->v_type = VAR_NUMBER;
1207 tv->vval.v_number = 0;
1208 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001209 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001211 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001212 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001213 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1214 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 goto done;
1216 }
1217
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001218 if (func_return(&ectx) == FAIL)
1219 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001220 }
1221 continue;
1222 }
1223
1224 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1225 switch (iptr->isn_type)
1226 {
1227 // execute Ex command line
1228 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001229 {
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001230 SOURCING_LNUM = iptr->isn_lnum;
1231 do_cmdline_cmd(iptr->isn_arg.string);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001232 if (did_emsg)
1233 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235 break;
1236
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001237 // execute Ex command from pieces on the stack
1238 case ISN_EXECCONCAT:
1239 {
1240 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001241 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001242 int pass;
1243 int i;
1244 char_u *cmd = NULL;
1245 char_u *str;
1246
1247 for (pass = 1; pass <= 2; ++pass)
1248 {
1249 for (i = 0; i < count; ++i)
1250 {
1251 tv = STACK_TV_BOT(i - count);
1252 str = tv->vval.v_string;
1253 if (str != NULL && *str != NUL)
1254 {
1255 if (pass == 2)
1256 STRCPY(cmd + len, str);
1257 len += STRLEN(str);
1258 }
1259 if (pass == 2)
1260 clear_tv(tv);
1261 }
1262 if (pass == 1)
1263 {
1264 cmd = alloc(len + 1);
1265 if (cmd == NULL)
1266 goto failed;
1267 len = 0;
1268 }
1269 }
1270
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001271 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001272 do_cmdline_cmd(cmd);
1273 vim_free(cmd);
1274 }
1275 break;
1276
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 // execute :echo {string} ...
1278 case ISN_ECHO:
1279 {
1280 int count = iptr->isn_arg.echo.echo_count;
1281 int atstart = TRUE;
1282 int needclr = TRUE;
1283
1284 for (idx = 0; idx < count; ++idx)
1285 {
1286 tv = STACK_TV_BOT(idx - count);
1287 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1288 &atstart, &needclr);
1289 clear_tv(tv);
1290 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001291 if (needclr)
1292 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 ectx.ec_stack.ga_len -= count;
1294 }
1295 break;
1296
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001297 // :execute {string} ...
1298 // :echomsg {string} ...
1299 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001300 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001301 case ISN_ECHOMSG:
1302 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001303 {
1304 int count = iptr->isn_arg.number;
1305 garray_T ga;
1306 char_u buf[NUMBUFLEN];
1307 char_u *p;
1308 int len;
1309 int failed = FALSE;
1310
1311 ga_init2(&ga, 1, 80);
1312 for (idx = 0; idx < count; ++idx)
1313 {
1314 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001315 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001316 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001317 if (tv->v_type == VAR_CHANNEL
1318 || tv->v_type == VAR_JOB)
1319 {
1320 SOURCING_LNUM = iptr->isn_lnum;
1321 emsg(_(e_inval_string));
1322 break;
1323 }
1324 else
1325 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001326 }
1327 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001328 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001329
1330 len = (int)STRLEN(p);
1331 if (ga_grow(&ga, len + 2) == FAIL)
1332 failed = TRUE;
1333 else
1334 {
1335 if (ga.ga_len > 0)
1336 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1337 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1338 ga.ga_len += len;
1339 }
1340 clear_tv(tv);
1341 }
1342 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001343 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001344 {
1345 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001346 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001347 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001348
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001349 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001350 {
1351 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001352 {
1353 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001354 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001355 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001356 {
1357 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001358 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001359 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001360 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001361 else
1362 {
1363 msg_sb_eol();
1364 if (iptr->isn_type == ISN_ECHOMSG)
1365 {
1366 msg_attr(ga.ga_data, echo_attr);
1367 out_flush();
1368 }
1369 else
1370 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001371 SOURCING_LNUM = iptr->isn_lnum;
1372 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001373 }
1374 }
1375 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001376 ga_clear(&ga);
1377 }
1378 break;
1379
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001380 // load local variable or argument
1381 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001382 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001383 goto failed;
1384 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1385 ++ectx.ec_stack.ga_len;
1386 break;
1387
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001388 // load variable or argument from outer scope
1389 case ISN_LOADOUTER:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001390 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02001391 goto failed;
1392 copy_tv(STACK_OUT_TV_VAR(iptr->isn_arg.number),
1393 STACK_TV_BOT(0));
1394 ++ectx.ec_stack.ga_len;
1395 break;
1396
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 // load v: variable
1398 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001399 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001400 goto failed;
1401 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1402 ++ectx.ec_stack.ga_len;
1403 break;
1404
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001405 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001406 case ISN_LOADSCRIPT:
1407 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001408 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1409 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1410 + ectx.ec_dfunc_idx;
1411 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001412 svar_T *sv;
1413
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001414 if (sref->sref_seq != si->sn_script_seq)
1415 {
1416 // The script was reloaded after the function was
1417 // compiled, the script_idx may not be valid.
1418 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1419 dfunc->df_ufunc->uf_name_exp);
1420 goto failed;
1421 }
1422 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001423 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001424 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001425 goto failed;
1426 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1427 ++ectx.ec_stack.ga_len;
1428 }
1429 break;
1430
1431 // load s: variable in old script
1432 case ISN_LOADS:
1433 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001434 hashtab_T *ht = &SCRIPT_VARS(
1435 iptr->isn_arg.loadstore.ls_sid);
1436 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001437 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001438
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001439 if (di == NULL)
1440 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001442 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001443 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 }
1445 else
1446 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001447 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448 goto failed;
1449 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1450 ++ectx.ec_stack.ga_len;
1451 }
1452 }
1453 break;
1454
Bram Moolenaard3aac292020-04-19 14:32:17 +02001455 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001456 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001457 case ISN_LOADB:
1458 case ISN_LOADW:
1459 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001461 dictitem_T *di = NULL;
1462 hashtab_T *ht = NULL;
1463 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001464
Bram Moolenaard3aac292020-04-19 14:32:17 +02001465 switch (iptr->isn_type)
1466 {
1467 case ISN_LOADG:
1468 ht = get_globvar_ht();
1469 namespace = 'g';
1470 break;
1471 case ISN_LOADB:
1472 ht = &curbuf->b_vars->dv_hashtab;
1473 namespace = 'b';
1474 break;
1475 case ISN_LOADW:
1476 ht = &curwin->w_vars->dv_hashtab;
1477 namespace = 'w';
1478 break;
1479 case ISN_LOADT:
1480 ht = &curtab->tp_vars->dv_hashtab;
1481 namespace = 't';
1482 break;
1483 default: // Cannot reach here
1484 goto failed;
1485 }
1486 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 if (di == NULL)
1489 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001490 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001491 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001492 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001493 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001494 }
1495 else
1496 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001497 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001498 goto failed;
1499 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1500 ++ectx.ec_stack.ga_len;
1501 }
1502 }
1503 break;
1504
Bram Moolenaar03290b82020-12-19 16:30:44 +01001505 // load autoload variable
1506 case ISN_LOADAUTO:
1507 {
1508 char_u *name = iptr->isn_arg.string;
1509
1510 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1511 goto failed;
1512 SOURCING_LNUM = iptr->isn_lnum;
1513 if (eval_variable(name, STRLEN(name),
1514 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1515 goto on_error;
1516 ++ectx.ec_stack.ga_len;
1517 }
1518 break;
1519
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001520 // load g:/b:/w:/t: namespace
1521 case ISN_LOADGDICT:
1522 case ISN_LOADBDICT:
1523 case ISN_LOADWDICT:
1524 case ISN_LOADTDICT:
1525 {
1526 dict_T *d = NULL;
1527
1528 switch (iptr->isn_type)
1529 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001530 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1531 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1532 case ISN_LOADWDICT: d = curwin->w_vars; break;
1533 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001534 default: // Cannot reach here
1535 goto failed;
1536 }
1537 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1538 goto failed;
1539 tv = STACK_TV_BOT(0);
1540 tv->v_type = VAR_DICT;
1541 tv->v_lock = 0;
1542 tv->vval.v_dict = d;
1543 ++ectx.ec_stack.ga_len;
1544 }
1545 break;
1546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001547 // load &option
1548 case ISN_LOADOPT:
1549 {
1550 typval_T optval;
1551 char_u *name = iptr->isn_arg.string;
1552
Bram Moolenaara8c17702020-04-01 21:17:24 +02001553 // This is not expected to fail, name is checked during
1554 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001555 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001556 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001557 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001558 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 *STACK_TV_BOT(0) = optval;
1560 ++ectx.ec_stack.ga_len;
1561 }
1562 break;
1563
1564 // load $ENV
1565 case ISN_LOADENV:
1566 {
1567 typval_T optval;
1568 char_u *name = iptr->isn_arg.string;
1569
Bram Moolenaar270d0382020-05-15 21:42:53 +02001570 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001571 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001572 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001573 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001574 *STACK_TV_BOT(0) = optval;
1575 ++ectx.ec_stack.ga_len;
1576 }
1577 break;
1578
1579 // load @register
1580 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001581 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001582 goto failed;
1583 tv = STACK_TV_BOT(0);
1584 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001585 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001586 // This may result in NULL, which should be equivalent to an
1587 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 tv->vval.v_string = get_reg_contents(
1589 iptr->isn_arg.number, GREG_EXPR_SRC);
1590 ++ectx.ec_stack.ga_len;
1591 break;
1592
1593 // store local variable
1594 case ISN_STORE:
1595 --ectx.ec_stack.ga_len;
1596 tv = STACK_TV_VAR(iptr->isn_arg.number);
1597 clear_tv(tv);
1598 *tv = *STACK_TV_BOT(0);
1599 break;
1600
Bram Moolenaarb68b3462020-05-06 21:06:30 +02001601 // store variable or argument in outer scope
1602 case ISN_STOREOUTER:
1603 --ectx.ec_stack.ga_len;
1604 tv = STACK_OUT_TV_VAR(iptr->isn_arg.number);
1605 clear_tv(tv);
1606 *tv = *STACK_TV_BOT(0);
1607 break;
1608
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001609 // store s: variable in old script
1610 case ISN_STORES:
1611 {
1612 hashtab_T *ht = &SCRIPT_VARS(
1613 iptr->isn_arg.loadstore.ls_sid);
1614 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001615 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001616
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001617 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001618 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001619 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001620 else
1621 {
1622 clear_tv(&di->di_tv);
1623 di->di_tv = *STACK_TV_BOT(0);
1624 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001625 }
1626 break;
1627
1628 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 case ISN_STORESCRIPT:
1630 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001631 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1632 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1633 + ectx.ec_dfunc_idx;
1634 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1635 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001637 if (sref->sref_seq != si->sn_script_seq)
1638 {
1639 // The script was reloaded after the function was
1640 // compiled, the script_idx may not be valid.
1641 SOURCING_LNUM = iptr->isn_lnum;
1642 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1643 dfunc->df_ufunc->uf_name_exp);
1644 goto failed;
1645 }
1646 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001647 --ectx.ec_stack.ga_len;
1648 clear_tv(sv->sv_tv);
1649 *sv->sv_tv = *STACK_TV_BOT(0);
1650 }
1651 break;
1652
1653 // store option
1654 case ISN_STOREOPT:
1655 {
1656 long n = 0;
1657 char_u *s = NULL;
1658 char *msg;
1659
1660 --ectx.ec_stack.ga_len;
1661 tv = STACK_TV_BOT(0);
1662 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001663 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001664 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001665 if (s == NULL)
1666 s = (char_u *)"";
1667 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001669 // must be VAR_NUMBER, CHECKTYPE makes sure
1670 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1672 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001673 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 if (msg != NULL)
1675 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001676 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001678 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001680 }
1681 break;
1682
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001683 // store $ENV
1684 case ISN_STOREENV:
1685 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001686 tv = STACK_TV_BOT(0);
1687 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1688 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001689 break;
1690
1691 // store @r
1692 case ISN_STOREREG:
1693 {
1694 int reg = iptr->isn_arg.number;
1695
1696 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001697 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001698 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001699 tv_get_string(tv), -1, FALSE);
1700 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001701 }
1702 break;
1703
1704 // store v: variable
1705 case ISN_STOREV:
1706 --ectx.ec_stack.ga_len;
1707 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1708 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001709 // should not happen, type is checked when compiling
1710 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001711 break;
1712
Bram Moolenaard3aac292020-04-19 14:32:17 +02001713 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001714 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001715 case ISN_STOREB:
1716 case ISN_STOREW:
1717 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001718 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001719 dictitem_T *di;
1720 hashtab_T *ht;
1721 char_u *name = iptr->isn_arg.string + 2;
1722
Bram Moolenaard3aac292020-04-19 14:32:17 +02001723 switch (iptr->isn_type)
1724 {
1725 case ISN_STOREG:
1726 ht = get_globvar_ht();
1727 break;
1728 case ISN_STOREB:
1729 ht = &curbuf->b_vars->dv_hashtab;
1730 break;
1731 case ISN_STOREW:
1732 ht = &curwin->w_vars->dv_hashtab;
1733 break;
1734 case ISN_STORET:
1735 ht = &curtab->tp_vars->dv_hashtab;
1736 break;
1737 default: // Cannot reach here
1738 goto failed;
1739 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001740
1741 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001742 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001743 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001744 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 else
1746 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001747 SOURCING_LNUM = iptr->isn_lnum;
1748 if (var_check_permission(di, name) == FAIL)
1749 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 clear_tv(&di->di_tv);
1751 di->di_tv = *STACK_TV_BOT(0);
1752 }
1753 }
1754 break;
1755
Bram Moolenaar03290b82020-12-19 16:30:44 +01001756 // store an autoload variable
1757 case ISN_STOREAUTO:
1758 SOURCING_LNUM = iptr->isn_lnum;
1759 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1760 clear_tv(STACK_TV_BOT(-1));
1761 --ectx.ec_stack.ga_len;
1762 break;
1763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001764 // store number in local variable
1765 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001766 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 clear_tv(tv);
1768 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001769 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001770 break;
1771
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001772 // store value in list or dict variable
1773 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001774 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001775 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001776 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001777 typval_T *tv_dest = STACK_TV_BOT(-1);
1778 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001779
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001780 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001781 SOURCING_LNUM = iptr->isn_lnum;
1782 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001783 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001784 dest_type = tv_dest->v_type;
1785 if (dest_type == VAR_DICT)
1786 status = do_2string(tv_idx, TRUE);
1787 else if (dest_type == VAR_LIST
1788 && tv_idx->v_type != VAR_NUMBER)
1789 {
1790 emsg(_(e_number_exp));
1791 status = FAIL;
1792 }
1793 }
1794 else if (dest_type != tv_dest->v_type)
1795 {
1796 // just in case, should be OK
1797 semsg(_(e_expected_str_but_got_str),
1798 vartype_name(dest_type),
1799 vartype_name(tv_dest->v_type));
1800 status = FAIL;
1801 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001802
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001803 if (status == OK && dest_type == VAR_LIST)
1804 {
1805 varnumber_T lidx = tv_idx->vval.v_number;
1806 list_T *list = tv_dest->vval.v_list;
1807
1808 if (list == NULL)
1809 {
1810 emsg(_(e_list_not_set));
1811 goto on_error;
1812 }
1813 if (lidx < 0 && list->lv_len + lidx >= 0)
1814 // negative index is relative to the end
1815 lidx = list->lv_len + lidx;
1816 if (lidx < 0 || lidx > list->lv_len)
1817 {
1818 semsg(_(e_listidx), lidx);
1819 goto on_error;
1820 }
1821 if (lidx < list->lv_len)
1822 {
1823 listitem_T *li = list_find(list, lidx);
1824
1825 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001826 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001827 goto on_error;
1828 // overwrite existing list item
1829 clear_tv(&li->li_tv);
1830 li->li_tv = *tv;
1831 }
1832 else
1833 {
1834 if (error_if_locked(list->lv_lock,
1835 e_cannot_change_list))
1836 goto on_error;
1837 // append to list, only fails when out of memory
1838 if (list_append_tv(list, tv) == FAIL)
1839 goto failed;
1840 clear_tv(tv);
1841 }
1842 }
1843 else if (status == OK && dest_type == VAR_DICT)
1844 {
1845 char_u *key = tv_idx->vval.v_string;
1846 dict_T *dict = tv_dest->vval.v_dict;
1847 dictitem_T *di;
1848
1849 SOURCING_LNUM = iptr->isn_lnum;
1850 if (dict == NULL)
1851 {
1852 emsg(_(e_dictionary_not_set));
1853 goto on_error;
1854 }
1855 if (key == NULL)
1856 key = (char_u *)"";
1857 di = dict_find(dict, key, -1);
1858 if (di != NULL)
1859 {
1860 if (error_if_locked(di->di_tv.v_lock,
1861 e_cannot_change_dict_item))
1862 goto on_error;
1863 // overwrite existing value
1864 clear_tv(&di->di_tv);
1865 di->di_tv = *tv;
1866 }
1867 else
1868 {
1869 if (error_if_locked(dict->dv_lock,
1870 e_cannot_change_dict))
1871 goto on_error;
1872 // add to dict, only fails when out of memory
1873 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
1874 goto failed;
1875 clear_tv(tv);
1876 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001877 }
1878 else
1879 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001880 status = FAIL;
1881 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001882 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001883
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001884 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001885 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02001886 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001887 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001888 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001889 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02001890 goto on_error;
1891 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001892 }
1893 break;
1894
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001895 // push constant
1896 case ISN_PUSHNR:
1897 case ISN_PUSHBOOL:
1898 case ISN_PUSHSPEC:
1899 case ISN_PUSHF:
1900 case ISN_PUSHS:
1901 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001902 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001903 case ISN_PUSHCHANNEL:
1904 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001905 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 goto failed;
1907 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001908 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001909 ++ectx.ec_stack.ga_len;
1910 switch (iptr->isn_type)
1911 {
1912 case ISN_PUSHNR:
1913 tv->v_type = VAR_NUMBER;
1914 tv->vval.v_number = iptr->isn_arg.number;
1915 break;
1916 case ISN_PUSHBOOL:
1917 tv->v_type = VAR_BOOL;
1918 tv->vval.v_number = iptr->isn_arg.number;
1919 break;
1920 case ISN_PUSHSPEC:
1921 tv->v_type = VAR_SPECIAL;
1922 tv->vval.v_number = iptr->isn_arg.number;
1923 break;
1924#ifdef FEAT_FLOAT
1925 case ISN_PUSHF:
1926 tv->v_type = VAR_FLOAT;
1927 tv->vval.v_float = iptr->isn_arg.fnumber;
1928 break;
1929#endif
1930 case ISN_PUSHBLOB:
1931 blob_copy(iptr->isn_arg.blob, tv);
1932 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001933 case ISN_PUSHFUNC:
1934 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01001935 if (iptr->isn_arg.string == NULL)
1936 tv->vval.v_string = NULL;
1937 else
1938 tv->vval.v_string =
1939 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001940 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01001941 case ISN_PUSHCHANNEL:
1942#ifdef FEAT_JOB_CHANNEL
1943 tv->v_type = VAR_CHANNEL;
1944 tv->vval.v_channel = iptr->isn_arg.channel;
1945 if (tv->vval.v_channel != NULL)
1946 ++tv->vval.v_channel->ch_refcount;
1947#endif
1948 break;
1949 case ISN_PUSHJOB:
1950#ifdef FEAT_JOB_CHANNEL
1951 tv->v_type = VAR_JOB;
1952 tv->vval.v_job = iptr->isn_arg.job;
1953 if (tv->vval.v_job != NULL)
1954 ++tv->vval.v_job->jv_refcount;
1955#endif
1956 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 default:
1958 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02001959 tv->vval.v_string = vim_strsave(
1960 iptr->isn_arg.string == NULL
1961 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001962 }
1963 break;
1964
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001965 case ISN_UNLET:
1966 if (do_unlet(iptr->isn_arg.unlet.ul_name,
1967 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001968 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001969 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02001970 case ISN_UNLETENV:
1971 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
1972 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02001973
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001974 case ISN_LOCKCONST:
1975 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
1976 break;
1977
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001978 // create a list from items on the stack; uses a single allocation
1979 // for the list header and the items
1980 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02001981 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
1982 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 break;
1984
1985 // create a dict from items on the stack
1986 case ISN_NEWDICT:
1987 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001988 int count = iptr->isn_arg.number;
1989 dict_T *dict = dict_alloc();
1990 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01001991 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992
1993 if (dict == NULL)
1994 goto failed;
1995 for (idx = 0; idx < count; ++idx)
1996 {
Bram Moolenaare8593122020-07-18 15:17:02 +02001997 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02001999 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002000 key = tv->vval.v_string == NULL
2001 ? (char_u *)"" : tv->vval.v_string;
2002 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002003 if (item != NULL)
2004 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002005 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002006 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002007 dict_unref(dict);
2008 goto on_error;
2009 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002010 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 clear_tv(tv);
2012 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002013 {
2014 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002015 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002016 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2018 item->di_tv.v_lock = 0;
2019 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002020 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002021 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002022 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002023 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002024 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002025 }
2026
2027 if (count > 0)
2028 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002029 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002030 goto failed;
2031 else
2032 ++ectx.ec_stack.ga_len;
2033 tv = STACK_TV_BOT(-1);
2034 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002035 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002036 tv->vval.v_dict = dict;
2037 ++dict->dv_refcount;
2038 }
2039 break;
2040
2041 // call a :def function
2042 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002043 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002044 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx,
2045 iptr->isn_arg.dfunc.cdf_argcount,
2046 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002047 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002048 break;
2049
2050 // call a builtin function
2051 case ISN_BCALL:
2052 SOURCING_LNUM = iptr->isn_lnum;
2053 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2054 iptr->isn_arg.bfunc.cbf_argcount,
2055 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002056 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002057 break;
2058
2059 // call a funcref or partial
2060 case ISN_PCALL:
2061 {
2062 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2063 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002064 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002065
2066 SOURCING_LNUM = iptr->isn_lnum;
2067 if (pfunc->cpf_top)
2068 {
2069 // funcref is above the arguments
2070 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2071 }
2072 else
2073 {
2074 // Get the funcref from the stack.
2075 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002076 partial_tv = *STACK_TV_BOT(0);
2077 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002078 }
2079 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002080 if (tv == &partial_tv)
2081 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002082 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002083 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002084 }
2085 break;
2086
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002087 case ISN_PCALL_END:
2088 // PCALL finished, arguments have been consumed and replaced by
2089 // the return value. Now clear the funcref from the stack,
2090 // and move the return value in its place.
2091 --ectx.ec_stack.ga_len;
2092 clear_tv(STACK_TV_BOT(-1));
2093 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2094 break;
2095
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002096 // call a user defined function or funcref/partial
2097 case ISN_UCALL:
2098 {
2099 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2100
2101 SOURCING_LNUM = iptr->isn_lnum;
2102 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002103 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002104 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002105 }
2106 break;
2107
2108 // return from a :def function call
2109 case ISN_RETURN:
2110 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002111 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002112 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002113
2114 if (trystack->ga_len > 0)
2115 trycmd = ((trycmd_T *)trystack->ga_data)
2116 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002117 if (trycmd != NULL
2118 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002119 && trycmd->tcd_finally_idx != 0)
2120 {
2121 // jump to ":finally"
2122 ectx.ec_iidx = trycmd->tcd_finally_idx;
2123 trycmd->tcd_return = TRUE;
2124 }
2125 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002126 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002127 }
2128 break;
2129
2130 // push a function reference to a compiled function
2131 case ISN_FUNCREF:
2132 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002133 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2134 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2135 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002136
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002137 if (pt == NULL)
2138 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002139 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002140 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002141 vim_free(pt);
2142 goto failed;
2143 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002144 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2145 &ectx) == FAIL)
2146 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002147
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002148 tv = STACK_TV_BOT(0);
2149 ++ectx.ec_stack.ga_len;
2150 tv->vval.v_partial = pt;
2151 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002152 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002153 }
2154 break;
2155
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002156 // Create a global function from a lambda.
2157 case ISN_NEWFUNC:
2158 {
2159 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2160
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002161 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002162 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002163 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002164 }
2165 break;
2166
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002167 // List functions
2168 case ISN_DEF:
2169 if (iptr->isn_arg.string == NULL)
2170 list_functions(NULL);
2171 else
2172 {
2173 exarg_T ea;
2174
2175 CLEAR_FIELD(ea);
2176 ea.cmd = ea.arg = iptr->isn_arg.string;
2177 define_function(&ea, NULL);
2178 }
2179 break;
2180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 // jump if a condition is met
2182 case ISN_JUMP:
2183 {
2184 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002185 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 int jump = TRUE;
2187
2188 if (when != JUMP_ALWAYS)
2189 {
2190 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002191 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002192 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002193 || when == JUMP_IF_COND_TRUE)
2194 {
2195 SOURCING_LNUM = iptr->isn_lnum;
2196 jump = tv_get_bool_chk(tv, &error);
2197 if (error)
2198 goto on_error;
2199 }
2200 else
2201 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002202 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002203 || when == JUMP_AND_KEEP_IF_FALSE
2204 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002205 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002206 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002207 {
2208 // drop the value from the stack
2209 clear_tv(tv);
2210 --ectx.ec_stack.ga_len;
2211 }
2212 }
2213 if (jump)
2214 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2215 }
2216 break;
2217
2218 // top of a for loop
2219 case ISN_FOR:
2220 {
2221 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2222 typval_T *idxtv =
2223 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2224
2225 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002226 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002227 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002228 ++idxtv->vval.v_number;
2229 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002230 // past the end of the list, jump to "endfor"
2231 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2232 else if (list->lv_first == &range_list_item)
2233 {
2234 // non-materialized range() list
2235 tv = STACK_TV_BOT(0);
2236 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002237 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 tv->vval.v_number = list_find_nr(
2239 list, idxtv->vval.v_number, NULL);
2240 ++ectx.ec_stack.ga_len;
2241 }
2242 else
2243 {
2244 listitem_T *li = list_find(list, idxtv->vval.v_number);
2245
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002246 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2247 ++ectx.ec_stack.ga_len;
2248 }
2249 }
2250 break;
2251
2252 // start of ":try" block
2253 case ISN_TRY:
2254 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002255 trycmd_T *trycmd = NULL;
2256
Bram Moolenaar270d0382020-05-15 21:42:53 +02002257 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 goto failed;
2259 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2260 + ectx.ec_trystack.ga_len;
2261 ++ectx.ec_trystack.ga_len;
2262 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002263 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2265 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002266 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002267 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 }
2269 break;
2270
2271 case ISN_PUSHEXC:
2272 if (current_exception == NULL)
2273 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002274 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 iemsg("Evaluating catch while current_exception is NULL");
2276 goto failed;
2277 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002278 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002279 goto failed;
2280 tv = STACK_TV_BOT(0);
2281 ++ectx.ec_stack.ga_len;
2282 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002283 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 tv->vval.v_string = vim_strsave(
2285 (char_u *)current_exception->value);
2286 break;
2287
2288 case ISN_CATCH:
2289 {
2290 garray_T *trystack = &ectx.ec_trystack;
2291
Bram Moolenaar20a76292020-12-25 19:47:24 +01002292 if (restore_cmdmod)
2293 {
2294 cmdmod.cmod_filter_regmatch.regprog = NULL;
2295 undo_cmdmod(&cmdmod);
2296 cmdmod = save_cmdmod;
2297 restore_cmdmod = FALSE;
2298 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002299 if (trystack->ga_len > 0)
2300 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002301 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 + trystack->ga_len - 1;
2303 trycmd->tcd_caught = TRUE;
2304 }
2305 did_emsg = got_int = did_throw = FALSE;
2306 catch_exception(current_exception);
2307 }
2308 break;
2309
2310 // end of ":try" block
2311 case ISN_ENDTRY:
2312 {
2313 garray_T *trystack = &ectx.ec_trystack;
2314
2315 if (trystack->ga_len > 0)
2316 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002317 trycmd_T *trycmd = NULL;
2318
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 --trystack->ga_len;
2320 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002321 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 trycmd = ((trycmd_T *)trystack->ga_data)
2323 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002324 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325 {
2326 // discard the exception
2327 if (caught_stack == current_exception)
2328 caught_stack = caught_stack->caught;
2329 discard_current_exception();
2330 }
2331
2332 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002333 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 }
2335 }
2336 break;
2337
2338 case ISN_THROW:
2339 --ectx.ec_stack.ga_len;
2340 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002341 if (tv->vval.v_string == NULL
2342 || *skipwhite(tv->vval.v_string) == NUL)
2343 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002344 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002345 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002346 emsg(_(e_throw_with_empty_string));
2347 goto failed;
2348 }
2349
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002350 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2351 {
2352 vim_free(tv->vval.v_string);
2353 goto failed;
2354 }
2355 did_throw = TRUE;
2356 break;
2357
2358 // compare with special values
2359 case ISN_COMPAREBOOL:
2360 case ISN_COMPARESPECIAL:
2361 {
2362 typval_T *tv1 = STACK_TV_BOT(-2);
2363 typval_T *tv2 = STACK_TV_BOT(-1);
2364 varnumber_T arg1 = tv1->vval.v_number;
2365 varnumber_T arg2 = tv2->vval.v_number;
2366 int res;
2367
2368 switch (iptr->isn_arg.op.op_type)
2369 {
2370 case EXPR_EQUAL: res = arg1 == arg2; break;
2371 case EXPR_NEQUAL: res = arg1 != arg2; break;
2372 default: res = 0; break;
2373 }
2374
2375 --ectx.ec_stack.ga_len;
2376 tv1->v_type = VAR_BOOL;
2377 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2378 }
2379 break;
2380
2381 // Operation with two number arguments
2382 case ISN_OPNR:
2383 case ISN_COMPARENR:
2384 {
2385 typval_T *tv1 = STACK_TV_BOT(-2);
2386 typval_T *tv2 = STACK_TV_BOT(-1);
2387 varnumber_T arg1 = tv1->vval.v_number;
2388 varnumber_T arg2 = tv2->vval.v_number;
2389 varnumber_T res;
2390
2391 switch (iptr->isn_arg.op.op_type)
2392 {
2393 case EXPR_MULT: res = arg1 * arg2; break;
2394 case EXPR_DIV: res = arg1 / arg2; break;
2395 case EXPR_REM: res = arg1 % arg2; break;
2396 case EXPR_SUB: res = arg1 - arg2; break;
2397 case EXPR_ADD: res = arg1 + arg2; break;
2398
2399 case EXPR_EQUAL: res = arg1 == arg2; break;
2400 case EXPR_NEQUAL: res = arg1 != arg2; break;
2401 case EXPR_GREATER: res = arg1 > arg2; break;
2402 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2403 case EXPR_SMALLER: res = arg1 < arg2; break;
2404 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2405 default: res = 0; break;
2406 }
2407
2408 --ectx.ec_stack.ga_len;
2409 if (iptr->isn_type == ISN_COMPARENR)
2410 {
2411 tv1->v_type = VAR_BOOL;
2412 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2413 }
2414 else
2415 tv1->vval.v_number = res;
2416 }
2417 break;
2418
2419 // Computation with two float arguments
2420 case ISN_OPFLOAT:
2421 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002422#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 {
2424 typval_T *tv1 = STACK_TV_BOT(-2);
2425 typval_T *tv2 = STACK_TV_BOT(-1);
2426 float_T arg1 = tv1->vval.v_float;
2427 float_T arg2 = tv2->vval.v_float;
2428 float_T res = 0;
2429 int cmp = FALSE;
2430
2431 switch (iptr->isn_arg.op.op_type)
2432 {
2433 case EXPR_MULT: res = arg1 * arg2; break;
2434 case EXPR_DIV: res = arg1 / arg2; break;
2435 case EXPR_SUB: res = arg1 - arg2; break;
2436 case EXPR_ADD: res = arg1 + arg2; break;
2437
2438 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2439 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2440 case EXPR_GREATER: cmp = arg1 > arg2; break;
2441 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2442 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2443 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2444 default: cmp = 0; break;
2445 }
2446 --ectx.ec_stack.ga_len;
2447 if (iptr->isn_type == ISN_COMPAREFLOAT)
2448 {
2449 tv1->v_type = VAR_BOOL;
2450 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2451 }
2452 else
2453 tv1->vval.v_float = res;
2454 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002455#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 break;
2457
2458 case ISN_COMPARELIST:
2459 {
2460 typval_T *tv1 = STACK_TV_BOT(-2);
2461 typval_T *tv2 = STACK_TV_BOT(-1);
2462 list_T *arg1 = tv1->vval.v_list;
2463 list_T *arg2 = tv2->vval.v_list;
2464 int cmp = FALSE;
2465 int ic = iptr->isn_arg.op.op_ic;
2466
2467 switch (iptr->isn_arg.op.op_type)
2468 {
2469 case EXPR_EQUAL: cmp =
2470 list_equal(arg1, arg2, ic, FALSE); break;
2471 case EXPR_NEQUAL: cmp =
2472 !list_equal(arg1, arg2, ic, FALSE); break;
2473 case EXPR_IS: cmp = arg1 == arg2; break;
2474 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2475 default: cmp = 0; break;
2476 }
2477 --ectx.ec_stack.ga_len;
2478 clear_tv(tv1);
2479 clear_tv(tv2);
2480 tv1->v_type = VAR_BOOL;
2481 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2482 }
2483 break;
2484
2485 case ISN_COMPAREBLOB:
2486 {
2487 typval_T *tv1 = STACK_TV_BOT(-2);
2488 typval_T *tv2 = STACK_TV_BOT(-1);
2489 blob_T *arg1 = tv1->vval.v_blob;
2490 blob_T *arg2 = tv2->vval.v_blob;
2491 int cmp = FALSE;
2492
2493 switch (iptr->isn_arg.op.op_type)
2494 {
2495 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2496 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2497 case EXPR_IS: cmp = arg1 == arg2; break;
2498 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2499 default: cmp = 0; break;
2500 }
2501 --ectx.ec_stack.ga_len;
2502 clear_tv(tv1);
2503 clear_tv(tv2);
2504 tv1->v_type = VAR_BOOL;
2505 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2506 }
2507 break;
2508
2509 // TODO: handle separately
2510 case ISN_COMPARESTRING:
2511 case ISN_COMPAREDICT:
2512 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002513 case ISN_COMPAREANY:
2514 {
2515 typval_T *tv1 = STACK_TV_BOT(-2);
2516 typval_T *tv2 = STACK_TV_BOT(-1);
2517 exptype_T exptype = iptr->isn_arg.op.op_type;
2518 int ic = iptr->isn_arg.op.op_ic;
2519
Bram Moolenaareb26f432020-09-14 16:50:05 +02002520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002521 typval_compare(tv1, tv2, exptype, ic);
2522 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 --ectx.ec_stack.ga_len;
2524 }
2525 break;
2526
2527 case ISN_ADDLIST:
2528 case ISN_ADDBLOB:
2529 {
2530 typval_T *tv1 = STACK_TV_BOT(-2);
2531 typval_T *tv2 = STACK_TV_BOT(-1);
2532
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002533 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 if (iptr->isn_type == ISN_ADDLIST)
2535 eval_addlist(tv1, tv2);
2536 else
2537 eval_addblob(tv1, tv2);
2538 clear_tv(tv2);
2539 --ectx.ec_stack.ga_len;
2540 }
2541 break;
2542
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002543 case ISN_LISTAPPEND:
2544 {
2545 typval_T *tv1 = STACK_TV_BOT(-2);
2546 typval_T *tv2 = STACK_TV_BOT(-1);
2547 list_T *l = tv1->vval.v_list;
2548
2549 // add an item to a list
2550 if (l == NULL)
2551 {
2552 SOURCING_LNUM = iptr->isn_lnum;
2553 emsg(_(e_cannot_add_to_null_list));
2554 goto on_error;
2555 }
2556 if (list_append_tv(l, tv2) == FAIL)
2557 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002558 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002559 --ectx.ec_stack.ga_len;
2560 }
2561 break;
2562
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002563 case ISN_BLOBAPPEND:
2564 {
2565 typval_T *tv1 = STACK_TV_BOT(-2);
2566 typval_T *tv2 = STACK_TV_BOT(-1);
2567 blob_T *b = tv1->vval.v_blob;
2568 int error = FALSE;
2569 varnumber_T n;
2570
2571 // add a number to a blob
2572 if (b == NULL)
2573 {
2574 SOURCING_LNUM = iptr->isn_lnum;
2575 emsg(_(e_cannot_add_to_null_blob));
2576 goto on_error;
2577 }
2578 n = tv_get_number_chk(tv2, &error);
2579 if (error)
2580 goto on_error;
2581 ga_append(&b->bv_ga, (int)n);
2582 --ectx.ec_stack.ga_len;
2583 }
2584 break;
2585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 // Computation with two arguments of unknown type
2587 case ISN_OPANY:
2588 {
2589 typval_T *tv1 = STACK_TV_BOT(-2);
2590 typval_T *tv2 = STACK_TV_BOT(-1);
2591 varnumber_T n1, n2;
2592#ifdef FEAT_FLOAT
2593 float_T f1 = 0, f2 = 0;
2594#endif
2595 int error = FALSE;
2596
2597 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2598 {
2599 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2600 {
2601 eval_addlist(tv1, tv2);
2602 clear_tv(tv2);
2603 --ectx.ec_stack.ga_len;
2604 break;
2605 }
2606 else if (tv1->v_type == VAR_BLOB
2607 && tv2->v_type == VAR_BLOB)
2608 {
2609 eval_addblob(tv1, tv2);
2610 clear_tv(tv2);
2611 --ectx.ec_stack.ga_len;
2612 break;
2613 }
2614 }
2615#ifdef FEAT_FLOAT
2616 if (tv1->v_type == VAR_FLOAT)
2617 {
2618 f1 = tv1->vval.v_float;
2619 n1 = 0;
2620 }
2621 else
2622#endif
2623 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002624 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 n1 = tv_get_number_chk(tv1, &error);
2626 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002627 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628#ifdef FEAT_FLOAT
2629 if (tv2->v_type == VAR_FLOAT)
2630 f1 = n1;
2631#endif
2632 }
2633#ifdef FEAT_FLOAT
2634 if (tv2->v_type == VAR_FLOAT)
2635 {
2636 f2 = tv2->vval.v_float;
2637 n2 = 0;
2638 }
2639 else
2640#endif
2641 {
2642 n2 = tv_get_number_chk(tv2, &error);
2643 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002644 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645#ifdef FEAT_FLOAT
2646 if (tv1->v_type == VAR_FLOAT)
2647 f2 = n2;
2648#endif
2649 }
2650#ifdef FEAT_FLOAT
2651 // if there is a float on either side the result is a float
2652 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2653 {
2654 switch (iptr->isn_arg.op.op_type)
2655 {
2656 case EXPR_MULT: f1 = f1 * f2; break;
2657 case EXPR_DIV: f1 = f1 / f2; break;
2658 case EXPR_SUB: f1 = f1 - f2; break;
2659 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002660 default: SOURCING_LNUM = iptr->isn_lnum;
2661 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002662 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 }
2664 clear_tv(tv1);
2665 clear_tv(tv2);
2666 tv1->v_type = VAR_FLOAT;
2667 tv1->vval.v_float = f1;
2668 --ectx.ec_stack.ga_len;
2669 }
2670 else
2671#endif
2672 {
2673 switch (iptr->isn_arg.op.op_type)
2674 {
2675 case EXPR_MULT: n1 = n1 * n2; break;
2676 case EXPR_DIV: n1 = num_divide(n1, n2); break;
2677 case EXPR_SUB: n1 = n1 - n2; break;
2678 case EXPR_ADD: n1 = n1 + n2; break;
2679 default: n1 = num_modulus(n1, n2); break;
2680 }
2681 clear_tv(tv1);
2682 clear_tv(tv2);
2683 tv1->v_type = VAR_NUMBER;
2684 tv1->vval.v_number = n1;
2685 --ectx.ec_stack.ga_len;
2686 }
2687 }
2688 break;
2689
2690 case ISN_CONCAT:
2691 {
2692 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2693 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2694 char_u *res;
2695
2696 res = concat_str(str1, str2);
2697 clear_tv(STACK_TV_BOT(-2));
2698 clear_tv(STACK_TV_BOT(-1));
2699 --ectx.ec_stack.ga_len;
2700 STACK_TV_BOT(-1)->vval.v_string = res;
2701 }
2702 break;
2703
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002704 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002705 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002706 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002707 int is_slice = iptr->isn_type == ISN_STRSLICE;
2708 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002709 char_u *res;
2710
2711 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002712 // string slice: string is at stack-3, first index at
2713 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002714 if (is_slice)
2715 {
2716 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002717 n1 = tv->vval.v_number;
2718 }
2719
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002720 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002721 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002722
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002723 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002724 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002725 if (is_slice)
2726 // Slice: Select the characters from the string
2727 res = string_slice(tv->vval.v_string, n1, n2);
2728 else
2729 // Index: The resulting variable is a string of a
2730 // single character. If the index is too big or
2731 // negative the result is empty.
2732 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002733 vim_free(tv->vval.v_string);
2734 tv->vval.v_string = res;
2735 }
2736 break;
2737
2738 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02002739 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002740 {
Bram Moolenaared591872020-08-15 22:14:53 +02002741 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002742 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02002743 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002744
2745 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02002746 // list slice: list is at stack-3, indexes at stack-2 and
2747 // stack-1
2748 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002749 list = tv->vval.v_list;
2750
2751 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02002752 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002753 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02002754
2755 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002756 {
Bram Moolenaared591872020-08-15 22:14:53 +02002757 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02002758 n1 = tv->vval.v_number;
2759 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002760 }
Bram Moolenaared591872020-08-15 22:14:53 +02002761
2762 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02002763 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02002764 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaared591872020-08-15 22:14:53 +02002765 if (list_slice_or_index(list, is_slice, n1, n2, tv, TRUE)
2766 == FAIL)
2767 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002768 }
2769 break;
2770
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002771 case ISN_ANYINDEX:
2772 case ISN_ANYSLICE:
2773 {
2774 int is_slice = iptr->isn_type == ISN_ANYSLICE;
2775 typval_T *var1, *var2;
2776 int res;
2777
2778 // index: composite is at stack-2, index at stack-1
2779 // slice: composite is at stack-3, indexes at stack-2 and
2780 // stack-1
2781 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002782 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02002783 if (check_can_index(tv, TRUE, TRUE) == FAIL)
2784 goto on_error;
2785 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
2786 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
2787 res = eval_index_inner(tv, is_slice,
2788 var1, var2, NULL, -1, TRUE);
2789 clear_tv(var1);
2790 if (is_slice)
2791 clear_tv(var2);
2792 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
2793 if (res == FAIL)
2794 goto on_error;
2795 }
2796 break;
2797
Bram Moolenaar9af78762020-06-16 11:34:42 +02002798 case ISN_SLICE:
2799 {
2800 list_T *list;
2801 int count = iptr->isn_arg.number;
2802
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02002803 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02002804 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02002805 list = tv->vval.v_list;
2806
2807 // no error for short list, expect it to be checked earlier
2808 if (list != NULL && list->lv_len >= count)
2809 {
2810 list_T *newlist = list_slice(list,
2811 count, list->lv_len - 1);
2812
2813 if (newlist != NULL)
2814 {
2815 list_unref(list);
2816 tv->vval.v_list = newlist;
2817 ++newlist->lv_refcount;
2818 }
2819 }
2820 }
2821 break;
2822
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002823 case ISN_GETITEM:
2824 {
2825 listitem_T *li;
2826 int index = iptr->isn_arg.number;
2827
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002828 // Get list item: list is at stack-1, push item.
2829 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002830 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02002831 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02002832
2833 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2834 goto failed;
2835 ++ectx.ec_stack.ga_len;
2836 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
2837 }
2838 break;
2839
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002840 case ISN_MEMBER:
2841 {
2842 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002843 char_u *key;
2844 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002845 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002846
2847 // dict member: dict is at stack-2, key at stack-1
2848 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002849 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002850 dict = tv->vval.v_dict;
2851
2852 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002853 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002854 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01002855 if (key == NULL)
2856 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02002857
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002858 if ((di = dict_find(dict, key, -1)) == NULL)
2859 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002860 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002861 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01002862
2863 // If :silent! is used we will continue, make sure the
2864 // stack contents makes sense.
2865 clear_tv(tv);
2866 --ectx.ec_stack.ga_len;
2867 tv = STACK_TV_BOT(-1);
2868 clear_tv(tv);
2869 tv->v_type = VAR_NUMBER;
2870 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002871 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002872 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002873 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002874 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01002875 // Clear the dict only after getting the item, to avoid
2876 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02002877 tv = STACK_TV_BOT(-1);
2878 temp_tv = *tv;
2879 copy_tv(&di->di_tv, tv);
2880 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002881 }
2882 break;
2883
2884 // dict member with string key
2885 case ISN_STRINGMEMBER:
2886 {
2887 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002888 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002889 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002890
2891 tv = STACK_TV_BOT(-1);
2892 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
2893 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002894 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002895 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02002896 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002897 }
2898 dict = tv->vval.v_dict;
2899
2900 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
2901 == NULL)
2902 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002903 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002904 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02002905 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002906 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002907 // Clear the dict after getting the item, to avoid that it
2908 // make the item invalid.
2909 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02002911 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002912 }
2913 break;
2914
2915 case ISN_NEGATENR:
2916 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002917 if (tv->v_type != VAR_NUMBER
2918#ifdef FEAT_FLOAT
2919 && tv->v_type != VAR_FLOAT
2920#endif
2921 )
2922 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002923 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002924 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002925 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02002926 }
2927#ifdef FEAT_FLOAT
2928 if (tv->v_type == VAR_FLOAT)
2929 tv->vval.v_float = -tv->vval.v_float;
2930 else
2931#endif
2932 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002933 break;
2934
2935 case ISN_CHECKNR:
2936 {
2937 int error = FALSE;
2938
2939 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02002940 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002941 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002942 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002943 (void)tv_get_number_chk(tv, &error);
2944 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002945 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002946 }
2947 break;
2948
2949 case ISN_CHECKTYPE:
2950 {
2951 checktype_T *ct = &iptr->isn_arg.type;
2952
2953 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02002954 SOURCING_LNUM = iptr->isn_lnum;
2955 if (check_typval_type(ct->ct_type, tv, 0) == FAIL)
2956 goto on_error;
2957
2958 // number 0 is FALSE, number 1 is TRUE
2959 if (tv->v_type == VAR_NUMBER
2960 && ct->ct_type->tt_type == VAR_BOOL
2961 && (tv->vval.v_number == 0
2962 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002963 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02002964 tv->v_type = VAR_BOOL;
2965 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02002966 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002967 }
2968 }
2969 break;
2970
Bram Moolenaar9af78762020-06-16 11:34:42 +02002971 case ISN_CHECKLEN:
2972 {
2973 int min_len = iptr->isn_arg.checklen.cl_min_len;
2974 list_T *list = NULL;
2975
2976 tv = STACK_TV_BOT(-1);
2977 if (tv->v_type == VAR_LIST)
2978 list = tv->vval.v_list;
2979 if (list == NULL || list->lv_len < min_len
2980 || (list->lv_len > min_len
2981 && !iptr->isn_arg.checklen.cl_more_OK))
2982 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002983 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02002984 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02002985 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002986 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02002987 }
2988 }
2989 break;
2990
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002992 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002993 {
2994 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002995 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002996
2997 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002998 if (iptr->isn_type == ISN_2BOOL)
2999 {
3000 n = tv2bool(tv);
3001 if (iptr->isn_arg.number) // invert
3002 n = !n;
3003 }
3004 else
3005 {
3006 SOURCING_LNUM = iptr->isn_lnum;
3007 n = tv_get_bool_chk(tv, &error);
3008 if (error)
3009 goto on_error;
3010 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003011 clear_tv(tv);
3012 tv->v_type = VAR_BOOL;
3013 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3014 }
3015 break;
3016
3017 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003018 case ISN_2STRING_ANY:
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003019 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3020 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3021 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 break;
3023
Bram Moolenaar08597872020-12-10 19:43:40 +01003024 case ISN_RANGE:
3025 {
3026 exarg_T ea;
3027 char *errormsg;
3028
3029 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3030 goto failed;
3031 ++ectx.ec_stack.ga_len;
3032 tv = STACK_TV_BOT(-1);
3033 ea.addr_type = ADDR_LINES;
3034 ea.cmd = iptr->isn_arg.string;
3035 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
3036 goto failed;
3037 if (ea.addr_count == 0)
3038 tv->vval.v_number = curwin->w_cursor.lnum;
3039 else
3040 tv->vval.v_number = ea.line2;
3041 }
3042 break;
3043
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003044 case ISN_PUT:
3045 {
3046 int regname = iptr->isn_arg.put.put_regname;
3047 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3048 char_u *expr = NULL;
3049 int dir = FORWARD;
3050
3051 if (regname == '=')
3052 {
3053 tv = STACK_TV_BOT(-1);
3054 if (tv->v_type == VAR_STRING)
3055 expr = tv->vval.v_string;
3056 else
3057 {
3058 expr = typval_tostring(tv); // allocates value
3059 clear_tv(tv);
3060 }
3061 --ectx.ec_stack.ga_len;
3062 }
Bram Moolenaar08597872020-12-10 19:43:40 +01003063 if (lnum < -2)
3064 {
3065 // line number was put on the stack by ISN_RANGE
3066 tv = STACK_TV_BOT(-1);
3067 curwin->w_cursor.lnum = tv->vval.v_number;
3068 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3069 dir = BACKWARD;
3070 --ectx.ec_stack.ga_len;
3071 }
3072 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003073 // :put! above cursor
3074 dir = BACKWARD;
3075 else if (lnum >= 0)
3076 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
3077 check_cursor();
3078 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3079 vim_free(expr);
3080 }
3081 break;
3082
Bram Moolenaar02194d22020-10-24 23:08:38 +02003083 case ISN_CMDMOD:
3084 save_cmdmod = cmdmod;
3085 restore_cmdmod = TRUE;
3086 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3087 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003088 break;
3089
Bram Moolenaar02194d22020-10-24 23:08:38 +02003090 case ISN_CMDMOD_REV:
3091 // filter regprog is owned by the instruction, don't free it
3092 cmdmod.cmod_filter_regmatch.regprog = NULL;
3093 undo_cmdmod(&cmdmod);
3094 cmdmod = save_cmdmod;
3095 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003096 break;
3097
Bram Moolenaar792f7862020-11-23 08:31:18 +01003098 case ISN_UNPACK:
3099 {
3100 int count = iptr->isn_arg.unpack.unp_count;
3101 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3102 list_T *l;
3103 listitem_T *li;
3104 int i;
3105
3106 // Check there is a valid list to unpack.
3107 tv = STACK_TV_BOT(-1);
3108 if (tv->v_type != VAR_LIST)
3109 {
3110 SOURCING_LNUM = iptr->isn_lnum;
3111 emsg(_(e_for_argument_must_be_sequence_of_lists));
3112 goto on_error;
3113 }
3114 l = tv->vval.v_list;
3115 if (l == NULL
3116 || l->lv_len < (semicolon ? count - 1 : count))
3117 {
3118 SOURCING_LNUM = iptr->isn_lnum;
3119 emsg(_(e_list_value_does_not_have_enough_items));
3120 goto on_error;
3121 }
3122 else if (!semicolon && l->lv_len > count)
3123 {
3124 SOURCING_LNUM = iptr->isn_lnum;
3125 emsg(_(e_list_value_has_more_items_than_targets));
3126 goto on_error;
3127 }
3128
3129 CHECK_LIST_MATERIALIZE(l);
3130 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3131 goto failed;
3132 ectx.ec_stack.ga_len += count - 1;
3133
3134 // Variable after semicolon gets a list with the remaining
3135 // items.
3136 if (semicolon)
3137 {
3138 list_T *rem_list =
3139 list_alloc_with_items(l->lv_len - count + 1);
3140
3141 if (rem_list == NULL)
3142 goto failed;
3143 tv = STACK_TV_BOT(-count);
3144 tv->vval.v_list = rem_list;
3145 ++rem_list->lv_refcount;
3146 tv->v_lock = 0;
3147 li = l->lv_first;
3148 for (i = 0; i < count - 1; ++i)
3149 li = li->li_next;
3150 for (i = 0; li != NULL; ++i)
3151 {
3152 list_set_item(rem_list, i, &li->li_tv);
3153 li = li->li_next;
3154 }
3155 --count;
3156 }
3157
3158 // Produce the values in reverse order, first item last.
3159 li = l->lv_first;
3160 for (i = 0; i < count; ++i)
3161 {
3162 tv = STACK_TV_BOT(-i - 1);
3163 copy_tv(&li->li_tv, tv);
3164 li = li->li_next;
3165 }
3166
3167 list_unref(l);
3168 }
3169 break;
3170
Bram Moolenaar389df252020-07-09 21:20:47 +02003171 case ISN_SHUFFLE:
3172 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003173 typval_T tmp_tv;
3174 int item = iptr->isn_arg.shuffle.shfl_item;
3175 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003176
3177 tmp_tv = *STACK_TV_BOT(-item);
3178 for ( ; up > 0 && item > 1; --up)
3179 {
3180 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3181 --item;
3182 }
3183 *STACK_TV_BOT(-item) = tmp_tv;
3184 }
3185 break;
3186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 case ISN_DROP:
3188 --ectx.ec_stack.ga_len;
3189 clear_tv(STACK_TV_BOT(0));
3190 break;
3191 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003192 continue;
3193
Bram Moolenaard032f342020-07-18 18:13:02 +02003194func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003195 // Restore previous function. If the frame pointer is where we started
3196 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003197 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003198 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003199
Bram Moolenaard032f342020-07-18 18:13:02 +02003200 if (func_return(&ectx) == FAIL)
3201 // only fails when out of memory
3202 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003203 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003204
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003205on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003206 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003207 // If "emsg_silent" is set then ignore the error, unless it was set
3208 // when calling the function.
3209 if (did_emsg_cumul + did_emsg == did_emsg_before
3210 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003211 continue;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003212on_fatal_error:
3213 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003214 // If we are not inside a try-catch started here, abort execution.
3215 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003216 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003217 }
3218
3219done:
3220 // function finished, get result from the stack.
3221 tv = STACK_TV_BOT(-1);
3222 *rettv = *tv;
3223 tv->v_type = VAR_UNKNOWN;
3224 ret = OK;
3225
3226failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003227 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003228 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003229 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003230
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003231 // Deal with any remaining closures, they may be in use somewhere.
3232 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003233 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003234 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003235 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3236 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003237
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003238 estack_pop();
3239 current_sctx = save_current_sctx;
3240
Bram Moolenaar352134b2020-10-17 22:04:08 +02003241 if (*msg_list != NULL && saved_msg_list != NULL)
3242 {
3243 msglist_T **plist = saved_msg_list;
3244
3245 // Append entries from the current msg_list (uncaught exceptions) to
3246 // the saved msg_list.
3247 while (*plist != NULL)
3248 plist = &(*plist)->next;
3249
3250 *plist = *msg_list;
3251 }
3252 msg_list = saved_msg_list;
3253
Bram Moolenaar02194d22020-10-24 23:08:38 +02003254 if (restore_cmdmod)
3255 {
3256 cmdmod.cmod_filter_regmatch.regprog = NULL;
3257 undo_cmdmod(&cmdmod);
3258 cmdmod = save_cmdmod;
3259 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003260 emsg_silent_def = save_emsg_silent_def;
3261 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003262
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003263failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003264 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3266 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003268 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003269 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003270
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003271 // Not sure if this is necessary.
3272 suppress_errthrow = save_suppress_errthrow;
3273
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003274 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003275 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003276 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003277 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 return ret;
3279}
3280
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003282 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003283 * We don't really need this at runtime, but we do have tests that require it,
3284 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 */
3286 void
3287ex_disassemble(exarg_T *eap)
3288{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003289 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003290 char_u *fname;
3291 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003292 dfunc_T *dfunc;
3293 isn_T *instr;
3294 int current;
3295 int line_idx = 0;
3296 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003297 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003298
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003299 if (STRNCMP(arg, "<lambda>", 8) == 0)
3300 {
3301 arg += 8;
3302 (void)getdigits(&arg);
3303 fname = vim_strnsave(eap->arg, arg - eap->arg);
3304 }
3305 else
3306 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003307 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003308 if (fname == NULL)
3309 {
3310 semsg(_(e_invarg2), eap->arg);
3311 return;
3312 }
3313
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003314 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003315 if (ufunc == NULL)
3316 {
3317 char_u *p = untrans_function_name(fname);
3318
3319 if (p != NULL)
3320 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003321 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003322 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003323 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 if (ufunc == NULL)
3325 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003326 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003327 return;
3328 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003329 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003330 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3331 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003332 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003333 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003334 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003335 return;
3336 }
3337 if (ufunc->uf_name_exp != NULL)
3338 msg((char *)ufunc->uf_name_exp);
3339 else
3340 msg((char *)ufunc->uf_name);
3341
3342 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3343 instr = dfunc->df_instr;
3344 for (current = 0; current < dfunc->df_instr_count; ++current)
3345 {
3346 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003347 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348
3349 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3350 {
3351 if (current > prev_current)
3352 {
3353 msg_puts("\n\n");
3354 prev_current = current;
3355 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003356 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3357 if (line != NULL)
3358 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003359 }
3360
3361 switch (iptr->isn_type)
3362 {
3363 case ISN_EXEC:
3364 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3365 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003366 case ISN_EXECCONCAT:
3367 smsg("%4d EXECCONCAT %lld", current,
3368 (long long)iptr->isn_arg.number);
3369 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 case ISN_ECHO:
3371 {
3372 echo_T *echo = &iptr->isn_arg.echo;
3373
3374 smsg("%4d %s %d", current,
3375 echo->echo_with_white ? "ECHO" : "ECHON",
3376 echo->echo_count);
3377 }
3378 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003379 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003380 smsg("%4d EXECUTE %lld", current,
3381 (long long)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003382 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003383 case ISN_ECHOMSG:
3384 smsg("%4d ECHOMSG %lld", current,
3385 (long long)(iptr->isn_arg.number));
3386 break;
3387 case ISN_ECHOERR:
3388 smsg("%4d ECHOERR %lld", current,
3389 (long long)(iptr->isn_arg.number));
3390 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003391 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003392 case ISN_LOADOUTER:
3393 {
3394 char *add = iptr->isn_type == ISN_LOAD ? "" : "OUTER";
3395
3396 if (iptr->isn_arg.number < 0)
3397 smsg("%4d LOAD%s arg[%lld]", current, add,
3398 (long long)(iptr->isn_arg.number
3399 + STACK_FRAME_SIZE));
3400 else
3401 smsg("%4d LOAD%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003402 (long long)(iptr->isn_arg.number));
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003403 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003404 break;
3405 case ISN_LOADV:
3406 smsg("%4d LOADV v:%s", current,
3407 get_vim_var_name(iptr->isn_arg.number));
3408 break;
3409 case ISN_LOADSCRIPT:
3410 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003411 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3412 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003413 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003414 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003415
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003416 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3417 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003418 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003419 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420 }
3421 break;
3422 case ISN_LOADS:
3423 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003424 scriptitem_T *si = SCRIPT_ITEM(
3425 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426
3427 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003428 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003429 }
3430 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003431 case ISN_LOADAUTO:
3432 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3433 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 case ISN_LOADG:
3435 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3436 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003437 case ISN_LOADB:
3438 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3439 break;
3440 case ISN_LOADW:
3441 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3442 break;
3443 case ISN_LOADT:
3444 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3445 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003446 case ISN_LOADGDICT:
3447 smsg("%4d LOAD g:", current);
3448 break;
3449 case ISN_LOADBDICT:
3450 smsg("%4d LOAD b:", current);
3451 break;
3452 case ISN_LOADWDICT:
3453 smsg("%4d LOAD w:", current);
3454 break;
3455 case ISN_LOADTDICT:
3456 smsg("%4d LOAD t:", current);
3457 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003458 case ISN_LOADOPT:
3459 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3460 break;
3461 case ISN_LOADENV:
3462 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3463 break;
3464 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003465 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 break;
3467
3468 case ISN_STORE:
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003469 case ISN_STOREOUTER:
3470 {
3471 char *add = iptr->isn_type == ISN_STORE ? "" : "OUTER";
3472
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003473 if (iptr->isn_arg.number < 0)
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003474 smsg("%4d STORE%s arg[%lld]", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003475 (long long)(iptr->isn_arg.number + STACK_FRAME_SIZE));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003476 else
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003477 smsg("%4d STORE%s $%lld", current, add,
Bram Moolenaar10827722020-03-23 22:53:22 +01003478 (long long)(iptr->isn_arg.number));
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003479 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003480 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003481 case ISN_STOREV:
3482 smsg("%4d STOREV v:%s", current,
3483 get_vim_var_name(iptr->isn_arg.number));
3484 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003485 case ISN_STOREAUTO:
3486 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3487 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003488 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003489 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3490 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003491 case ISN_STOREB:
3492 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3493 break;
3494 case ISN_STOREW:
3495 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3496 break;
3497 case ISN_STORET:
3498 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3499 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003500 case ISN_STORES:
3501 {
3502 scriptitem_T *si = SCRIPT_ITEM(
3503 iptr->isn_arg.loadstore.ls_sid);
3504
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003505 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003506 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003507 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003508 break;
3509 case ISN_STORESCRIPT:
3510 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003511 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3512 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003513 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003514 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003515
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003516 smsg("%4d STORESCRIPT %s-%d in %s", current,
3517 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003518 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003519 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003520 }
3521 break;
3522 case ISN_STOREOPT:
3523 smsg("%4d STOREOPT &%s", current,
3524 iptr->isn_arg.storeopt.so_name);
3525 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003526 case ISN_STOREENV:
3527 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3528 break;
3529 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003530 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003531 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 case ISN_STORENR:
3533 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003534 iptr->isn_arg.storenr.stnr_val,
3535 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003536 break;
3537
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003538 case ISN_STOREINDEX:
3539 switch (iptr->isn_arg.vartype)
3540 {
3541 case VAR_LIST:
3542 smsg("%4d STORELIST", current);
3543 break;
3544 case VAR_DICT:
3545 smsg("%4d STOREDICT", current);
3546 break;
3547 case VAR_ANY:
3548 smsg("%4d STOREINDEX", current);
3549 break;
3550 default: break;
3551 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003552 break;
3553
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003554 // constants
3555 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003556 smsg("%4d PUSHNR %lld", current,
3557 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003558 break;
3559 case ISN_PUSHBOOL:
3560 case ISN_PUSHSPEC:
3561 smsg("%4d PUSH %s", current,
3562 get_var_special_name(iptr->isn_arg.number));
3563 break;
3564 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003565#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003566 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003567#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003568 break;
3569 case ISN_PUSHS:
3570 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3571 break;
3572 case ISN_PUSHBLOB:
3573 {
3574 char_u *r;
3575 char_u numbuf[NUMBUFLEN];
3576 char_u *tofree;
3577
3578 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003579 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003580 vim_free(tofree);
3581 }
3582 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003583 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003584 {
3585 char *name = (char *)iptr->isn_arg.string;
3586
3587 smsg("%4d PUSHFUNC \"%s\"", current,
3588 name == NULL ? "[none]" : name);
3589 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003590 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003591 case ISN_PUSHCHANNEL:
3592#ifdef FEAT_JOB_CHANNEL
3593 {
3594 channel_T *channel = iptr->isn_arg.channel;
3595
3596 smsg("%4d PUSHCHANNEL %d", current,
3597 channel == NULL ? 0 : channel->ch_id);
3598 }
3599#endif
3600 break;
3601 case ISN_PUSHJOB:
3602#ifdef FEAT_JOB_CHANNEL
3603 {
3604 typval_T tv;
3605 char_u *name;
3606
3607 tv.v_type = VAR_JOB;
3608 tv.vval.v_job = iptr->isn_arg.job;
3609 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003610 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003611 }
3612#endif
3613 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003614 case ISN_PUSHEXC:
3615 smsg("%4d PUSH v:exception", current);
3616 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003617 case ISN_UNLET:
3618 smsg("%4d UNLET%s %s", current,
3619 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3620 iptr->isn_arg.unlet.ul_name);
3621 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003622 case ISN_UNLETENV:
3623 smsg("%4d UNLETENV%s $%s", current,
3624 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3625 iptr->isn_arg.unlet.ul_name);
3626 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003627 case ISN_LOCKCONST:
3628 smsg("%4d LOCKCONST", current);
3629 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003630 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003631 smsg("%4d NEWLIST size %lld", current,
3632 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003633 break;
3634 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003635 smsg("%4d NEWDICT size %lld", current,
3636 (long long)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003637 break;
3638
3639 // function call
3640 case ISN_BCALL:
3641 {
3642 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
3643
3644 smsg("%4d BCALL %s(argc %d)", current,
3645 internal_func_name(cbfunc->cbf_idx),
3646 cbfunc->cbf_argcount);
3647 }
3648 break;
3649 case ISN_DCALL:
3650 {
3651 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
3652 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
3653 + cdfunc->cdf_idx;
3654
3655 smsg("%4d DCALL %s(argc %d)", current,
3656 df->df_ufunc->uf_name_exp != NULL
3657 ? df->df_ufunc->uf_name_exp
3658 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
3659 }
3660 break;
3661 case ISN_UCALL:
3662 {
3663 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
3664
3665 smsg("%4d UCALL %s(argc %d)", current,
3666 cufunc->cuf_name, cufunc->cuf_argcount);
3667 }
3668 break;
3669 case ISN_PCALL:
3670 {
3671 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
3672
3673 smsg("%4d PCALL%s (argc %d)", current,
3674 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
3675 }
3676 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02003677 case ISN_PCALL_END:
3678 smsg("%4d PCALL end", current);
3679 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 case ISN_RETURN:
3681 smsg("%4d RETURN", current);
3682 break;
3683 case ISN_FUNCREF:
3684 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003685 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003686 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02003687 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02003689 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690 }
3691 break;
3692
Bram Moolenaar38ddf332020-07-31 22:05:04 +02003693 case ISN_NEWFUNC:
3694 {
3695 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
3696
3697 smsg("%4d NEWFUNC %s %s", current,
3698 newfunc->nf_lambda, newfunc->nf_global);
3699 }
3700 break;
3701
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01003702 case ISN_DEF:
3703 {
3704 char_u *name = iptr->isn_arg.string;
3705
3706 smsg("%4d DEF %s", current,
3707 name == NULL ? (char_u *)"" : name);
3708 }
3709 break;
3710
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003711 case ISN_JUMP:
3712 {
3713 char *when = "?";
3714
3715 switch (iptr->isn_arg.jump.jump_when)
3716 {
3717 case JUMP_ALWAYS:
3718 when = "JUMP";
3719 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003720 case JUMP_AND_KEEP_IF_TRUE:
3721 when = "JUMP_AND_KEEP_IF_TRUE";
3722 break;
3723 case JUMP_IF_FALSE:
3724 when = "JUMP_IF_FALSE";
3725 break;
3726 case JUMP_AND_KEEP_IF_FALSE:
3727 when = "JUMP_AND_KEEP_IF_FALSE";
3728 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003729 case JUMP_IF_COND_FALSE:
3730 when = "JUMP_IF_COND_FALSE";
3731 break;
3732 case JUMP_IF_COND_TRUE:
3733 when = "JUMP_IF_COND_TRUE";
3734 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01003736 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003737 iptr->isn_arg.jump.jump_where);
3738 }
3739 break;
3740
3741 case ISN_FOR:
3742 {
3743 forloop_T *forloop = &iptr->isn_arg.forloop;
3744
3745 smsg("%4d FOR $%d -> %d", current,
3746 forloop->for_idx, forloop->for_end);
3747 }
3748 break;
3749
3750 case ISN_TRY:
3751 {
3752 try_T *try = &iptr->isn_arg.try;
3753
3754 smsg("%4d TRY catch -> %d, finally -> %d", current,
3755 try->try_catch, try->try_finally);
3756 }
3757 break;
3758 case ISN_CATCH:
3759 // TODO
3760 smsg("%4d CATCH", current);
3761 break;
3762 case ISN_ENDTRY:
3763 smsg("%4d ENDTRY", current);
3764 break;
3765 case ISN_THROW:
3766 smsg("%4d THROW", current);
3767 break;
3768
3769 // expression operations on number
3770 case ISN_OPNR:
3771 case ISN_OPFLOAT:
3772 case ISN_OPANY:
3773 {
3774 char *what;
3775 char *ins;
3776
3777 switch (iptr->isn_arg.op.op_type)
3778 {
3779 case EXPR_MULT: what = "*"; break;
3780 case EXPR_DIV: what = "/"; break;
3781 case EXPR_REM: what = "%"; break;
3782 case EXPR_SUB: what = "-"; break;
3783 case EXPR_ADD: what = "+"; break;
3784 default: what = "???"; break;
3785 }
3786 switch (iptr->isn_type)
3787 {
3788 case ISN_OPNR: ins = "OPNR"; break;
3789 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
3790 case ISN_OPANY: ins = "OPANY"; break;
3791 default: ins = "???"; break;
3792 }
3793 smsg("%4d %s %s", current, ins, what);
3794 }
3795 break;
3796
3797 case ISN_COMPAREBOOL:
3798 case ISN_COMPARESPECIAL:
3799 case ISN_COMPARENR:
3800 case ISN_COMPAREFLOAT:
3801 case ISN_COMPARESTRING:
3802 case ISN_COMPAREBLOB:
3803 case ISN_COMPARELIST:
3804 case ISN_COMPAREDICT:
3805 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003806 case ISN_COMPAREANY:
3807 {
3808 char *p;
3809 char buf[10];
3810 char *type;
3811
3812 switch (iptr->isn_arg.op.op_type)
3813 {
3814 case EXPR_EQUAL: p = "=="; break;
3815 case EXPR_NEQUAL: p = "!="; break;
3816 case EXPR_GREATER: p = ">"; break;
3817 case EXPR_GEQUAL: p = ">="; break;
3818 case EXPR_SMALLER: p = "<"; break;
3819 case EXPR_SEQUAL: p = "<="; break;
3820 case EXPR_MATCH: p = "=~"; break;
3821 case EXPR_IS: p = "is"; break;
3822 case EXPR_ISNOT: p = "isnot"; break;
3823 case EXPR_NOMATCH: p = "!~"; break;
3824 default: p = "???"; break;
3825 }
3826 STRCPY(buf, p);
3827 if (iptr->isn_arg.op.op_ic == TRUE)
3828 strcat(buf, "?");
3829 switch(iptr->isn_type)
3830 {
3831 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
3832 case ISN_COMPARESPECIAL:
3833 type = "COMPARESPECIAL"; break;
3834 case ISN_COMPARENR: type = "COMPARENR"; break;
3835 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
3836 case ISN_COMPARESTRING:
3837 type = "COMPARESTRING"; break;
3838 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
3839 case ISN_COMPARELIST: type = "COMPARELIST"; break;
3840 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
3841 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003842 case ISN_COMPAREANY: type = "COMPAREANY"; break;
3843 default: type = "???"; break;
3844 }
3845
3846 smsg("%4d %s %s", current, type, buf);
3847 }
3848 break;
3849
3850 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
3851 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
3852
3853 // expression operations
3854 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003855 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003856 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003857 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003858 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003859 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02003860 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003861 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
3862 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003863 case ISN_SLICE: smsg("%4d SLICE %lld",
3864 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003865 case ISN_GETITEM: smsg("%4d ITEM %lld",
3866 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003867 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
3868 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003869 iptr->isn_arg.string); break;
3870 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
3871
3872 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003873 case ISN_CHECKTYPE:
3874 {
3875 char *tofree;
3876
3877 smsg("%4d CHECKTYPE %s stack[%d]", current,
3878 type_name(iptr->isn_arg.type.ct_type, &tofree),
3879 iptr->isn_arg.type.ct_off);
3880 vim_free(tofree);
3881 break;
3882 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02003883 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
3884 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
3885 iptr->isn_arg.checklen.cl_min_len);
3886 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003887 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003888 case ISN_2BOOL: if (iptr->isn_arg.number)
3889 smsg("%4d INVERT (!val)", current);
3890 else
3891 smsg("%4d 2BOOL (!!val)", current);
3892 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01003893 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
3894 (long long)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02003895 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003896 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
3897 (long long)(iptr->isn_arg.number));
3898 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01003899 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
3900 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003901 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01003902 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
3903 smsg("%4d PUT %c above range",
3904 current, iptr->isn_arg.put.put_regname);
3905 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
3906 smsg("%4d PUT %c range",
3907 current, iptr->isn_arg.put.put_regname);
3908 else
3909 smsg("%4d PUT %c %ld", current,
3910 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003911 (long)iptr->isn_arg.put.put_lnum);
3912 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003913
Bram Moolenaar02194d22020-10-24 23:08:38 +02003914 // TODO: summarize modifiers
3915 case ISN_CMDMOD:
3916 {
3917 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01003918 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02003919 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3920
3921 buf = alloc(len + 1);
3922 if (buf != NULL)
3923 {
3924 (void)produce_cmdmods(
3925 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
3926 smsg("%4d CMDMOD %s", current, buf);
3927 vim_free(buf);
3928 }
3929 break;
3930 }
3931 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003932
Bram Moolenaar792f7862020-11-23 08:31:18 +01003933 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
3934 iptr->isn_arg.unpack.unp_count,
3935 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
3936 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02003937 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
3938 iptr->isn_arg.shuffle.shfl_item,
3939 iptr->isn_arg.shuffle.shfl_up);
3940 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003941 case ISN_DROP: smsg("%4d DROP", current); break;
3942 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02003943
3944 out_flush(); // output one line at a time
3945 ui_breakcheck();
3946 if (got_int)
3947 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949}
3950
3951/*
Bram Moolenaar13106602020-10-04 16:06:05 +02003952 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003953 * list, etc. Mostly like what JavaScript does, except that empty list and
3954 * empty dictionary are FALSE.
3955 */
3956 int
3957tv2bool(typval_T *tv)
3958{
3959 switch (tv->v_type)
3960 {
3961 case VAR_NUMBER:
3962 return tv->vval.v_number != 0;
3963 case VAR_FLOAT:
3964#ifdef FEAT_FLOAT
3965 return tv->vval.v_float != 0.0;
3966#else
3967 break;
3968#endif
3969 case VAR_PARTIAL:
3970 return tv->vval.v_partial != NULL;
3971 case VAR_FUNC:
3972 case VAR_STRING:
3973 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
3974 case VAR_LIST:
3975 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
3976 case VAR_DICT:
3977 return tv->vval.v_dict != NULL
3978 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
3979 case VAR_BOOL:
3980 case VAR_SPECIAL:
3981 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
3982 case VAR_JOB:
3983#ifdef FEAT_JOB_CHANNEL
3984 return tv->vval.v_job != NULL;
3985#else
3986 break;
3987#endif
3988 case VAR_CHANNEL:
3989#ifdef FEAT_JOB_CHANNEL
3990 return tv->vval.v_channel != NULL;
3991#else
3992 break;
3993#endif
3994 case VAR_BLOB:
3995 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
3996 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02003997 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 case VAR_VOID:
3999 break;
4000 }
4001 return FALSE;
4002}
4003
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004004 void
4005emsg_using_string_as(typval_T *tv, int as_number)
4006{
4007 semsg(_(as_number ? e_using_string_as_number_str
4008 : e_using_string_as_bool_str),
4009 tv->vval.v_string == NULL
4010 ? (char_u *)"" : tv->vval.v_string);
4011}
4012
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004013/*
4014 * If "tv" is a string give an error and return FAIL.
4015 */
4016 int
4017check_not_string(typval_T *tv)
4018{
4019 if (tv->v_type == VAR_STRING)
4020 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004021 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 clear_tv(tv);
4023 return FAIL;
4024 }
4025 return OK;
4026}
4027
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028
4029#endif // FEAT_EVAL