blob: 7d1d079bb19c93db677dffb69380298b58ab9cd6 [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 Moolenaar0186e582021-01-10 18:33:11 +010061 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020062
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063 garray_T ec_trystack; // stack of trycmd_T values
64 int ec_in_catch; // when TRUE in catch or finally block
65
66 int ec_dfunc_idx; // current function index
67 isn_T *ec_instr; // array with instructions
68 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020069
70 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010071};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010072
73// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020074#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 +010075
Bram Moolenaar418f1df2020-08-12 21:34:49 +020076 void
77to_string_error(vartype_T vartype)
78{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020079 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020080}
81
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010082/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010083 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010084 */
85 static int
86ufunc_argcount(ufunc_T *ufunc)
87{
88 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
89}
90
91/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010092 * Set the instruction index, depending on omitted arguments, where the default
93 * values are to be computed. If all optional arguments are present, start
94 * with the function body.
95 * The expression evaluation is at the start of the instructions:
96 * 0 -> EVAL default1
97 * STORE arg[-2]
98 * 1 -> EVAL default2
99 * STORE arg[-1]
100 * 2 -> function body
101 */
102 static void
103init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
104{
105 if (ufunc->uf_def_args.ga_len == 0)
106 ectx->ec_iidx = 0;
107 else
108 {
109 int defcount = ufunc->uf_args.ga_len - argcount;
110
111 // If there is a varargs argument defcount can be negative, no defaults
112 // to evaluate then.
113 if (defcount < 0)
114 defcount = 0;
115 ectx->ec_iidx = ufunc->uf_def_arg_idx[
116 ufunc->uf_def_args.ga_len - defcount];
117 }
118}
119
120/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200121 * Create a new list from "count" items at the bottom of the stack.
122 * When "count" is zero an empty list is added to the stack.
123 */
124 static int
125exe_newlist(int count, ectx_T *ectx)
126{
127 list_T *list = list_alloc_with_items(count);
128 int idx;
129 typval_T *tv;
130
131 if (list == NULL)
132 return FAIL;
133 for (idx = 0; idx < count; ++idx)
134 list_set_item(list, idx, STACK_TV_BOT(idx - count));
135
136 if (count > 0)
137 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200138 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200139 return FAIL;
140 else
141 ++ectx->ec_stack.ga_len;
142 tv = STACK_TV_BOT(-1);
143 tv->v_type = VAR_LIST;
144 tv->vval.v_list = list;
145 ++list->lv_refcount;
146 return OK;
147}
148
149/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100150 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100151 * This adds a stack frame and sets the instruction pointer to the start of the
152 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100153 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100154 *
155 * Stack has:
156 * - current arguments (already there)
157 * - omitted optional argument (default values) added here
158 * - stack frame:
159 * - pointer to calling function
160 * - Index of next instruction in calling function
161 * - previous frame pointer
162 * - reserved space for local variables
163 */
164 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100165call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100166{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200167 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100168 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
169 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200170 int arg_to_add;
171 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200172 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100173 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200174 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100175
176 if (dfunc->df_deleted)
177 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100178 // don't use ufunc->uf_name, it may have been freed
179 emsg_funcname(e_func_deleted,
180 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 return FAIL;
182 }
183
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200184 if (ufunc->uf_va_name != NULL)
185 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200186 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200187 // Stack at time of call with 2 varargs:
188 // normal_arg
189 // optional_arg
190 // vararg_1
191 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200192 // After creating the list:
193 // normal_arg
194 // optional_arg
195 // vararg-list
196 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200198 // After creating the list
199 // normal_arg
200 // (space for optional_arg)
201 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200202 vararg_count = argcount - ufunc->uf_args.ga_len;
203 if (vararg_count < 0)
204 vararg_count = 0;
205 else
206 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200207 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200208 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209
210 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200211 }
212
Bram Moolenaarfe270812020-04-11 22:31:27 +0200213 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200214 if (arg_to_add < 0)
215 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200216 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200217 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200218 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200219 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200220 return FAIL;
221 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200222
223 // Reserve space for:
224 // - missing arguments
225 // - stack frame
226 // - local variables
227 // - if needed: a counter for number of closures created in
228 // ectx->ec_funcrefs.
229 varcount = dfunc->df_varcount + dfunc->df_has_closure;
230 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
231 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100232 return FAIL;
233
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100234 // If depth of calling is getting too high, don't execute the function.
235 if (funcdepth_increment() == FAIL)
236 return FAIL;
237
Bram Moolenaarfe270812020-04-11 22:31:27 +0200238 // Move the vararg-list to below the missing optional arguments.
239 if (vararg_count > 0 && arg_to_add > 0)
240 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100241
242 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200244 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200245 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100246
247 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100248 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
249 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100250 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
251 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200252 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100253
254 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200255 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200257 if (dfunc->df_has_closure)
258 {
259 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
260
261 tv->v_type = VAR_NUMBER;
262 tv->vval.v_number = 0;
263 }
264 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100265
Bram Moolenaar0186e582021-01-10 18:33:11 +0100266 if (pt != NULL || ufunc->uf_partial != NULL || ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100267 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100268 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
269
270 if (outer == NULL)
271 return FAIL;
272 if (pt != NULL)
273 {
274 *outer = pt->pt_outer;
275 outer->out_up_is_copy = TRUE;
276 }
277 else if (ufunc->uf_partial != NULL)
278 {
279 *outer = ufunc->uf_partial->pt_outer;
280 outer->out_up_is_copy = TRUE;
281 }
282 else
283 {
284 outer->out_stack = &ectx->ec_stack;
285 outer->out_frame_idx = ectx->ec_frame_idx;
286 outer->out_up = ectx->ec_outer;
287 }
288 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100289 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100290 else
291 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100293 // Set execution state to the start of the called function.
294 ectx->ec_dfunc_idx = cdf_idx;
295 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200296 entry = estack_push_ufunc(dfunc->df_ufunc, 1);
297 if (entry != NULL)
298 {
299 // Set the script context to the script where the function was defined.
300 // TODO: save more than the SID?
301 entry->es_save_sid = current_sctx.sc_sid;
302 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
303 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100304
305 // Decide where to start execution, handles optional arguments.
306 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100307
308 return OK;
309}
310
311// Get pointer to item in the stack.
312#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
313
314/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200315 * Used when returning from a function: Check if any closure is still
316 * referenced. If so then move the arguments and variables to a separate piece
317 * of stack to be used when the closure is called.
318 * When "free_arguments" is TRUE the arguments are to be freed.
319 * Returns FAIL when out of memory.
320 */
321 static int
322handle_closure_in_use(ectx_T *ectx, int free_arguments)
323{
324 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
325 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200326 int argcount;
327 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200328 int idx;
329 typval_T *tv;
330 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200331 garray_T *gap = &ectx->ec_funcrefs;
332 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200333
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200334 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200335 return OK; // function was freed
336 if (dfunc->df_has_closure == 0)
337 return OK; // no closures
338 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
339 closure_count = tv->vval.v_number;
340 if (closure_count == 0)
341 return OK; // no funcrefs created
342
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200343 argcount = ufunc_argcount(dfunc->df_ufunc);
344 top = ectx->ec_frame_idx - argcount;
345
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200346 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200347 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200349 partial_T *pt;
350 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200351
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200352 if (off < 0)
353 continue; // count is off or already done
354 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200355 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200356 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200357 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200358 int i;
359
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200360 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200361 // unreferenced on return.
362 for (i = 0; i < dfunc->df_varcount; ++i)
363 {
364 typval_T *stv = STACK_TV(ectx->ec_frame_idx
365 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200366 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200367 --refcount;
368 }
369 if (refcount > 1)
370 {
371 closure_in_use = TRUE;
372 break;
373 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200374 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200375 }
376
377 if (closure_in_use)
378 {
379 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
380 typval_T *stack;
381
382 // A closure is using the arguments and/or local variables.
383 // Move them to the called function.
384 if (funcstack == NULL)
385 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200386 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
387 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200388 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
389 funcstack->fs_ga.ga_data = stack;
390 if (stack == NULL)
391 {
392 vim_free(funcstack);
393 return FAIL;
394 }
395
396 // Move or copy the arguments.
397 for (idx = 0; idx < argcount; ++idx)
398 {
399 tv = STACK_TV(top + idx);
400 if (free_arguments)
401 {
402 *(stack + idx) = *tv;
403 tv->v_type = VAR_UNKNOWN;
404 }
405 else
406 copy_tv(tv, stack + idx);
407 }
408 // Move the local variables.
409 for (idx = 0; idx < dfunc->df_varcount; ++idx)
410 {
411 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200412
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200413 // A partial created for a local function, that is also used as a
414 // local variable, has a reference count for the variable, thus
415 // will never go down to zero. When all these refcounts are one
416 // then the funcstack is unused. We need to count how many we have
417 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200418 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
419 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200420 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200421
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200422 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200423 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
424 gap->ga_len - closure_count + i])
425 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200426 }
427
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200428 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200429 tv->v_type = VAR_UNKNOWN;
430 }
431
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200432 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200433 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200434 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
435 - closure_count + idx];
436 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200437 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200438 ++funcstack->fs_refcount;
439 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100440 pt->pt_outer.out_stack = &funcstack->fs_ga;
441 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
442 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200443 }
444 }
445 }
446
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200447 for (idx = 0; idx < closure_count; ++idx)
448 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
449 - closure_count + idx]);
450 gap->ga_len -= closure_count;
451 if (gap->ga_len == 0)
452 ga_clear(gap);
453
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200454 return OK;
455}
456
457/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200458 * Called when a partial is freed or its reference count goes down to one. The
459 * funcstack may be the only reference to the partials in the local variables.
460 * Go over all of them, the funcref and can be freed if all partials
461 * referencing the funcstack have a reference count of one.
462 */
463 void
464funcstack_check_refcount(funcstack_T *funcstack)
465{
466 int i;
467 garray_T *gap = &funcstack->fs_ga;
468 int done = 0;
469
470 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
471 return;
472 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
473 {
474 typval_T *tv = ((typval_T *)gap->ga_data) + i;
475
476 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
477 && tv->vval.v_partial->pt_funcstack == funcstack
478 && tv->vval.v_partial->pt_refcount == 1)
479 ++done;
480 }
481 if (done == funcstack->fs_min_refcount)
482 {
483 typval_T *stack = gap->ga_data;
484
485 // All partials referencing the funcstack have a reference count of
486 // one, thus the funcstack is no longer of use.
487 for (i = 0; i < gap->ga_len; ++i)
488 clear_tv(stack + i);
489 vim_free(stack);
490 vim_free(funcstack);
491 }
492}
493
494/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100495 * Return from the current function.
496 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200497 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100498func_return(ectx_T *ectx)
499{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100500 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100501 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200502 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
503 + ectx->ec_dfunc_idx;
504 int argcount = ufunc_argcount(dfunc->df_ufunc);
505 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200506 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507
508 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200509 entry = estack_pop();
510 if (entry != NULL)
511 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 if (handle_closure_in_use(ectx, TRUE) == FAIL)
514 return FAIL;
515
516 // Clear the arguments.
517 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
518 clear_tv(STACK_TV(idx));
519
520 // Clear local variables and temp values, but not the return value.
521 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100522 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100524
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100525 // The return value should be on top of the stack. However, when aborting
526 // it may not be there and ec_frame_idx is the top of the stack.
527 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100528 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100529 ret_idx = 0;
530
Bram Moolenaar0186e582021-01-10 18:33:11 +0100531 vim_free(ectx->ec_outer);
532
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100533 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100534 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
535 + STACK_FRAME_FUNC_OFF)->vval.v_number;
536 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
537 + STACK_FRAME_IIDX_OFF)->vval.v_number;
538 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
539 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200540 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100541 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
542 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100543 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
544 ectx->ec_instr = dfunc->df_instr;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100545
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100546 if (ret_idx > 0)
547 {
548 // Reset the stack to the position before the call, with a spot for the
549 // return value, moved there from above the frame.
550 ectx->ec_stack.ga_len = top + 1;
551 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
552 }
553 else
554 // Reset the stack to the position before the call.
555 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200556
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100557 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200558 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559}
560
561#undef STACK_TV
562
563/*
564 * Prepare arguments and rettv for calling a builtin or user function.
565 */
566 static int
567call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
568{
569 int idx;
570 typval_T *tv;
571
572 // Move arguments from bottom of the stack to argvars[] and add terminator.
573 for (idx = 0; idx < argcount; ++idx)
574 argvars[idx] = *STACK_TV_BOT(idx - argcount);
575 argvars[argcount].v_type = VAR_UNKNOWN;
576
577 // Result replaces the arguments on the stack.
578 if (argcount > 0)
579 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200580 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100581 return FAIL;
582 else
583 ++ectx->ec_stack.ga_len;
584
585 // Default return value is zero.
586 tv = STACK_TV_BOT(-1);
587 tv->v_type = VAR_NUMBER;
588 tv->vval.v_number = 0;
589
590 return OK;
591}
592
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200593// Ugly global to avoid passing the execution context around through many
594// layers.
595static ectx_T *current_ectx = NULL;
596
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100597/*
598 * Call a builtin function by index.
599 */
600 static int
601call_bfunc(int func_idx, int argcount, ectx_T *ectx)
602{
603 typval_T argvars[MAX_FUNC_ARGS];
604 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100605 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200606 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100607
608 if (call_prepare(argcount, argvars, ectx) == FAIL)
609 return FAIL;
610
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200611 // Call the builtin function. Set "current_ectx" so that when it
612 // recursively invokes call_def_function() a closure context can be set.
613 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100614 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200615 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100616
617 // Clear the arguments.
618 for (idx = 0; idx < argcount; ++idx)
619 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200620
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100621 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200622 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100623 return OK;
624}
625
626/*
627 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100628 * If the function is compiled this will add a stack frame and set the
629 * instruction pointer at the start of the function.
630 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100631 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100632 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100633 */
634 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100635call_ufunc(
636 ufunc_T *ufunc,
637 partial_T *pt,
638 int argcount,
639 ectx_T *ectx,
640 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100641{
642 typval_T argvars[MAX_FUNC_ARGS];
643 funcexe_T funcexe;
644 int error;
645 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100646 int did_emsg_before = did_emsg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100647
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200648 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +0200649 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
650 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200651 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100652 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100653 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100654 if (error != FCERR_UNKNOWN)
655 {
656 if (error == FCERR_TOOMANY)
657 semsg(_(e_toomanyarg), ufunc->uf_name);
658 else
659 semsg(_(e_toofewarg), ufunc->uf_name);
660 return FAIL;
661 }
662
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100663 // The function has been compiled, can call it quickly. For a function
664 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100665 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100666 if (iptr != NULL)
667 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100668 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 iptr->isn_type = ISN_DCALL;
670 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
671 iptr->isn_arg.dfunc.cdf_argcount = argcount;
672 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100673 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100674 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100675
676 if (call_prepare(argcount, argvars, ectx) == FAIL)
677 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200678 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100679 funcexe.evaluate = TRUE;
680
681 // Call the user function. Result goes in last position on the stack.
682 // TODO: add selfdict if there is one
683 error = call_user_func_check(ufunc, argcount, argvars,
684 STACK_TV_BOT(-1), &funcexe, NULL);
685
686 // Clear the arguments.
687 for (idx = 0; idx < argcount; ++idx)
688 clear_tv(&argvars[idx]);
689
690 if (error != FCERR_NONE)
691 {
692 user_func_error(error, ufunc->uf_name);
693 return FAIL;
694 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200696 // Error other than from calling the function itself.
697 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100698 return OK;
699}
700
701/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200702 * Return TRUE if an error was given or CTRL-C was pressed.
703 */
704 static int
705vim9_aborting(int prev_called_emsg)
706{
707 return called_emsg > prev_called_emsg || got_int || did_throw;
708}
709
710/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100711 * Execute a function by "name".
712 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100713 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 * Returns FAIL if not found without an error message.
715 */
716 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100718{
719 ufunc_T *ufunc;
720
721 if (builtin_function(name, -1))
722 {
723 int func_idx = find_internal_func(name);
724
725 if (func_idx < 0)
726 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200727 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 return FAIL;
729 return call_bfunc(func_idx, argcount, ectx);
730 }
731
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200732 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200733
734 if (ufunc == NULL)
735 {
736 int called_emsg_before = called_emsg;
737
738 if (script_autoload(name, TRUE))
739 // loaded a package, search for the function again
740 ufunc = find_func(name, FALSE, NULL);
741 if (vim9_aborting(called_emsg_before))
742 return FAIL; // bail out if loading the script caused an error
743 }
744
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100746 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100747
748 return FAIL;
749}
750
751 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200752call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100753{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200754 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200755 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100756 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100757 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100758
759 if (tv->v_type == VAR_PARTIAL)
760 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200761 partial_T *pt = tv->vval.v_partial;
762 int i;
763
764 if (pt->pt_argc > 0)
765 {
766 // Make space for arguments from the partial, shift the "argcount"
767 // arguments up.
768 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
769 return FAIL;
770 for (i = 1; i <= argcount; ++i)
771 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
772 ectx->ec_stack.ga_len += pt->pt_argc;
773 argcount += pt->pt_argc;
774
775 // copy the arguments from the partial onto the stack
776 for (i = 0; i < pt->pt_argc; ++i)
777 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
778 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100779
780 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100781 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200782
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100783 name = pt->pt_name;
784 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200785 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100786 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200787 if (name != NULL)
788 {
789 char_u fname_buf[FLEN_FIXED + 1];
790 char_u *tofree = NULL;
791 int error = FCERR_NONE;
792 char_u *fname;
793
794 // May need to translate <SNR>123_ to K_SNR.
795 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
796 if (error != FCERR_NONE)
797 res = FAIL;
798 else
799 res = call_by_name(fname, argcount, ectx, NULL);
800 vim_free(tofree);
801 }
802
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100803 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 {
805 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200806 semsg(_(e_unknownfunc),
807 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100808 return FAIL;
809 }
810 return OK;
811}
812
813/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200814 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
815 * TRUE.
816 */
817 static int
818error_if_locked(int lock, char *error)
819{
820 if (lock & (VAR_LOCKED | VAR_FIXED))
821 {
822 emsg(_(error));
823 return TRUE;
824 }
825 return FALSE;
826}
827
828/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100829 * Store "tv" in variable "name".
830 * This is for s: and g: variables.
831 */
832 static void
833store_var(char_u *name, typval_T *tv)
834{
835 funccal_entry_T entry;
836
837 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100838 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100839 restore_funccal();
840}
841
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100842/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100843 * Convert "tv" to a string.
844 * Return FAIL if not allowed.
845 */
846 static int
847do_2string(typval_T *tv, int is_2string_any)
848{
849 if (tv->v_type != VAR_STRING)
850 {
851 char_u *str;
852
853 if (is_2string_any)
854 {
855 switch (tv->v_type)
856 {
857 case VAR_SPECIAL:
858 case VAR_BOOL:
859 case VAR_NUMBER:
860 case VAR_FLOAT:
861 case VAR_BLOB: break;
862 default: to_string_error(tv->v_type);
863 return FAIL;
864 }
865 }
866 str = typval_tostring(tv);
867 clear_tv(tv);
868 tv->v_type = VAR_STRING;
869 tv->vval.v_string = str;
870 }
871 return OK;
872}
873
874/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100875 * When the value of "sv" is a null list of dict, allocate it.
876 */
877 static void
878allocate_if_null(typval_T *tv)
879{
880 switch (tv->v_type)
881 {
882 case VAR_LIST:
883 if (tv->vval.v_list == NULL)
884 rettv_list_alloc(tv);
885 break;
886 case VAR_DICT:
887 if (tv->vval.v_dict == NULL)
888 rettv_dict_alloc(tv);
889 break;
890 default:
891 break;
892 }
893}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200894
Bram Moolenaare7525c52021-01-09 13:20:37 +0100895/*
896 * Return the character "str[index]" where "index" is the character index. If
897 * "index" is out of range NULL is returned.
898 */
899 char_u *
900char_from_string(char_u *str, varnumber_T index)
901{
902 size_t nbyte = 0;
903 varnumber_T nchar = index;
904 size_t slen;
905
906 if (str == NULL)
907 return NULL;
908 slen = STRLEN(str);
909
910 // do the same as for a list: a negative index counts from the end
911 if (index < 0)
912 {
913 int clen = 0;
914
915 for (nbyte = 0; nbyte < slen; ++clen)
916 nbyte += MB_CPTR2LEN(str + nbyte);
917 nchar = clen + index;
918 if (nchar < 0)
919 // unlike list: index out of range results in empty string
920 return NULL;
921 }
922
923 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
924 nbyte += MB_CPTR2LEN(str + nbyte);
925 if (nbyte >= slen)
926 return NULL;
927 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
928}
929
930/*
931 * Get the byte index for character index "idx" in string "str" with length
932 * "str_len".
933 * If going over the end return "str_len".
934 * If "idx" is negative count from the end, -1 is the last character.
935 * When going over the start return -1.
936 */
937 static long
938char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
939{
940 varnumber_T nchar = idx;
941 size_t nbyte = 0;
942
943 if (nchar >= 0)
944 {
945 while (nchar > 0 && nbyte < str_len)
946 {
947 nbyte += MB_CPTR2LEN(str + nbyte);
948 --nchar;
949 }
950 }
951 else
952 {
953 nbyte = str_len;
954 while (nchar < 0 && nbyte > 0)
955 {
956 --nbyte;
957 nbyte -= mb_head_off(str, str + nbyte);
958 ++nchar;
959 }
960 if (nchar < 0)
961 return -1;
962 }
963 return (long)nbyte;
964}
965
966/*
967 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100968 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100969 * Return NULL when the result is empty.
970 */
971 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100972string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100973{
974 long start_byte, end_byte;
975 size_t slen;
976
977 if (str == NULL)
978 return NULL;
979 slen = STRLEN(str);
980 start_byte = char_idx2byte(str, slen, first);
981 if (start_byte < 0)
982 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100983 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100984 end_byte = (long)slen;
985 else
986 {
987 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +0100988 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100989 // end index is inclusive
990 end_byte += MB_CPTR2LEN(str + end_byte);
991 }
992
993 if (start_byte >= (long)slen || end_byte <= start_byte)
994 return NULL;
995 return vim_strnsave(str + start_byte, end_byte - start_byte);
996}
997
Bram Moolenaar07a65d22020-12-26 20:09:15 +0100998 static svar_T *
999get_script_svar(scriptref_T *sref, ectx_T *ectx)
1000{
1001 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1002 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1003 + ectx->ec_dfunc_idx;
1004 svar_T *sv;
1005
1006 if (sref->sref_seq != si->sn_script_seq)
1007 {
1008 // The script was reloaded after the function was
1009 // compiled, the script_idx may not be valid.
1010 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1011 dfunc->df_ufunc->uf_name_exp);
1012 return NULL;
1013 }
1014 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1015 if (!equal_type(sv->sv_type, sref->sref_type))
1016 {
1017 emsg(_(e_script_variable_type_changed));
1018 return NULL;
1019 }
1020 return sv;
1021}
1022
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001023/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001024 * Execute a function by "name".
1025 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001026 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001027 */
1028 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001029call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001030{
Bram Moolenaared677f52020-08-12 16:38:10 +02001031 int called_emsg_before = called_emsg;
1032 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001033
Bram Moolenaared677f52020-08-12 16:38:10 +02001034 res = call_by_name(name, argcount, ectx, iptr);
1035 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001036 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001037 dictitem_T *v;
1038
1039 v = find_var(name, NULL, FALSE);
1040 if (v == NULL)
1041 {
1042 semsg(_(e_unknownfunc), name);
1043 return FAIL;
1044 }
1045 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1046 {
1047 semsg(_(e_unknownfunc), name);
1048 return FAIL;
1049 }
1050 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001051 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001052 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053}
1054
1055/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001056 * When a function reference is used, fill a partial with the information
1057 * needed, especially when it is used as a closure.
1058 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001059 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001060fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1061{
1062 pt->pt_func = ufunc;
1063 pt->pt_refcount = 1;
1064
1065 if (pt->pt_func->uf_flags & FC_CLOSURE)
1066 {
1067 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1068 + ectx->ec_dfunc_idx;
1069
1070 // The closure needs to find arguments and local
1071 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001072 pt->pt_outer.out_stack = &ectx->ec_stack;
1073 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1074 pt->pt_outer.out_up = ectx->ec_outer;
1075 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001076
1077 // If this function returns and the closure is still
1078 // being used, we need to make a copy of the context
1079 // (arguments and local variables). Store a reference
1080 // to the partial so we can handle that.
1081 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1082 {
1083 vim_free(pt);
1084 return FAIL;
1085 }
1086 // Extra variable keeps the count of closures created
1087 // in the current function call.
1088 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1089 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1090
1091 ((partial_T **)ectx->ec_funcrefs.ga_data)
1092 [ectx->ec_funcrefs.ga_len] = pt;
1093 ++pt->pt_refcount;
1094 ++ectx->ec_funcrefs.ga_len;
1095 }
1096 ++pt->pt_func->uf_refcount;
1097 return OK;
1098}
1099
1100/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001101 * Call a "def" function from old Vim script.
1102 * Return OK or FAIL.
1103 */
1104 int
1105call_def_function(
1106 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001107 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001108 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001109 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001110 typval_T *rettv) // return value
1111{
1112 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001113 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001114 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001115 typval_T *tv;
1116 int idx;
1117 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001118 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001119 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001120 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001121 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001122 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001123 msglist_T **saved_msg_list = NULL;
1124 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001125 cmdmod_T save_cmdmod;
1126 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001127 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001128 int save_emsg_silent_def = emsg_silent_def;
1129 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001130 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001131 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132
1133// Get pointer to item in the stack.
1134#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1135
1136// Get pointer to item at the bottom of the stack, -1 is the bottom.
1137#undef STACK_TV_BOT
1138#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1139
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001140// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001141#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 +01001142
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001143 if (ufunc->uf_def_status == UF_NOT_COMPILED
1144 || (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02001145 && compile_def_function(ufunc, FALSE, NULL) == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001146 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001147 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001148 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001149 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001150 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001151 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001152
Bram Moolenaar09689a02020-05-09 22:50:08 +02001153 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001154 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001155 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1156 + ufunc->uf_dfunc_idx;
1157 if (dfunc->df_instr == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001158 {
1159 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001160 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001161 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001162 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001163
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001164 // If depth of calling is getting too high, don't execute the function.
1165 orig_funcdepth = funcdepth_get();
1166 if (funcdepth_increment() == FAIL)
1167 return FAIL;
1168
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001169 CLEAR_FIELD(ectx);
1170 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1171 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1172 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001173 {
1174 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001175 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001176 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001177 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001178 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001179
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001180 // Put arguments on the stack, but no more than what the function expects.
1181 // A lambda can be called with more arguments than it uses.
1182 for (idx = 0; idx < argc
1183 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1184 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001186 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001187 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1188 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001189 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001190 copy_tv(&argv[idx], STACK_TV_BOT(0));
1191 ++ectx.ec_stack.ga_len;
1192 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001193
1194 // Turn varargs into a list. Empty list if no args.
1195 if (ufunc->uf_va_name != NULL)
1196 {
1197 int vararg_count = argc - ufunc->uf_args.ga_len;
1198
1199 if (vararg_count < 0)
1200 vararg_count = 0;
1201 else
1202 argc -= vararg_count;
1203 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001204 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001205
1206 // Check the type of the list items.
1207 tv = STACK_TV_BOT(-1);
1208 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001209 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001210 && ufunc->uf_va_type->tt_member != &t_any
1211 && tv->vval.v_list != NULL)
1212 {
1213 type_T *expected = ufunc->uf_va_type->tt_member;
1214 listitem_T *li = tv->vval.v_list->lv_first;
1215
1216 for (idx = 0; idx < vararg_count; ++idx)
1217 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001218 if (check_typval_type(expected, &li->li_tv,
1219 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001220 goto failed_early;
1221 li = li->li_next;
1222 }
1223 }
1224
Bram Moolenaar23e03252020-04-12 22:22:31 +02001225 if (defcount > 0)
1226 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001227 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001228 --ectx.ec_stack.ga_len;
1229 }
1230
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001231 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001232 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001233 if (defcount > 0)
1234 for (idx = 0; idx < defcount; ++idx)
1235 {
1236 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1237 ++ectx.ec_stack.ga_len;
1238 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001239 if (ufunc->uf_va_name != NULL)
1240 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001241
1242 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001243 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1244 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001245
Bram Moolenaar0186e582021-01-10 18:33:11 +01001246 if (partial != NULL || ufunc->uf_partial != NULL)
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001247 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001248 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1249 if (ectx.ec_outer == NULL)
1250 goto failed_early;
1251 if (partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001252 {
Bram Moolenaar0186e582021-01-10 18:33:11 +01001253 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1254 {
1255 if (current_ectx->ec_outer != NULL)
1256 *ectx.ec_outer = *current_ectx->ec_outer;
1257 }
1258 else
1259 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001260 }
1261 else
Bram Moolenaar0186e582021-01-10 18:33:11 +01001262 *ectx.ec_outer = ufunc->uf_partial->pt_outer;
1263 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001264 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001265
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001266 // dummy frame entries
1267 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1268 {
1269 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1270 ++ectx.ec_stack.ga_len;
1271 }
1272
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001273 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001274 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001275 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1276 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001278 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001279 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001280 ectx.ec_stack.ga_len += dfunc->df_varcount;
1281 if (dfunc->df_has_closure)
1282 {
1283 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1284 STACK_TV_VAR(idx)->vval.v_number = 0;
1285 ++ectx.ec_stack.ga_len;
1286 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001287
1288 ectx.ec_instr = dfunc->df_instr;
1289 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001290
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001291 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001292 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001293 estack_push_ufunc(ufunc, 1);
1294 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001295 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1296
Bram Moolenaar352134b2020-10-17 22:04:08 +02001297 // Use a specific location for storing error messages to be converted to an
1298 // exception.
1299 saved_msg_list = msg_list;
1300 msg_list = &private_msg_list;
1301
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001302 // Do turn errors into exceptions.
1303 suppress_errthrow = FALSE;
1304
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001305 // When ":silent!" was used before calling then we still abort the
1306 // function. If ":silent!" is used in the function then we don't.
1307 emsg_silent_def = emsg_silent;
1308 did_emsg_def = 0;
1309
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001310 // Decide where to start execution, handles optional arguments.
1311 init_instr_idx(ufunc, argc, &ectx);
1312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313 for (;;)
1314 {
1315 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001316
Bram Moolenaar270d0382020-05-15 21:42:53 +02001317 if (++breakcheck_count >= 100)
1318 {
1319 line_breakcheck();
1320 breakcheck_count = 0;
1321 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001322 if (got_int)
1323 {
1324 // Turn CTRL-C into an exception.
1325 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001326 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001327 goto failed;
1328 did_throw = TRUE;
1329 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001330
Bram Moolenaara26b9702020-04-18 19:53:28 +02001331 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1332 {
1333 // Turn an error message into an exception.
1334 did_emsg = FALSE;
1335 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1336 goto failed;
1337 did_throw = TRUE;
1338 *msg_list = NULL;
1339 }
1340
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001341 if (did_throw && !ectx.ec_in_catch)
1342 {
1343 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001344 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001345
1346 // An exception jumps to the first catch, finally, or returns from
1347 // the current function.
1348 if (trystack->ga_len > 0)
1349 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001350 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001351 {
1352 // jump to ":catch" or ":finally"
1353 ectx.ec_in_catch = TRUE;
1354 ectx.ec_iidx = trycmd->tcd_catch_idx;
1355 }
1356 else
1357 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001358 // Not inside try or need to return from current functions.
1359 // Push a dummy return value.
1360 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1361 goto failed;
1362 tv = STACK_TV_BOT(0);
1363 tv->v_type = VAR_NUMBER;
1364 tv->vval.v_number = 0;
1365 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001366 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001368 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001369 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001370 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1371 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 goto done;
1373 }
1374
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001375 if (func_return(&ectx) == FAIL)
1376 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 }
1378 continue;
1379 }
1380
1381 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1382 switch (iptr->isn_type)
1383 {
1384 // execute Ex command line
1385 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001386 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001387 source_cookie_T cookie;
1388
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001389 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001390 // Pass getsourceline to get an error for a missing ":end"
1391 // command.
1392 CLEAR_FIELD(cookie);
1393 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1394 if (do_cmdline(iptr->isn_arg.string,
1395 getsourceline, &cookie,
1396 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1397 == FAIL
1398 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001399 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001400 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001401 break;
1402
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001403 // execute Ex command from pieces on the stack
1404 case ISN_EXECCONCAT:
1405 {
1406 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001407 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001408 int pass;
1409 int i;
1410 char_u *cmd = NULL;
1411 char_u *str;
1412
1413 for (pass = 1; pass <= 2; ++pass)
1414 {
1415 for (i = 0; i < count; ++i)
1416 {
1417 tv = STACK_TV_BOT(i - count);
1418 str = tv->vval.v_string;
1419 if (str != NULL && *str != NUL)
1420 {
1421 if (pass == 2)
1422 STRCPY(cmd + len, str);
1423 len += STRLEN(str);
1424 }
1425 if (pass == 2)
1426 clear_tv(tv);
1427 }
1428 if (pass == 1)
1429 {
1430 cmd = alloc(len + 1);
1431 if (cmd == NULL)
1432 goto failed;
1433 len = 0;
1434 }
1435 }
1436
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001437 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001438 do_cmdline_cmd(cmd);
1439 vim_free(cmd);
1440 }
1441 break;
1442
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001443 // execute :echo {string} ...
1444 case ISN_ECHO:
1445 {
1446 int count = iptr->isn_arg.echo.echo_count;
1447 int atstart = TRUE;
1448 int needclr = TRUE;
1449
1450 for (idx = 0; idx < count; ++idx)
1451 {
1452 tv = STACK_TV_BOT(idx - count);
1453 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1454 &atstart, &needclr);
1455 clear_tv(tv);
1456 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001457 if (needclr)
1458 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 ectx.ec_stack.ga_len -= count;
1460 }
1461 break;
1462
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001463 // :execute {string} ...
1464 // :echomsg {string} ...
1465 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001466 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001467 case ISN_ECHOMSG:
1468 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001469 {
1470 int count = iptr->isn_arg.number;
1471 garray_T ga;
1472 char_u buf[NUMBUFLEN];
1473 char_u *p;
1474 int len;
1475 int failed = FALSE;
1476
1477 ga_init2(&ga, 1, 80);
1478 for (idx = 0; idx < count; ++idx)
1479 {
1480 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001481 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001482 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001483 if (tv->v_type == VAR_CHANNEL
1484 || tv->v_type == VAR_JOB)
1485 {
1486 SOURCING_LNUM = iptr->isn_lnum;
1487 emsg(_(e_inval_string));
1488 break;
1489 }
1490 else
1491 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001492 }
1493 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001494 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001495
1496 len = (int)STRLEN(p);
1497 if (ga_grow(&ga, len + 2) == FAIL)
1498 failed = TRUE;
1499 else
1500 {
1501 if (ga.ga_len > 0)
1502 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1503 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1504 ga.ga_len += len;
1505 }
1506 clear_tv(tv);
1507 }
1508 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001509 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001510 {
1511 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001512 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001513 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001514
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001515 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001516 {
1517 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001518 {
1519 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001520 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001521 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001522 {
1523 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001524 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001525 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001526 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001527 else
1528 {
1529 msg_sb_eol();
1530 if (iptr->isn_type == ISN_ECHOMSG)
1531 {
1532 msg_attr(ga.ga_data, echo_attr);
1533 out_flush();
1534 }
1535 else
1536 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001537 SOURCING_LNUM = iptr->isn_lnum;
1538 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001539 }
1540 }
1541 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001542 ga_clear(&ga);
1543 }
1544 break;
1545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 // load local variable or argument
1547 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001548 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001549 goto failed;
1550 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1551 ++ectx.ec_stack.ga_len;
1552 break;
1553
1554 // load v: variable
1555 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001556 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001557 goto failed;
1558 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1559 ++ectx.ec_stack.ga_len;
1560 break;
1561
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001562 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001563 case ISN_LOADSCRIPT:
1564 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001565 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001566 svar_T *sv;
1567
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001568 sv = get_script_svar(sref, &ectx);
1569 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001570 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001571 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001572 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001573 goto failed;
1574 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1575 ++ectx.ec_stack.ga_len;
1576 }
1577 break;
1578
1579 // load s: variable in old script
1580 case ISN_LOADS:
1581 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001582 hashtab_T *ht = &SCRIPT_VARS(
1583 iptr->isn_arg.loadstore.ls_sid);
1584 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001585 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001586
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 if (di == NULL)
1588 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001589 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001590 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001591 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 }
1593 else
1594 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001595 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 goto failed;
1597 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1598 ++ectx.ec_stack.ga_len;
1599 }
1600 }
1601 break;
1602
Bram Moolenaard3aac292020-04-19 14:32:17 +02001603 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001605 case ISN_LOADB:
1606 case ISN_LOADW:
1607 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001608 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001609 dictitem_T *di = NULL;
1610 hashtab_T *ht = NULL;
1611 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001612
Bram Moolenaard3aac292020-04-19 14:32:17 +02001613 switch (iptr->isn_type)
1614 {
1615 case ISN_LOADG:
1616 ht = get_globvar_ht();
1617 namespace = 'g';
1618 break;
1619 case ISN_LOADB:
1620 ht = &curbuf->b_vars->dv_hashtab;
1621 namespace = 'b';
1622 break;
1623 case ISN_LOADW:
1624 ht = &curwin->w_vars->dv_hashtab;
1625 namespace = 'w';
1626 break;
1627 case ISN_LOADT:
1628 ht = &curtab->tp_vars->dv_hashtab;
1629 namespace = 't';
1630 break;
1631 default: // Cannot reach here
1632 goto failed;
1633 }
1634 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001635
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001636 if (di == NULL)
1637 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001638 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001639 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001640 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001641 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001642 }
1643 else
1644 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001645 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 goto failed;
1647 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1648 ++ectx.ec_stack.ga_len;
1649 }
1650 }
1651 break;
1652
Bram Moolenaar03290b82020-12-19 16:30:44 +01001653 // load autoload variable
1654 case ISN_LOADAUTO:
1655 {
1656 char_u *name = iptr->isn_arg.string;
1657
1658 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1659 goto failed;
1660 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001661 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001662 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1663 goto on_error;
1664 ++ectx.ec_stack.ga_len;
1665 }
1666 break;
1667
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001668 // load g:/b:/w:/t: namespace
1669 case ISN_LOADGDICT:
1670 case ISN_LOADBDICT:
1671 case ISN_LOADWDICT:
1672 case ISN_LOADTDICT:
1673 {
1674 dict_T *d = NULL;
1675
1676 switch (iptr->isn_type)
1677 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001678 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1679 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1680 case ISN_LOADWDICT: d = curwin->w_vars; break;
1681 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001682 default: // Cannot reach here
1683 goto failed;
1684 }
1685 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1686 goto failed;
1687 tv = STACK_TV_BOT(0);
1688 tv->v_type = VAR_DICT;
1689 tv->v_lock = 0;
1690 tv->vval.v_dict = d;
1691 ++ectx.ec_stack.ga_len;
1692 }
1693 break;
1694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 // load &option
1696 case ISN_LOADOPT:
1697 {
1698 typval_T optval;
1699 char_u *name = iptr->isn_arg.string;
1700
Bram Moolenaara8c17702020-04-01 21:17:24 +02001701 // This is not expected to fail, name is checked during
1702 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001703 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001705 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001706 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 *STACK_TV_BOT(0) = optval;
1708 ++ectx.ec_stack.ga_len;
1709 }
1710 break;
1711
1712 // load $ENV
1713 case ISN_LOADENV:
1714 {
1715 typval_T optval;
1716 char_u *name = iptr->isn_arg.string;
1717
Bram Moolenaar270d0382020-05-15 21:42:53 +02001718 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001720 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001721 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001722 *STACK_TV_BOT(0) = optval;
1723 ++ectx.ec_stack.ga_len;
1724 }
1725 break;
1726
1727 // load @register
1728 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001729 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001730 goto failed;
1731 tv = STACK_TV_BOT(0);
1732 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001733 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001734 // This may result in NULL, which should be equivalent to an
1735 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001736 tv->vval.v_string = get_reg_contents(
1737 iptr->isn_arg.number, GREG_EXPR_SRC);
1738 ++ectx.ec_stack.ga_len;
1739 break;
1740
1741 // store local variable
1742 case ISN_STORE:
1743 --ectx.ec_stack.ga_len;
1744 tv = STACK_TV_VAR(iptr->isn_arg.number);
1745 clear_tv(tv);
1746 *tv = *STACK_TV_BOT(0);
1747 break;
1748
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001749 // store s: variable in old script
1750 case ISN_STORES:
1751 {
1752 hashtab_T *ht = &SCRIPT_VARS(
1753 iptr->isn_arg.loadstore.ls_sid);
1754 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001755 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001756
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001757 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001758 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001759 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001760 else
1761 {
1762 clear_tv(&di->di_tv);
1763 di->di_tv = *STACK_TV_BOT(0);
1764 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001765 }
1766 break;
1767
1768 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001769 case ISN_STORESCRIPT:
1770 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001771 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1772 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001773
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001774 sv = get_script_svar(sref, &ectx);
1775 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001776 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001777 --ectx.ec_stack.ga_len;
1778 clear_tv(sv->sv_tv);
1779 *sv->sv_tv = *STACK_TV_BOT(0);
1780 }
1781 break;
1782
1783 // store option
1784 case ISN_STOREOPT:
1785 {
1786 long n = 0;
1787 char_u *s = NULL;
1788 char *msg;
1789
1790 --ectx.ec_stack.ga_len;
1791 tv = STACK_TV_BOT(0);
1792 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001793 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001794 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001795 if (s == NULL)
1796 s = (char_u *)"";
1797 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001798 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001799 // must be VAR_NUMBER, CHECKTYPE makes sure
1800 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001801 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1802 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001803 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 if (msg != NULL)
1805 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001806 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001808 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001809 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001810 }
1811 break;
1812
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001813 // store $ENV
1814 case ISN_STOREENV:
1815 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001816 tv = STACK_TV_BOT(0);
1817 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1818 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001819 break;
1820
1821 // store @r
1822 case ISN_STOREREG:
1823 {
1824 int reg = iptr->isn_arg.number;
1825
1826 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001827 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001828 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001829 tv_get_string(tv), -1, FALSE);
1830 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001831 }
1832 break;
1833
1834 // store v: variable
1835 case ISN_STOREV:
1836 --ectx.ec_stack.ga_len;
1837 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1838 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001839 // should not happen, type is checked when compiling
1840 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001841 break;
1842
Bram Moolenaard3aac292020-04-19 14:32:17 +02001843 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001844 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001845 case ISN_STOREB:
1846 case ISN_STOREW:
1847 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001848 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001849 dictitem_T *di;
1850 hashtab_T *ht;
1851 char_u *name = iptr->isn_arg.string + 2;
1852
Bram Moolenaard3aac292020-04-19 14:32:17 +02001853 switch (iptr->isn_type)
1854 {
1855 case ISN_STOREG:
1856 ht = get_globvar_ht();
1857 break;
1858 case ISN_STOREB:
1859 ht = &curbuf->b_vars->dv_hashtab;
1860 break;
1861 case ISN_STOREW:
1862 ht = &curwin->w_vars->dv_hashtab;
1863 break;
1864 case ISN_STORET:
1865 ht = &curtab->tp_vars->dv_hashtab;
1866 break;
1867 default: // Cannot reach here
1868 goto failed;
1869 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870
1871 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001872 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001874 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 else
1876 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001877 SOURCING_LNUM = iptr->isn_lnum;
1878 if (var_check_permission(di, name) == FAIL)
1879 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001880 clear_tv(&di->di_tv);
1881 di->di_tv = *STACK_TV_BOT(0);
1882 }
1883 }
1884 break;
1885
Bram Moolenaar03290b82020-12-19 16:30:44 +01001886 // store an autoload variable
1887 case ISN_STOREAUTO:
1888 SOURCING_LNUM = iptr->isn_lnum;
1889 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1890 clear_tv(STACK_TV_BOT(-1));
1891 --ectx.ec_stack.ga_len;
1892 break;
1893
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 // store number in local variable
1895 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001896 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001897 clear_tv(tv);
1898 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001899 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900 break;
1901
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001902 // store value in list or dict variable
1903 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001904 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001905 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001906 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001907 typval_T *tv_dest = STACK_TV_BOT(-1);
1908 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001909
Bram Moolenaar752fc692021-01-04 21:57:11 +01001910 // Stack contains:
1911 // -3 value to be stored
1912 // -2 index
1913 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001914 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001915 SOURCING_LNUM = iptr->isn_lnum;
1916 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001917 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001918 dest_type = tv_dest->v_type;
1919 if (dest_type == VAR_DICT)
1920 status = do_2string(tv_idx, TRUE);
1921 else if (dest_type == VAR_LIST
1922 && tv_idx->v_type != VAR_NUMBER)
1923 {
1924 emsg(_(e_number_exp));
1925 status = FAIL;
1926 }
1927 }
1928 else if (dest_type != tv_dest->v_type)
1929 {
1930 // just in case, should be OK
1931 semsg(_(e_expected_str_but_got_str),
1932 vartype_name(dest_type),
1933 vartype_name(tv_dest->v_type));
1934 status = FAIL;
1935 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001936
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001937 if (status == OK && dest_type == VAR_LIST)
1938 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001939 long lidx = (long)tv_idx->vval.v_number;
1940 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001941
1942 if (list == NULL)
1943 {
1944 emsg(_(e_list_not_set));
1945 goto on_error;
1946 }
1947 if (lidx < 0 && list->lv_len + lidx >= 0)
1948 // negative index is relative to the end
1949 lidx = list->lv_len + lidx;
1950 if (lidx < 0 || lidx > list->lv_len)
1951 {
1952 semsg(_(e_listidx), lidx);
1953 goto on_error;
1954 }
1955 if (lidx < list->lv_len)
1956 {
1957 listitem_T *li = list_find(list, lidx);
1958
1959 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001960 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001961 goto on_error;
1962 // overwrite existing list item
1963 clear_tv(&li->li_tv);
1964 li->li_tv = *tv;
1965 }
1966 else
1967 {
1968 if (error_if_locked(list->lv_lock,
1969 e_cannot_change_list))
1970 goto on_error;
1971 // append to list, only fails when out of memory
1972 if (list_append_tv(list, tv) == FAIL)
1973 goto failed;
1974 clear_tv(tv);
1975 }
1976 }
1977 else if (status == OK && dest_type == VAR_DICT)
1978 {
1979 char_u *key = tv_idx->vval.v_string;
1980 dict_T *dict = tv_dest->vval.v_dict;
1981 dictitem_T *di;
1982
1983 SOURCING_LNUM = iptr->isn_lnum;
1984 if (dict == NULL)
1985 {
1986 emsg(_(e_dictionary_not_set));
1987 goto on_error;
1988 }
1989 if (key == NULL)
1990 key = (char_u *)"";
1991 di = dict_find(dict, key, -1);
1992 if (di != NULL)
1993 {
1994 if (error_if_locked(di->di_tv.v_lock,
1995 e_cannot_change_dict_item))
1996 goto on_error;
1997 // overwrite existing value
1998 clear_tv(&di->di_tv);
1999 di->di_tv = *tv;
2000 }
2001 else
2002 {
2003 if (error_if_locked(dict->dv_lock,
2004 e_cannot_change_dict))
2005 goto on_error;
2006 // add to dict, only fails when out of memory
2007 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2008 goto failed;
2009 clear_tv(tv);
2010 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002011 }
2012 else
2013 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002014 status = FAIL;
2015 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002016 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002017
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002018 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002019 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002020 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002021 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002022 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002023 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002024 goto on_error;
2025 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002026 }
2027 break;
2028
Bram Moolenaar0186e582021-01-10 18:33:11 +01002029 // load or store variable or argument from outer scope
2030 case ISN_LOADOUTER:
2031 case ISN_STOREOUTER:
2032 {
2033 int depth = iptr->isn_arg.outer.outer_depth;
2034 outer_T *outer = ectx.ec_outer;
2035
2036 while (depth > 1 && outer != NULL)
2037 {
2038 outer = outer->out_up;
2039 --depth;
2040 }
2041 if (outer == NULL)
2042 {
2043 SOURCING_LNUM = iptr->isn_lnum;
2044 iemsg("LOADOUTER depth more than scope levels");
2045 goto failed;
2046 }
2047 tv = ((typval_T *)outer->out_stack->ga_data)
2048 + outer->out_frame_idx + STACK_FRAME_SIZE
2049 + iptr->isn_arg.outer.outer_idx;
2050 if (iptr->isn_type == ISN_LOADOUTER)
2051 {
2052 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2053 goto failed;
2054 copy_tv(tv, STACK_TV_BOT(0));
2055 ++ectx.ec_stack.ga_len;
2056 }
2057 else
2058 {
2059 --ectx.ec_stack.ga_len;
2060 clear_tv(tv);
2061 *tv = *STACK_TV_BOT(0);
2062 }
2063 }
2064 break;
2065
Bram Moolenaar752fc692021-01-04 21:57:11 +01002066 // unlet item in list or dict variable
2067 case ISN_UNLETINDEX:
2068 {
2069 typval_T *tv_idx = STACK_TV_BOT(-2);
2070 typval_T *tv_dest = STACK_TV_BOT(-1);
2071 int status = OK;
2072
2073 // Stack contains:
2074 // -2 index
2075 // -1 dict or list
2076 if (tv_dest->v_type == VAR_DICT)
2077 {
2078 // unlet a dict item, index must be a string
2079 if (tv_idx->v_type != VAR_STRING)
2080 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002081 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002082 semsg(_(e_expected_str_but_got_str),
2083 vartype_name(VAR_STRING),
2084 vartype_name(tv_idx->v_type));
2085 status = FAIL;
2086 }
2087 else
2088 {
2089 dict_T *d = tv_dest->vval.v_dict;
2090 char_u *key = tv_idx->vval.v_string;
2091 dictitem_T *di = NULL;
2092
2093 if (key == NULL)
2094 key = (char_u *)"";
2095 if (d != NULL)
2096 di = dict_find(d, key, (int)STRLEN(key));
2097 if (di == NULL)
2098 {
2099 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002100 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002101 semsg(_(e_dictkey), key);
2102 status = FAIL;
2103 }
2104 else
2105 {
2106 // TODO: check for dict or item locked
2107 dictitem_remove(d, di);
2108 }
2109 }
2110 }
2111 else if (tv_dest->v_type == VAR_LIST)
2112 {
2113 // unlet a List item, index must be a number
2114 if (tv_idx->v_type != VAR_NUMBER)
2115 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002116 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002117 semsg(_(e_expected_str_but_got_str),
2118 vartype_name(VAR_NUMBER),
2119 vartype_name(tv_idx->v_type));
2120 status = FAIL;
2121 }
2122 else
2123 {
2124 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002125 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002126 listitem_T *li = NULL;
2127
2128 li = list_find(l, n);
2129 if (li == NULL)
2130 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002131 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002132 semsg(_(e_listidx), n);
2133 status = FAIL;
2134 }
2135 else
2136 // TODO: check for list or item locked
2137 listitem_remove(l, li);
2138 }
2139 }
2140 else
2141 {
2142 status = FAIL;
2143 semsg(_(e_cannot_index_str),
2144 vartype_name(tv_dest->v_type));
2145 }
2146
2147 clear_tv(tv_idx);
2148 clear_tv(tv_dest);
2149 ectx.ec_stack.ga_len -= 2;
2150 if (status == FAIL)
2151 goto on_error;
2152 }
2153 break;
2154
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002155 // push constant
2156 case ISN_PUSHNR:
2157 case ISN_PUSHBOOL:
2158 case ISN_PUSHSPEC:
2159 case ISN_PUSHF:
2160 case ISN_PUSHS:
2161 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002162 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002163 case ISN_PUSHCHANNEL:
2164 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002165 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002166 goto failed;
2167 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002168 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002169 ++ectx.ec_stack.ga_len;
2170 switch (iptr->isn_type)
2171 {
2172 case ISN_PUSHNR:
2173 tv->v_type = VAR_NUMBER;
2174 tv->vval.v_number = iptr->isn_arg.number;
2175 break;
2176 case ISN_PUSHBOOL:
2177 tv->v_type = VAR_BOOL;
2178 tv->vval.v_number = iptr->isn_arg.number;
2179 break;
2180 case ISN_PUSHSPEC:
2181 tv->v_type = VAR_SPECIAL;
2182 tv->vval.v_number = iptr->isn_arg.number;
2183 break;
2184#ifdef FEAT_FLOAT
2185 case ISN_PUSHF:
2186 tv->v_type = VAR_FLOAT;
2187 tv->vval.v_float = iptr->isn_arg.fnumber;
2188 break;
2189#endif
2190 case ISN_PUSHBLOB:
2191 blob_copy(iptr->isn_arg.blob, tv);
2192 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002193 case ISN_PUSHFUNC:
2194 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002195 if (iptr->isn_arg.string == NULL)
2196 tv->vval.v_string = NULL;
2197 else
2198 tv->vval.v_string =
2199 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002200 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002201 case ISN_PUSHCHANNEL:
2202#ifdef FEAT_JOB_CHANNEL
2203 tv->v_type = VAR_CHANNEL;
2204 tv->vval.v_channel = iptr->isn_arg.channel;
2205 if (tv->vval.v_channel != NULL)
2206 ++tv->vval.v_channel->ch_refcount;
2207#endif
2208 break;
2209 case ISN_PUSHJOB:
2210#ifdef FEAT_JOB_CHANNEL
2211 tv->v_type = VAR_JOB;
2212 tv->vval.v_job = iptr->isn_arg.job;
2213 if (tv->vval.v_job != NULL)
2214 ++tv->vval.v_job->jv_refcount;
2215#endif
2216 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002217 default:
2218 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002219 tv->vval.v_string = vim_strsave(
2220 iptr->isn_arg.string == NULL
2221 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002222 }
2223 break;
2224
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002225 case ISN_UNLET:
2226 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2227 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002228 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002229 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002230 case ISN_UNLETENV:
2231 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2232 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002233
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002234 case ISN_LOCKCONST:
2235 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2236 break;
2237
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002238 // create a list from items on the stack; uses a single allocation
2239 // for the list header and the items
2240 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002241 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2242 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 break;
2244
2245 // create a dict from items on the stack
2246 case ISN_NEWDICT:
2247 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002248 int count = iptr->isn_arg.number;
2249 dict_T *dict = dict_alloc();
2250 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002251 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252
2253 if (dict == NULL)
2254 goto failed;
2255 for (idx = 0; idx < count; ++idx)
2256 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002257 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002258 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002259 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002260 key = tv->vval.v_string == NULL
2261 ? (char_u *)"" : tv->vval.v_string;
2262 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002263 if (item != NULL)
2264 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002265 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002266 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002267 dict_unref(dict);
2268 goto on_error;
2269 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002270 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002271 clear_tv(tv);
2272 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002273 {
2274 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002275 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002276 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002277 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2278 item->di_tv.v_lock = 0;
2279 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002280 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002281 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002282 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002284 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002285 }
2286
2287 if (count > 0)
2288 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002289 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002290 goto failed;
2291 else
2292 ++ectx.ec_stack.ga_len;
2293 tv = STACK_TV_BOT(-1);
2294 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002295 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002296 tv->vval.v_dict = dict;
2297 ++dict->dv_refcount;
2298 }
2299 break;
2300
2301 // call a :def function
2302 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002303 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002304 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 iptr->isn_arg.dfunc.cdf_argcount,
2306 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002307 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 break;
2309
2310 // call a builtin function
2311 case ISN_BCALL:
2312 SOURCING_LNUM = iptr->isn_lnum;
2313 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2314 iptr->isn_arg.bfunc.cbf_argcount,
2315 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002316 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002317 break;
2318
2319 // call a funcref or partial
2320 case ISN_PCALL:
2321 {
2322 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2323 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002324 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002325
2326 SOURCING_LNUM = iptr->isn_lnum;
2327 if (pfunc->cpf_top)
2328 {
2329 // funcref is above the arguments
2330 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2331 }
2332 else
2333 {
2334 // Get the funcref from the stack.
2335 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002336 partial_tv = *STACK_TV_BOT(0);
2337 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 }
2339 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002340 if (tv == &partial_tv)
2341 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002342 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002343 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002344 }
2345 break;
2346
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002347 case ISN_PCALL_END:
2348 // PCALL finished, arguments have been consumed and replaced by
2349 // the return value. Now clear the funcref from the stack,
2350 // and move the return value in its place.
2351 --ectx.ec_stack.ga_len;
2352 clear_tv(STACK_TV_BOT(-1));
2353 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2354 break;
2355
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356 // call a user defined function or funcref/partial
2357 case ISN_UCALL:
2358 {
2359 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2360
2361 SOURCING_LNUM = iptr->isn_lnum;
2362 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002363 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002364 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002365 }
2366 break;
2367
2368 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002369 case ISN_RETURN_ZERO:
2370 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2371 goto failed;
2372 tv = STACK_TV_BOT(0);
2373 ++ectx.ec_stack.ga_len;
2374 tv->v_type = VAR_NUMBER;
2375 tv->vval.v_number = 0;
2376 tv->v_lock = 0;
2377 // FALLTHROUGH
2378
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002379 case ISN_RETURN:
2380 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002381 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002382 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002383
2384 if (trystack->ga_len > 0)
2385 trycmd = ((trycmd_T *)trystack->ga_data)
2386 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002387 if (trycmd != NULL
2388 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002389 && trycmd->tcd_finally_idx != 0)
2390 {
2391 // jump to ":finally"
2392 ectx.ec_iidx = trycmd->tcd_finally_idx;
2393 trycmd->tcd_return = TRUE;
2394 }
2395 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002396 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002397 }
2398 break;
2399
2400 // push a function reference to a compiled function
2401 case ISN_FUNCREF:
2402 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002403 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2404 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2405 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002406
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 if (pt == NULL)
2408 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002409 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002410 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002411 vim_free(pt);
2412 goto failed;
2413 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002414 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2415 &ectx) == FAIL)
2416 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002417
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418 tv = STACK_TV_BOT(0);
2419 ++ectx.ec_stack.ga_len;
2420 tv->vval.v_partial = pt;
2421 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002422 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 }
2424 break;
2425
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002426 // Create a global function from a lambda.
2427 case ISN_NEWFUNC:
2428 {
2429 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2430
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002431 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002432 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002433 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002434 }
2435 break;
2436
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002437 // List functions
2438 case ISN_DEF:
2439 if (iptr->isn_arg.string == NULL)
2440 list_functions(NULL);
2441 else
2442 {
2443 exarg_T ea;
2444
2445 CLEAR_FIELD(ea);
2446 ea.cmd = ea.arg = iptr->isn_arg.string;
2447 define_function(&ea, NULL);
2448 }
2449 break;
2450
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 // jump if a condition is met
2452 case ISN_JUMP:
2453 {
2454 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002455 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 int jump = TRUE;
2457
2458 if (when != JUMP_ALWAYS)
2459 {
2460 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002461 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002462 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002463 || when == JUMP_IF_COND_TRUE)
2464 {
2465 SOURCING_LNUM = iptr->isn_lnum;
2466 jump = tv_get_bool_chk(tv, &error);
2467 if (error)
2468 goto on_error;
2469 }
2470 else
2471 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002472 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002473 || when == JUMP_AND_KEEP_IF_FALSE
2474 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002476 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 {
2478 // drop the value from the stack
2479 clear_tv(tv);
2480 --ectx.ec_stack.ga_len;
2481 }
2482 }
2483 if (jump)
2484 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2485 }
2486 break;
2487
2488 // top of a for loop
2489 case ISN_FOR:
2490 {
2491 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2492 typval_T *idxtv =
2493 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2494
2495 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002496 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002498 ++idxtv->vval.v_number;
2499 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 // past the end of the list, jump to "endfor"
2501 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2502 else if (list->lv_first == &range_list_item)
2503 {
2504 // non-materialized range() list
2505 tv = STACK_TV_BOT(0);
2506 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002507 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 tv->vval.v_number = list_find_nr(
2509 list, idxtv->vval.v_number, NULL);
2510 ++ectx.ec_stack.ga_len;
2511 }
2512 else
2513 {
2514 listitem_T *li = list_find(list, idxtv->vval.v_number);
2515
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002516 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2517 ++ectx.ec_stack.ga_len;
2518 }
2519 }
2520 break;
2521
2522 // start of ":try" block
2523 case ISN_TRY:
2524 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002525 trycmd_T *trycmd = NULL;
2526
Bram Moolenaar270d0382020-05-15 21:42:53 +02002527 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 goto failed;
2529 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2530 + ectx.ec_trystack.ga_len;
2531 ++ectx.ec_trystack.ga_len;
2532 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002533 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2535 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002536 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002537 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 }
2539 break;
2540
2541 case ISN_PUSHEXC:
2542 if (current_exception == NULL)
2543 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002544 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 iemsg("Evaluating catch while current_exception is NULL");
2546 goto failed;
2547 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002548 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002549 goto failed;
2550 tv = STACK_TV_BOT(0);
2551 ++ectx.ec_stack.ga_len;
2552 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002553 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 tv->vval.v_string = vim_strsave(
2555 (char_u *)current_exception->value);
2556 break;
2557
2558 case ISN_CATCH:
2559 {
2560 garray_T *trystack = &ectx.ec_trystack;
2561
Bram Moolenaar20a76292020-12-25 19:47:24 +01002562 if (restore_cmdmod)
2563 {
2564 cmdmod.cmod_filter_regmatch.regprog = NULL;
2565 undo_cmdmod(&cmdmod);
2566 cmdmod = save_cmdmod;
2567 restore_cmdmod = FALSE;
2568 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002569 if (trystack->ga_len > 0)
2570 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002571 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002572 + trystack->ga_len - 1;
2573 trycmd->tcd_caught = TRUE;
2574 }
2575 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002576 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 catch_exception(current_exception);
2578 }
2579 break;
2580
2581 // end of ":try" block
2582 case ISN_ENDTRY:
2583 {
2584 garray_T *trystack = &ectx.ec_trystack;
2585
2586 if (trystack->ga_len > 0)
2587 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002588 trycmd_T *trycmd = NULL;
2589
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002590 --trystack->ga_len;
2591 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002592 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002593 trycmd = ((trycmd_T *)trystack->ga_data)
2594 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002595 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002596 {
2597 // discard the exception
2598 if (caught_stack == current_exception)
2599 caught_stack = caught_stack->caught;
2600 discard_current_exception();
2601 }
2602
2603 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002604 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002605 }
2606 }
2607 break;
2608
2609 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002610 if (ectx.ec_trystack.ga_len == 0 && trylevel == 0
2611 && emsg_silent)
2612 {
2613 // throwing an exception while using "silent!" causes the
2614 // function to abort but not display an error.
2615 tv = STACK_TV_BOT(-1);
2616 clear_tv(tv);
2617 tv->v_type = VAR_NUMBER;
2618 tv->vval.v_number = 0;
2619 goto done;
2620 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002621 --ectx.ec_stack.ga_len;
2622 tv = STACK_TV_BOT(0);
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002623 if (tv->vval.v_string == NULL
2624 || *skipwhite(tv->vval.v_string) == NUL)
2625 {
Bram Moolenaar335e6712020-10-17 22:58:21 +02002626 vim_free(tv->vval.v_string);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002628 emsg(_(e_throw_with_empty_string));
2629 goto failed;
2630 }
2631
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (throw_exception(tv->vval.v_string, ET_USER, NULL) == FAIL)
2633 {
2634 vim_free(tv->vval.v_string);
2635 goto failed;
2636 }
2637 did_throw = TRUE;
2638 break;
2639
2640 // compare with special values
2641 case ISN_COMPAREBOOL:
2642 case ISN_COMPARESPECIAL:
2643 {
2644 typval_T *tv1 = STACK_TV_BOT(-2);
2645 typval_T *tv2 = STACK_TV_BOT(-1);
2646 varnumber_T arg1 = tv1->vval.v_number;
2647 varnumber_T arg2 = tv2->vval.v_number;
2648 int res;
2649
2650 switch (iptr->isn_arg.op.op_type)
2651 {
2652 case EXPR_EQUAL: res = arg1 == arg2; break;
2653 case EXPR_NEQUAL: res = arg1 != arg2; break;
2654 default: res = 0; break;
2655 }
2656
2657 --ectx.ec_stack.ga_len;
2658 tv1->v_type = VAR_BOOL;
2659 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2660 }
2661 break;
2662
2663 // Operation with two number arguments
2664 case ISN_OPNR:
2665 case ISN_COMPARENR:
2666 {
2667 typval_T *tv1 = STACK_TV_BOT(-2);
2668 typval_T *tv2 = STACK_TV_BOT(-1);
2669 varnumber_T arg1 = tv1->vval.v_number;
2670 varnumber_T arg2 = tv2->vval.v_number;
2671 varnumber_T res;
2672
2673 switch (iptr->isn_arg.op.op_type)
2674 {
2675 case EXPR_MULT: res = arg1 * arg2; break;
2676 case EXPR_DIV: res = arg1 / arg2; break;
2677 case EXPR_REM: res = arg1 % arg2; break;
2678 case EXPR_SUB: res = arg1 - arg2; break;
2679 case EXPR_ADD: res = arg1 + arg2; break;
2680
2681 case EXPR_EQUAL: res = arg1 == arg2; break;
2682 case EXPR_NEQUAL: res = arg1 != arg2; break;
2683 case EXPR_GREATER: res = arg1 > arg2; break;
2684 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2685 case EXPR_SMALLER: res = arg1 < arg2; break;
2686 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2687 default: res = 0; break;
2688 }
2689
2690 --ectx.ec_stack.ga_len;
2691 if (iptr->isn_type == ISN_COMPARENR)
2692 {
2693 tv1->v_type = VAR_BOOL;
2694 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2695 }
2696 else
2697 tv1->vval.v_number = res;
2698 }
2699 break;
2700
2701 // Computation with two float arguments
2702 case ISN_OPFLOAT:
2703 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002704#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 {
2706 typval_T *tv1 = STACK_TV_BOT(-2);
2707 typval_T *tv2 = STACK_TV_BOT(-1);
2708 float_T arg1 = tv1->vval.v_float;
2709 float_T arg2 = tv2->vval.v_float;
2710 float_T res = 0;
2711 int cmp = FALSE;
2712
2713 switch (iptr->isn_arg.op.op_type)
2714 {
2715 case EXPR_MULT: res = arg1 * arg2; break;
2716 case EXPR_DIV: res = arg1 / arg2; break;
2717 case EXPR_SUB: res = arg1 - arg2; break;
2718 case EXPR_ADD: res = arg1 + arg2; break;
2719
2720 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2721 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2722 case EXPR_GREATER: cmp = arg1 > arg2; break;
2723 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2724 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2725 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2726 default: cmp = 0; break;
2727 }
2728 --ectx.ec_stack.ga_len;
2729 if (iptr->isn_type == ISN_COMPAREFLOAT)
2730 {
2731 tv1->v_type = VAR_BOOL;
2732 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2733 }
2734 else
2735 tv1->vval.v_float = res;
2736 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002737#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 break;
2739
2740 case ISN_COMPARELIST:
2741 {
2742 typval_T *tv1 = STACK_TV_BOT(-2);
2743 typval_T *tv2 = STACK_TV_BOT(-1);
2744 list_T *arg1 = tv1->vval.v_list;
2745 list_T *arg2 = tv2->vval.v_list;
2746 int cmp = FALSE;
2747 int ic = iptr->isn_arg.op.op_ic;
2748
2749 switch (iptr->isn_arg.op.op_type)
2750 {
2751 case EXPR_EQUAL: cmp =
2752 list_equal(arg1, arg2, ic, FALSE); break;
2753 case EXPR_NEQUAL: cmp =
2754 !list_equal(arg1, arg2, ic, FALSE); break;
2755 case EXPR_IS: cmp = arg1 == arg2; break;
2756 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2757 default: cmp = 0; break;
2758 }
2759 --ectx.ec_stack.ga_len;
2760 clear_tv(tv1);
2761 clear_tv(tv2);
2762 tv1->v_type = VAR_BOOL;
2763 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2764 }
2765 break;
2766
2767 case ISN_COMPAREBLOB:
2768 {
2769 typval_T *tv1 = STACK_TV_BOT(-2);
2770 typval_T *tv2 = STACK_TV_BOT(-1);
2771 blob_T *arg1 = tv1->vval.v_blob;
2772 blob_T *arg2 = tv2->vval.v_blob;
2773 int cmp = FALSE;
2774
2775 switch (iptr->isn_arg.op.op_type)
2776 {
2777 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2778 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2779 case EXPR_IS: cmp = arg1 == arg2; break;
2780 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2781 default: cmp = 0; break;
2782 }
2783 --ectx.ec_stack.ga_len;
2784 clear_tv(tv1);
2785 clear_tv(tv2);
2786 tv1->v_type = VAR_BOOL;
2787 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2788 }
2789 break;
2790
2791 // TODO: handle separately
2792 case ISN_COMPARESTRING:
2793 case ISN_COMPAREDICT:
2794 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002795 case ISN_COMPAREANY:
2796 {
2797 typval_T *tv1 = STACK_TV_BOT(-2);
2798 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002799 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 int ic = iptr->isn_arg.op.op_ic;
2801
Bram Moolenaareb26f432020-09-14 16:50:05 +02002802 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002803 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002804 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 --ectx.ec_stack.ga_len;
2806 }
2807 break;
2808
2809 case ISN_ADDLIST:
2810 case ISN_ADDBLOB:
2811 {
2812 typval_T *tv1 = STACK_TV_BOT(-2);
2813 typval_T *tv2 = STACK_TV_BOT(-1);
2814
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002815 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002816 if (iptr->isn_type == ISN_ADDLIST)
2817 eval_addlist(tv1, tv2);
2818 else
2819 eval_addblob(tv1, tv2);
2820 clear_tv(tv2);
2821 --ectx.ec_stack.ga_len;
2822 }
2823 break;
2824
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002825 case ISN_LISTAPPEND:
2826 {
2827 typval_T *tv1 = STACK_TV_BOT(-2);
2828 typval_T *tv2 = STACK_TV_BOT(-1);
2829 list_T *l = tv1->vval.v_list;
2830
2831 // add an item to a list
2832 if (l == NULL)
2833 {
2834 SOURCING_LNUM = iptr->isn_lnum;
2835 emsg(_(e_cannot_add_to_null_list));
2836 goto on_error;
2837 }
2838 if (list_append_tv(l, tv2) == FAIL)
2839 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002840 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002841 --ectx.ec_stack.ga_len;
2842 }
2843 break;
2844
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002845 case ISN_BLOBAPPEND:
2846 {
2847 typval_T *tv1 = STACK_TV_BOT(-2);
2848 typval_T *tv2 = STACK_TV_BOT(-1);
2849 blob_T *b = tv1->vval.v_blob;
2850 int error = FALSE;
2851 varnumber_T n;
2852
2853 // add a number to a blob
2854 if (b == NULL)
2855 {
2856 SOURCING_LNUM = iptr->isn_lnum;
2857 emsg(_(e_cannot_add_to_null_blob));
2858 goto on_error;
2859 }
2860 n = tv_get_number_chk(tv2, &error);
2861 if (error)
2862 goto on_error;
2863 ga_append(&b->bv_ga, (int)n);
2864 --ectx.ec_stack.ga_len;
2865 }
2866 break;
2867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002868 // Computation with two arguments of unknown type
2869 case ISN_OPANY:
2870 {
2871 typval_T *tv1 = STACK_TV_BOT(-2);
2872 typval_T *tv2 = STACK_TV_BOT(-1);
2873 varnumber_T n1, n2;
2874#ifdef FEAT_FLOAT
2875 float_T f1 = 0, f2 = 0;
2876#endif
2877 int error = FALSE;
2878
2879 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2880 {
2881 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2882 {
2883 eval_addlist(tv1, tv2);
2884 clear_tv(tv2);
2885 --ectx.ec_stack.ga_len;
2886 break;
2887 }
2888 else if (tv1->v_type == VAR_BLOB
2889 && tv2->v_type == VAR_BLOB)
2890 {
2891 eval_addblob(tv1, tv2);
2892 clear_tv(tv2);
2893 --ectx.ec_stack.ga_len;
2894 break;
2895 }
2896 }
2897#ifdef FEAT_FLOAT
2898 if (tv1->v_type == VAR_FLOAT)
2899 {
2900 f1 = tv1->vval.v_float;
2901 n1 = 0;
2902 }
2903 else
2904#endif
2905 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002906 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002907 n1 = tv_get_number_chk(tv1, &error);
2908 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002909 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002910#ifdef FEAT_FLOAT
2911 if (tv2->v_type == VAR_FLOAT)
2912 f1 = n1;
2913#endif
2914 }
2915#ifdef FEAT_FLOAT
2916 if (tv2->v_type == VAR_FLOAT)
2917 {
2918 f2 = tv2->vval.v_float;
2919 n2 = 0;
2920 }
2921 else
2922#endif
2923 {
2924 n2 = tv_get_number_chk(tv2, &error);
2925 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002926 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002927#ifdef FEAT_FLOAT
2928 if (tv1->v_type == VAR_FLOAT)
2929 f2 = n2;
2930#endif
2931 }
2932#ifdef FEAT_FLOAT
2933 // if there is a float on either side the result is a float
2934 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2935 {
2936 switch (iptr->isn_arg.op.op_type)
2937 {
2938 case EXPR_MULT: f1 = f1 * f2; break;
2939 case EXPR_DIV: f1 = f1 / f2; break;
2940 case EXPR_SUB: f1 = f1 - f2; break;
2941 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002942 default: SOURCING_LNUM = iptr->isn_lnum;
2943 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002944 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002945 }
2946 clear_tv(tv1);
2947 clear_tv(tv2);
2948 tv1->v_type = VAR_FLOAT;
2949 tv1->vval.v_float = f1;
2950 --ectx.ec_stack.ga_len;
2951 }
2952 else
2953#endif
2954 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002955 int failed = FALSE;
2956
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 switch (iptr->isn_arg.op.op_type)
2958 {
2959 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002960 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
2961 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002962 goto on_error;
2963 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002964 case EXPR_SUB: n1 = n1 - n2; break;
2965 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01002966 default: n1 = num_modulus(n1, n2, &failed);
2967 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01002968 goto on_error;
2969 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002970 }
2971 clear_tv(tv1);
2972 clear_tv(tv2);
2973 tv1->v_type = VAR_NUMBER;
2974 tv1->vval.v_number = n1;
2975 --ectx.ec_stack.ga_len;
2976 }
2977 }
2978 break;
2979
2980 case ISN_CONCAT:
2981 {
2982 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
2983 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
2984 char_u *res;
2985
2986 res = concat_str(str1, str2);
2987 clear_tv(STACK_TV_BOT(-2));
2988 clear_tv(STACK_TV_BOT(-1));
2989 --ectx.ec_stack.ga_len;
2990 STACK_TV_BOT(-1)->vval.v_string = res;
2991 }
2992 break;
2993
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002994 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002995 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002996 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02002997 int is_slice = iptr->isn_type == ISN_STRSLICE;
2998 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02002999 char_u *res;
3000
3001 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003002 // string slice: string is at stack-3, first index at
3003 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003004 if (is_slice)
3005 {
3006 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003007 n1 = tv->vval.v_number;
3008 }
3009
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003010 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003011 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003012
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003013 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003014 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003015 if (is_slice)
3016 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003017 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003018 else
3019 // Index: The resulting variable is a string of a
3020 // single character. If the index is too big or
3021 // negative the result is empty.
3022 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003023 vim_free(tv->vval.v_string);
3024 tv->vval.v_string = res;
3025 }
3026 break;
3027
3028 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003029 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003030 {
Bram Moolenaared591872020-08-15 22:14:53 +02003031 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003033 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034
3035 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003036 // list slice: list is at stack-3, indexes at stack-2 and
3037 // stack-1
3038 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 list = tv->vval.v_list;
3040
3041 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003042 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003044
3045 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 {
Bram Moolenaared591872020-08-15 22:14:53 +02003047 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003048 n1 = tv->vval.v_number;
3049 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 }
Bram Moolenaared591872020-08-15 22:14:53 +02003051
3052 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003053 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003054 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003055 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3056 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003057 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 }
3059 break;
3060
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003061 case ISN_ANYINDEX:
3062 case ISN_ANYSLICE:
3063 {
3064 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3065 typval_T *var1, *var2;
3066 int res;
3067
3068 // index: composite is at stack-2, index at stack-1
3069 // slice: composite is at stack-3, indexes at stack-2 and
3070 // stack-1
3071 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003072 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003073 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3074 goto on_error;
3075 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3076 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003077 res = eval_index_inner(tv, is_slice, var1, var2,
3078 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003079 clear_tv(var1);
3080 if (is_slice)
3081 clear_tv(var2);
3082 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3083 if (res == FAIL)
3084 goto on_error;
3085 }
3086 break;
3087
Bram Moolenaar9af78762020-06-16 11:34:42 +02003088 case ISN_SLICE:
3089 {
3090 list_T *list;
3091 int count = iptr->isn_arg.number;
3092
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003093 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003094 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003095 list = tv->vval.v_list;
3096
3097 // no error for short list, expect it to be checked earlier
3098 if (list != NULL && list->lv_len >= count)
3099 {
3100 list_T *newlist = list_slice(list,
3101 count, list->lv_len - 1);
3102
3103 if (newlist != NULL)
3104 {
3105 list_unref(list);
3106 tv->vval.v_list = newlist;
3107 ++newlist->lv_refcount;
3108 }
3109 }
3110 }
3111 break;
3112
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003113 case ISN_GETITEM:
3114 {
3115 listitem_T *li;
3116 int index = iptr->isn_arg.number;
3117
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003118 // Get list item: list is at stack-1, push item.
3119 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003120 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003121 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003122
3123 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3124 goto failed;
3125 ++ectx.ec_stack.ga_len;
3126 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3127 }
3128 break;
3129
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003130 case ISN_MEMBER:
3131 {
3132 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003133 char_u *key;
3134 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003135 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003136
3137 // dict member: dict is at stack-2, key at stack-1
3138 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003139 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003140 dict = tv->vval.v_dict;
3141
3142 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003143 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003144 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003145 if (key == NULL)
3146 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003147
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003148 if ((di = dict_find(dict, key, -1)) == NULL)
3149 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003150 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003151 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003152
3153 // If :silent! is used we will continue, make sure the
3154 // stack contents makes sense.
3155 clear_tv(tv);
3156 --ectx.ec_stack.ga_len;
3157 tv = STACK_TV_BOT(-1);
3158 clear_tv(tv);
3159 tv->v_type = VAR_NUMBER;
3160 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003161 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003162 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003163 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003164 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003165 // Clear the dict only after getting the item, to avoid
3166 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003167 tv = STACK_TV_BOT(-1);
3168 temp_tv = *tv;
3169 copy_tv(&di->di_tv, tv);
3170 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003171 }
3172 break;
3173
3174 // dict member with string key
3175 case ISN_STRINGMEMBER:
3176 {
3177 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003178 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003179 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003180
3181 tv = STACK_TV_BOT(-1);
3182 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3183 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003184 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003186 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 }
3188 dict = tv->vval.v_dict;
3189
3190 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3191 == NULL)
3192 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003193 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003194 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003195 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003196 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003197 // Clear the dict after getting the item, to avoid that it
3198 // make the item invalid.
3199 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003200 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003201 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003202 }
3203 break;
3204
3205 case ISN_NEGATENR:
3206 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003207 if (tv->v_type != VAR_NUMBER
3208#ifdef FEAT_FLOAT
3209 && tv->v_type != VAR_FLOAT
3210#endif
3211 )
3212 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003213 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003214 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003215 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003216 }
3217#ifdef FEAT_FLOAT
3218 if (tv->v_type == VAR_FLOAT)
3219 tv->vval.v_float = -tv->vval.v_float;
3220 else
3221#endif
3222 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003223 break;
3224
3225 case ISN_CHECKNR:
3226 {
3227 int error = FALSE;
3228
3229 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003230 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 (void)tv_get_number_chk(tv, &error);
3234 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003235 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 }
3237 break;
3238
3239 case ISN_CHECKTYPE:
3240 {
3241 checktype_T *ct = &iptr->isn_arg.type;
3242
3243 tv = STACK_TV_BOT(ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003244 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare32e5162021-01-21 20:21:29 +01003245 if (check_typval_type(ct->ct_type, tv, ct->ct_arg_idx)
3246 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003247 goto on_error;
3248
3249 // number 0 is FALSE, number 1 is TRUE
3250 if (tv->v_type == VAR_NUMBER
3251 && ct->ct_type->tt_type == VAR_BOOL
3252 && (tv->vval.v_number == 0
3253 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003255 tv->v_type = VAR_BOOL;
3256 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003257 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003258 }
3259 }
3260 break;
3261
Bram Moolenaar9af78762020-06-16 11:34:42 +02003262 case ISN_CHECKLEN:
3263 {
3264 int min_len = iptr->isn_arg.checklen.cl_min_len;
3265 list_T *list = NULL;
3266
3267 tv = STACK_TV_BOT(-1);
3268 if (tv->v_type == VAR_LIST)
3269 list = tv->vval.v_list;
3270 if (list == NULL || list->lv_len < min_len
3271 || (list->lv_len > min_len
3272 && !iptr->isn_arg.checklen.cl_more_OK))
3273 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003274 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003275 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003276 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003277 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003278 }
3279 }
3280 break;
3281
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003282 case ISN_SETTYPE:
3283 {
3284 checktype_T *ct = &iptr->isn_arg.type;
3285
3286 tv = STACK_TV_BOT(-1);
3287 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3288 {
3289 free_type(tv->vval.v_dict->dv_type);
3290 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3291 }
3292 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3293 {
3294 free_type(tv->vval.v_list->lv_type);
3295 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3296 }
3297 }
3298 break;
3299
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003301 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003302 {
3303 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003304 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003305
3306 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003307 if (iptr->isn_type == ISN_2BOOL)
3308 {
3309 n = tv2bool(tv);
3310 if (iptr->isn_arg.number) // invert
3311 n = !n;
3312 }
3313 else
3314 {
3315 SOURCING_LNUM = iptr->isn_lnum;
3316 n = tv_get_bool_chk(tv, &error);
3317 if (error)
3318 goto on_error;
3319 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 clear_tv(tv);
3321 tv->v_type = VAR_BOOL;
3322 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3323 }
3324 break;
3325
3326 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003327 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003328 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003329 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3330 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3331 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003332 break;
3333
Bram Moolenaar08597872020-12-10 19:43:40 +01003334 case ISN_RANGE:
3335 {
3336 exarg_T ea;
3337 char *errormsg;
3338
Bram Moolenaarece0b872021-01-08 20:40:45 +01003339 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003340 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003341 ea.addr_type = ADDR_LINES;
3342 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003343 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003344 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003345 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003346
3347 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3348 goto failed;
3349 ++ectx.ec_stack.ga_len;
3350 tv = STACK_TV_BOT(-1);
3351 tv->v_type = VAR_NUMBER;
3352 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003353 if (ea.addr_count == 0)
3354 tv->vval.v_number = curwin->w_cursor.lnum;
3355 else
3356 tv->vval.v_number = ea.line2;
3357 }
3358 break;
3359
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003360 case ISN_PUT:
3361 {
3362 int regname = iptr->isn_arg.put.put_regname;
3363 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3364 char_u *expr = NULL;
3365 int dir = FORWARD;
3366
Bram Moolenaar08597872020-12-10 19:43:40 +01003367 if (lnum < -2)
3368 {
3369 // line number was put on the stack by ISN_RANGE
3370 tv = STACK_TV_BOT(-1);
3371 curwin->w_cursor.lnum = tv->vval.v_number;
3372 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3373 dir = BACKWARD;
3374 --ectx.ec_stack.ga_len;
3375 }
3376 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003377 // :put! above cursor
3378 dir = BACKWARD;
3379 else if (lnum >= 0)
3380 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003381
3382 if (regname == '=')
3383 {
3384 tv = STACK_TV_BOT(-1);
3385 if (tv->v_type == VAR_STRING)
3386 expr = tv->vval.v_string;
3387 else
3388 {
3389 expr = typval2string(tv, TRUE); // allocates value
3390 clear_tv(tv);
3391 }
3392 --ectx.ec_stack.ga_len;
3393 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003394 check_cursor();
3395 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3396 vim_free(expr);
3397 }
3398 break;
3399
Bram Moolenaar02194d22020-10-24 23:08:38 +02003400 case ISN_CMDMOD:
3401 save_cmdmod = cmdmod;
3402 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003403 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003404 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3405 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003406 break;
3407
Bram Moolenaar02194d22020-10-24 23:08:38 +02003408 case ISN_CMDMOD_REV:
3409 // filter regprog is owned by the instruction, don't free it
3410 cmdmod.cmod_filter_regmatch.regprog = NULL;
3411 undo_cmdmod(&cmdmod);
3412 cmdmod = save_cmdmod;
3413 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003414 break;
3415
Bram Moolenaar792f7862020-11-23 08:31:18 +01003416 case ISN_UNPACK:
3417 {
3418 int count = iptr->isn_arg.unpack.unp_count;
3419 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3420 list_T *l;
3421 listitem_T *li;
3422 int i;
3423
3424 // Check there is a valid list to unpack.
3425 tv = STACK_TV_BOT(-1);
3426 if (tv->v_type != VAR_LIST)
3427 {
3428 SOURCING_LNUM = iptr->isn_lnum;
3429 emsg(_(e_for_argument_must_be_sequence_of_lists));
3430 goto on_error;
3431 }
3432 l = tv->vval.v_list;
3433 if (l == NULL
3434 || l->lv_len < (semicolon ? count - 1 : count))
3435 {
3436 SOURCING_LNUM = iptr->isn_lnum;
3437 emsg(_(e_list_value_does_not_have_enough_items));
3438 goto on_error;
3439 }
3440 else if (!semicolon && l->lv_len > count)
3441 {
3442 SOURCING_LNUM = iptr->isn_lnum;
3443 emsg(_(e_list_value_has_more_items_than_targets));
3444 goto on_error;
3445 }
3446
3447 CHECK_LIST_MATERIALIZE(l);
3448 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3449 goto failed;
3450 ectx.ec_stack.ga_len += count - 1;
3451
3452 // Variable after semicolon gets a list with the remaining
3453 // items.
3454 if (semicolon)
3455 {
3456 list_T *rem_list =
3457 list_alloc_with_items(l->lv_len - count + 1);
3458
3459 if (rem_list == NULL)
3460 goto failed;
3461 tv = STACK_TV_BOT(-count);
3462 tv->vval.v_list = rem_list;
3463 ++rem_list->lv_refcount;
3464 tv->v_lock = 0;
3465 li = l->lv_first;
3466 for (i = 0; i < count - 1; ++i)
3467 li = li->li_next;
3468 for (i = 0; li != NULL; ++i)
3469 {
3470 list_set_item(rem_list, i, &li->li_tv);
3471 li = li->li_next;
3472 }
3473 --count;
3474 }
3475
3476 // Produce the values in reverse order, first item last.
3477 li = l->lv_first;
3478 for (i = 0; i < count; ++i)
3479 {
3480 tv = STACK_TV_BOT(-i - 1);
3481 copy_tv(&li->li_tv, tv);
3482 li = li->li_next;
3483 }
3484
3485 list_unref(l);
3486 }
3487 break;
3488
Bram Moolenaar389df252020-07-09 21:20:47 +02003489 case ISN_SHUFFLE:
3490 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003491 typval_T tmp_tv;
3492 int item = iptr->isn_arg.shuffle.shfl_item;
3493 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003494
3495 tmp_tv = *STACK_TV_BOT(-item);
3496 for ( ; up > 0 && item > 1; --up)
3497 {
3498 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3499 --item;
3500 }
3501 *STACK_TV_BOT(-item) = tmp_tv;
3502 }
3503 break;
3504
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 case ISN_DROP:
3506 --ectx.ec_stack.ga_len;
3507 clear_tv(STACK_TV_BOT(0));
3508 break;
3509 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003510 continue;
3511
Bram Moolenaard032f342020-07-18 18:13:02 +02003512func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003513 // Restore previous function. If the frame pointer is where we started
3514 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003515 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003516 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003517
Bram Moolenaard032f342020-07-18 18:13:02 +02003518 if (func_return(&ectx) == FAIL)
3519 // only fails when out of memory
3520 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003521 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003522
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003523on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003524 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003525 // If "emsg_silent" is set then ignore the error, unless it was set
3526 // when calling the function.
3527 if (did_emsg_cumul + did_emsg == did_emsg_before
3528 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003529 {
3530 // If a sequence of instructions causes an error while ":silent!"
3531 // was used, restore the stack length and jump ahead to restoring
3532 // the cmdmod.
3533 if (restore_cmdmod)
3534 {
3535 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3536 {
3537 --ectx.ec_stack.ga_len;
3538 clear_tv(STACK_TV_BOT(0));
3539 }
3540 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3541 ++ectx.ec_iidx;
3542 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003543 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003544 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003545on_fatal_error:
3546 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003547 // If we are not inside a try-catch started here, abort execution.
3548 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003549 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003550 }
3551
3552done:
3553 // function finished, get result from the stack.
3554 tv = STACK_TV_BOT(-1);
3555 *rettv = *tv;
3556 tv->v_type = VAR_UNKNOWN;
3557 ret = OK;
3558
3559failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003560 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003561 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003562 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003563
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003564 // Deal with any remaining closures, they may be in use somewhere.
3565 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003566 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003567 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003568 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3569 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003570
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003571 estack_pop();
3572 current_sctx = save_current_sctx;
3573
Bram Moolenaar352134b2020-10-17 22:04:08 +02003574 if (*msg_list != NULL && saved_msg_list != NULL)
3575 {
3576 msglist_T **plist = saved_msg_list;
3577
3578 // Append entries from the current msg_list (uncaught exceptions) to
3579 // the saved msg_list.
3580 while (*plist != NULL)
3581 plist = &(*plist)->next;
3582
3583 *plist = *msg_list;
3584 }
3585 msg_list = saved_msg_list;
3586
Bram Moolenaar02194d22020-10-24 23:08:38 +02003587 if (restore_cmdmod)
3588 {
3589 cmdmod.cmod_filter_regmatch.regprog = NULL;
3590 undo_cmdmod(&cmdmod);
3591 cmdmod = save_cmdmod;
3592 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003593 emsg_silent_def = save_emsg_silent_def;
3594 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003595
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003596failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003597 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3599 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003600
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003601 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003602 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003603
Bram Moolenaar0186e582021-01-10 18:33:11 +01003604 while (ectx.ec_outer != NULL)
3605 {
3606 outer_T *up = ectx.ec_outer->out_up_is_copy
3607 ? NULL : ectx.ec_outer->out_up;
3608
3609 vim_free(ectx.ec_outer);
3610 ectx.ec_outer = up;
3611 }
3612
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003613 // Not sure if this is necessary.
3614 suppress_errthrow = save_suppress_errthrow;
3615
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003616 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003617 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003618 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003619 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003620 return ret;
3621}
3622
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003623/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003624 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003625 * We don't really need this at runtime, but we do have tests that require it,
3626 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003627 */
3628 void
3629ex_disassemble(exarg_T *eap)
3630{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003631 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003632 char_u *fname;
3633 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003634 dfunc_T *dfunc;
3635 isn_T *instr;
3636 int current;
3637 int line_idx = 0;
3638 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003639 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003641 if (STRNCMP(arg, "<lambda>", 8) == 0)
3642 {
3643 arg += 8;
3644 (void)getdigits(&arg);
3645 fname = vim_strnsave(eap->arg, arg - eap->arg);
3646 }
3647 else
3648 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003649 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003650 if (fname == NULL)
3651 {
3652 semsg(_(e_invarg2), eap->arg);
3653 return;
3654 }
3655
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003656 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003657 if (ufunc == NULL)
3658 {
3659 char_u *p = untrans_function_name(fname);
3660
3661 if (p != NULL)
3662 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003663 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003664 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003665 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 if (ufunc == NULL)
3667 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003668 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 return;
3670 }
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003671 if (ufunc->uf_def_status == UF_TO_BE_COMPILED
Bram Moolenaar822ba242020-05-24 23:00:18 +02003672 && compile_def_function(ufunc, FALSE, NULL) == FAIL)
3673 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003674 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003675 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003676 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003677 return;
3678 }
3679 if (ufunc->uf_name_exp != NULL)
3680 msg((char *)ufunc->uf_name_exp);
3681 else
3682 msg((char *)ufunc->uf_name);
3683
3684 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
3685 instr = dfunc->df_instr;
3686 for (current = 0; current < dfunc->df_instr_count; ++current)
3687 {
3688 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003689 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003690
3691 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3692 {
3693 if (current > prev_current)
3694 {
3695 msg_puts("\n\n");
3696 prev_current = current;
3697 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003698 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3699 if (line != NULL)
3700 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003701 }
3702
3703 switch (iptr->isn_type)
3704 {
3705 case ISN_EXEC:
3706 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3707 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003708 case ISN_EXECCONCAT:
3709 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003710 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003711 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003712 case ISN_ECHO:
3713 {
3714 echo_T *echo = &iptr->isn_arg.echo;
3715
3716 smsg("%4d %s %d", current,
3717 echo->echo_with_white ? "ECHO" : "ECHON",
3718 echo->echo_count);
3719 }
3720 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003721 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003722 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003723 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003724 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003725 case ISN_ECHOMSG:
3726 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003727 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003728 break;
3729 case ISN_ECHOERR:
3730 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003731 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003732 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003733 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003734 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003735 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003736 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003737 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003738 + STACK_FRAME_SIZE));
3739 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003740 smsg("%4d LOAD $%lld", current,
3741 (varnumber_T)(iptr->isn_arg.number));
3742 }
3743 break;
3744 case ISN_LOADOUTER:
3745 {
3746 if (iptr->isn_arg.number < 0)
3747 smsg("%4d LOADOUTER level %d arg[%d]", current,
3748 iptr->isn_arg.outer.outer_depth,
3749 iptr->isn_arg.outer.outer_idx
3750 + STACK_FRAME_SIZE);
3751 else
3752 smsg("%4d LOADOUTER level %d $%d", current,
3753 iptr->isn_arg.outer.outer_depth,
3754 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003755 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003756 break;
3757 case ISN_LOADV:
3758 smsg("%4d LOADV v:%s", current,
3759 get_vim_var_name(iptr->isn_arg.number));
3760 break;
3761 case ISN_LOADSCRIPT:
3762 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003763 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3764 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003765 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003766 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003768 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3769 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003770 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003771 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003772 }
3773 break;
3774 case ISN_LOADS:
3775 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003776 scriptitem_T *si = SCRIPT_ITEM(
3777 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778
3779 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003780 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003781 }
3782 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003783 case ISN_LOADAUTO:
3784 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3785 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 case ISN_LOADG:
3787 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3788 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003789 case ISN_LOADB:
3790 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3791 break;
3792 case ISN_LOADW:
3793 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3794 break;
3795 case ISN_LOADT:
3796 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3797 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003798 case ISN_LOADGDICT:
3799 smsg("%4d LOAD g:", current);
3800 break;
3801 case ISN_LOADBDICT:
3802 smsg("%4d LOAD b:", current);
3803 break;
3804 case ISN_LOADWDICT:
3805 smsg("%4d LOAD w:", current);
3806 break;
3807 case ISN_LOADTDICT:
3808 smsg("%4d LOAD t:", current);
3809 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 case ISN_LOADOPT:
3811 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3812 break;
3813 case ISN_LOADENV:
3814 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3815 break;
3816 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003817 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003818 break;
3819
3820 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003821 if (iptr->isn_arg.number < 0)
3822 smsg("%4d STORE arg[%lld]", current,
3823 iptr->isn_arg.number + STACK_FRAME_SIZE);
3824 else
3825 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3826 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003827 case ISN_STOREOUTER:
3828 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003829 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003830 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3831 iptr->isn_arg.outer.outer_depth,
3832 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003833 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003834 smsg("%4d STOREOUTER level %d $%d", current,
3835 iptr->isn_arg.outer.outer_depth,
3836 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003837 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003838 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003839 case ISN_STOREV:
3840 smsg("%4d STOREV v:%s", current,
3841 get_vim_var_name(iptr->isn_arg.number));
3842 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003843 case ISN_STOREAUTO:
3844 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3845 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003847 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3848 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003849 case ISN_STOREB:
3850 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3851 break;
3852 case ISN_STOREW:
3853 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3854 break;
3855 case ISN_STORET:
3856 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3857 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003858 case ISN_STORES:
3859 {
3860 scriptitem_T *si = SCRIPT_ITEM(
3861 iptr->isn_arg.loadstore.ls_sid);
3862
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003863 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003864 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003865 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003866 break;
3867 case ISN_STORESCRIPT:
3868 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003869 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3870 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003872 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003874 smsg("%4d STORESCRIPT %s-%d in %s", current,
3875 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003876 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003877 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003878 }
3879 break;
3880 case ISN_STOREOPT:
3881 smsg("%4d STOREOPT &%s", current,
3882 iptr->isn_arg.storeopt.so_name);
3883 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003884 case ISN_STOREENV:
3885 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3886 break;
3887 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003888 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003889 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890 case ISN_STORENR:
3891 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003892 iptr->isn_arg.storenr.stnr_val,
3893 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 break;
3895
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003896 case ISN_STOREINDEX:
3897 switch (iptr->isn_arg.vartype)
3898 {
3899 case VAR_LIST:
3900 smsg("%4d STORELIST", current);
3901 break;
3902 case VAR_DICT:
3903 smsg("%4d STOREDICT", current);
3904 break;
3905 case VAR_ANY:
3906 smsg("%4d STOREINDEX", current);
3907 break;
3908 default: break;
3909 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003910 break;
3911
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 // constants
3913 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003914 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003915 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 break;
3917 case ISN_PUSHBOOL:
3918 case ISN_PUSHSPEC:
3919 smsg("%4d PUSH %s", current,
3920 get_var_special_name(iptr->isn_arg.number));
3921 break;
3922 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003923#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003924 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01003925#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 break;
3927 case ISN_PUSHS:
3928 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
3929 break;
3930 case ISN_PUSHBLOB:
3931 {
3932 char_u *r;
3933 char_u numbuf[NUMBUFLEN];
3934 char_u *tofree;
3935
3936 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01003937 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003938 vim_free(tofree);
3939 }
3940 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003941 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01003942 {
3943 char *name = (char *)iptr->isn_arg.string;
3944
3945 smsg("%4d PUSHFUNC \"%s\"", current,
3946 name == NULL ? "[none]" : name);
3947 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003948 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003949 case ISN_PUSHCHANNEL:
3950#ifdef FEAT_JOB_CHANNEL
3951 {
3952 channel_T *channel = iptr->isn_arg.channel;
3953
3954 smsg("%4d PUSHCHANNEL %d", current,
3955 channel == NULL ? 0 : channel->ch_id);
3956 }
3957#endif
3958 break;
3959 case ISN_PUSHJOB:
3960#ifdef FEAT_JOB_CHANNEL
3961 {
3962 typval_T tv;
3963 char_u *name;
3964
3965 tv.v_type = VAR_JOB;
3966 tv.vval.v_job = iptr->isn_arg.job;
3967 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01003968 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01003969 }
3970#endif
3971 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003972 case ISN_PUSHEXC:
3973 smsg("%4d PUSH v:exception", current);
3974 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02003975 case ISN_UNLET:
3976 smsg("%4d UNLET%s %s", current,
3977 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3978 iptr->isn_arg.unlet.ul_name);
3979 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02003980 case ISN_UNLETENV:
3981 smsg("%4d UNLETENV%s $%s", current,
3982 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
3983 iptr->isn_arg.unlet.ul_name);
3984 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01003985 case ISN_UNLETINDEX:
3986 smsg("%4d UNLETINDEX", current);
3987 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02003988 case ISN_LOCKCONST:
3989 smsg("%4d LOCKCONST", current);
3990 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003991 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01003992 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003993 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003994 break;
3995 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01003996 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003997 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 break;
3999
4000 // function call
4001 case ISN_BCALL:
4002 {
4003 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4004
4005 smsg("%4d BCALL %s(argc %d)", current,
4006 internal_func_name(cbfunc->cbf_idx),
4007 cbfunc->cbf_argcount);
4008 }
4009 break;
4010 case ISN_DCALL:
4011 {
4012 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4013 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4014 + cdfunc->cdf_idx;
4015
4016 smsg("%4d DCALL %s(argc %d)", current,
4017 df->df_ufunc->uf_name_exp != NULL
4018 ? df->df_ufunc->uf_name_exp
4019 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4020 }
4021 break;
4022 case ISN_UCALL:
4023 {
4024 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4025
4026 smsg("%4d UCALL %s(argc %d)", current,
4027 cufunc->cuf_name, cufunc->cuf_argcount);
4028 }
4029 break;
4030 case ISN_PCALL:
4031 {
4032 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4033
4034 smsg("%4d PCALL%s (argc %d)", current,
4035 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4036 }
4037 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004038 case ISN_PCALL_END:
4039 smsg("%4d PCALL end", current);
4040 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004041 case ISN_RETURN:
4042 smsg("%4d RETURN", current);
4043 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004044 case ISN_RETURN_ZERO:
4045 smsg("%4d RETURN 0", current);
4046 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 case ISN_FUNCREF:
4048 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004049 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004050 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004051 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004052
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004053 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 }
4055 break;
4056
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004057 case ISN_NEWFUNC:
4058 {
4059 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4060
4061 smsg("%4d NEWFUNC %s %s", current,
4062 newfunc->nf_lambda, newfunc->nf_global);
4063 }
4064 break;
4065
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004066 case ISN_DEF:
4067 {
4068 char_u *name = iptr->isn_arg.string;
4069
4070 smsg("%4d DEF %s", current,
4071 name == NULL ? (char_u *)"" : name);
4072 }
4073 break;
4074
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004075 case ISN_JUMP:
4076 {
4077 char *when = "?";
4078
4079 switch (iptr->isn_arg.jump.jump_when)
4080 {
4081 case JUMP_ALWAYS:
4082 when = "JUMP";
4083 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 case JUMP_AND_KEEP_IF_TRUE:
4085 when = "JUMP_AND_KEEP_IF_TRUE";
4086 break;
4087 case JUMP_IF_FALSE:
4088 when = "JUMP_IF_FALSE";
4089 break;
4090 case JUMP_AND_KEEP_IF_FALSE:
4091 when = "JUMP_AND_KEEP_IF_FALSE";
4092 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004093 case JUMP_IF_COND_FALSE:
4094 when = "JUMP_IF_COND_FALSE";
4095 break;
4096 case JUMP_IF_COND_TRUE:
4097 when = "JUMP_IF_COND_TRUE";
4098 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004099 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004100 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004101 iptr->isn_arg.jump.jump_where);
4102 }
4103 break;
4104
4105 case ISN_FOR:
4106 {
4107 forloop_T *forloop = &iptr->isn_arg.forloop;
4108
4109 smsg("%4d FOR $%d -> %d", current,
4110 forloop->for_idx, forloop->for_end);
4111 }
4112 break;
4113
4114 case ISN_TRY:
4115 {
4116 try_T *try = &iptr->isn_arg.try;
4117
4118 smsg("%4d TRY catch -> %d, finally -> %d", current,
4119 try->try_catch, try->try_finally);
4120 }
4121 break;
4122 case ISN_CATCH:
4123 // TODO
4124 smsg("%4d CATCH", current);
4125 break;
4126 case ISN_ENDTRY:
4127 smsg("%4d ENDTRY", current);
4128 break;
4129 case ISN_THROW:
4130 smsg("%4d THROW", current);
4131 break;
4132
4133 // expression operations on number
4134 case ISN_OPNR:
4135 case ISN_OPFLOAT:
4136 case ISN_OPANY:
4137 {
4138 char *what;
4139 char *ins;
4140
4141 switch (iptr->isn_arg.op.op_type)
4142 {
4143 case EXPR_MULT: what = "*"; break;
4144 case EXPR_DIV: what = "/"; break;
4145 case EXPR_REM: what = "%"; break;
4146 case EXPR_SUB: what = "-"; break;
4147 case EXPR_ADD: what = "+"; break;
4148 default: what = "???"; break;
4149 }
4150 switch (iptr->isn_type)
4151 {
4152 case ISN_OPNR: ins = "OPNR"; break;
4153 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4154 case ISN_OPANY: ins = "OPANY"; break;
4155 default: ins = "???"; break;
4156 }
4157 smsg("%4d %s %s", current, ins, what);
4158 }
4159 break;
4160
4161 case ISN_COMPAREBOOL:
4162 case ISN_COMPARESPECIAL:
4163 case ISN_COMPARENR:
4164 case ISN_COMPAREFLOAT:
4165 case ISN_COMPARESTRING:
4166 case ISN_COMPAREBLOB:
4167 case ISN_COMPARELIST:
4168 case ISN_COMPAREDICT:
4169 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 case ISN_COMPAREANY:
4171 {
4172 char *p;
4173 char buf[10];
4174 char *type;
4175
4176 switch (iptr->isn_arg.op.op_type)
4177 {
4178 case EXPR_EQUAL: p = "=="; break;
4179 case EXPR_NEQUAL: p = "!="; break;
4180 case EXPR_GREATER: p = ">"; break;
4181 case EXPR_GEQUAL: p = ">="; break;
4182 case EXPR_SMALLER: p = "<"; break;
4183 case EXPR_SEQUAL: p = "<="; break;
4184 case EXPR_MATCH: p = "=~"; break;
4185 case EXPR_IS: p = "is"; break;
4186 case EXPR_ISNOT: p = "isnot"; break;
4187 case EXPR_NOMATCH: p = "!~"; break;
4188 default: p = "???"; break;
4189 }
4190 STRCPY(buf, p);
4191 if (iptr->isn_arg.op.op_ic == TRUE)
4192 strcat(buf, "?");
4193 switch(iptr->isn_type)
4194 {
4195 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4196 case ISN_COMPARESPECIAL:
4197 type = "COMPARESPECIAL"; break;
4198 case ISN_COMPARENR: type = "COMPARENR"; break;
4199 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4200 case ISN_COMPARESTRING:
4201 type = "COMPARESTRING"; break;
4202 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4203 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4204 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4205 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004206 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4207 default: type = "???"; break;
4208 }
4209
4210 smsg("%4d %s %s", current, type, buf);
4211 }
4212 break;
4213
4214 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4215 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4216
4217 // expression operations
4218 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004219 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004220 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004221 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004222 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004223 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004224 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004225 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4226 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004227 case ISN_SLICE: smsg("%4d SLICE %lld",
4228 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004229 case ISN_GETITEM: smsg("%4d ITEM %lld",
4230 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004231 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4232 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004233 iptr->isn_arg.string); break;
4234 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4235
4236 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004237 case ISN_CHECKTYPE:
4238 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004239 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004240 char *tofree;
4241
Bram Moolenaare32e5162021-01-21 20:21:29 +01004242 if (ct->ct_arg_idx == 0)
4243 smsg("%4d CHECKTYPE %s stack[%d]", current,
4244 type_name(ct->ct_type, &tofree),
4245 (int)ct->ct_off);
4246 else
4247 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4248 type_name(ct->ct_type, &tofree),
4249 (int)ct->ct_off,
4250 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004251 vim_free(tofree);
4252 break;
4253 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004254 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4255 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4256 iptr->isn_arg.checklen.cl_min_len);
4257 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004258 case ISN_SETTYPE:
4259 {
4260 char *tofree;
4261
4262 smsg("%4d SETTYPE %s", current,
4263 type_name(iptr->isn_arg.type.ct_type, &tofree));
4264 vim_free(tofree);
4265 break;
4266 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004267 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 case ISN_2BOOL: if (iptr->isn_arg.number)
4269 smsg("%4d INVERT (!val)", current);
4270 else
4271 smsg("%4d 2BOOL (!!val)", current);
4272 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004273 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004274 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004275 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004276 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004277 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004278 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004279 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4280 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004281 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004282 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4283 smsg("%4d PUT %c above range",
4284 current, iptr->isn_arg.put.put_regname);
4285 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4286 smsg("%4d PUT %c range",
4287 current, iptr->isn_arg.put.put_regname);
4288 else
4289 smsg("%4d PUT %c %ld", current,
4290 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004291 (long)iptr->isn_arg.put.put_lnum);
4292 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004293
Bram Moolenaar02194d22020-10-24 23:08:38 +02004294 // TODO: summarize modifiers
4295 case ISN_CMDMOD:
4296 {
4297 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004298 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004299 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4300
4301 buf = alloc(len + 1);
4302 if (buf != NULL)
4303 {
4304 (void)produce_cmdmods(
4305 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4306 smsg("%4d CMDMOD %s", current, buf);
4307 vim_free(buf);
4308 }
4309 break;
4310 }
4311 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004312
Bram Moolenaar792f7862020-11-23 08:31:18 +01004313 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4314 iptr->isn_arg.unpack.unp_count,
4315 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4316 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004317 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4318 iptr->isn_arg.shuffle.shfl_item,
4319 iptr->isn_arg.shuffle.shfl_up);
4320 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004321 case ISN_DROP: smsg("%4d DROP", current); break;
4322 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004323
4324 out_flush(); // output one line at a time
4325 ui_breakcheck();
4326 if (got_int)
4327 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004329}
4330
4331/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004332 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004333 * list, etc. Mostly like what JavaScript does, except that empty list and
4334 * empty dictionary are FALSE.
4335 */
4336 int
4337tv2bool(typval_T *tv)
4338{
4339 switch (tv->v_type)
4340 {
4341 case VAR_NUMBER:
4342 return tv->vval.v_number != 0;
4343 case VAR_FLOAT:
4344#ifdef FEAT_FLOAT
4345 return tv->vval.v_float != 0.0;
4346#else
4347 break;
4348#endif
4349 case VAR_PARTIAL:
4350 return tv->vval.v_partial != NULL;
4351 case VAR_FUNC:
4352 case VAR_STRING:
4353 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4354 case VAR_LIST:
4355 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4356 case VAR_DICT:
4357 return tv->vval.v_dict != NULL
4358 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4359 case VAR_BOOL:
4360 case VAR_SPECIAL:
4361 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4362 case VAR_JOB:
4363#ifdef FEAT_JOB_CHANNEL
4364 return tv->vval.v_job != NULL;
4365#else
4366 break;
4367#endif
4368 case VAR_CHANNEL:
4369#ifdef FEAT_JOB_CHANNEL
4370 return tv->vval.v_channel != NULL;
4371#else
4372 break;
4373#endif
4374 case VAR_BLOB:
4375 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4376 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004377 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004378 case VAR_VOID:
4379 break;
4380 }
4381 return FALSE;
4382}
4383
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004384 void
4385emsg_using_string_as(typval_T *tv, int as_number)
4386{
4387 semsg(_(as_number ? e_using_string_as_number_str
4388 : e_using_string_as_bool_str),
4389 tv->vval.v_string == NULL
4390 ? (char_u *)"" : tv->vval.v_string);
4391}
4392
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004393/*
4394 * If "tv" is a string give an error and return FAIL.
4395 */
4396 int
4397check_not_string(typval_T *tv)
4398{
4399 if (tv->v_type == VAR_STRING)
4400 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004401 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004402 clear_tv(tv);
4403 return FAIL;
4404 }
4405 return OK;
4406}
4407
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004408
4409#endif // FEAT_EVAL