blob: 8728088c08f4213206cde227315b654450b9abe3 [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 Moolenaare5ea3462021-01-25 21:01:48 +0100184#ifdef FEAT_PROFILE
185 // Profiling might be enabled/disabled along the way. This should not
186 // fail, since the function was compiled before and toggling profiling
187 // doesn't change any errors.
188 if (func_needs_compiling(ufunc, PROFILING(ufunc))
189 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
190 == FAIL)
191 return FAIL;
192#endif
193
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200194 if (ufunc->uf_va_name != NULL)
195 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200196 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200197 // Stack at time of call with 2 varargs:
198 // normal_arg
199 // optional_arg
200 // vararg_1
201 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200202 // After creating the list:
203 // normal_arg
204 // optional_arg
205 // vararg-list
206 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200207 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200208 // After creating the list
209 // normal_arg
210 // (space for optional_arg)
211 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200212 vararg_count = argcount - ufunc->uf_args.ga_len;
213 if (vararg_count < 0)
214 vararg_count = 0;
215 else
216 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200217 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200218 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200219
220 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200221 }
222
Bram Moolenaarfe270812020-04-11 22:31:27 +0200223 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200224 if (arg_to_add < 0)
225 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200226 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200227 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200228 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200229 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200230 return FAIL;
231 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200232
233 // Reserve space for:
234 // - missing arguments
235 // - stack frame
236 // - local variables
237 // - if needed: a counter for number of closures created in
238 // ectx->ec_funcrefs.
239 varcount = dfunc->df_varcount + dfunc->df_has_closure;
240 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
241 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100242 return FAIL;
243
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100244 // If depth of calling is getting too high, don't execute the function.
245 if (funcdepth_increment() == FAIL)
246 return FAIL;
247
Bram Moolenaarfe270812020-04-11 22:31:27 +0200248 // Move the vararg-list to below the missing optional arguments.
249 if (vararg_count > 0 && arg_to_add > 0)
250 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100251
252 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200253 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200254 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200255 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100256
257 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100258 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
259 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100260 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
261 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200262 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100263
264 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200265 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100266 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200267 if (dfunc->df_has_closure)
268 {
269 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
270
271 tv->v_type = VAR_NUMBER;
272 tv->vval.v_number = 0;
273 }
274 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100275
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100276 if (pt != NULL || ufunc->uf_partial != NULL
277 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100278 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100279 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
280
281 if (outer == NULL)
282 return FAIL;
283 if (pt != NULL)
284 {
285 *outer = pt->pt_outer;
286 outer->out_up_is_copy = TRUE;
287 }
288 else if (ufunc->uf_partial != NULL)
289 {
290 *outer = ufunc->uf_partial->pt_outer;
291 outer->out_up_is_copy = TRUE;
292 }
293 else
294 {
295 outer->out_stack = &ectx->ec_stack;
296 outer->out_frame_idx = ectx->ec_frame_idx;
297 outer->out_up = ectx->ec_outer;
298 }
299 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100300 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100301 else
302 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100303
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100304 // Set execution state to the start of the called function.
305 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100306 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100307 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200308 if (entry != NULL)
309 {
310 // Set the script context to the script where the function was defined.
311 // TODO: save more than the SID?
312 entry->es_save_sid = current_sctx.sc_sid;
313 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
314 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100315
316 // Decide where to start execution, handles optional arguments.
317 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100318
319 return OK;
320}
321
322// Get pointer to item in the stack.
323#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
324
325/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200326 * Used when returning from a function: Check if any closure is still
327 * referenced. If so then move the arguments and variables to a separate piece
328 * of stack to be used when the closure is called.
329 * When "free_arguments" is TRUE the arguments are to be freed.
330 * Returns FAIL when out of memory.
331 */
332 static int
333handle_closure_in_use(ectx_T *ectx, int free_arguments)
334{
335 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
336 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200337 int argcount;
338 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200339 int idx;
340 typval_T *tv;
341 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200342 garray_T *gap = &ectx->ec_funcrefs;
343 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200344
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200345 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200346 return OK; // function was freed
347 if (dfunc->df_has_closure == 0)
348 return OK; // no closures
349 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
350 closure_count = tv->vval.v_number;
351 if (closure_count == 0)
352 return OK; // no funcrefs created
353
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200354 argcount = ufunc_argcount(dfunc->df_ufunc);
355 top = ectx->ec_frame_idx - argcount;
356
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200357 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200358 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200359 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200360 partial_T *pt;
361 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200362
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200363 if (off < 0)
364 continue; // count is off or already done
365 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200366 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200367 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200369 int i;
370
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200371 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200372 // unreferenced on return.
373 for (i = 0; i < dfunc->df_varcount; ++i)
374 {
375 typval_T *stv = STACK_TV(ectx->ec_frame_idx
376 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200377 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200378 --refcount;
379 }
380 if (refcount > 1)
381 {
382 closure_in_use = TRUE;
383 break;
384 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200385 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200386 }
387
388 if (closure_in_use)
389 {
390 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
391 typval_T *stack;
392
393 // A closure is using the arguments and/or local variables.
394 // Move them to the called function.
395 if (funcstack == NULL)
396 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200397 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
398 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200399 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
400 funcstack->fs_ga.ga_data = stack;
401 if (stack == NULL)
402 {
403 vim_free(funcstack);
404 return FAIL;
405 }
406
407 // Move or copy the arguments.
408 for (idx = 0; idx < argcount; ++idx)
409 {
410 tv = STACK_TV(top + idx);
411 if (free_arguments)
412 {
413 *(stack + idx) = *tv;
414 tv->v_type = VAR_UNKNOWN;
415 }
416 else
417 copy_tv(tv, stack + idx);
418 }
419 // Move the local variables.
420 for (idx = 0; idx < dfunc->df_varcount; ++idx)
421 {
422 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200423
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200424 // A partial created for a local function, that is also used as a
425 // local variable, has a reference count for the variable, thus
426 // will never go down to zero. When all these refcounts are one
427 // then the funcstack is unused. We need to count how many we have
428 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200429 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
430 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200431 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200432
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200433 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200434 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
435 gap->ga_len - closure_count + i])
436 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200437 }
438
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200439 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200440 tv->v_type = VAR_UNKNOWN;
441 }
442
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200443 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200444 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200445 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
446 - closure_count + idx];
447 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200448 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200449 ++funcstack->fs_refcount;
450 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100451 pt->pt_outer.out_stack = &funcstack->fs_ga;
452 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
453 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200454 }
455 }
456 }
457
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200458 for (idx = 0; idx < closure_count; ++idx)
459 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
460 - closure_count + idx]);
461 gap->ga_len -= closure_count;
462 if (gap->ga_len == 0)
463 ga_clear(gap);
464
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200465 return OK;
466}
467
468/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200469 * Called when a partial is freed or its reference count goes down to one. The
470 * funcstack may be the only reference to the partials in the local variables.
471 * Go over all of them, the funcref and can be freed if all partials
472 * referencing the funcstack have a reference count of one.
473 */
474 void
475funcstack_check_refcount(funcstack_T *funcstack)
476{
477 int i;
478 garray_T *gap = &funcstack->fs_ga;
479 int done = 0;
480
481 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
482 return;
483 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
484 {
485 typval_T *tv = ((typval_T *)gap->ga_data) + i;
486
487 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
488 && tv->vval.v_partial->pt_funcstack == funcstack
489 && tv->vval.v_partial->pt_refcount == 1)
490 ++done;
491 }
492 if (done == funcstack->fs_min_refcount)
493 {
494 typval_T *stack = gap->ga_data;
495
496 // All partials referencing the funcstack have a reference count of
497 // one, thus the funcstack is no longer of use.
498 for (i = 0; i < gap->ga_len; ++i)
499 clear_tv(stack + i);
500 vim_free(stack);
501 vim_free(funcstack);
502 }
503}
504
505/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100506 * Return from the current function.
507 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200508 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100509func_return(ectx_T *ectx)
510{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100511 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100512 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200513 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
514 + ectx->ec_dfunc_idx;
515 int argcount = ufunc_argcount(dfunc->df_ufunc);
516 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200517 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100518
519 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200520 entry = estack_pop();
521 if (entry != NULL)
522 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100523
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200524 if (handle_closure_in_use(ectx, TRUE) == FAIL)
525 return FAIL;
526
527 // Clear the arguments.
528 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
529 clear_tv(STACK_TV(idx));
530
531 // Clear local variables and temp values, but not the return value.
532 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100533 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100534 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100535
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100536 // The return value should be on top of the stack. However, when aborting
537 // it may not be there and ec_frame_idx is the top of the stack.
538 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100539 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100540 ret_idx = 0;
541
Bram Moolenaar0186e582021-01-10 18:33:11 +0100542 vim_free(ectx->ec_outer);
543
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100544 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100545 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
546 + STACK_FRAME_FUNC_OFF)->vval.v_number;
547 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
548 + STACK_FRAME_IIDX_OFF)->vval.v_number;
549 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
550 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200551 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100552 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
553 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100554 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100555 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100556
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100557 if (ret_idx > 0)
558 {
559 // Reset the stack to the position before the call, with a spot for the
560 // return value, moved there from above the frame.
561 ectx->ec_stack.ga_len = top + 1;
562 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
563 }
564 else
565 // Reset the stack to the position before the call.
566 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200567
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100568 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200569 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100570}
571
572#undef STACK_TV
573
574/*
575 * Prepare arguments and rettv for calling a builtin or user function.
576 */
577 static int
578call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
579{
580 int idx;
581 typval_T *tv;
582
583 // Move arguments from bottom of the stack to argvars[] and add terminator.
584 for (idx = 0; idx < argcount; ++idx)
585 argvars[idx] = *STACK_TV_BOT(idx - argcount);
586 argvars[argcount].v_type = VAR_UNKNOWN;
587
588 // Result replaces the arguments on the stack.
589 if (argcount > 0)
590 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200591 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100592 return FAIL;
593 else
594 ++ectx->ec_stack.ga_len;
595
596 // Default return value is zero.
597 tv = STACK_TV_BOT(-1);
598 tv->v_type = VAR_NUMBER;
599 tv->vval.v_number = 0;
600
601 return OK;
602}
603
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200604// Ugly global to avoid passing the execution context around through many
605// layers.
606static ectx_T *current_ectx = NULL;
607
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608/*
609 * Call a builtin function by index.
610 */
611 static int
612call_bfunc(int func_idx, int argcount, ectx_T *ectx)
613{
614 typval_T argvars[MAX_FUNC_ARGS];
615 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100616 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200617 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100618
619 if (call_prepare(argcount, argvars, ectx) == FAIL)
620 return FAIL;
621
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200622 // Call the builtin function. Set "current_ectx" so that when it
623 // recursively invokes call_def_function() a closure context can be set.
624 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100625 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200626 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100627
628 // Clear the arguments.
629 for (idx = 0; idx < argcount; ++idx)
630 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200631
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100632 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200633 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100634 return OK;
635}
636
637/*
638 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100639 * If the function is compiled this will add a stack frame and set the
640 * instruction pointer at the start of the function.
641 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100642 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100643 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100644 */
645 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100646call_ufunc(
647 ufunc_T *ufunc,
648 partial_T *pt,
649 int argcount,
650 ectx_T *ectx,
651 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100652{
653 typval_T argvars[MAX_FUNC_ARGS];
654 funcexe_T funcexe;
655 int error;
656 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100657 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100658#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100659 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100660#else
661# define profiling FALSE
662#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663
Bram Moolenaarb2049902021-01-24 12:53:53 +0100664 if (func_needs_compiling(ufunc, profiling)
665 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200666 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200667 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100668 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100669 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100670 if (error != FCERR_UNKNOWN)
671 {
672 if (error == FCERR_TOOMANY)
673 semsg(_(e_toomanyarg), ufunc->uf_name);
674 else
675 semsg(_(e_toofewarg), ufunc->uf_name);
676 return FAIL;
677 }
678
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100679 // The function has been compiled, can call it quickly. For a function
680 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100681 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100682 if (iptr != NULL)
683 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100684 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100685 iptr->isn_type = ISN_DCALL;
686 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
687 iptr->isn_arg.dfunc.cdf_argcount = argcount;
688 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100689 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100690 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100691
692 if (call_prepare(argcount, argvars, ectx) == FAIL)
693 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200694 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695 funcexe.evaluate = TRUE;
696
697 // Call the user function. Result goes in last position on the stack.
698 // TODO: add selfdict if there is one
699 error = call_user_func_check(ufunc, argcount, argvars,
700 STACK_TV_BOT(-1), &funcexe, NULL);
701
702 // Clear the arguments.
703 for (idx = 0; idx < argcount; ++idx)
704 clear_tv(&argvars[idx]);
705
706 if (error != FCERR_NONE)
707 {
708 user_func_error(error, ufunc->uf_name);
709 return FAIL;
710 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100711 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200712 // Error other than from calling the function itself.
713 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100714 return OK;
715}
716
717/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200718 * Return TRUE if an error was given or CTRL-C was pressed.
719 */
720 static int
721vim9_aborting(int prev_called_emsg)
722{
723 return called_emsg > prev_called_emsg || got_int || did_throw;
724}
725
726/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100727 * Execute a function by "name".
728 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100729 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100730 * Returns FAIL if not found without an error message.
731 */
732 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100733call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734{
735 ufunc_T *ufunc;
736
737 if (builtin_function(name, -1))
738 {
739 int func_idx = find_internal_func(name);
740
741 if (func_idx < 0)
742 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200743 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100744 return FAIL;
745 return call_bfunc(func_idx, argcount, ectx);
746 }
747
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200748 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200749
750 if (ufunc == NULL)
751 {
752 int called_emsg_before = called_emsg;
753
754 if (script_autoload(name, TRUE))
755 // loaded a package, search for the function again
756 ufunc = find_func(name, FALSE, NULL);
757 if (vim9_aborting(called_emsg_before))
758 return FAIL; // bail out if loading the script caused an error
759 }
760
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100761 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100762 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100763
764 return FAIL;
765}
766
767 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200768call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100769{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200770 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200771 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100773 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100774
775 if (tv->v_type == VAR_PARTIAL)
776 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200777 partial_T *pt = tv->vval.v_partial;
778 int i;
779
780 if (pt->pt_argc > 0)
781 {
782 // Make space for arguments from the partial, shift the "argcount"
783 // arguments up.
784 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
785 return FAIL;
786 for (i = 1; i <= argcount; ++i)
787 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
788 ectx->ec_stack.ga_len += pt->pt_argc;
789 argcount += pt->pt_argc;
790
791 // copy the arguments from the partial onto the stack
792 for (i = 0; i < pt->pt_argc; ++i)
793 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
794 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100795
796 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100797 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 name = pt->pt_name;
800 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200801 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100802 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200803 if (name != NULL)
804 {
805 char_u fname_buf[FLEN_FIXED + 1];
806 char_u *tofree = NULL;
807 int error = FCERR_NONE;
808 char_u *fname;
809
810 // May need to translate <SNR>123_ to K_SNR.
811 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
812 if (error != FCERR_NONE)
813 res = FAIL;
814 else
815 res = call_by_name(fname, argcount, ectx, NULL);
816 vim_free(tofree);
817 }
818
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100819 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100820 {
821 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200822 semsg(_(e_unknownfunc),
823 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824 return FAIL;
825 }
826 return OK;
827}
828
829/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200830 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
831 * TRUE.
832 */
833 static int
834error_if_locked(int lock, char *error)
835{
836 if (lock & (VAR_LOCKED | VAR_FIXED))
837 {
838 emsg(_(error));
839 return TRUE;
840 }
841 return FALSE;
842}
843
844/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100845 * Store "tv" in variable "name".
846 * This is for s: and g: variables.
847 */
848 static void
849store_var(char_u *name, typval_T *tv)
850{
851 funccal_entry_T entry;
852
853 save_funccal(&entry);
Bram Moolenaar3862ea32021-01-01 21:05:55 +0100854 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100855 restore_funccal();
856}
857
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100858/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100859 * Convert "tv" to a string.
860 * Return FAIL if not allowed.
861 */
862 static int
863do_2string(typval_T *tv, int is_2string_any)
864{
865 if (tv->v_type != VAR_STRING)
866 {
867 char_u *str;
868
869 if (is_2string_any)
870 {
871 switch (tv->v_type)
872 {
873 case VAR_SPECIAL:
874 case VAR_BOOL:
875 case VAR_NUMBER:
876 case VAR_FLOAT:
877 case VAR_BLOB: break;
878 default: to_string_error(tv->v_type);
879 return FAIL;
880 }
881 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100882 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100883 clear_tv(tv);
884 tv->v_type = VAR_STRING;
885 tv->vval.v_string = str;
886 }
887 return OK;
888}
889
890/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100891 * When the value of "sv" is a null list of dict, allocate it.
892 */
893 static void
894allocate_if_null(typval_T *tv)
895{
896 switch (tv->v_type)
897 {
898 case VAR_LIST:
899 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100900 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100901 break;
902 case VAR_DICT:
903 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100904 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100905 break;
906 default:
907 break;
908 }
909}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200910
Bram Moolenaare7525c52021-01-09 13:20:37 +0100911/*
912 * Return the character "str[index]" where "index" is the character index. If
913 * "index" is out of range NULL is returned.
914 */
915 char_u *
916char_from_string(char_u *str, varnumber_T index)
917{
918 size_t nbyte = 0;
919 varnumber_T nchar = index;
920 size_t slen;
921
922 if (str == NULL)
923 return NULL;
924 slen = STRLEN(str);
925
926 // do the same as for a list: a negative index counts from the end
927 if (index < 0)
928 {
929 int clen = 0;
930
931 for (nbyte = 0; nbyte < slen; ++clen)
932 nbyte += MB_CPTR2LEN(str + nbyte);
933 nchar = clen + index;
934 if (nchar < 0)
935 // unlike list: index out of range results in empty string
936 return NULL;
937 }
938
939 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
940 nbyte += MB_CPTR2LEN(str + nbyte);
941 if (nbyte >= slen)
942 return NULL;
943 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
944}
945
946/*
947 * Get the byte index for character index "idx" in string "str" with length
948 * "str_len".
949 * If going over the end return "str_len".
950 * If "idx" is negative count from the end, -1 is the last character.
951 * When going over the start return -1.
952 */
953 static long
954char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
955{
956 varnumber_T nchar = idx;
957 size_t nbyte = 0;
958
959 if (nchar >= 0)
960 {
961 while (nchar > 0 && nbyte < str_len)
962 {
963 nbyte += MB_CPTR2LEN(str + nbyte);
964 --nchar;
965 }
966 }
967 else
968 {
969 nbyte = str_len;
970 while (nchar < 0 && nbyte > 0)
971 {
972 --nbyte;
973 nbyte -= mb_head_off(str, str + nbyte);
974 ++nchar;
975 }
976 if (nchar < 0)
977 return -1;
978 }
979 return (long)nbyte;
980}
981
982/*
983 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100984 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100985 * Return NULL when the result is empty.
986 */
987 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100988string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100989{
990 long start_byte, end_byte;
991 size_t slen;
992
993 if (str == NULL)
994 return NULL;
995 slen = STRLEN(str);
996 start_byte = char_idx2byte(str, slen, first);
997 if (start_byte < 0)
998 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +0100999 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001000 end_byte = (long)slen;
1001 else
1002 {
1003 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001004 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001005 // end index is inclusive
1006 end_byte += MB_CPTR2LEN(str + end_byte);
1007 }
1008
1009 if (start_byte >= (long)slen || end_byte <= start_byte)
1010 return NULL;
1011 return vim_strnsave(str + start_byte, end_byte - start_byte);
1012}
1013
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001014 static svar_T *
1015get_script_svar(scriptref_T *sref, ectx_T *ectx)
1016{
1017 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1018 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1019 + ectx->ec_dfunc_idx;
1020 svar_T *sv;
1021
1022 if (sref->sref_seq != si->sn_script_seq)
1023 {
1024 // The script was reloaded after the function was
1025 // compiled, the script_idx may not be valid.
1026 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1027 dfunc->df_ufunc->uf_name_exp);
1028 return NULL;
1029 }
1030 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1031 if (!equal_type(sv->sv_type, sref->sref_type))
1032 {
1033 emsg(_(e_script_variable_type_changed));
1034 return NULL;
1035 }
1036 return sv;
1037}
1038
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001039/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001040 * Execute a function by "name".
1041 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001042 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001043 */
1044 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001045call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001046{
Bram Moolenaared677f52020-08-12 16:38:10 +02001047 int called_emsg_before = called_emsg;
1048 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001049
Bram Moolenaared677f52020-08-12 16:38:10 +02001050 res = call_by_name(name, argcount, ectx, iptr);
1051 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001052 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001053 dictitem_T *v;
1054
1055 v = find_var(name, NULL, FALSE);
1056 if (v == NULL)
1057 {
1058 semsg(_(e_unknownfunc), name);
1059 return FAIL;
1060 }
1061 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1062 {
1063 semsg(_(e_unknownfunc), name);
1064 return FAIL;
1065 }
1066 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001067 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001068 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001069}
1070
1071/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001072 * When a function reference is used, fill a partial with the information
1073 * needed, especially when it is used as a closure.
1074 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001075 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001076fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1077{
1078 pt->pt_func = ufunc;
1079 pt->pt_refcount = 1;
1080
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001081 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001082 {
1083 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1084 + ectx->ec_dfunc_idx;
1085
1086 // The closure needs to find arguments and local
1087 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001088 pt->pt_outer.out_stack = &ectx->ec_stack;
1089 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1090 pt->pt_outer.out_up = ectx->ec_outer;
1091 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001092
1093 // If this function returns and the closure is still
1094 // being used, we need to make a copy of the context
1095 // (arguments and local variables). Store a reference
1096 // to the partial so we can handle that.
1097 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1098 {
1099 vim_free(pt);
1100 return FAIL;
1101 }
1102 // Extra variable keeps the count of closures created
1103 // in the current function call.
1104 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1105 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1106
1107 ((partial_T **)ectx->ec_funcrefs.ga_data)
1108 [ectx->ec_funcrefs.ga_len] = pt;
1109 ++pt->pt_refcount;
1110 ++ectx->ec_funcrefs.ga_len;
1111 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001112 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001113 return OK;
1114}
1115
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001116
Bram Moolenaarf112f302020-12-20 17:47:52 +01001117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 * Call a "def" function from old Vim script.
1119 * Return OK or FAIL.
1120 */
1121 int
1122call_def_function(
1123 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001124 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001126 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127 typval_T *rettv) // return value
1128{
1129 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001130 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001131 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132 typval_T *tv;
1133 int idx;
1134 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001135 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001136 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001137 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001138 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001139 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001140 msglist_T **saved_msg_list = NULL;
1141 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001142 cmdmod_T save_cmdmod;
1143 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001144 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001145 int save_emsg_silent_def = emsg_silent_def;
1146 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001147 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001148 int orig_funcdepth;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001149
1150// Get pointer to item in the stack.
1151#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1152
1153// Get pointer to item at the bottom of the stack, -1 is the bottom.
1154#undef STACK_TV_BOT
1155#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1156
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001157// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001158#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 +01001159
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001160 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001161 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1162 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001163 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001164 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001165 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001166 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001167 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001168 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001169 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001170
Bram Moolenaar09689a02020-05-09 22:50:08 +02001171 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001172 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001173 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1174 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001175 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001176 {
1177 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001178 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001179 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001180 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001181
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001182 // If depth of calling is getting too high, don't execute the function.
1183 orig_funcdepth = funcdepth_get();
1184 if (funcdepth_increment() == FAIL)
1185 return FAIL;
1186
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001187 CLEAR_FIELD(ectx);
1188 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1189 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1190 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001191 {
1192 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001193 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001194 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001195 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001196 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001198 // Put arguments on the stack, but no more than what the function expects.
1199 // A lambda can be called with more arguments than it uses.
1200 for (idx = 0; idx < argc
1201 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1202 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001204 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001205 && check_typval_type(ufunc->uf_arg_types[idx], &argv[idx],
1206 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001207 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 copy_tv(&argv[idx], STACK_TV_BOT(0));
1209 ++ectx.ec_stack.ga_len;
1210 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001211
1212 // Turn varargs into a list. Empty list if no args.
1213 if (ufunc->uf_va_name != NULL)
1214 {
1215 int vararg_count = argc - ufunc->uf_args.ga_len;
1216
1217 if (vararg_count < 0)
1218 vararg_count = 0;
1219 else
1220 argc -= vararg_count;
1221 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001222 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001223
1224 // Check the type of the list items.
1225 tv = STACK_TV_BOT(-1);
1226 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001227 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001228 && ufunc->uf_va_type->tt_member != &t_any
1229 && tv->vval.v_list != NULL)
1230 {
1231 type_T *expected = ufunc->uf_va_type->tt_member;
1232 listitem_T *li = tv->vval.v_list->lv_first;
1233
1234 for (idx = 0; idx < vararg_count; ++idx)
1235 {
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001236 if (check_typval_type(expected, &li->li_tv,
1237 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001238 goto failed_early;
1239 li = li->li_next;
1240 }
1241 }
1242
Bram Moolenaar23e03252020-04-12 22:22:31 +02001243 if (defcount > 0)
1244 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001245 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001246 --ectx.ec_stack.ga_len;
1247 }
1248
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001249 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001250 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001251 if (defcount > 0)
1252 for (idx = 0; idx < defcount; ++idx)
1253 {
1254 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1255 ++ectx.ec_stack.ga_len;
1256 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001257 if (ufunc->uf_va_name != NULL)
1258 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001259
1260 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001261 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1262 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001263
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001264 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001265 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1266 + ufunc->uf_dfunc_idx;
1267 ufunc_T *base_ufunc = dfunc->df_ufunc;
1268
1269 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1270 // by copy_func().
1271 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001272 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001273 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1274 if (ectx.ec_outer == NULL)
1275 goto failed_early;
1276 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001277 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001278 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1279 {
1280 if (current_ectx->ec_outer != NULL)
1281 *ectx.ec_outer = *current_ectx->ec_outer;
1282 }
1283 else
1284 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001285 }
1286 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001287 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1288 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001289 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001290 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001291
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 // dummy frame entries
1293 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1294 {
1295 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1296 ++ectx.ec_stack.ga_len;
1297 }
1298
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001299 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001300 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001301 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1302 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001303
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001304 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001305 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001306 ectx.ec_stack.ga_len += dfunc->df_varcount;
1307 if (dfunc->df_has_closure)
1308 {
1309 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1310 STACK_TV_VAR(idx)->vval.v_number = 0;
1311 ++ectx.ec_stack.ga_len;
1312 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001313
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001314 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001315 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001316
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001317 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001318 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001319 estack_push_ufunc(ufunc, 1);
1320 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001321 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1322
Bram Moolenaar352134b2020-10-17 22:04:08 +02001323 // Use a specific location for storing error messages to be converted to an
1324 // exception.
1325 saved_msg_list = msg_list;
1326 msg_list = &private_msg_list;
1327
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001328 // Do turn errors into exceptions.
1329 suppress_errthrow = FALSE;
1330
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001331 // When ":silent!" was used before calling then we still abort the
1332 // function. If ":silent!" is used in the function then we don't.
1333 emsg_silent_def = emsg_silent;
1334 did_emsg_def = 0;
1335
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001336 // Decide where to start execution, handles optional arguments.
1337 init_instr_idx(ufunc, argc, &ectx);
1338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001339 for (;;)
1340 {
1341 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001342
Bram Moolenaar270d0382020-05-15 21:42:53 +02001343 if (++breakcheck_count >= 100)
1344 {
1345 line_breakcheck();
1346 breakcheck_count = 0;
1347 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001348 if (got_int)
1349 {
1350 // Turn CTRL-C into an exception.
1351 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001352 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001353 goto failed;
1354 did_throw = TRUE;
1355 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001356
Bram Moolenaara26b9702020-04-18 19:53:28 +02001357 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1358 {
1359 // Turn an error message into an exception.
1360 did_emsg = FALSE;
1361 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1362 goto failed;
1363 did_throw = TRUE;
1364 *msg_list = NULL;
1365 }
1366
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001367 if (did_throw && !ectx.ec_in_catch)
1368 {
1369 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001370 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371
1372 // An exception jumps to the first catch, finally, or returns from
1373 // the current function.
1374 if (trystack->ga_len > 0)
1375 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001376 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377 {
1378 // jump to ":catch" or ":finally"
1379 ectx.ec_in_catch = TRUE;
1380 ectx.ec_iidx = trycmd->tcd_catch_idx;
1381 }
1382 else
1383 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001384 // Not inside try or need to return from current functions.
1385 // Push a dummy return value.
1386 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1387 goto failed;
1388 tv = STACK_TV_BOT(0);
1389 tv->v_type = VAR_NUMBER;
1390 tv->vval.v_number = 0;
1391 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001392 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001393 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001394 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001395 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001396 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1397 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 goto done;
1399 }
1400
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001401 if (func_return(&ectx) == FAIL)
1402 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 }
1404 continue;
1405 }
1406
1407 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1408 switch (iptr->isn_type)
1409 {
1410 // execute Ex command line
1411 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001412 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001413 source_cookie_T cookie;
1414
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001415 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001416 // Pass getsourceline to get an error for a missing ":end"
1417 // command.
1418 CLEAR_FIELD(cookie);
1419 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1420 if (do_cmdline(iptr->isn_arg.string,
1421 getsourceline, &cookie,
1422 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1423 == FAIL
1424 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001425 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001427 break;
1428
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001429 // execute Ex command from pieces on the stack
1430 case ISN_EXECCONCAT:
1431 {
1432 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001433 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001434 int pass;
1435 int i;
1436 char_u *cmd = NULL;
1437 char_u *str;
1438
1439 for (pass = 1; pass <= 2; ++pass)
1440 {
1441 for (i = 0; i < count; ++i)
1442 {
1443 tv = STACK_TV_BOT(i - count);
1444 str = tv->vval.v_string;
1445 if (str != NULL && *str != NUL)
1446 {
1447 if (pass == 2)
1448 STRCPY(cmd + len, str);
1449 len += STRLEN(str);
1450 }
1451 if (pass == 2)
1452 clear_tv(tv);
1453 }
1454 if (pass == 1)
1455 {
1456 cmd = alloc(len + 1);
1457 if (cmd == NULL)
1458 goto failed;
1459 len = 0;
1460 }
1461 }
1462
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001463 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001464 do_cmdline_cmd(cmd);
1465 vim_free(cmd);
1466 }
1467 break;
1468
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001469 // execute :echo {string} ...
1470 case ISN_ECHO:
1471 {
1472 int count = iptr->isn_arg.echo.echo_count;
1473 int atstart = TRUE;
1474 int needclr = TRUE;
1475
1476 for (idx = 0; idx < count; ++idx)
1477 {
1478 tv = STACK_TV_BOT(idx - count);
1479 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1480 &atstart, &needclr);
1481 clear_tv(tv);
1482 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001483 if (needclr)
1484 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 ectx.ec_stack.ga_len -= count;
1486 }
1487 break;
1488
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001489 // :execute {string} ...
1490 // :echomsg {string} ...
1491 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001492 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001493 case ISN_ECHOMSG:
1494 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001495 {
1496 int count = iptr->isn_arg.number;
1497 garray_T ga;
1498 char_u buf[NUMBUFLEN];
1499 char_u *p;
1500 int len;
1501 int failed = FALSE;
1502
1503 ga_init2(&ga, 1, 80);
1504 for (idx = 0; idx < count; ++idx)
1505 {
1506 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001507 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001508 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001509 if (tv->v_type == VAR_CHANNEL
1510 || tv->v_type == VAR_JOB)
1511 {
1512 SOURCING_LNUM = iptr->isn_lnum;
1513 emsg(_(e_inval_string));
1514 break;
1515 }
1516 else
1517 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001518 }
1519 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001520 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001521
1522 len = (int)STRLEN(p);
1523 if (ga_grow(&ga, len + 2) == FAIL)
1524 failed = TRUE;
1525 else
1526 {
1527 if (ga.ga_len > 0)
1528 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1529 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1530 ga.ga_len += len;
1531 }
1532 clear_tv(tv);
1533 }
1534 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001535 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001536 {
1537 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001538 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001539 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001540
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001541 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001542 {
1543 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001544 {
1545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001546 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001547 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001548 {
1549 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001550 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001551 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001552 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001553 else
1554 {
1555 msg_sb_eol();
1556 if (iptr->isn_type == ISN_ECHOMSG)
1557 {
1558 msg_attr(ga.ga_data, echo_attr);
1559 out_flush();
1560 }
1561 else
1562 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001563 SOURCING_LNUM = iptr->isn_lnum;
1564 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001565 }
1566 }
1567 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001568 ga_clear(&ga);
1569 }
1570 break;
1571
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001572 // load local variable or argument
1573 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001574 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 goto failed;
1576 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1577 ++ectx.ec_stack.ga_len;
1578 break;
1579
1580 // load v: variable
1581 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001582 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001583 goto failed;
1584 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1585 ++ectx.ec_stack.ga_len;
1586 break;
1587
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001588 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001589 case ISN_LOADSCRIPT:
1590 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001591 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001592 svar_T *sv;
1593
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001594 sv = get_script_svar(sref, &ectx);
1595 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001596 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001597 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001598 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001599 goto failed;
1600 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1601 ++ectx.ec_stack.ga_len;
1602 }
1603 break;
1604
1605 // load s: variable in old script
1606 case ISN_LOADS:
1607 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001608 hashtab_T *ht = &SCRIPT_VARS(
1609 iptr->isn_arg.loadstore.ls_sid);
1610 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001611 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001612
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001613 if (di == NULL)
1614 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001615 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001616 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001617 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 }
1619 else
1620 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001621 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 goto failed;
1623 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1624 ++ectx.ec_stack.ga_len;
1625 }
1626 }
1627 break;
1628
Bram Moolenaard3aac292020-04-19 14:32:17 +02001629 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001630 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001631 case ISN_LOADB:
1632 case ISN_LOADW:
1633 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001635 dictitem_T *di = NULL;
1636 hashtab_T *ht = NULL;
1637 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001638
Bram Moolenaard3aac292020-04-19 14:32:17 +02001639 switch (iptr->isn_type)
1640 {
1641 case ISN_LOADG:
1642 ht = get_globvar_ht();
1643 namespace = 'g';
1644 break;
1645 case ISN_LOADB:
1646 ht = &curbuf->b_vars->dv_hashtab;
1647 namespace = 'b';
1648 break;
1649 case ISN_LOADW:
1650 ht = &curwin->w_vars->dv_hashtab;
1651 namespace = 'w';
1652 break;
1653 case ISN_LOADT:
1654 ht = &curtab->tp_vars->dv_hashtab;
1655 namespace = 't';
1656 break;
1657 default: // Cannot reach here
1658 goto failed;
1659 }
1660 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 if (di == NULL)
1663 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001664 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001665 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001666 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001667 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 }
1669 else
1670 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001671 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 goto failed;
1673 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1674 ++ectx.ec_stack.ga_len;
1675 }
1676 }
1677 break;
1678
Bram Moolenaar03290b82020-12-19 16:30:44 +01001679 // load autoload variable
1680 case ISN_LOADAUTO:
1681 {
1682 char_u *name = iptr->isn_arg.string;
1683
1684 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1685 goto failed;
1686 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001687 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001688 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1689 goto on_error;
1690 ++ectx.ec_stack.ga_len;
1691 }
1692 break;
1693
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001694 // load g:/b:/w:/t: namespace
1695 case ISN_LOADGDICT:
1696 case ISN_LOADBDICT:
1697 case ISN_LOADWDICT:
1698 case ISN_LOADTDICT:
1699 {
1700 dict_T *d = NULL;
1701
1702 switch (iptr->isn_type)
1703 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001704 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1705 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1706 case ISN_LOADWDICT: d = curwin->w_vars; break;
1707 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001708 default: // Cannot reach here
1709 goto failed;
1710 }
1711 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1712 goto failed;
1713 tv = STACK_TV_BOT(0);
1714 tv->v_type = VAR_DICT;
1715 tv->v_lock = 0;
1716 tv->vval.v_dict = d;
1717 ++ectx.ec_stack.ga_len;
1718 }
1719 break;
1720
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001721 // load &option
1722 case ISN_LOADOPT:
1723 {
1724 typval_T optval;
1725 char_u *name = iptr->isn_arg.string;
1726
Bram Moolenaara8c17702020-04-01 21:17:24 +02001727 // This is not expected to fail, name is checked during
1728 // compilation: don't set SOURCING_LNUM.
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;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001731 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001732 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001733 *STACK_TV_BOT(0) = optval;
1734 ++ectx.ec_stack.ga_len;
1735 }
1736 break;
1737
1738 // load $ENV
1739 case ISN_LOADENV:
1740 {
1741 typval_T optval;
1742 char_u *name = iptr->isn_arg.string;
1743
Bram Moolenaar270d0382020-05-15 21:42:53 +02001744 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001746 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001747 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001748 *STACK_TV_BOT(0) = optval;
1749 ++ectx.ec_stack.ga_len;
1750 }
1751 break;
1752
1753 // load @register
1754 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001755 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001756 goto failed;
1757 tv = STACK_TV_BOT(0);
1758 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001759 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001760 // This may result in NULL, which should be equivalent to an
1761 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 tv->vval.v_string = get_reg_contents(
1763 iptr->isn_arg.number, GREG_EXPR_SRC);
1764 ++ectx.ec_stack.ga_len;
1765 break;
1766
1767 // store local variable
1768 case ISN_STORE:
1769 --ectx.ec_stack.ga_len;
1770 tv = STACK_TV_VAR(iptr->isn_arg.number);
1771 clear_tv(tv);
1772 *tv = *STACK_TV_BOT(0);
1773 break;
1774
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001775 // store s: variable in old script
1776 case ISN_STORES:
1777 {
1778 hashtab_T *ht = &SCRIPT_VARS(
1779 iptr->isn_arg.loadstore.ls_sid);
1780 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001781 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001782
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001783 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001784 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001785 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001786 else
1787 {
1788 clear_tv(&di->di_tv);
1789 di->di_tv = *STACK_TV_BOT(0);
1790 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001791 }
1792 break;
1793
1794 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001795 case ISN_STORESCRIPT:
1796 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001797 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1798 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001800 sv = get_script_svar(sref, &ectx);
1801 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001802 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803 --ectx.ec_stack.ga_len;
1804 clear_tv(sv->sv_tv);
1805 *sv->sv_tv = *STACK_TV_BOT(0);
1806 }
1807 break;
1808
1809 // store option
1810 case ISN_STOREOPT:
1811 {
1812 long n = 0;
1813 char_u *s = NULL;
1814 char *msg;
1815
1816 --ectx.ec_stack.ga_len;
1817 tv = STACK_TV_BOT(0);
1818 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001819 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001820 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001821 if (s == NULL)
1822 s = (char_u *)"";
1823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001825 // must be VAR_NUMBER, CHECKTYPE makes sure
1826 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001827 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1828 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001829 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001830 if (msg != NULL)
1831 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001832 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001833 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001834 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 }
1837 break;
1838
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001839 // store $ENV
1840 case ISN_STOREENV:
1841 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001842 tv = STACK_TV_BOT(0);
1843 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1844 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001845 break;
1846
1847 // store @r
1848 case ISN_STOREREG:
1849 {
1850 int reg = iptr->isn_arg.number;
1851
1852 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001853 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001854 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001855 tv_get_string(tv), -1, FALSE);
1856 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001857 }
1858 break;
1859
1860 // store v: variable
1861 case ISN_STOREV:
1862 --ectx.ec_stack.ga_len;
1863 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1864 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001865 // should not happen, type is checked when compiling
1866 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001867 break;
1868
Bram Moolenaard3aac292020-04-19 14:32:17 +02001869 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001870 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001871 case ISN_STOREB:
1872 case ISN_STOREW:
1873 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001875 dictitem_T *di;
1876 hashtab_T *ht;
1877 char_u *name = iptr->isn_arg.string + 2;
1878
Bram Moolenaard3aac292020-04-19 14:32:17 +02001879 switch (iptr->isn_type)
1880 {
1881 case ISN_STOREG:
1882 ht = get_globvar_ht();
1883 break;
1884 case ISN_STOREB:
1885 ht = &curbuf->b_vars->dv_hashtab;
1886 break;
1887 case ISN_STOREW:
1888 ht = &curwin->w_vars->dv_hashtab;
1889 break;
1890 case ISN_STORET:
1891 ht = &curtab->tp_vars->dv_hashtab;
1892 break;
1893 default: // Cannot reach here
1894 goto failed;
1895 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001896
1897 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001898 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001899 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001900 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901 else
1902 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001903 SOURCING_LNUM = iptr->isn_lnum;
1904 if (var_check_permission(di, name) == FAIL)
1905 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 clear_tv(&di->di_tv);
1907 di->di_tv = *STACK_TV_BOT(0);
1908 }
1909 }
1910 break;
1911
Bram Moolenaar03290b82020-12-19 16:30:44 +01001912 // store an autoload variable
1913 case ISN_STOREAUTO:
1914 SOURCING_LNUM = iptr->isn_lnum;
1915 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1916 clear_tv(STACK_TV_BOT(-1));
1917 --ectx.ec_stack.ga_len;
1918 break;
1919
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001920 // store number in local variable
1921 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001922 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001923 clear_tv(tv);
1924 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001925 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 break;
1927
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001928 // store value in list or dict variable
1929 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001930 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001931 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001932 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001933 typval_T *tv_dest = STACK_TV_BOT(-1);
1934 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001935
Bram Moolenaar752fc692021-01-04 21:57:11 +01001936 // Stack contains:
1937 // -3 value to be stored
1938 // -2 index
1939 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001940 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001941 SOURCING_LNUM = iptr->isn_lnum;
1942 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001943 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001944 dest_type = tv_dest->v_type;
1945 if (dest_type == VAR_DICT)
1946 status = do_2string(tv_idx, TRUE);
1947 else if (dest_type == VAR_LIST
1948 && tv_idx->v_type != VAR_NUMBER)
1949 {
1950 emsg(_(e_number_exp));
1951 status = FAIL;
1952 }
1953 }
1954 else if (dest_type != tv_dest->v_type)
1955 {
1956 // just in case, should be OK
1957 semsg(_(e_expected_str_but_got_str),
1958 vartype_name(dest_type),
1959 vartype_name(tv_dest->v_type));
1960 status = FAIL;
1961 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001962
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001963 if (status == OK && dest_type == VAR_LIST)
1964 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001965 long lidx = (long)tv_idx->vval.v_number;
1966 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001967
1968 if (list == NULL)
1969 {
1970 emsg(_(e_list_not_set));
1971 goto on_error;
1972 }
1973 if (lidx < 0 && list->lv_len + lidx >= 0)
1974 // negative index is relative to the end
1975 lidx = list->lv_len + lidx;
1976 if (lidx < 0 || lidx > list->lv_len)
1977 {
1978 semsg(_(e_listidx), lidx);
1979 goto on_error;
1980 }
1981 if (lidx < list->lv_len)
1982 {
1983 listitem_T *li = list_find(list, lidx);
1984
1985 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001986 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001987 goto on_error;
1988 // overwrite existing list item
1989 clear_tv(&li->li_tv);
1990 li->li_tv = *tv;
1991 }
1992 else
1993 {
1994 if (error_if_locked(list->lv_lock,
1995 e_cannot_change_list))
1996 goto on_error;
1997 // append to list, only fails when out of memory
1998 if (list_append_tv(list, tv) == FAIL)
1999 goto failed;
2000 clear_tv(tv);
2001 }
2002 }
2003 else if (status == OK && dest_type == VAR_DICT)
2004 {
2005 char_u *key = tv_idx->vval.v_string;
2006 dict_T *dict = tv_dest->vval.v_dict;
2007 dictitem_T *di;
2008
2009 SOURCING_LNUM = iptr->isn_lnum;
2010 if (dict == NULL)
2011 {
2012 emsg(_(e_dictionary_not_set));
2013 goto on_error;
2014 }
2015 if (key == NULL)
2016 key = (char_u *)"";
2017 di = dict_find(dict, key, -1);
2018 if (di != NULL)
2019 {
2020 if (error_if_locked(di->di_tv.v_lock,
2021 e_cannot_change_dict_item))
2022 goto on_error;
2023 // overwrite existing value
2024 clear_tv(&di->di_tv);
2025 di->di_tv = *tv;
2026 }
2027 else
2028 {
2029 if (error_if_locked(dict->dv_lock,
2030 e_cannot_change_dict))
2031 goto on_error;
2032 // add to dict, only fails when out of memory
2033 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2034 goto failed;
2035 clear_tv(tv);
2036 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002037 }
2038 else
2039 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002040 status = FAIL;
2041 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002042 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002043
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002044 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002045 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002046 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002047 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002048 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002049 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002050 goto on_error;
2051 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002052 }
2053 break;
2054
Bram Moolenaar0186e582021-01-10 18:33:11 +01002055 // load or store variable or argument from outer scope
2056 case ISN_LOADOUTER:
2057 case ISN_STOREOUTER:
2058 {
2059 int depth = iptr->isn_arg.outer.outer_depth;
2060 outer_T *outer = ectx.ec_outer;
2061
2062 while (depth > 1 && outer != NULL)
2063 {
2064 outer = outer->out_up;
2065 --depth;
2066 }
2067 if (outer == NULL)
2068 {
2069 SOURCING_LNUM = iptr->isn_lnum;
2070 iemsg("LOADOUTER depth more than scope levels");
2071 goto failed;
2072 }
2073 tv = ((typval_T *)outer->out_stack->ga_data)
2074 + outer->out_frame_idx + STACK_FRAME_SIZE
2075 + iptr->isn_arg.outer.outer_idx;
2076 if (iptr->isn_type == ISN_LOADOUTER)
2077 {
2078 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2079 goto failed;
2080 copy_tv(tv, STACK_TV_BOT(0));
2081 ++ectx.ec_stack.ga_len;
2082 }
2083 else
2084 {
2085 --ectx.ec_stack.ga_len;
2086 clear_tv(tv);
2087 *tv = *STACK_TV_BOT(0);
2088 }
2089 }
2090 break;
2091
Bram Moolenaar752fc692021-01-04 21:57:11 +01002092 // unlet item in list or dict variable
2093 case ISN_UNLETINDEX:
2094 {
2095 typval_T *tv_idx = STACK_TV_BOT(-2);
2096 typval_T *tv_dest = STACK_TV_BOT(-1);
2097 int status = OK;
2098
2099 // Stack contains:
2100 // -2 index
2101 // -1 dict or list
2102 if (tv_dest->v_type == VAR_DICT)
2103 {
2104 // unlet a dict item, index must be a string
2105 if (tv_idx->v_type != VAR_STRING)
2106 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002107 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002108 semsg(_(e_expected_str_but_got_str),
2109 vartype_name(VAR_STRING),
2110 vartype_name(tv_idx->v_type));
2111 status = FAIL;
2112 }
2113 else
2114 {
2115 dict_T *d = tv_dest->vval.v_dict;
2116 char_u *key = tv_idx->vval.v_string;
2117 dictitem_T *di = NULL;
2118
2119 if (key == NULL)
2120 key = (char_u *)"";
2121 if (d != NULL)
2122 di = dict_find(d, key, (int)STRLEN(key));
2123 if (di == NULL)
2124 {
2125 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002126 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002127 semsg(_(e_dictkey), key);
2128 status = FAIL;
2129 }
2130 else
2131 {
2132 // TODO: check for dict or item locked
2133 dictitem_remove(d, di);
2134 }
2135 }
2136 }
2137 else if (tv_dest->v_type == VAR_LIST)
2138 {
2139 // unlet a List item, index must be a number
2140 if (tv_idx->v_type != VAR_NUMBER)
2141 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002142 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002143 semsg(_(e_expected_str_but_got_str),
2144 vartype_name(VAR_NUMBER),
2145 vartype_name(tv_idx->v_type));
2146 status = FAIL;
2147 }
2148 else
2149 {
2150 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002151 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002152 listitem_T *li = NULL;
2153
2154 li = list_find(l, n);
2155 if (li == NULL)
2156 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002157 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002158 semsg(_(e_listidx), n);
2159 status = FAIL;
2160 }
2161 else
2162 // TODO: check for list or item locked
2163 listitem_remove(l, li);
2164 }
2165 }
2166 else
2167 {
2168 status = FAIL;
2169 semsg(_(e_cannot_index_str),
2170 vartype_name(tv_dest->v_type));
2171 }
2172
2173 clear_tv(tv_idx);
2174 clear_tv(tv_dest);
2175 ectx.ec_stack.ga_len -= 2;
2176 if (status == FAIL)
2177 goto on_error;
2178 }
2179 break;
2180
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002181 // push constant
2182 case ISN_PUSHNR:
2183 case ISN_PUSHBOOL:
2184 case ISN_PUSHSPEC:
2185 case ISN_PUSHF:
2186 case ISN_PUSHS:
2187 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002188 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002189 case ISN_PUSHCHANNEL:
2190 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002191 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002192 goto failed;
2193 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002194 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002195 ++ectx.ec_stack.ga_len;
2196 switch (iptr->isn_type)
2197 {
2198 case ISN_PUSHNR:
2199 tv->v_type = VAR_NUMBER;
2200 tv->vval.v_number = iptr->isn_arg.number;
2201 break;
2202 case ISN_PUSHBOOL:
2203 tv->v_type = VAR_BOOL;
2204 tv->vval.v_number = iptr->isn_arg.number;
2205 break;
2206 case ISN_PUSHSPEC:
2207 tv->v_type = VAR_SPECIAL;
2208 tv->vval.v_number = iptr->isn_arg.number;
2209 break;
2210#ifdef FEAT_FLOAT
2211 case ISN_PUSHF:
2212 tv->v_type = VAR_FLOAT;
2213 tv->vval.v_float = iptr->isn_arg.fnumber;
2214 break;
2215#endif
2216 case ISN_PUSHBLOB:
2217 blob_copy(iptr->isn_arg.blob, tv);
2218 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002219 case ISN_PUSHFUNC:
2220 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002221 if (iptr->isn_arg.string == NULL)
2222 tv->vval.v_string = NULL;
2223 else
2224 tv->vval.v_string =
2225 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002226 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002227 case ISN_PUSHCHANNEL:
2228#ifdef FEAT_JOB_CHANNEL
2229 tv->v_type = VAR_CHANNEL;
2230 tv->vval.v_channel = iptr->isn_arg.channel;
2231 if (tv->vval.v_channel != NULL)
2232 ++tv->vval.v_channel->ch_refcount;
2233#endif
2234 break;
2235 case ISN_PUSHJOB:
2236#ifdef FEAT_JOB_CHANNEL
2237 tv->v_type = VAR_JOB;
2238 tv->vval.v_job = iptr->isn_arg.job;
2239 if (tv->vval.v_job != NULL)
2240 ++tv->vval.v_job->jv_refcount;
2241#endif
2242 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002243 default:
2244 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002245 tv->vval.v_string = vim_strsave(
2246 iptr->isn_arg.string == NULL
2247 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 }
2249 break;
2250
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002251 case ISN_UNLET:
2252 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2253 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002254 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002255 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002256 case ISN_UNLETENV:
2257 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2258 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002259
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002260 case ISN_LOCKCONST:
2261 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2262 break;
2263
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002264 // create a list from items on the stack; uses a single allocation
2265 // for the list header and the items
2266 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002267 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2268 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 break;
2270
2271 // create a dict from items on the stack
2272 case ISN_NEWDICT:
2273 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002274 int count = iptr->isn_arg.number;
2275 dict_T *dict = dict_alloc();
2276 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002277 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002278
2279 if (dict == NULL)
2280 goto failed;
2281 for (idx = 0; idx < count; ++idx)
2282 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002283 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002284 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002285 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002286 key = tv->vval.v_string == NULL
2287 ? (char_u *)"" : tv->vval.v_string;
2288 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002289 if (item != NULL)
2290 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002291 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002292 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002293 dict_unref(dict);
2294 goto on_error;
2295 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002296 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002297 clear_tv(tv);
2298 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002299 {
2300 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002302 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002303 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2304 item->di_tv.v_lock = 0;
2305 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002306 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002307 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002308 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002309 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002310 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002311 }
2312
2313 if (count > 0)
2314 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002315 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 goto failed;
2317 else
2318 ++ectx.ec_stack.ga_len;
2319 tv = STACK_TV_BOT(-1);
2320 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002321 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 tv->vval.v_dict = dict;
2323 ++dict->dv_refcount;
2324 }
2325 break;
2326
2327 // call a :def function
2328 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002329 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002330 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002331 iptr->isn_arg.dfunc.cdf_argcount,
2332 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002333 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002334 break;
2335
2336 // call a builtin function
2337 case ISN_BCALL:
2338 SOURCING_LNUM = iptr->isn_lnum;
2339 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2340 iptr->isn_arg.bfunc.cbf_argcount,
2341 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002342 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002343 break;
2344
2345 // call a funcref or partial
2346 case ISN_PCALL:
2347 {
2348 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2349 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002350 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002351
2352 SOURCING_LNUM = iptr->isn_lnum;
2353 if (pfunc->cpf_top)
2354 {
2355 // funcref is above the arguments
2356 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2357 }
2358 else
2359 {
2360 // Get the funcref from the stack.
2361 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002362 partial_tv = *STACK_TV_BOT(0);
2363 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002364 }
2365 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002366 if (tv == &partial_tv)
2367 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002369 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 }
2371 break;
2372
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002373 case ISN_PCALL_END:
2374 // PCALL finished, arguments have been consumed and replaced by
2375 // the return value. Now clear the funcref from the stack,
2376 // and move the return value in its place.
2377 --ectx.ec_stack.ga_len;
2378 clear_tv(STACK_TV_BOT(-1));
2379 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2380 break;
2381
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002382 // call a user defined function or funcref/partial
2383 case ISN_UCALL:
2384 {
2385 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2386
2387 SOURCING_LNUM = iptr->isn_lnum;
2388 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002389 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002390 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 }
2392 break;
2393
2394 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002395 case ISN_RETURN_ZERO:
2396 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2397 goto failed;
2398 tv = STACK_TV_BOT(0);
2399 ++ectx.ec_stack.ga_len;
2400 tv->v_type = VAR_NUMBER;
2401 tv->vval.v_number = 0;
2402 tv->v_lock = 0;
2403 // FALLTHROUGH
2404
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405 case ISN_RETURN:
2406 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002407 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002408 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002409
2410 if (trystack->ga_len > 0)
2411 trycmd = ((trycmd_T *)trystack->ga_data)
2412 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002413 if (trycmd != NULL
2414 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 && trycmd->tcd_finally_idx != 0)
2416 {
2417 // jump to ":finally"
2418 ectx.ec_iidx = trycmd->tcd_finally_idx;
2419 trycmd->tcd_return = TRUE;
2420 }
2421 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002422 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002423 }
2424 break;
2425
2426 // push a function reference to a compiled function
2427 case ISN_FUNCREF:
2428 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002429 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2430 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2431 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002432
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 if (pt == NULL)
2434 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002435 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002436 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002437 vim_free(pt);
2438 goto failed;
2439 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002440 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2441 &ectx) == FAIL)
2442 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002444 tv = STACK_TV_BOT(0);
2445 ++ectx.ec_stack.ga_len;
2446 tv->vval.v_partial = pt;
2447 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002448 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 }
2450 break;
2451
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002452 // Create a global function from a lambda.
2453 case ISN_NEWFUNC:
2454 {
2455 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2456
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002457 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002458 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002459 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002460 }
2461 break;
2462
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002463 // List functions
2464 case ISN_DEF:
2465 if (iptr->isn_arg.string == NULL)
2466 list_functions(NULL);
2467 else
2468 {
2469 exarg_T ea;
2470
2471 CLEAR_FIELD(ea);
2472 ea.cmd = ea.arg = iptr->isn_arg.string;
2473 define_function(&ea, NULL);
2474 }
2475 break;
2476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002477 // jump if a condition is met
2478 case ISN_JUMP:
2479 {
2480 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002481 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 int jump = TRUE;
2483
2484 if (when != JUMP_ALWAYS)
2485 {
2486 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002487 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002488 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002489 || when == JUMP_IF_COND_TRUE)
2490 {
2491 SOURCING_LNUM = iptr->isn_lnum;
2492 jump = tv_get_bool_chk(tv, &error);
2493 if (error)
2494 goto on_error;
2495 }
2496 else
2497 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002498 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002499 || when == JUMP_AND_KEEP_IF_FALSE
2500 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002501 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002502 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 {
2504 // drop the value from the stack
2505 clear_tv(tv);
2506 --ectx.ec_stack.ga_len;
2507 }
2508 }
2509 if (jump)
2510 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2511 }
2512 break;
2513
2514 // top of a for loop
2515 case ISN_FOR:
2516 {
2517 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2518 typval_T *idxtv =
2519 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2520
2521 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002522 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002524 ++idxtv->vval.v_number;
2525 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002526 // past the end of the list, jump to "endfor"
2527 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2528 else if (list->lv_first == &range_list_item)
2529 {
2530 // non-materialized range() list
2531 tv = STACK_TV_BOT(0);
2532 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002533 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002534 tv->vval.v_number = list_find_nr(
2535 list, idxtv->vval.v_number, NULL);
2536 ++ectx.ec_stack.ga_len;
2537 }
2538 else
2539 {
2540 listitem_T *li = list_find(list, idxtv->vval.v_number);
2541
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2543 ++ectx.ec_stack.ga_len;
2544 }
2545 }
2546 break;
2547
2548 // start of ":try" block
2549 case ISN_TRY:
2550 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002551 trycmd_T *trycmd = NULL;
2552
Bram Moolenaar270d0382020-05-15 21:42:53 +02002553 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002554 goto failed;
2555 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2556 + ectx.ec_trystack.ga_len;
2557 ++ectx.ec_trystack.ga_len;
2558 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002559 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002560 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2561 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002562 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002563 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 }
2565 break;
2566
2567 case ISN_PUSHEXC:
2568 if (current_exception == NULL)
2569 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002570 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002571 iemsg("Evaluating catch while current_exception is NULL");
2572 goto failed;
2573 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002574 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 goto failed;
2576 tv = STACK_TV_BOT(0);
2577 ++ectx.ec_stack.ga_len;
2578 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002579 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002580 tv->vval.v_string = vim_strsave(
2581 (char_u *)current_exception->value);
2582 break;
2583
2584 case ISN_CATCH:
2585 {
2586 garray_T *trystack = &ectx.ec_trystack;
2587
Bram Moolenaar20a76292020-12-25 19:47:24 +01002588 if (restore_cmdmod)
2589 {
2590 cmdmod.cmod_filter_regmatch.regprog = NULL;
2591 undo_cmdmod(&cmdmod);
2592 cmdmod = save_cmdmod;
2593 restore_cmdmod = FALSE;
2594 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002595 if (trystack->ga_len > 0)
2596 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002597 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002598 + trystack->ga_len - 1;
2599 trycmd->tcd_caught = TRUE;
2600 }
2601 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002602 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002603 catch_exception(current_exception);
2604 }
2605 break;
2606
2607 // end of ":try" block
2608 case ISN_ENDTRY:
2609 {
2610 garray_T *trystack = &ectx.ec_trystack;
2611
2612 if (trystack->ga_len > 0)
2613 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002614 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002615
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 --trystack->ga_len;
2617 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002618 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 trycmd = ((trycmd_T *)trystack->ga_data)
2620 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002621 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 {
2623 // discard the exception
2624 if (caught_stack == current_exception)
2625 caught_stack = caught_stack->caught;
2626 discard_current_exception();
2627 }
2628
2629 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002630 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002631 }
2632 }
2633 break;
2634
2635 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002636 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002637 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002638
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002639 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2640 {
2641 // throwing an exception while using "silent!" causes
2642 // the function to abort but not display an error.
2643 tv = STACK_TV_BOT(-1);
2644 clear_tv(tv);
2645 tv->v_type = VAR_NUMBER;
2646 tv->vval.v_number = 0;
2647 goto done;
2648 }
2649 --ectx.ec_stack.ga_len;
2650 tv = STACK_TV_BOT(0);
2651 if (tv->vval.v_string == NULL
2652 || *skipwhite(tv->vval.v_string) == NUL)
2653 {
2654 vim_free(tv->vval.v_string);
2655 SOURCING_LNUM = iptr->isn_lnum;
2656 emsg(_(e_throw_with_empty_string));
2657 goto failed;
2658 }
2659
2660 // Inside a "catch" we need to first discard the caught
2661 // exception.
2662 if (trystack->ga_len > 0)
2663 {
2664 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2665 + trystack->ga_len - 1;
2666 if (trycmd->tcd_caught && current_exception != NULL)
2667 {
2668 // discard the exception
2669 if (caught_stack == current_exception)
2670 caught_stack = caught_stack->caught;
2671 discard_current_exception();
2672 trycmd->tcd_caught = FALSE;
2673 }
2674 }
2675
2676 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2677 == FAIL)
2678 {
2679 vim_free(tv->vval.v_string);
2680 goto failed;
2681 }
2682 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 break;
2685
2686 // compare with special values
2687 case ISN_COMPAREBOOL:
2688 case ISN_COMPARESPECIAL:
2689 {
2690 typval_T *tv1 = STACK_TV_BOT(-2);
2691 typval_T *tv2 = STACK_TV_BOT(-1);
2692 varnumber_T arg1 = tv1->vval.v_number;
2693 varnumber_T arg2 = tv2->vval.v_number;
2694 int res;
2695
2696 switch (iptr->isn_arg.op.op_type)
2697 {
2698 case EXPR_EQUAL: res = arg1 == arg2; break;
2699 case EXPR_NEQUAL: res = arg1 != arg2; break;
2700 default: res = 0; break;
2701 }
2702
2703 --ectx.ec_stack.ga_len;
2704 tv1->v_type = VAR_BOOL;
2705 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2706 }
2707 break;
2708
2709 // Operation with two number arguments
2710 case ISN_OPNR:
2711 case ISN_COMPARENR:
2712 {
2713 typval_T *tv1 = STACK_TV_BOT(-2);
2714 typval_T *tv2 = STACK_TV_BOT(-1);
2715 varnumber_T arg1 = tv1->vval.v_number;
2716 varnumber_T arg2 = tv2->vval.v_number;
2717 varnumber_T res;
2718
2719 switch (iptr->isn_arg.op.op_type)
2720 {
2721 case EXPR_MULT: res = arg1 * arg2; break;
2722 case EXPR_DIV: res = arg1 / arg2; break;
2723 case EXPR_REM: res = arg1 % arg2; break;
2724 case EXPR_SUB: res = arg1 - arg2; break;
2725 case EXPR_ADD: res = arg1 + arg2; break;
2726
2727 case EXPR_EQUAL: res = arg1 == arg2; break;
2728 case EXPR_NEQUAL: res = arg1 != arg2; break;
2729 case EXPR_GREATER: res = arg1 > arg2; break;
2730 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2731 case EXPR_SMALLER: res = arg1 < arg2; break;
2732 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2733 default: res = 0; break;
2734 }
2735
2736 --ectx.ec_stack.ga_len;
2737 if (iptr->isn_type == ISN_COMPARENR)
2738 {
2739 tv1->v_type = VAR_BOOL;
2740 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2741 }
2742 else
2743 tv1->vval.v_number = res;
2744 }
2745 break;
2746
2747 // Computation with two float arguments
2748 case ISN_OPFLOAT:
2749 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002750#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002751 {
2752 typval_T *tv1 = STACK_TV_BOT(-2);
2753 typval_T *tv2 = STACK_TV_BOT(-1);
2754 float_T arg1 = tv1->vval.v_float;
2755 float_T arg2 = tv2->vval.v_float;
2756 float_T res = 0;
2757 int cmp = FALSE;
2758
2759 switch (iptr->isn_arg.op.op_type)
2760 {
2761 case EXPR_MULT: res = arg1 * arg2; break;
2762 case EXPR_DIV: res = arg1 / arg2; break;
2763 case EXPR_SUB: res = arg1 - arg2; break;
2764 case EXPR_ADD: res = arg1 + arg2; break;
2765
2766 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2767 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2768 case EXPR_GREATER: cmp = arg1 > arg2; break;
2769 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2770 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2771 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2772 default: cmp = 0; break;
2773 }
2774 --ectx.ec_stack.ga_len;
2775 if (iptr->isn_type == ISN_COMPAREFLOAT)
2776 {
2777 tv1->v_type = VAR_BOOL;
2778 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2779 }
2780 else
2781 tv1->vval.v_float = res;
2782 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002783#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002784 break;
2785
2786 case ISN_COMPARELIST:
2787 {
2788 typval_T *tv1 = STACK_TV_BOT(-2);
2789 typval_T *tv2 = STACK_TV_BOT(-1);
2790 list_T *arg1 = tv1->vval.v_list;
2791 list_T *arg2 = tv2->vval.v_list;
2792 int cmp = FALSE;
2793 int ic = iptr->isn_arg.op.op_ic;
2794
2795 switch (iptr->isn_arg.op.op_type)
2796 {
2797 case EXPR_EQUAL: cmp =
2798 list_equal(arg1, arg2, ic, FALSE); break;
2799 case EXPR_NEQUAL: cmp =
2800 !list_equal(arg1, arg2, ic, FALSE); break;
2801 case EXPR_IS: cmp = arg1 == arg2; break;
2802 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2803 default: cmp = 0; break;
2804 }
2805 --ectx.ec_stack.ga_len;
2806 clear_tv(tv1);
2807 clear_tv(tv2);
2808 tv1->v_type = VAR_BOOL;
2809 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2810 }
2811 break;
2812
2813 case ISN_COMPAREBLOB:
2814 {
2815 typval_T *tv1 = STACK_TV_BOT(-2);
2816 typval_T *tv2 = STACK_TV_BOT(-1);
2817 blob_T *arg1 = tv1->vval.v_blob;
2818 blob_T *arg2 = tv2->vval.v_blob;
2819 int cmp = FALSE;
2820
2821 switch (iptr->isn_arg.op.op_type)
2822 {
2823 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2824 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2825 case EXPR_IS: cmp = arg1 == arg2; break;
2826 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2827 default: cmp = 0; break;
2828 }
2829 --ectx.ec_stack.ga_len;
2830 clear_tv(tv1);
2831 clear_tv(tv2);
2832 tv1->v_type = VAR_BOOL;
2833 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2834 }
2835 break;
2836
2837 // TODO: handle separately
2838 case ISN_COMPARESTRING:
2839 case ISN_COMPAREDICT:
2840 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 case ISN_COMPAREANY:
2842 {
2843 typval_T *tv1 = STACK_TV_BOT(-2);
2844 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002845 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002846 int ic = iptr->isn_arg.op.op_ic;
2847
Bram Moolenaareb26f432020-09-14 16:50:05 +02002848 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002849 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002851 --ectx.ec_stack.ga_len;
2852 }
2853 break;
2854
2855 case ISN_ADDLIST:
2856 case ISN_ADDBLOB:
2857 {
2858 typval_T *tv1 = STACK_TV_BOT(-2);
2859 typval_T *tv2 = STACK_TV_BOT(-1);
2860
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002861 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 if (iptr->isn_type == ISN_ADDLIST)
2863 eval_addlist(tv1, tv2);
2864 else
2865 eval_addblob(tv1, tv2);
2866 clear_tv(tv2);
2867 --ectx.ec_stack.ga_len;
2868 }
2869 break;
2870
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002871 case ISN_LISTAPPEND:
2872 {
2873 typval_T *tv1 = STACK_TV_BOT(-2);
2874 typval_T *tv2 = STACK_TV_BOT(-1);
2875 list_T *l = tv1->vval.v_list;
2876
2877 // add an item to a list
2878 if (l == NULL)
2879 {
2880 SOURCING_LNUM = iptr->isn_lnum;
2881 emsg(_(e_cannot_add_to_null_list));
2882 goto on_error;
2883 }
2884 if (list_append_tv(l, tv2) == FAIL)
2885 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002886 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002887 --ectx.ec_stack.ga_len;
2888 }
2889 break;
2890
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002891 case ISN_BLOBAPPEND:
2892 {
2893 typval_T *tv1 = STACK_TV_BOT(-2);
2894 typval_T *tv2 = STACK_TV_BOT(-1);
2895 blob_T *b = tv1->vval.v_blob;
2896 int error = FALSE;
2897 varnumber_T n;
2898
2899 // add a number to a blob
2900 if (b == NULL)
2901 {
2902 SOURCING_LNUM = iptr->isn_lnum;
2903 emsg(_(e_cannot_add_to_null_blob));
2904 goto on_error;
2905 }
2906 n = tv_get_number_chk(tv2, &error);
2907 if (error)
2908 goto on_error;
2909 ga_append(&b->bv_ga, (int)n);
2910 --ectx.ec_stack.ga_len;
2911 }
2912 break;
2913
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002914 // Computation with two arguments of unknown type
2915 case ISN_OPANY:
2916 {
2917 typval_T *tv1 = STACK_TV_BOT(-2);
2918 typval_T *tv2 = STACK_TV_BOT(-1);
2919 varnumber_T n1, n2;
2920#ifdef FEAT_FLOAT
2921 float_T f1 = 0, f2 = 0;
2922#endif
2923 int error = FALSE;
2924
2925 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2926 {
2927 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2928 {
2929 eval_addlist(tv1, tv2);
2930 clear_tv(tv2);
2931 --ectx.ec_stack.ga_len;
2932 break;
2933 }
2934 else if (tv1->v_type == VAR_BLOB
2935 && tv2->v_type == VAR_BLOB)
2936 {
2937 eval_addblob(tv1, tv2);
2938 clear_tv(tv2);
2939 --ectx.ec_stack.ga_len;
2940 break;
2941 }
2942 }
2943#ifdef FEAT_FLOAT
2944 if (tv1->v_type == VAR_FLOAT)
2945 {
2946 f1 = tv1->vval.v_float;
2947 n1 = 0;
2948 }
2949 else
2950#endif
2951 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002952 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002953 n1 = tv_get_number_chk(tv1, &error);
2954 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002955 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002956#ifdef FEAT_FLOAT
2957 if (tv2->v_type == VAR_FLOAT)
2958 f1 = n1;
2959#endif
2960 }
2961#ifdef FEAT_FLOAT
2962 if (tv2->v_type == VAR_FLOAT)
2963 {
2964 f2 = tv2->vval.v_float;
2965 n2 = 0;
2966 }
2967 else
2968#endif
2969 {
2970 n2 = tv_get_number_chk(tv2, &error);
2971 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002972 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002973#ifdef FEAT_FLOAT
2974 if (tv1->v_type == VAR_FLOAT)
2975 f2 = n2;
2976#endif
2977 }
2978#ifdef FEAT_FLOAT
2979 // if there is a float on either side the result is a float
2980 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2981 {
2982 switch (iptr->isn_arg.op.op_type)
2983 {
2984 case EXPR_MULT: f1 = f1 * f2; break;
2985 case EXPR_DIV: f1 = f1 / f2; break;
2986 case EXPR_SUB: f1 = f1 - f2; break;
2987 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002988 default: SOURCING_LNUM = iptr->isn_lnum;
2989 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002990 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002991 }
2992 clear_tv(tv1);
2993 clear_tv(tv2);
2994 tv1->v_type = VAR_FLOAT;
2995 tv1->vval.v_float = f1;
2996 --ectx.ec_stack.ga_len;
2997 }
2998 else
2999#endif
3000 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003001 int failed = FALSE;
3002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 switch (iptr->isn_arg.op.op_type)
3004 {
3005 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003006 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3007 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003008 goto on_error;
3009 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003010 case EXPR_SUB: n1 = n1 - n2; break;
3011 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003012 default: n1 = num_modulus(n1, n2, &failed);
3013 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003014 goto on_error;
3015 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003016 }
3017 clear_tv(tv1);
3018 clear_tv(tv2);
3019 tv1->v_type = VAR_NUMBER;
3020 tv1->vval.v_number = n1;
3021 --ectx.ec_stack.ga_len;
3022 }
3023 }
3024 break;
3025
3026 case ISN_CONCAT:
3027 {
3028 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3029 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3030 char_u *res;
3031
3032 res = concat_str(str1, str2);
3033 clear_tv(STACK_TV_BOT(-2));
3034 clear_tv(STACK_TV_BOT(-1));
3035 --ectx.ec_stack.ga_len;
3036 STACK_TV_BOT(-1)->vval.v_string = res;
3037 }
3038 break;
3039
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003040 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003041 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003042 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003043 int is_slice = iptr->isn_type == ISN_STRSLICE;
3044 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003045 char_u *res;
3046
3047 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003048 // string slice: string is at stack-3, first index at
3049 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003050 if (is_slice)
3051 {
3052 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003053 n1 = tv->vval.v_number;
3054 }
3055
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003056 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003057 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003058
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003059 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003060 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003061 if (is_slice)
3062 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003063 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003064 else
3065 // Index: The resulting variable is a string of a
3066 // single character. If the index is too big or
3067 // negative the result is empty.
3068 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003069 vim_free(tv->vval.v_string);
3070 tv->vval.v_string = res;
3071 }
3072 break;
3073
3074 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003075 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003076 {
Bram Moolenaared591872020-08-15 22:14:53 +02003077 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003078 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003079 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080
3081 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003082 // list slice: list is at stack-3, indexes at stack-2 and
3083 // stack-1
3084 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003085 list = tv->vval.v_list;
3086
3087 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003088 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003090
3091 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092 {
Bram Moolenaared591872020-08-15 22:14:53 +02003093 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003094 n1 = tv->vval.v_number;
3095 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 }
Bram Moolenaared591872020-08-15 22:14:53 +02003097
3098 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003099 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003100 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003101 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3102 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003103 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 }
3105 break;
3106
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003107 case ISN_ANYINDEX:
3108 case ISN_ANYSLICE:
3109 {
3110 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3111 typval_T *var1, *var2;
3112 int res;
3113
3114 // index: composite is at stack-2, index at stack-1
3115 // slice: composite is at stack-3, indexes at stack-2 and
3116 // stack-1
3117 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003118 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003119 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3120 goto on_error;
3121 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3122 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003123 res = eval_index_inner(tv, is_slice, var1, var2,
3124 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003125 clear_tv(var1);
3126 if (is_slice)
3127 clear_tv(var2);
3128 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3129 if (res == FAIL)
3130 goto on_error;
3131 }
3132 break;
3133
Bram Moolenaar9af78762020-06-16 11:34:42 +02003134 case ISN_SLICE:
3135 {
3136 list_T *list;
3137 int count = iptr->isn_arg.number;
3138
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003139 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003140 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003141 list = tv->vval.v_list;
3142
3143 // no error for short list, expect it to be checked earlier
3144 if (list != NULL && list->lv_len >= count)
3145 {
3146 list_T *newlist = list_slice(list,
3147 count, list->lv_len - 1);
3148
3149 if (newlist != NULL)
3150 {
3151 list_unref(list);
3152 tv->vval.v_list = newlist;
3153 ++newlist->lv_refcount;
3154 }
3155 }
3156 }
3157 break;
3158
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003159 case ISN_GETITEM:
3160 {
3161 listitem_T *li;
3162 int index = iptr->isn_arg.number;
3163
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003164 // Get list item: list is at stack-1, push item.
3165 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003166 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003167 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003168
3169 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3170 goto failed;
3171 ++ectx.ec_stack.ga_len;
3172 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
3173 }
3174 break;
3175
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003176 case ISN_MEMBER:
3177 {
3178 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003179 char_u *key;
3180 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003181 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003182
3183 // dict member: dict is at stack-2, key at stack-1
3184 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003185 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003186 dict = tv->vval.v_dict;
3187
3188 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003189 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003190 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003191 if (key == NULL)
3192 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003193
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003194 if ((di = dict_find(dict, key, -1)) == NULL)
3195 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003196 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003197 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003198
3199 // If :silent! is used we will continue, make sure the
3200 // stack contents makes sense.
3201 clear_tv(tv);
3202 --ectx.ec_stack.ga_len;
3203 tv = STACK_TV_BOT(-1);
3204 clear_tv(tv);
3205 tv->v_type = VAR_NUMBER;
3206 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003207 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003208 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003209 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003210 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003211 // Clear the dict only after getting the item, to avoid
3212 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003213 tv = STACK_TV_BOT(-1);
3214 temp_tv = *tv;
3215 copy_tv(&di->di_tv, tv);
3216 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003217 }
3218 break;
3219
3220 // dict member with string key
3221 case ISN_STRINGMEMBER:
3222 {
3223 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003224 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003225 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003226
3227 tv = STACK_TV_BOT(-1);
3228 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3229 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003230 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003231 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003232 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 }
3234 dict = tv->vval.v_dict;
3235
3236 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3237 == NULL)
3238 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003241 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003243 // Clear the dict after getting the item, to avoid that it
3244 // make the item invalid.
3245 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003246 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003247 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 }
3249 break;
3250
3251 case ISN_NEGATENR:
3252 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003253 if (tv->v_type != VAR_NUMBER
3254#ifdef FEAT_FLOAT
3255 && tv->v_type != VAR_FLOAT
3256#endif
3257 )
3258 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003259 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003260 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003261 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003262 }
3263#ifdef FEAT_FLOAT
3264 if (tv->v_type == VAR_FLOAT)
3265 tv->vval.v_float = -tv->vval.v_float;
3266 else
3267#endif
3268 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269 break;
3270
3271 case ISN_CHECKNR:
3272 {
3273 int error = FALSE;
3274
3275 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003278 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003279 (void)tv_get_number_chk(tv, &error);
3280 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003281 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 }
3283 break;
3284
3285 case ISN_CHECKTYPE:
3286 {
3287 checktype_T *ct = &iptr->isn_arg.type;
3288
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003289 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003290 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaare32e5162021-01-21 20:21:29 +01003291 if (check_typval_type(ct->ct_type, tv, ct->ct_arg_idx)
3292 == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003293 goto on_error;
3294
3295 // number 0 is FALSE, number 1 is TRUE
3296 if (tv->v_type == VAR_NUMBER
3297 && ct->ct_type->tt_type == VAR_BOOL
3298 && (tv->vval.v_number == 0
3299 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003300 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003301 tv->v_type = VAR_BOOL;
3302 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003303 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003304 }
3305 }
3306 break;
3307
Bram Moolenaar9af78762020-06-16 11:34:42 +02003308 case ISN_CHECKLEN:
3309 {
3310 int min_len = iptr->isn_arg.checklen.cl_min_len;
3311 list_T *list = NULL;
3312
3313 tv = STACK_TV_BOT(-1);
3314 if (tv->v_type == VAR_LIST)
3315 list = tv->vval.v_list;
3316 if (list == NULL || list->lv_len < min_len
3317 || (list->lv_len > min_len
3318 && !iptr->isn_arg.checklen.cl_more_OK))
3319 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003320 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003321 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003322 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003323 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003324 }
3325 }
3326 break;
3327
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003328 case ISN_SETTYPE:
3329 {
3330 checktype_T *ct = &iptr->isn_arg.type;
3331
3332 tv = STACK_TV_BOT(-1);
3333 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3334 {
3335 free_type(tv->vval.v_dict->dv_type);
3336 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3337 }
3338 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3339 {
3340 free_type(tv->vval.v_list->lv_type);
3341 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3342 }
3343 }
3344 break;
3345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003346 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003347 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003348 {
3349 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003350 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003351
3352 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003353 if (iptr->isn_type == ISN_2BOOL)
3354 {
3355 n = tv2bool(tv);
3356 if (iptr->isn_arg.number) // invert
3357 n = !n;
3358 }
3359 else
3360 {
3361 SOURCING_LNUM = iptr->isn_lnum;
3362 n = tv_get_bool_chk(tv, &error);
3363 if (error)
3364 goto on_error;
3365 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 clear_tv(tv);
3367 tv->v_type = VAR_BOOL;
3368 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3369 }
3370 break;
3371
3372 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003373 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003374 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003375 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3376 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3377 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 break;
3379
Bram Moolenaar08597872020-12-10 19:43:40 +01003380 case ISN_RANGE:
3381 {
3382 exarg_T ea;
3383 char *errormsg;
3384
Bram Moolenaarece0b872021-01-08 20:40:45 +01003385 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003386 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003387 ea.addr_type = ADDR_LINES;
3388 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003389 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003390 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003391 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003392
3393 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3394 goto failed;
3395 ++ectx.ec_stack.ga_len;
3396 tv = STACK_TV_BOT(-1);
3397 tv->v_type = VAR_NUMBER;
3398 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003399 if (ea.addr_count == 0)
3400 tv->vval.v_number = curwin->w_cursor.lnum;
3401 else
3402 tv->vval.v_number = ea.line2;
3403 }
3404 break;
3405
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003406 case ISN_PUT:
3407 {
3408 int regname = iptr->isn_arg.put.put_regname;
3409 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3410 char_u *expr = NULL;
3411 int dir = FORWARD;
3412
Bram Moolenaar08597872020-12-10 19:43:40 +01003413 if (lnum < -2)
3414 {
3415 // line number was put on the stack by ISN_RANGE
3416 tv = STACK_TV_BOT(-1);
3417 curwin->w_cursor.lnum = tv->vval.v_number;
3418 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3419 dir = BACKWARD;
3420 --ectx.ec_stack.ga_len;
3421 }
3422 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003423 // :put! above cursor
3424 dir = BACKWARD;
3425 else if (lnum >= 0)
3426 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003427
3428 if (regname == '=')
3429 {
3430 tv = STACK_TV_BOT(-1);
3431 if (tv->v_type == VAR_STRING)
3432 expr = tv->vval.v_string;
3433 else
3434 {
3435 expr = typval2string(tv, TRUE); // allocates value
3436 clear_tv(tv);
3437 }
3438 --ectx.ec_stack.ga_len;
3439 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003440 check_cursor();
3441 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3442 vim_free(expr);
3443 }
3444 break;
3445
Bram Moolenaar02194d22020-10-24 23:08:38 +02003446 case ISN_CMDMOD:
3447 save_cmdmod = cmdmod;
3448 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003449 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003450 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3451 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003452 break;
3453
Bram Moolenaar02194d22020-10-24 23:08:38 +02003454 case ISN_CMDMOD_REV:
3455 // filter regprog is owned by the instruction, don't free it
3456 cmdmod.cmod_filter_regmatch.regprog = NULL;
3457 undo_cmdmod(&cmdmod);
3458 cmdmod = save_cmdmod;
3459 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003460 break;
3461
Bram Moolenaar792f7862020-11-23 08:31:18 +01003462 case ISN_UNPACK:
3463 {
3464 int count = iptr->isn_arg.unpack.unp_count;
3465 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3466 list_T *l;
3467 listitem_T *li;
3468 int i;
3469
3470 // Check there is a valid list to unpack.
3471 tv = STACK_TV_BOT(-1);
3472 if (tv->v_type != VAR_LIST)
3473 {
3474 SOURCING_LNUM = iptr->isn_lnum;
3475 emsg(_(e_for_argument_must_be_sequence_of_lists));
3476 goto on_error;
3477 }
3478 l = tv->vval.v_list;
3479 if (l == NULL
3480 || l->lv_len < (semicolon ? count - 1 : count))
3481 {
3482 SOURCING_LNUM = iptr->isn_lnum;
3483 emsg(_(e_list_value_does_not_have_enough_items));
3484 goto on_error;
3485 }
3486 else if (!semicolon && l->lv_len > count)
3487 {
3488 SOURCING_LNUM = iptr->isn_lnum;
3489 emsg(_(e_list_value_has_more_items_than_targets));
3490 goto on_error;
3491 }
3492
3493 CHECK_LIST_MATERIALIZE(l);
3494 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3495 goto failed;
3496 ectx.ec_stack.ga_len += count - 1;
3497
3498 // Variable after semicolon gets a list with the remaining
3499 // items.
3500 if (semicolon)
3501 {
3502 list_T *rem_list =
3503 list_alloc_with_items(l->lv_len - count + 1);
3504
3505 if (rem_list == NULL)
3506 goto failed;
3507 tv = STACK_TV_BOT(-count);
3508 tv->vval.v_list = rem_list;
3509 ++rem_list->lv_refcount;
3510 tv->v_lock = 0;
3511 li = l->lv_first;
3512 for (i = 0; i < count - 1; ++i)
3513 li = li->li_next;
3514 for (i = 0; li != NULL; ++i)
3515 {
3516 list_set_item(rem_list, i, &li->li_tv);
3517 li = li->li_next;
3518 }
3519 --count;
3520 }
3521
3522 // Produce the values in reverse order, first item last.
3523 li = l->lv_first;
3524 for (i = 0; i < count; ++i)
3525 {
3526 tv = STACK_TV_BOT(-i - 1);
3527 copy_tv(&li->li_tv, tv);
3528 li = li->li_next;
3529 }
3530
3531 list_unref(l);
3532 }
3533 break;
3534
Bram Moolenaarb2049902021-01-24 12:53:53 +01003535 case ISN_PROF_START:
3536 case ISN_PROF_END:
3537 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003538#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003539 funccall_T cookie;
3540 ufunc_T *cur_ufunc =
3541 (((dfunc_T *)def_functions.ga_data)
3542 + ectx.ec_dfunc_idx)->df_ufunc;
3543
3544 cookie.func = cur_ufunc;
3545 if (iptr->isn_type == ISN_PROF_START)
3546 {
3547 func_line_start(&cookie, iptr->isn_lnum);
3548 // if we get here the instruction is executed
3549 func_line_exec(&cookie);
3550 }
3551 else
3552 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003553#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003554 }
3555 break;
3556
Bram Moolenaar389df252020-07-09 21:20:47 +02003557 case ISN_SHUFFLE:
3558 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003559 typval_T tmp_tv;
3560 int item = iptr->isn_arg.shuffle.shfl_item;
3561 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003562
3563 tmp_tv = *STACK_TV_BOT(-item);
3564 for ( ; up > 0 && item > 1; --up)
3565 {
3566 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3567 --item;
3568 }
3569 *STACK_TV_BOT(-item) = tmp_tv;
3570 }
3571 break;
3572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003573 case ISN_DROP:
3574 --ectx.ec_stack.ga_len;
3575 clear_tv(STACK_TV_BOT(0));
3576 break;
3577 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003578 continue;
3579
Bram Moolenaard032f342020-07-18 18:13:02 +02003580func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003581 // Restore previous function. If the frame pointer is where we started
3582 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003583 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003584 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003585
Bram Moolenaard032f342020-07-18 18:13:02 +02003586 if (func_return(&ectx) == FAIL)
3587 // only fails when out of memory
3588 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003589 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003590
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003591on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003592 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003593 // If "emsg_silent" is set then ignore the error, unless it was set
3594 // when calling the function.
3595 if (did_emsg_cumul + did_emsg == did_emsg_before
3596 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003597 {
3598 // If a sequence of instructions causes an error while ":silent!"
3599 // was used, restore the stack length and jump ahead to restoring
3600 // the cmdmod.
3601 if (restore_cmdmod)
3602 {
3603 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3604 {
3605 --ectx.ec_stack.ga_len;
3606 clear_tv(STACK_TV_BOT(0));
3607 }
3608 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3609 ++ectx.ec_iidx;
3610 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003611 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003612 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003613on_fatal_error:
3614 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003615 // If we are not inside a try-catch started here, abort execution.
3616 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003617 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003618 }
3619
3620done:
3621 // function finished, get result from the stack.
3622 tv = STACK_TV_BOT(-1);
3623 *rettv = *tv;
3624 tv->v_type = VAR_UNKNOWN;
3625 ret = OK;
3626
3627failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003628 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003629 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003630 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003631
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003632 // Deal with any remaining closures, they may be in use somewhere.
3633 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003634 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003635 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003636 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3637 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003638
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003639 estack_pop();
3640 current_sctx = save_current_sctx;
3641
Bram Moolenaar352134b2020-10-17 22:04:08 +02003642 if (*msg_list != NULL && saved_msg_list != NULL)
3643 {
3644 msglist_T **plist = saved_msg_list;
3645
3646 // Append entries from the current msg_list (uncaught exceptions) to
3647 // the saved msg_list.
3648 while (*plist != NULL)
3649 plist = &(*plist)->next;
3650
3651 *plist = *msg_list;
3652 }
3653 msg_list = saved_msg_list;
3654
Bram Moolenaar02194d22020-10-24 23:08:38 +02003655 if (restore_cmdmod)
3656 {
3657 cmdmod.cmod_filter_regmatch.regprog = NULL;
3658 undo_cmdmod(&cmdmod);
3659 cmdmod = save_cmdmod;
3660 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003661 emsg_silent_def = save_emsg_silent_def;
3662 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003663
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003664failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003665 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003666 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3667 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003668
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003669 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003670 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003671
Bram Moolenaar0186e582021-01-10 18:33:11 +01003672 while (ectx.ec_outer != NULL)
3673 {
3674 outer_T *up = ectx.ec_outer->out_up_is_copy
3675 ? NULL : ectx.ec_outer->out_up;
3676
3677 vim_free(ectx.ec_outer);
3678 ectx.ec_outer = up;
3679 }
3680
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003681 // Not sure if this is necessary.
3682 suppress_errthrow = save_suppress_errthrow;
3683
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003684 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003685 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003686 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003687 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 return ret;
3689}
3690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003692 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003693 * We don't really need this at runtime, but we do have tests that require it,
3694 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003695 */
3696 void
3697ex_disassemble(exarg_T *eap)
3698{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003699 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003700 char_u *fname;
3701 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 dfunc_T *dfunc;
3703 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003704 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705 int current;
3706 int line_idx = 0;
3707 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003708 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003710 if (STRNCMP(arg, "<lambda>", 8) == 0)
3711 {
3712 arg += 8;
3713 (void)getdigits(&arg);
3714 fname = vim_strnsave(eap->arg, arg - eap->arg);
3715 }
3716 else
3717 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003718 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003719 if (fname == NULL)
3720 {
3721 semsg(_(e_invarg2), eap->arg);
3722 return;
3723 }
3724
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003725 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003726 if (ufunc == NULL)
3727 {
3728 char_u *p = untrans_function_name(fname);
3729
3730 if (p != NULL)
3731 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003732 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003733 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003734 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003735 if (ufunc == NULL)
3736 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003737 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003738 return;
3739 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003740 if (func_needs_compiling(ufunc, eap->forceit)
3741 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003742 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003743 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003745 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003746 return;
3747 }
3748 if (ufunc->uf_name_exp != NULL)
3749 msg((char *)ufunc->uf_name_exp);
3750 else
3751 msg((char *)ufunc->uf_name);
3752
3753 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003754#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003755 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3756 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3757 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003758#else
3759 instr = dfunc->df_instr;
3760 instr_count = dfunc->df_instr_count;
3761#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003762 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003763 {
3764 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003765 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766
3767 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3768 {
3769 if (current > prev_current)
3770 {
3771 msg_puts("\n\n");
3772 prev_current = current;
3773 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003774 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3775 if (line != NULL)
3776 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 }
3778
3779 switch (iptr->isn_type)
3780 {
3781 case ISN_EXEC:
3782 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3783 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003784 case ISN_EXECCONCAT:
3785 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003786 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003787 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788 case ISN_ECHO:
3789 {
3790 echo_T *echo = &iptr->isn_arg.echo;
3791
3792 smsg("%4d %s %d", current,
3793 echo->echo_with_white ? "ECHO" : "ECHON",
3794 echo->echo_count);
3795 }
3796 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003797 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003798 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003799 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003800 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003801 case ISN_ECHOMSG:
3802 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003803 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003804 break;
3805 case ISN_ECHOERR:
3806 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003807 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003808 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003810 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003811 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003812 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003813 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003814 + STACK_FRAME_SIZE));
3815 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003816 smsg("%4d LOAD $%lld", current,
3817 (varnumber_T)(iptr->isn_arg.number));
3818 }
3819 break;
3820 case ISN_LOADOUTER:
3821 {
3822 if (iptr->isn_arg.number < 0)
3823 smsg("%4d LOADOUTER level %d arg[%d]", current,
3824 iptr->isn_arg.outer.outer_depth,
3825 iptr->isn_arg.outer.outer_idx
3826 + STACK_FRAME_SIZE);
3827 else
3828 smsg("%4d LOADOUTER level %d $%d", current,
3829 iptr->isn_arg.outer.outer_depth,
3830 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003831 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003832 break;
3833 case ISN_LOADV:
3834 smsg("%4d LOADV v:%s", current,
3835 get_vim_var_name(iptr->isn_arg.number));
3836 break;
3837 case ISN_LOADSCRIPT:
3838 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003839 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3840 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003841 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003842 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003843
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003844 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3845 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003846 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003847 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003848 }
3849 break;
3850 case ISN_LOADS:
3851 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003852 scriptitem_T *si = SCRIPT_ITEM(
3853 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854
3855 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003856 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 }
3858 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003859 case ISN_LOADAUTO:
3860 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3861 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 case ISN_LOADG:
3863 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3864 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003865 case ISN_LOADB:
3866 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3867 break;
3868 case ISN_LOADW:
3869 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3870 break;
3871 case ISN_LOADT:
3872 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3873 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003874 case ISN_LOADGDICT:
3875 smsg("%4d LOAD g:", current);
3876 break;
3877 case ISN_LOADBDICT:
3878 smsg("%4d LOAD b:", current);
3879 break;
3880 case ISN_LOADWDICT:
3881 smsg("%4d LOAD w:", current);
3882 break;
3883 case ISN_LOADTDICT:
3884 smsg("%4d LOAD t:", current);
3885 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 case ISN_LOADOPT:
3887 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3888 break;
3889 case ISN_LOADENV:
3890 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3891 break;
3892 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003893 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 break;
3895
3896 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003897 if (iptr->isn_arg.number < 0)
3898 smsg("%4d STORE arg[%lld]", current,
3899 iptr->isn_arg.number + STACK_FRAME_SIZE);
3900 else
3901 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3902 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003903 case ISN_STOREOUTER:
3904 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003905 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003906 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3907 iptr->isn_arg.outer.outer_depth,
3908 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003909 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003910 smsg("%4d STOREOUTER level %d $%d", current,
3911 iptr->isn_arg.outer.outer_depth,
3912 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003915 case ISN_STOREV:
3916 smsg("%4d STOREV v:%s", current,
3917 get_vim_var_name(iptr->isn_arg.number));
3918 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003919 case ISN_STOREAUTO:
3920 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3921 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003922 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003923 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3924 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003925 case ISN_STOREB:
3926 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3927 break;
3928 case ISN_STOREW:
3929 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3930 break;
3931 case ISN_STORET:
3932 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3933 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003934 case ISN_STORES:
3935 {
3936 scriptitem_T *si = SCRIPT_ITEM(
3937 iptr->isn_arg.loadstore.ls_sid);
3938
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003939 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003940 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003941 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003942 break;
3943 case ISN_STORESCRIPT:
3944 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003945 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3946 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003947 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003948 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003949
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003950 smsg("%4d STORESCRIPT %s-%d in %s", current,
3951 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003952 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003953 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 }
3955 break;
3956 case ISN_STOREOPT:
3957 smsg("%4d STOREOPT &%s", current,
3958 iptr->isn_arg.storeopt.so_name);
3959 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003960 case ISN_STOREENV:
3961 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3962 break;
3963 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003964 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003965 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003966 case ISN_STORENR:
3967 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003968 iptr->isn_arg.storenr.stnr_val,
3969 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003970 break;
3971
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003972 case ISN_STOREINDEX:
3973 switch (iptr->isn_arg.vartype)
3974 {
3975 case VAR_LIST:
3976 smsg("%4d STORELIST", current);
3977 break;
3978 case VAR_DICT:
3979 smsg("%4d STOREDICT", current);
3980 break;
3981 case VAR_ANY:
3982 smsg("%4d STOREINDEX", current);
3983 break;
3984 default: break;
3985 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003986 break;
3987
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 // constants
3989 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01003990 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003991 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 break;
3993 case ISN_PUSHBOOL:
3994 case ISN_PUSHSPEC:
3995 smsg("%4d PUSH %s", current,
3996 get_var_special_name(iptr->isn_arg.number));
3997 break;
3998 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01003999#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004001#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 break;
4003 case ISN_PUSHS:
4004 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4005 break;
4006 case ISN_PUSHBLOB:
4007 {
4008 char_u *r;
4009 char_u numbuf[NUMBUFLEN];
4010 char_u *tofree;
4011
4012 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004013 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 vim_free(tofree);
4015 }
4016 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004017 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004018 {
4019 char *name = (char *)iptr->isn_arg.string;
4020
4021 smsg("%4d PUSHFUNC \"%s\"", current,
4022 name == NULL ? "[none]" : name);
4023 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004024 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004025 case ISN_PUSHCHANNEL:
4026#ifdef FEAT_JOB_CHANNEL
4027 {
4028 channel_T *channel = iptr->isn_arg.channel;
4029
4030 smsg("%4d PUSHCHANNEL %d", current,
4031 channel == NULL ? 0 : channel->ch_id);
4032 }
4033#endif
4034 break;
4035 case ISN_PUSHJOB:
4036#ifdef FEAT_JOB_CHANNEL
4037 {
4038 typval_T tv;
4039 char_u *name;
4040
4041 tv.v_type = VAR_JOB;
4042 tv.vval.v_job = iptr->isn_arg.job;
4043 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004044 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004045 }
4046#endif
4047 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 case ISN_PUSHEXC:
4049 smsg("%4d PUSH v:exception", current);
4050 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004051 case ISN_UNLET:
4052 smsg("%4d UNLET%s %s", current,
4053 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4054 iptr->isn_arg.unlet.ul_name);
4055 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004056 case ISN_UNLETENV:
4057 smsg("%4d UNLETENV%s $%s", current,
4058 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4059 iptr->isn_arg.unlet.ul_name);
4060 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004061 case ISN_UNLETINDEX:
4062 smsg("%4d UNLETINDEX", current);
4063 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004064 case ISN_LOCKCONST:
4065 smsg("%4d LOCKCONST", current);
4066 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004068 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004069 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 break;
4071 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004072 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004073 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004074 break;
4075
4076 // function call
4077 case ISN_BCALL:
4078 {
4079 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4080
4081 smsg("%4d BCALL %s(argc %d)", current,
4082 internal_func_name(cbfunc->cbf_idx),
4083 cbfunc->cbf_argcount);
4084 }
4085 break;
4086 case ISN_DCALL:
4087 {
4088 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4089 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4090 + cdfunc->cdf_idx;
4091
4092 smsg("%4d DCALL %s(argc %d)", current,
4093 df->df_ufunc->uf_name_exp != NULL
4094 ? df->df_ufunc->uf_name_exp
4095 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4096 }
4097 break;
4098 case ISN_UCALL:
4099 {
4100 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4101
4102 smsg("%4d UCALL %s(argc %d)", current,
4103 cufunc->cuf_name, cufunc->cuf_argcount);
4104 }
4105 break;
4106 case ISN_PCALL:
4107 {
4108 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4109
4110 smsg("%4d PCALL%s (argc %d)", current,
4111 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4112 }
4113 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004114 case ISN_PCALL_END:
4115 smsg("%4d PCALL end", current);
4116 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117 case ISN_RETURN:
4118 smsg("%4d RETURN", current);
4119 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004120 case ISN_RETURN_ZERO:
4121 smsg("%4d RETURN 0", current);
4122 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004123 case ISN_FUNCREF:
4124 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004125 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004126 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004127 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004128
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004129 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004130 }
4131 break;
4132
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004133 case ISN_NEWFUNC:
4134 {
4135 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4136
4137 smsg("%4d NEWFUNC %s %s", current,
4138 newfunc->nf_lambda, newfunc->nf_global);
4139 }
4140 break;
4141
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004142 case ISN_DEF:
4143 {
4144 char_u *name = iptr->isn_arg.string;
4145
4146 smsg("%4d DEF %s", current,
4147 name == NULL ? (char_u *)"" : name);
4148 }
4149 break;
4150
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004151 case ISN_JUMP:
4152 {
4153 char *when = "?";
4154
4155 switch (iptr->isn_arg.jump.jump_when)
4156 {
4157 case JUMP_ALWAYS:
4158 when = "JUMP";
4159 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 case JUMP_AND_KEEP_IF_TRUE:
4161 when = "JUMP_AND_KEEP_IF_TRUE";
4162 break;
4163 case JUMP_IF_FALSE:
4164 when = "JUMP_IF_FALSE";
4165 break;
4166 case JUMP_AND_KEEP_IF_FALSE:
4167 when = "JUMP_AND_KEEP_IF_FALSE";
4168 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004169 case JUMP_IF_COND_FALSE:
4170 when = "JUMP_IF_COND_FALSE";
4171 break;
4172 case JUMP_IF_COND_TRUE:
4173 when = "JUMP_IF_COND_TRUE";
4174 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004175 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004176 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004177 iptr->isn_arg.jump.jump_where);
4178 }
4179 break;
4180
4181 case ISN_FOR:
4182 {
4183 forloop_T *forloop = &iptr->isn_arg.forloop;
4184
4185 smsg("%4d FOR $%d -> %d", current,
4186 forloop->for_idx, forloop->for_end);
4187 }
4188 break;
4189
4190 case ISN_TRY:
4191 {
4192 try_T *try = &iptr->isn_arg.try;
4193
4194 smsg("%4d TRY catch -> %d, finally -> %d", current,
4195 try->try_catch, try->try_finally);
4196 }
4197 break;
4198 case ISN_CATCH:
4199 // TODO
4200 smsg("%4d CATCH", current);
4201 break;
4202 case ISN_ENDTRY:
4203 smsg("%4d ENDTRY", current);
4204 break;
4205 case ISN_THROW:
4206 smsg("%4d THROW", current);
4207 break;
4208
4209 // expression operations on number
4210 case ISN_OPNR:
4211 case ISN_OPFLOAT:
4212 case ISN_OPANY:
4213 {
4214 char *what;
4215 char *ins;
4216
4217 switch (iptr->isn_arg.op.op_type)
4218 {
4219 case EXPR_MULT: what = "*"; break;
4220 case EXPR_DIV: what = "/"; break;
4221 case EXPR_REM: what = "%"; break;
4222 case EXPR_SUB: what = "-"; break;
4223 case EXPR_ADD: what = "+"; break;
4224 default: what = "???"; break;
4225 }
4226 switch (iptr->isn_type)
4227 {
4228 case ISN_OPNR: ins = "OPNR"; break;
4229 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4230 case ISN_OPANY: ins = "OPANY"; break;
4231 default: ins = "???"; break;
4232 }
4233 smsg("%4d %s %s", current, ins, what);
4234 }
4235 break;
4236
4237 case ISN_COMPAREBOOL:
4238 case ISN_COMPARESPECIAL:
4239 case ISN_COMPARENR:
4240 case ISN_COMPAREFLOAT:
4241 case ISN_COMPARESTRING:
4242 case ISN_COMPAREBLOB:
4243 case ISN_COMPARELIST:
4244 case ISN_COMPAREDICT:
4245 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004246 case ISN_COMPAREANY:
4247 {
4248 char *p;
4249 char buf[10];
4250 char *type;
4251
4252 switch (iptr->isn_arg.op.op_type)
4253 {
4254 case EXPR_EQUAL: p = "=="; break;
4255 case EXPR_NEQUAL: p = "!="; break;
4256 case EXPR_GREATER: p = ">"; break;
4257 case EXPR_GEQUAL: p = ">="; break;
4258 case EXPR_SMALLER: p = "<"; break;
4259 case EXPR_SEQUAL: p = "<="; break;
4260 case EXPR_MATCH: p = "=~"; break;
4261 case EXPR_IS: p = "is"; break;
4262 case EXPR_ISNOT: p = "isnot"; break;
4263 case EXPR_NOMATCH: p = "!~"; break;
4264 default: p = "???"; break;
4265 }
4266 STRCPY(buf, p);
4267 if (iptr->isn_arg.op.op_ic == TRUE)
4268 strcat(buf, "?");
4269 switch(iptr->isn_type)
4270 {
4271 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4272 case ISN_COMPARESPECIAL:
4273 type = "COMPARESPECIAL"; break;
4274 case ISN_COMPARENR: type = "COMPARENR"; break;
4275 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4276 case ISN_COMPARESTRING:
4277 type = "COMPARESTRING"; break;
4278 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4279 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4280 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4281 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004282 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4283 default: type = "???"; break;
4284 }
4285
4286 smsg("%4d %s %s", current, type, buf);
4287 }
4288 break;
4289
4290 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4291 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4292
4293 // expression operations
4294 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004295 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004296 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004297 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004298 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004299 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004300 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004301 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4302 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004303 case ISN_SLICE: smsg("%4d SLICE %lld",
4304 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004305 case ISN_GETITEM: smsg("%4d ITEM %lld",
4306 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004307 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4308 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004309 iptr->isn_arg.string); break;
4310 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4311
4312 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004313 case ISN_CHECKTYPE:
4314 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004315 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004316 char *tofree;
4317
Bram Moolenaare32e5162021-01-21 20:21:29 +01004318 if (ct->ct_arg_idx == 0)
4319 smsg("%4d CHECKTYPE %s stack[%d]", current,
4320 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004321 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004322 else
4323 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4324 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004325 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004326 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004327 vim_free(tofree);
4328 break;
4329 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004330 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4331 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4332 iptr->isn_arg.checklen.cl_min_len);
4333 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004334 case ISN_SETTYPE:
4335 {
4336 char *tofree;
4337
4338 smsg("%4d SETTYPE %s", current,
4339 type_name(iptr->isn_arg.type.ct_type, &tofree));
4340 vim_free(tofree);
4341 break;
4342 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004343 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004344 case ISN_2BOOL: if (iptr->isn_arg.number)
4345 smsg("%4d INVERT (!val)", current);
4346 else
4347 smsg("%4d 2BOOL (!!val)", current);
4348 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004349 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004350 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004351 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004352 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004353 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004354 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004355 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4356 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004357 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004358 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4359 smsg("%4d PUT %c above range",
4360 current, iptr->isn_arg.put.put_regname);
4361 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4362 smsg("%4d PUT %c range",
4363 current, iptr->isn_arg.put.put_regname);
4364 else
4365 smsg("%4d PUT %c %ld", current,
4366 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004367 (long)iptr->isn_arg.put.put_lnum);
4368 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369
Bram Moolenaar02194d22020-10-24 23:08:38 +02004370 // TODO: summarize modifiers
4371 case ISN_CMDMOD:
4372 {
4373 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004374 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004375 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4376
4377 buf = alloc(len + 1);
4378 if (buf != NULL)
4379 {
4380 (void)produce_cmdmods(
4381 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4382 smsg("%4d CMDMOD %s", current, buf);
4383 vim_free(buf);
4384 }
4385 break;
4386 }
4387 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004388
Bram Moolenaarb2049902021-01-24 12:53:53 +01004389 case ISN_PROF_START:
4390 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4391 break;
4392
4393 case ISN_PROF_END:
4394 smsg("%4d PROFILE END", current);
4395 break;
4396
Bram Moolenaar792f7862020-11-23 08:31:18 +01004397 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4398 iptr->isn_arg.unpack.unp_count,
4399 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4400 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004401 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4402 iptr->isn_arg.shuffle.shfl_item,
4403 iptr->isn_arg.shuffle.shfl_up);
4404 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004405 case ISN_DROP: smsg("%4d DROP", current); break;
4406 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004407
4408 out_flush(); // output one line at a time
4409 ui_breakcheck();
4410 if (got_int)
4411 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004413}
4414
4415/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004416 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004417 * list, etc. Mostly like what JavaScript does, except that empty list and
4418 * empty dictionary are FALSE.
4419 */
4420 int
4421tv2bool(typval_T *tv)
4422{
4423 switch (tv->v_type)
4424 {
4425 case VAR_NUMBER:
4426 return tv->vval.v_number != 0;
4427 case VAR_FLOAT:
4428#ifdef FEAT_FLOAT
4429 return tv->vval.v_float != 0.0;
4430#else
4431 break;
4432#endif
4433 case VAR_PARTIAL:
4434 return tv->vval.v_partial != NULL;
4435 case VAR_FUNC:
4436 case VAR_STRING:
4437 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4438 case VAR_LIST:
4439 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4440 case VAR_DICT:
4441 return tv->vval.v_dict != NULL
4442 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4443 case VAR_BOOL:
4444 case VAR_SPECIAL:
4445 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4446 case VAR_JOB:
4447#ifdef FEAT_JOB_CHANNEL
4448 return tv->vval.v_job != NULL;
4449#else
4450 break;
4451#endif
4452 case VAR_CHANNEL:
4453#ifdef FEAT_JOB_CHANNEL
4454 return tv->vval.v_channel != NULL;
4455#else
4456 break;
4457#endif
4458 case VAR_BLOB:
4459 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4460 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004461 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 case VAR_VOID:
4463 break;
4464 }
4465 return FALSE;
4466}
4467
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004468 void
4469emsg_using_string_as(typval_T *tv, int as_number)
4470{
4471 semsg(_(as_number ? e_using_string_as_number_str
4472 : e_using_string_as_bool_str),
4473 tv->vval.v_string == NULL
4474 ? (char_u *)"" : tv->vval.v_string);
4475}
4476
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004477/*
4478 * If "tv" is a string give an error and return FAIL.
4479 */
4480 int
4481check_not_string(typval_T *tv)
4482{
4483 if (tv->v_type == VAR_STRING)
4484 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004485 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004486 clear_tv(tv);
4487 return FAIL;
4488 }
4489 return OK;
4490}
4491
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004492
4493#endif // FEAT_EVAL