blob: 6d462eaf1dbdc3baa9f16f6b36918709488ed1b4 [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 Moolenaarf785aa12021-02-11 21:19:34 +0100854 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
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 Moolenaarf785aa12021-02-11 21:19:34 +01001149 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150
1151// Get pointer to item in the stack.
1152#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1153
1154// Get pointer to item at the bottom of the stack, -1 is the bottom.
1155#undef STACK_TV_BOT
1156#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1157
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001158// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001159#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 +01001160
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001161 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001162 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1163 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001164 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001165 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001166 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001167 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001168 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001169 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001170 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001171
Bram Moolenaar09689a02020-05-09 22:50:08 +02001172 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001173 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001174 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1175 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001176 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001177 {
1178 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001179 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001180 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001181 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001182
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001183 // If depth of calling is getting too high, don't execute the function.
1184 orig_funcdepth = funcdepth_get();
1185 if (funcdepth_increment() == FAIL)
1186 return FAIL;
1187
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001188 CLEAR_FIELD(ectx);
1189 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1190 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1191 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001192 {
1193 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001194 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001195 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001197 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001199 // Put arguments on the stack, but no more than what the function expects.
1200 // A lambda can be called with more arguments than it uses.
1201 for (idx = 0; idx < argc
1202 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1203 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001204 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001205 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001206 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001207 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001208 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001209 copy_tv(&argv[idx], STACK_TV_BOT(0));
1210 ++ectx.ec_stack.ga_len;
1211 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001212
1213 // Turn varargs into a list. Empty list if no args.
1214 if (ufunc->uf_va_name != NULL)
1215 {
1216 int vararg_count = argc - ufunc->uf_args.ga_len;
1217
1218 if (vararg_count < 0)
1219 vararg_count = 0;
1220 else
1221 argc -= vararg_count;
1222 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001223 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001224
1225 // Check the type of the list items.
1226 tv = STACK_TV_BOT(-1);
1227 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001228 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001229 && ufunc->uf_va_type->tt_member != &t_any
1230 && tv->vval.v_list != NULL)
1231 {
1232 type_T *expected = ufunc->uf_va_type->tt_member;
1233 listitem_T *li = tv->vval.v_list->lv_first;
1234
1235 for (idx = 0; idx < vararg_count; ++idx)
1236 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001237 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001238 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001239 goto failed_early;
1240 li = li->li_next;
1241 }
1242 }
1243
Bram Moolenaar23e03252020-04-12 22:22:31 +02001244 if (defcount > 0)
1245 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001246 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001247 --ectx.ec_stack.ga_len;
1248 }
1249
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001250 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001251 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001252 if (defcount > 0)
1253 for (idx = 0; idx < defcount; ++idx)
1254 {
1255 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1256 ++ectx.ec_stack.ga_len;
1257 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001258 if (ufunc->uf_va_name != NULL)
1259 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260
1261 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001262 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1263 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001264
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001265 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001266 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1267 + ufunc->uf_dfunc_idx;
1268 ufunc_T *base_ufunc = dfunc->df_ufunc;
1269
1270 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1271 // by copy_func().
1272 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001273 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001274 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1275 if (ectx.ec_outer == NULL)
1276 goto failed_early;
1277 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001278 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001279 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1280 {
1281 if (current_ectx->ec_outer != NULL)
1282 *ectx.ec_outer = *current_ectx->ec_outer;
1283 }
1284 else
1285 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001286 }
1287 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001288 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1289 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001290 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001291 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001292
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001293 // dummy frame entries
1294 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1295 {
1296 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1297 ++ectx.ec_stack.ga_len;
1298 }
1299
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001300 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001301 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001302 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1303 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001304
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001305 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001306 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001307 ectx.ec_stack.ga_len += dfunc->df_varcount;
1308 if (dfunc->df_has_closure)
1309 {
1310 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1311 STACK_TV_VAR(idx)->vval.v_number = 0;
1312 ++ectx.ec_stack.ga_len;
1313 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001314
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001315 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001316 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001317
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001318 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001319 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001320 estack_push_ufunc(ufunc, 1);
1321 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001322 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1323
Bram Moolenaar352134b2020-10-17 22:04:08 +02001324 // Use a specific location for storing error messages to be converted to an
1325 // exception.
1326 saved_msg_list = msg_list;
1327 msg_list = &private_msg_list;
1328
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001329 // Do turn errors into exceptions.
1330 suppress_errthrow = FALSE;
1331
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001332 // When ":silent!" was used before calling then we still abort the
1333 // function. If ":silent!" is used in the function then we don't.
1334 emsg_silent_def = emsg_silent;
1335 did_emsg_def = 0;
1336
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001337 where.wt_index = 0;
1338 where.wt_variable = FALSE;
1339
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001340 // Decide where to start execution, handles optional arguments.
1341 init_instr_idx(ufunc, argc, &ectx);
1342
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343 for (;;)
1344 {
1345 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001346
Bram Moolenaar270d0382020-05-15 21:42:53 +02001347 if (++breakcheck_count >= 100)
1348 {
1349 line_breakcheck();
1350 breakcheck_count = 0;
1351 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001352 if (got_int)
1353 {
1354 // Turn CTRL-C into an exception.
1355 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001356 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001357 goto failed;
1358 did_throw = TRUE;
1359 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001360
Bram Moolenaara26b9702020-04-18 19:53:28 +02001361 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1362 {
1363 // Turn an error message into an exception.
1364 did_emsg = FALSE;
1365 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1366 goto failed;
1367 did_throw = TRUE;
1368 *msg_list = NULL;
1369 }
1370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 if (did_throw && !ectx.ec_in_catch)
1372 {
1373 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001374 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001375
1376 // An exception jumps to the first catch, finally, or returns from
1377 // the current function.
1378 if (trystack->ga_len > 0)
1379 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001380 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001381 {
1382 // jump to ":catch" or ":finally"
1383 ectx.ec_in_catch = TRUE;
1384 ectx.ec_iidx = trycmd->tcd_catch_idx;
1385 }
1386 else
1387 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001388 // Not inside try or need to return from current functions.
1389 // Push a dummy return value.
1390 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1391 goto failed;
1392 tv = STACK_TV_BOT(0);
1393 tv->v_type = VAR_NUMBER;
1394 tv->vval.v_number = 0;
1395 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001396 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001397 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001398 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001399 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001400 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1401 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001402 goto done;
1403 }
1404
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001405 if (func_return(&ectx) == FAIL)
1406 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001407 }
1408 continue;
1409 }
1410
1411 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1412 switch (iptr->isn_type)
1413 {
1414 // execute Ex command line
1415 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001416 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001417 source_cookie_T cookie;
1418
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001419 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001420 // Pass getsourceline to get an error for a missing ":end"
1421 // command.
1422 CLEAR_FIELD(cookie);
1423 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1424 if (do_cmdline(iptr->isn_arg.string,
1425 getsourceline, &cookie,
1426 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1427 == FAIL
1428 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001429 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001430 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001431 break;
1432
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001433 // execute Ex command from pieces on the stack
1434 case ISN_EXECCONCAT:
1435 {
1436 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001437 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001438 int pass;
1439 int i;
1440 char_u *cmd = NULL;
1441 char_u *str;
1442
1443 for (pass = 1; pass <= 2; ++pass)
1444 {
1445 for (i = 0; i < count; ++i)
1446 {
1447 tv = STACK_TV_BOT(i - count);
1448 str = tv->vval.v_string;
1449 if (str != NULL && *str != NUL)
1450 {
1451 if (pass == 2)
1452 STRCPY(cmd + len, str);
1453 len += STRLEN(str);
1454 }
1455 if (pass == 2)
1456 clear_tv(tv);
1457 }
1458 if (pass == 1)
1459 {
1460 cmd = alloc(len + 1);
1461 if (cmd == NULL)
1462 goto failed;
1463 len = 0;
1464 }
1465 }
1466
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001467 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001468 do_cmdline_cmd(cmd);
1469 vim_free(cmd);
1470 }
1471 break;
1472
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001473 // execute :echo {string} ...
1474 case ISN_ECHO:
1475 {
1476 int count = iptr->isn_arg.echo.echo_count;
1477 int atstart = TRUE;
1478 int needclr = TRUE;
1479
1480 for (idx = 0; idx < count; ++idx)
1481 {
1482 tv = STACK_TV_BOT(idx - count);
1483 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1484 &atstart, &needclr);
1485 clear_tv(tv);
1486 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001487 if (needclr)
1488 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001489 ectx.ec_stack.ga_len -= count;
1490 }
1491 break;
1492
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001493 // :execute {string} ...
1494 // :echomsg {string} ...
1495 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001496 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001497 case ISN_ECHOMSG:
1498 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001499 {
1500 int count = iptr->isn_arg.number;
1501 garray_T ga;
1502 char_u buf[NUMBUFLEN];
1503 char_u *p;
1504 int len;
1505 int failed = FALSE;
1506
1507 ga_init2(&ga, 1, 80);
1508 for (idx = 0; idx < count; ++idx)
1509 {
1510 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001511 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001512 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001513 if (tv->v_type == VAR_CHANNEL
1514 || tv->v_type == VAR_JOB)
1515 {
1516 SOURCING_LNUM = iptr->isn_lnum;
1517 emsg(_(e_inval_string));
1518 break;
1519 }
1520 else
1521 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001522 }
1523 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001524 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001525
1526 len = (int)STRLEN(p);
1527 if (ga_grow(&ga, len + 2) == FAIL)
1528 failed = TRUE;
1529 else
1530 {
1531 if (ga.ga_len > 0)
1532 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1533 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1534 ga.ga_len += len;
1535 }
1536 clear_tv(tv);
1537 }
1538 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001539 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001540 {
1541 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001542 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001543 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001544
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001545 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001546 {
1547 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001548 {
1549 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001550 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001551 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001552 {
1553 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001554 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001555 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001556 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001557 else
1558 {
1559 msg_sb_eol();
1560 if (iptr->isn_type == ISN_ECHOMSG)
1561 {
1562 msg_attr(ga.ga_data, echo_attr);
1563 out_flush();
1564 }
1565 else
1566 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001567 SOURCING_LNUM = iptr->isn_lnum;
1568 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001569 }
1570 }
1571 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001572 ga_clear(&ga);
1573 }
1574 break;
1575
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001576 // load local variable or argument
1577 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001578 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001579 goto failed;
1580 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1581 ++ectx.ec_stack.ga_len;
1582 break;
1583
1584 // load v: variable
1585 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001586 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001587 goto failed;
1588 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1589 ++ectx.ec_stack.ga_len;
1590 break;
1591
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001592 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001593 case ISN_LOADSCRIPT:
1594 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001595 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001596 svar_T *sv;
1597
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001598 sv = get_script_svar(sref, &ectx);
1599 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001600 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001601 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001602 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001603 goto failed;
1604 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1605 ++ectx.ec_stack.ga_len;
1606 }
1607 break;
1608
1609 // load s: variable in old script
1610 case ISN_LOADS:
1611 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001612 hashtab_T *ht = &SCRIPT_VARS(
1613 iptr->isn_arg.loadstore.ls_sid);
1614 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001615 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001616
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001617 if (di == NULL)
1618 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001619 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001620 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001621 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001622 }
1623 else
1624 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001625 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001626 goto failed;
1627 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1628 ++ectx.ec_stack.ga_len;
1629 }
1630 }
1631 break;
1632
Bram Moolenaard3aac292020-04-19 14:32:17 +02001633 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001634 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001635 case ISN_LOADB:
1636 case ISN_LOADW:
1637 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001638 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001639 dictitem_T *di = NULL;
1640 hashtab_T *ht = NULL;
1641 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001642
Bram Moolenaard3aac292020-04-19 14:32:17 +02001643 switch (iptr->isn_type)
1644 {
1645 case ISN_LOADG:
1646 ht = get_globvar_ht();
1647 namespace = 'g';
1648 break;
1649 case ISN_LOADB:
1650 ht = &curbuf->b_vars->dv_hashtab;
1651 namespace = 'b';
1652 break;
1653 case ISN_LOADW:
1654 ht = &curwin->w_vars->dv_hashtab;
1655 namespace = 'w';
1656 break;
1657 case ISN_LOADT:
1658 ht = &curtab->tp_vars->dv_hashtab;
1659 namespace = 't';
1660 break;
1661 default: // Cannot reach here
1662 goto failed;
1663 }
1664 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001665
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 if (di == NULL)
1667 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001668 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001669 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001670 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001671 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001672 }
1673 else
1674 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001675 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 goto failed;
1677 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1678 ++ectx.ec_stack.ga_len;
1679 }
1680 }
1681 break;
1682
Bram Moolenaar03290b82020-12-19 16:30:44 +01001683 // load autoload variable
1684 case ISN_LOADAUTO:
1685 {
1686 char_u *name = iptr->isn_arg.string;
1687
1688 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1689 goto failed;
1690 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001691 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001692 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1693 goto on_error;
1694 ++ectx.ec_stack.ga_len;
1695 }
1696 break;
1697
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001698 // load g:/b:/w:/t: namespace
1699 case ISN_LOADGDICT:
1700 case ISN_LOADBDICT:
1701 case ISN_LOADWDICT:
1702 case ISN_LOADTDICT:
1703 {
1704 dict_T *d = NULL;
1705
1706 switch (iptr->isn_type)
1707 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001708 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1709 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1710 case ISN_LOADWDICT: d = curwin->w_vars; break;
1711 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001712 default: // Cannot reach here
1713 goto failed;
1714 }
1715 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1716 goto failed;
1717 tv = STACK_TV_BOT(0);
1718 tv->v_type = VAR_DICT;
1719 tv->v_lock = 0;
1720 tv->vval.v_dict = d;
1721 ++ectx.ec_stack.ga_len;
1722 }
1723 break;
1724
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 // load &option
1726 case ISN_LOADOPT:
1727 {
1728 typval_T optval;
1729 char_u *name = iptr->isn_arg.string;
1730
Bram Moolenaara8c17702020-04-01 21:17:24 +02001731 // This is not expected to fail, name is checked during
1732 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001733 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001734 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001735 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001736 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001737 *STACK_TV_BOT(0) = optval;
1738 ++ectx.ec_stack.ga_len;
1739 }
1740 break;
1741
1742 // load $ENV
1743 case ISN_LOADENV:
1744 {
1745 typval_T optval;
1746 char_u *name = iptr->isn_arg.string;
1747
Bram Moolenaar270d0382020-05-15 21:42:53 +02001748 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001750 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001751 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 *STACK_TV_BOT(0) = optval;
1753 ++ectx.ec_stack.ga_len;
1754 }
1755 break;
1756
1757 // load @register
1758 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001759 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001760 goto failed;
1761 tv = STACK_TV_BOT(0);
1762 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001763 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001764 // This may result in NULL, which should be equivalent to an
1765 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001766 tv->vval.v_string = get_reg_contents(
1767 iptr->isn_arg.number, GREG_EXPR_SRC);
1768 ++ectx.ec_stack.ga_len;
1769 break;
1770
1771 // store local variable
1772 case ISN_STORE:
1773 --ectx.ec_stack.ga_len;
1774 tv = STACK_TV_VAR(iptr->isn_arg.number);
1775 clear_tv(tv);
1776 *tv = *STACK_TV_BOT(0);
1777 break;
1778
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001779 // store s: variable in old script
1780 case ISN_STORES:
1781 {
1782 hashtab_T *ht = &SCRIPT_VARS(
1783 iptr->isn_arg.loadstore.ls_sid);
1784 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001785 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001786
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001787 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001788 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001789 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001790 else
1791 {
1792 clear_tv(&di->di_tv);
1793 di->di_tv = *STACK_TV_BOT(0);
1794 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001795 }
1796 break;
1797
1798 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 case ISN_STORESCRIPT:
1800 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001801 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1802 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001803
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001804 sv = get_script_svar(sref, &ectx);
1805 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001806 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001807 --ectx.ec_stack.ga_len;
1808 clear_tv(sv->sv_tv);
1809 *sv->sv_tv = *STACK_TV_BOT(0);
1810 }
1811 break;
1812
1813 // store option
1814 case ISN_STOREOPT:
1815 {
1816 long n = 0;
1817 char_u *s = NULL;
1818 char *msg;
1819
1820 --ectx.ec_stack.ga_len;
1821 tv = STACK_TV_BOT(0);
1822 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001823 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001825 if (s == NULL)
1826 s = (char_u *)"";
1827 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001829 // must be VAR_NUMBER, CHECKTYPE makes sure
1830 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1832 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001833 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 if (msg != NULL)
1835 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001836 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001837 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001838 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
1841 break;
1842
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001843 // store $ENV
1844 case ISN_STOREENV:
1845 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001846 tv = STACK_TV_BOT(0);
1847 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1848 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001849 break;
1850
1851 // store @r
1852 case ISN_STOREREG:
1853 {
1854 int reg = iptr->isn_arg.number;
1855
1856 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001857 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001858 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001859 tv_get_string(tv), -1, FALSE);
1860 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001861 }
1862 break;
1863
1864 // store v: variable
1865 case ISN_STOREV:
1866 --ectx.ec_stack.ga_len;
1867 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1868 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001869 // should not happen, type is checked when compiling
1870 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001871 break;
1872
Bram Moolenaard3aac292020-04-19 14:32:17 +02001873 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001874 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001875 case ISN_STOREB:
1876 case ISN_STOREW:
1877 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001879 dictitem_T *di;
1880 hashtab_T *ht;
1881 char_u *name = iptr->isn_arg.string + 2;
1882
Bram Moolenaard3aac292020-04-19 14:32:17 +02001883 switch (iptr->isn_type)
1884 {
1885 case ISN_STOREG:
1886 ht = get_globvar_ht();
1887 break;
1888 case ISN_STOREB:
1889 ht = &curbuf->b_vars->dv_hashtab;
1890 break;
1891 case ISN_STOREW:
1892 ht = &curwin->w_vars->dv_hashtab;
1893 break;
1894 case ISN_STORET:
1895 ht = &curtab->tp_vars->dv_hashtab;
1896 break;
1897 default: // Cannot reach here
1898 goto failed;
1899 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001900
1901 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001902 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001904 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 else
1906 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001907 SOURCING_LNUM = iptr->isn_lnum;
1908 if (var_check_permission(di, name) == FAIL)
1909 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 clear_tv(&di->di_tv);
1911 di->di_tv = *STACK_TV_BOT(0);
1912 }
1913 }
1914 break;
1915
Bram Moolenaar03290b82020-12-19 16:30:44 +01001916 // store an autoload variable
1917 case ISN_STOREAUTO:
1918 SOURCING_LNUM = iptr->isn_lnum;
1919 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1920 clear_tv(STACK_TV_BOT(-1));
1921 --ectx.ec_stack.ga_len;
1922 break;
1923
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 // store number in local variable
1925 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001926 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 clear_tv(tv);
1928 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001929 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001930 break;
1931
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001932 // store value in list or dict variable
1933 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001934 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001935 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001936 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001937 typval_T *tv_dest = STACK_TV_BOT(-1);
1938 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001939
Bram Moolenaar752fc692021-01-04 21:57:11 +01001940 // Stack contains:
1941 // -3 value to be stored
1942 // -2 index
1943 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001944 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001945 SOURCING_LNUM = iptr->isn_lnum;
1946 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001947 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001948 dest_type = tv_dest->v_type;
1949 if (dest_type == VAR_DICT)
1950 status = do_2string(tv_idx, TRUE);
1951 else if (dest_type == VAR_LIST
1952 && tv_idx->v_type != VAR_NUMBER)
1953 {
1954 emsg(_(e_number_exp));
1955 status = FAIL;
1956 }
1957 }
1958 else if (dest_type != tv_dest->v_type)
1959 {
1960 // just in case, should be OK
1961 semsg(_(e_expected_str_but_got_str),
1962 vartype_name(dest_type),
1963 vartype_name(tv_dest->v_type));
1964 status = FAIL;
1965 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001966
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001967 if (status == OK && dest_type == VAR_LIST)
1968 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001969 long lidx = (long)tv_idx->vval.v_number;
1970 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001971
1972 if (list == NULL)
1973 {
1974 emsg(_(e_list_not_set));
1975 goto on_error;
1976 }
1977 if (lidx < 0 && list->lv_len + lidx >= 0)
1978 // negative index is relative to the end
1979 lidx = list->lv_len + lidx;
1980 if (lidx < 0 || lidx > list->lv_len)
1981 {
1982 semsg(_(e_listidx), lidx);
1983 goto on_error;
1984 }
1985 if (lidx < list->lv_len)
1986 {
1987 listitem_T *li = list_find(list, lidx);
1988
1989 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001990 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001991 goto on_error;
1992 // overwrite existing list item
1993 clear_tv(&li->li_tv);
1994 li->li_tv = *tv;
1995 }
1996 else
1997 {
1998 if (error_if_locked(list->lv_lock,
1999 e_cannot_change_list))
2000 goto on_error;
2001 // append to list, only fails when out of memory
2002 if (list_append_tv(list, tv) == FAIL)
2003 goto failed;
2004 clear_tv(tv);
2005 }
2006 }
2007 else if (status == OK && dest_type == VAR_DICT)
2008 {
2009 char_u *key = tv_idx->vval.v_string;
2010 dict_T *dict = tv_dest->vval.v_dict;
2011 dictitem_T *di;
2012
2013 SOURCING_LNUM = iptr->isn_lnum;
2014 if (dict == NULL)
2015 {
2016 emsg(_(e_dictionary_not_set));
2017 goto on_error;
2018 }
2019 if (key == NULL)
2020 key = (char_u *)"";
2021 di = dict_find(dict, key, -1);
2022 if (di != NULL)
2023 {
2024 if (error_if_locked(di->di_tv.v_lock,
2025 e_cannot_change_dict_item))
2026 goto on_error;
2027 // overwrite existing value
2028 clear_tv(&di->di_tv);
2029 di->di_tv = *tv;
2030 }
2031 else
2032 {
2033 if (error_if_locked(dict->dv_lock,
2034 e_cannot_change_dict))
2035 goto on_error;
2036 // add to dict, only fails when out of memory
2037 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2038 goto failed;
2039 clear_tv(tv);
2040 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002041 }
2042 else
2043 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002044 status = FAIL;
2045 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002046 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002047
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002048 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002049 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002050 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002051 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002052 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002053 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002054 goto on_error;
2055 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002056 }
2057 break;
2058
Bram Moolenaar0186e582021-01-10 18:33:11 +01002059 // load or store variable or argument from outer scope
2060 case ISN_LOADOUTER:
2061 case ISN_STOREOUTER:
2062 {
2063 int depth = iptr->isn_arg.outer.outer_depth;
2064 outer_T *outer = ectx.ec_outer;
2065
2066 while (depth > 1 && outer != NULL)
2067 {
2068 outer = outer->out_up;
2069 --depth;
2070 }
2071 if (outer == NULL)
2072 {
2073 SOURCING_LNUM = iptr->isn_lnum;
2074 iemsg("LOADOUTER depth more than scope levels");
2075 goto failed;
2076 }
2077 tv = ((typval_T *)outer->out_stack->ga_data)
2078 + outer->out_frame_idx + STACK_FRAME_SIZE
2079 + iptr->isn_arg.outer.outer_idx;
2080 if (iptr->isn_type == ISN_LOADOUTER)
2081 {
2082 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2083 goto failed;
2084 copy_tv(tv, STACK_TV_BOT(0));
2085 ++ectx.ec_stack.ga_len;
2086 }
2087 else
2088 {
2089 --ectx.ec_stack.ga_len;
2090 clear_tv(tv);
2091 *tv = *STACK_TV_BOT(0);
2092 }
2093 }
2094 break;
2095
Bram Moolenaar752fc692021-01-04 21:57:11 +01002096 // unlet item in list or dict variable
2097 case ISN_UNLETINDEX:
2098 {
2099 typval_T *tv_idx = STACK_TV_BOT(-2);
2100 typval_T *tv_dest = STACK_TV_BOT(-1);
2101 int status = OK;
2102
2103 // Stack contains:
2104 // -2 index
2105 // -1 dict or list
2106 if (tv_dest->v_type == VAR_DICT)
2107 {
2108 // unlet a dict item, index must be a string
2109 if (tv_idx->v_type != VAR_STRING)
2110 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002111 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002112 semsg(_(e_expected_str_but_got_str),
2113 vartype_name(VAR_STRING),
2114 vartype_name(tv_idx->v_type));
2115 status = FAIL;
2116 }
2117 else
2118 {
2119 dict_T *d = tv_dest->vval.v_dict;
2120 char_u *key = tv_idx->vval.v_string;
2121 dictitem_T *di = NULL;
2122
2123 if (key == NULL)
2124 key = (char_u *)"";
2125 if (d != NULL)
2126 di = dict_find(d, key, (int)STRLEN(key));
2127 if (di == NULL)
2128 {
2129 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002131 semsg(_(e_dictkey), key);
2132 status = FAIL;
2133 }
2134 else
2135 {
2136 // TODO: check for dict or item locked
2137 dictitem_remove(d, di);
2138 }
2139 }
2140 }
2141 else if (tv_dest->v_type == VAR_LIST)
2142 {
2143 // unlet a List item, index must be a number
2144 if (tv_idx->v_type != VAR_NUMBER)
2145 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002146 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002147 semsg(_(e_expected_str_but_got_str),
2148 vartype_name(VAR_NUMBER),
2149 vartype_name(tv_idx->v_type));
2150 status = FAIL;
2151 }
2152 else
2153 {
2154 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002155 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002156 listitem_T *li = NULL;
2157
2158 li = list_find(l, n);
2159 if (li == NULL)
2160 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002161 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002162 semsg(_(e_listidx), n);
2163 status = FAIL;
2164 }
2165 else
2166 // TODO: check for list or item locked
2167 listitem_remove(l, li);
2168 }
2169 }
2170 else
2171 {
2172 status = FAIL;
2173 semsg(_(e_cannot_index_str),
2174 vartype_name(tv_dest->v_type));
2175 }
2176
2177 clear_tv(tv_idx);
2178 clear_tv(tv_dest);
2179 ectx.ec_stack.ga_len -= 2;
2180 if (status == FAIL)
2181 goto on_error;
2182 }
2183 break;
2184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002185 // push constant
2186 case ISN_PUSHNR:
2187 case ISN_PUSHBOOL:
2188 case ISN_PUSHSPEC:
2189 case ISN_PUSHF:
2190 case ISN_PUSHS:
2191 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002192 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002193 case ISN_PUSHCHANNEL:
2194 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002195 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002196 goto failed;
2197 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002198 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002199 ++ectx.ec_stack.ga_len;
2200 switch (iptr->isn_type)
2201 {
2202 case ISN_PUSHNR:
2203 tv->v_type = VAR_NUMBER;
2204 tv->vval.v_number = iptr->isn_arg.number;
2205 break;
2206 case ISN_PUSHBOOL:
2207 tv->v_type = VAR_BOOL;
2208 tv->vval.v_number = iptr->isn_arg.number;
2209 break;
2210 case ISN_PUSHSPEC:
2211 tv->v_type = VAR_SPECIAL;
2212 tv->vval.v_number = iptr->isn_arg.number;
2213 break;
2214#ifdef FEAT_FLOAT
2215 case ISN_PUSHF:
2216 tv->v_type = VAR_FLOAT;
2217 tv->vval.v_float = iptr->isn_arg.fnumber;
2218 break;
2219#endif
2220 case ISN_PUSHBLOB:
2221 blob_copy(iptr->isn_arg.blob, tv);
2222 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002223 case ISN_PUSHFUNC:
2224 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002225 if (iptr->isn_arg.string == NULL)
2226 tv->vval.v_string = NULL;
2227 else
2228 tv->vval.v_string =
2229 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002230 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002231 case ISN_PUSHCHANNEL:
2232#ifdef FEAT_JOB_CHANNEL
2233 tv->v_type = VAR_CHANNEL;
2234 tv->vval.v_channel = iptr->isn_arg.channel;
2235 if (tv->vval.v_channel != NULL)
2236 ++tv->vval.v_channel->ch_refcount;
2237#endif
2238 break;
2239 case ISN_PUSHJOB:
2240#ifdef FEAT_JOB_CHANNEL
2241 tv->v_type = VAR_JOB;
2242 tv->vval.v_job = iptr->isn_arg.job;
2243 if (tv->vval.v_job != NULL)
2244 ++tv->vval.v_job->jv_refcount;
2245#endif
2246 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002247 default:
2248 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002249 tv->vval.v_string = vim_strsave(
2250 iptr->isn_arg.string == NULL
2251 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002252 }
2253 break;
2254
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002255 case ISN_UNLET:
2256 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2257 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002258 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002259 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002260 case ISN_UNLETENV:
2261 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2262 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002263
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002264 case ISN_LOCKCONST:
2265 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2266 break;
2267
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002268 // create a list from items on the stack; uses a single allocation
2269 // for the list header and the items
2270 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002271 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2272 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002273 break;
2274
2275 // create a dict from items on the stack
2276 case ISN_NEWDICT:
2277 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002278 int count = iptr->isn_arg.number;
2279 dict_T *dict = dict_alloc();
2280 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002281 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002282
2283 if (dict == NULL)
2284 goto failed;
2285 for (idx = 0; idx < count; ++idx)
2286 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002287 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002288 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002289 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002290 key = tv->vval.v_string == NULL
2291 ? (char_u *)"" : tv->vval.v_string;
2292 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002293 if (item != NULL)
2294 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002295 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002296 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002297 dict_unref(dict);
2298 goto on_error;
2299 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002300 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 clear_tv(tv);
2302 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002303 {
2304 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002305 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002306 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002307 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2308 item->di_tv.v_lock = 0;
2309 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002310 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002311 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002312 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002314 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002315 }
2316
2317 if (count > 0)
2318 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002319 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002320 goto failed;
2321 else
2322 ++ectx.ec_stack.ga_len;
2323 tv = STACK_TV_BOT(-1);
2324 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002325 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002326 tv->vval.v_dict = dict;
2327 ++dict->dv_refcount;
2328 }
2329 break;
2330
2331 // call a :def function
2332 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002333 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002334 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 iptr->isn_arg.dfunc.cdf_argcount,
2336 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002337 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002338 break;
2339
2340 // call a builtin function
2341 case ISN_BCALL:
2342 SOURCING_LNUM = iptr->isn_lnum;
2343 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2344 iptr->isn_arg.bfunc.cbf_argcount,
2345 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002346 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002347 break;
2348
2349 // call a funcref or partial
2350 case ISN_PCALL:
2351 {
2352 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2353 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002354 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002355
2356 SOURCING_LNUM = iptr->isn_lnum;
2357 if (pfunc->cpf_top)
2358 {
2359 // funcref is above the arguments
2360 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2361 }
2362 else
2363 {
2364 // Get the funcref from the stack.
2365 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002366 partial_tv = *STACK_TV_BOT(0);
2367 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002368 }
2369 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002370 if (tv == &partial_tv)
2371 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002372 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002373 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002374 }
2375 break;
2376
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002377 case ISN_PCALL_END:
2378 // PCALL finished, arguments have been consumed and replaced by
2379 // the return value. Now clear the funcref from the stack,
2380 // and move the return value in its place.
2381 --ectx.ec_stack.ga_len;
2382 clear_tv(STACK_TV_BOT(-1));
2383 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2384 break;
2385
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002386 // call a user defined function or funcref/partial
2387 case ISN_UCALL:
2388 {
2389 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2390
2391 SOURCING_LNUM = iptr->isn_lnum;
2392 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002393 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002394 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002395 }
2396 break;
2397
2398 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002399 case ISN_RETURN_ZERO:
2400 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2401 goto failed;
2402 tv = STACK_TV_BOT(0);
2403 ++ectx.ec_stack.ga_len;
2404 tv->v_type = VAR_NUMBER;
2405 tv->vval.v_number = 0;
2406 tv->v_lock = 0;
2407 // FALLTHROUGH
2408
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 case ISN_RETURN:
2410 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002411 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002412 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002413
2414 if (trystack->ga_len > 0)
2415 trycmd = ((trycmd_T *)trystack->ga_data)
2416 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002417 if (trycmd != NULL
2418 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002419 && trycmd->tcd_finally_idx != 0)
2420 {
2421 // jump to ":finally"
2422 ectx.ec_iidx = trycmd->tcd_finally_idx;
2423 trycmd->tcd_return = TRUE;
2424 }
2425 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002426 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002427 }
2428 break;
2429
2430 // push a function reference to a compiled function
2431 case ISN_FUNCREF:
2432 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002433 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2434 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2435 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 if (pt == NULL)
2438 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002439 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002440 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002441 vim_free(pt);
2442 goto failed;
2443 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002444 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2445 &ectx) == FAIL)
2446 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002447
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 tv = STACK_TV_BOT(0);
2449 ++ectx.ec_stack.ga_len;
2450 tv->vval.v_partial = pt;
2451 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002452 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002453 }
2454 break;
2455
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002456 // Create a global function from a lambda.
2457 case ISN_NEWFUNC:
2458 {
2459 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2460
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002461 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002462 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002463 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002464 }
2465 break;
2466
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002467 // List functions
2468 case ISN_DEF:
2469 if (iptr->isn_arg.string == NULL)
2470 list_functions(NULL);
2471 else
2472 {
2473 exarg_T ea;
2474
2475 CLEAR_FIELD(ea);
2476 ea.cmd = ea.arg = iptr->isn_arg.string;
2477 define_function(&ea, NULL);
2478 }
2479 break;
2480
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002481 // jump if a condition is met
2482 case ISN_JUMP:
2483 {
2484 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002485 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002486 int jump = TRUE;
2487
2488 if (when != JUMP_ALWAYS)
2489 {
2490 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002491 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002492 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002493 || when == JUMP_IF_COND_TRUE)
2494 {
2495 SOURCING_LNUM = iptr->isn_lnum;
2496 jump = tv_get_bool_chk(tv, &error);
2497 if (error)
2498 goto on_error;
2499 }
2500 else
2501 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002503 || when == JUMP_AND_KEEP_IF_FALSE
2504 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002505 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002506 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002507 {
2508 // drop the value from the stack
2509 clear_tv(tv);
2510 --ectx.ec_stack.ga_len;
2511 }
2512 }
2513 if (jump)
2514 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2515 }
2516 break;
2517
2518 // top of a for loop
2519 case ISN_FOR:
2520 {
2521 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2522 typval_T *idxtv =
2523 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2524
2525 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002526 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002527 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002528 ++idxtv->vval.v_number;
2529 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002530 // past the end of the list, jump to "endfor"
2531 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2532 else if (list->lv_first == &range_list_item)
2533 {
2534 // non-materialized range() list
2535 tv = STACK_TV_BOT(0);
2536 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002537 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538 tv->vval.v_number = list_find_nr(
2539 list, idxtv->vval.v_number, NULL);
2540 ++ectx.ec_stack.ga_len;
2541 }
2542 else
2543 {
2544 listitem_T *li = list_find(list, idxtv->vval.v_number);
2545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002546 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2547 ++ectx.ec_stack.ga_len;
2548 }
2549 }
2550 break;
2551
2552 // start of ":try" block
2553 case ISN_TRY:
2554 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002555 trycmd_T *trycmd = NULL;
2556
Bram Moolenaar270d0382020-05-15 21:42:53 +02002557 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002558 goto failed;
2559 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2560 + ectx.ec_trystack.ga_len;
2561 ++ectx.ec_trystack.ga_len;
2562 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002563 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002564 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2565 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002566 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002567 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002568 }
2569 break;
2570
2571 case ISN_PUSHEXC:
2572 if (current_exception == NULL)
2573 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002574 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 iemsg("Evaluating catch while current_exception is NULL");
2576 goto failed;
2577 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002578 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002579 goto failed;
2580 tv = STACK_TV_BOT(0);
2581 ++ectx.ec_stack.ga_len;
2582 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002583 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002584 tv->vval.v_string = vim_strsave(
2585 (char_u *)current_exception->value);
2586 break;
2587
2588 case ISN_CATCH:
2589 {
2590 garray_T *trystack = &ectx.ec_trystack;
2591
Bram Moolenaar20a76292020-12-25 19:47:24 +01002592 if (restore_cmdmod)
2593 {
2594 cmdmod.cmod_filter_regmatch.regprog = NULL;
2595 undo_cmdmod(&cmdmod);
2596 cmdmod = save_cmdmod;
2597 restore_cmdmod = FALSE;
2598 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002599 if (trystack->ga_len > 0)
2600 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002601 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002602 + trystack->ga_len - 1;
2603 trycmd->tcd_caught = TRUE;
2604 }
2605 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002606 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 catch_exception(current_exception);
2608 }
2609 break;
2610
2611 // end of ":try" block
2612 case ISN_ENDTRY:
2613 {
2614 garray_T *trystack = &ectx.ec_trystack;
2615
2616 if (trystack->ga_len > 0)
2617 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002618 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002619
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002620 --trystack->ga_len;
2621 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002622 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002623 trycmd = ((trycmd_T *)trystack->ga_data)
2624 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002625 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002626 {
2627 // discard the exception
2628 if (caught_stack == current_exception)
2629 caught_stack = caught_stack->caught;
2630 discard_current_exception();
2631 }
2632
2633 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002634 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 }
2636 }
2637 break;
2638
2639 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002640 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002641 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002642
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002643 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2644 {
2645 // throwing an exception while using "silent!" causes
2646 // the function to abort but not display an error.
2647 tv = STACK_TV_BOT(-1);
2648 clear_tv(tv);
2649 tv->v_type = VAR_NUMBER;
2650 tv->vval.v_number = 0;
2651 goto done;
2652 }
2653 --ectx.ec_stack.ga_len;
2654 tv = STACK_TV_BOT(0);
2655 if (tv->vval.v_string == NULL
2656 || *skipwhite(tv->vval.v_string) == NUL)
2657 {
2658 vim_free(tv->vval.v_string);
2659 SOURCING_LNUM = iptr->isn_lnum;
2660 emsg(_(e_throw_with_empty_string));
2661 goto failed;
2662 }
2663
2664 // Inside a "catch" we need to first discard the caught
2665 // exception.
2666 if (trystack->ga_len > 0)
2667 {
2668 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2669 + trystack->ga_len - 1;
2670 if (trycmd->tcd_caught && current_exception != NULL)
2671 {
2672 // discard the exception
2673 if (caught_stack == current_exception)
2674 caught_stack = caught_stack->caught;
2675 discard_current_exception();
2676 trycmd->tcd_caught = FALSE;
2677 }
2678 }
2679
2680 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2681 == FAIL)
2682 {
2683 vim_free(tv->vval.v_string);
2684 goto failed;
2685 }
2686 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 break;
2689
2690 // compare with special values
2691 case ISN_COMPAREBOOL:
2692 case ISN_COMPARESPECIAL:
2693 {
2694 typval_T *tv1 = STACK_TV_BOT(-2);
2695 typval_T *tv2 = STACK_TV_BOT(-1);
2696 varnumber_T arg1 = tv1->vval.v_number;
2697 varnumber_T arg2 = tv2->vval.v_number;
2698 int res;
2699
2700 switch (iptr->isn_arg.op.op_type)
2701 {
2702 case EXPR_EQUAL: res = arg1 == arg2; break;
2703 case EXPR_NEQUAL: res = arg1 != arg2; break;
2704 default: res = 0; break;
2705 }
2706
2707 --ectx.ec_stack.ga_len;
2708 tv1->v_type = VAR_BOOL;
2709 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2710 }
2711 break;
2712
2713 // Operation with two number arguments
2714 case ISN_OPNR:
2715 case ISN_COMPARENR:
2716 {
2717 typval_T *tv1 = STACK_TV_BOT(-2);
2718 typval_T *tv2 = STACK_TV_BOT(-1);
2719 varnumber_T arg1 = tv1->vval.v_number;
2720 varnumber_T arg2 = tv2->vval.v_number;
2721 varnumber_T res;
2722
2723 switch (iptr->isn_arg.op.op_type)
2724 {
2725 case EXPR_MULT: res = arg1 * arg2; break;
2726 case EXPR_DIV: res = arg1 / arg2; break;
2727 case EXPR_REM: res = arg1 % arg2; break;
2728 case EXPR_SUB: res = arg1 - arg2; break;
2729 case EXPR_ADD: res = arg1 + arg2; break;
2730
2731 case EXPR_EQUAL: res = arg1 == arg2; break;
2732 case EXPR_NEQUAL: res = arg1 != arg2; break;
2733 case EXPR_GREATER: res = arg1 > arg2; break;
2734 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2735 case EXPR_SMALLER: res = arg1 < arg2; break;
2736 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2737 default: res = 0; break;
2738 }
2739
2740 --ectx.ec_stack.ga_len;
2741 if (iptr->isn_type == ISN_COMPARENR)
2742 {
2743 tv1->v_type = VAR_BOOL;
2744 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2745 }
2746 else
2747 tv1->vval.v_number = res;
2748 }
2749 break;
2750
2751 // Computation with two float arguments
2752 case ISN_OPFLOAT:
2753 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002754#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 {
2756 typval_T *tv1 = STACK_TV_BOT(-2);
2757 typval_T *tv2 = STACK_TV_BOT(-1);
2758 float_T arg1 = tv1->vval.v_float;
2759 float_T arg2 = tv2->vval.v_float;
2760 float_T res = 0;
2761 int cmp = FALSE;
2762
2763 switch (iptr->isn_arg.op.op_type)
2764 {
2765 case EXPR_MULT: res = arg1 * arg2; break;
2766 case EXPR_DIV: res = arg1 / arg2; break;
2767 case EXPR_SUB: res = arg1 - arg2; break;
2768 case EXPR_ADD: res = arg1 + arg2; break;
2769
2770 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2771 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2772 case EXPR_GREATER: cmp = arg1 > arg2; break;
2773 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2774 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2775 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2776 default: cmp = 0; break;
2777 }
2778 --ectx.ec_stack.ga_len;
2779 if (iptr->isn_type == ISN_COMPAREFLOAT)
2780 {
2781 tv1->v_type = VAR_BOOL;
2782 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2783 }
2784 else
2785 tv1->vval.v_float = res;
2786 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002787#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002788 break;
2789
2790 case ISN_COMPARELIST:
2791 {
2792 typval_T *tv1 = STACK_TV_BOT(-2);
2793 typval_T *tv2 = STACK_TV_BOT(-1);
2794 list_T *arg1 = tv1->vval.v_list;
2795 list_T *arg2 = tv2->vval.v_list;
2796 int cmp = FALSE;
2797 int ic = iptr->isn_arg.op.op_ic;
2798
2799 switch (iptr->isn_arg.op.op_type)
2800 {
2801 case EXPR_EQUAL: cmp =
2802 list_equal(arg1, arg2, ic, FALSE); break;
2803 case EXPR_NEQUAL: cmp =
2804 !list_equal(arg1, arg2, ic, FALSE); break;
2805 case EXPR_IS: cmp = arg1 == arg2; break;
2806 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2807 default: cmp = 0; break;
2808 }
2809 --ectx.ec_stack.ga_len;
2810 clear_tv(tv1);
2811 clear_tv(tv2);
2812 tv1->v_type = VAR_BOOL;
2813 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2814 }
2815 break;
2816
2817 case ISN_COMPAREBLOB:
2818 {
2819 typval_T *tv1 = STACK_TV_BOT(-2);
2820 typval_T *tv2 = STACK_TV_BOT(-1);
2821 blob_T *arg1 = tv1->vval.v_blob;
2822 blob_T *arg2 = tv2->vval.v_blob;
2823 int cmp = FALSE;
2824
2825 switch (iptr->isn_arg.op.op_type)
2826 {
2827 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2828 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2829 case EXPR_IS: cmp = arg1 == arg2; break;
2830 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2831 default: cmp = 0; break;
2832 }
2833 --ectx.ec_stack.ga_len;
2834 clear_tv(tv1);
2835 clear_tv(tv2);
2836 tv1->v_type = VAR_BOOL;
2837 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2838 }
2839 break;
2840
2841 // TODO: handle separately
2842 case ISN_COMPARESTRING:
2843 case ISN_COMPAREDICT:
2844 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002845 case ISN_COMPAREANY:
2846 {
2847 typval_T *tv1 = STACK_TV_BOT(-2);
2848 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002849 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002850 int ic = iptr->isn_arg.op.op_ic;
2851
Bram Moolenaareb26f432020-09-14 16:50:05 +02002852 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002853 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002854 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002855 --ectx.ec_stack.ga_len;
2856 }
2857 break;
2858
2859 case ISN_ADDLIST:
2860 case ISN_ADDBLOB:
2861 {
2862 typval_T *tv1 = STACK_TV_BOT(-2);
2863 typval_T *tv2 = STACK_TV_BOT(-1);
2864
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002865 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002866 if (iptr->isn_type == ISN_ADDLIST)
2867 eval_addlist(tv1, tv2);
2868 else
2869 eval_addblob(tv1, tv2);
2870 clear_tv(tv2);
2871 --ectx.ec_stack.ga_len;
2872 }
2873 break;
2874
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002875 case ISN_LISTAPPEND:
2876 {
2877 typval_T *tv1 = STACK_TV_BOT(-2);
2878 typval_T *tv2 = STACK_TV_BOT(-1);
2879 list_T *l = tv1->vval.v_list;
2880
2881 // add an item to a list
2882 if (l == NULL)
2883 {
2884 SOURCING_LNUM = iptr->isn_lnum;
2885 emsg(_(e_cannot_add_to_null_list));
2886 goto on_error;
2887 }
2888 if (list_append_tv(l, tv2) == FAIL)
2889 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002890 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002891 --ectx.ec_stack.ga_len;
2892 }
2893 break;
2894
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002895 case ISN_BLOBAPPEND:
2896 {
2897 typval_T *tv1 = STACK_TV_BOT(-2);
2898 typval_T *tv2 = STACK_TV_BOT(-1);
2899 blob_T *b = tv1->vval.v_blob;
2900 int error = FALSE;
2901 varnumber_T n;
2902
2903 // add a number to a blob
2904 if (b == NULL)
2905 {
2906 SOURCING_LNUM = iptr->isn_lnum;
2907 emsg(_(e_cannot_add_to_null_blob));
2908 goto on_error;
2909 }
2910 n = tv_get_number_chk(tv2, &error);
2911 if (error)
2912 goto on_error;
2913 ga_append(&b->bv_ga, (int)n);
2914 --ectx.ec_stack.ga_len;
2915 }
2916 break;
2917
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002918 // Computation with two arguments of unknown type
2919 case ISN_OPANY:
2920 {
2921 typval_T *tv1 = STACK_TV_BOT(-2);
2922 typval_T *tv2 = STACK_TV_BOT(-1);
2923 varnumber_T n1, n2;
2924#ifdef FEAT_FLOAT
2925 float_T f1 = 0, f2 = 0;
2926#endif
2927 int error = FALSE;
2928
2929 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2930 {
2931 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2932 {
2933 eval_addlist(tv1, tv2);
2934 clear_tv(tv2);
2935 --ectx.ec_stack.ga_len;
2936 break;
2937 }
2938 else if (tv1->v_type == VAR_BLOB
2939 && tv2->v_type == VAR_BLOB)
2940 {
2941 eval_addblob(tv1, tv2);
2942 clear_tv(tv2);
2943 --ectx.ec_stack.ga_len;
2944 break;
2945 }
2946 }
2947#ifdef FEAT_FLOAT
2948 if (tv1->v_type == VAR_FLOAT)
2949 {
2950 f1 = tv1->vval.v_float;
2951 n1 = 0;
2952 }
2953 else
2954#endif
2955 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002956 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002957 n1 = tv_get_number_chk(tv1, &error);
2958 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002959 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002960#ifdef FEAT_FLOAT
2961 if (tv2->v_type == VAR_FLOAT)
2962 f1 = n1;
2963#endif
2964 }
2965#ifdef FEAT_FLOAT
2966 if (tv2->v_type == VAR_FLOAT)
2967 {
2968 f2 = tv2->vval.v_float;
2969 n2 = 0;
2970 }
2971 else
2972#endif
2973 {
2974 n2 = tv_get_number_chk(tv2, &error);
2975 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002976 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002977#ifdef FEAT_FLOAT
2978 if (tv1->v_type == VAR_FLOAT)
2979 f2 = n2;
2980#endif
2981 }
2982#ifdef FEAT_FLOAT
2983 // if there is a float on either side the result is a float
2984 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2985 {
2986 switch (iptr->isn_arg.op.op_type)
2987 {
2988 case EXPR_MULT: f1 = f1 * f2; break;
2989 case EXPR_DIV: f1 = f1 / f2; break;
2990 case EXPR_SUB: f1 = f1 - f2; break;
2991 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002992 default: SOURCING_LNUM = iptr->isn_lnum;
2993 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02002994 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002995 }
2996 clear_tv(tv1);
2997 clear_tv(tv2);
2998 tv1->v_type = VAR_FLOAT;
2999 tv1->vval.v_float = f1;
3000 --ectx.ec_stack.ga_len;
3001 }
3002 else
3003#endif
3004 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003005 int failed = FALSE;
3006
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003007 switch (iptr->isn_arg.op.op_type)
3008 {
3009 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003010 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3011 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003012 goto on_error;
3013 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003014 case EXPR_SUB: n1 = n1 - n2; break;
3015 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003016 default: n1 = num_modulus(n1, n2, &failed);
3017 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003018 goto on_error;
3019 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 }
3021 clear_tv(tv1);
3022 clear_tv(tv2);
3023 tv1->v_type = VAR_NUMBER;
3024 tv1->vval.v_number = n1;
3025 --ectx.ec_stack.ga_len;
3026 }
3027 }
3028 break;
3029
3030 case ISN_CONCAT:
3031 {
3032 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3033 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3034 char_u *res;
3035
3036 res = concat_str(str1, str2);
3037 clear_tv(STACK_TV_BOT(-2));
3038 clear_tv(STACK_TV_BOT(-1));
3039 --ectx.ec_stack.ga_len;
3040 STACK_TV_BOT(-1)->vval.v_string = res;
3041 }
3042 break;
3043
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003044 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003045 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003046 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003047 int is_slice = iptr->isn_type == ISN_STRSLICE;
3048 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003049 char_u *res;
3050
3051 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003052 // string slice: string is at stack-3, first index at
3053 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003054 if (is_slice)
3055 {
3056 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003057 n1 = tv->vval.v_number;
3058 }
3059
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003060 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003061 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003062
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003063 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003064 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003065 if (is_slice)
3066 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003067 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003068 else
3069 // Index: The resulting variable is a string of a
3070 // single character. If the index is too big or
3071 // negative the result is empty.
3072 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003073 vim_free(tv->vval.v_string);
3074 tv->vval.v_string = res;
3075 }
3076 break;
3077
3078 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003079 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003080 {
Bram Moolenaared591872020-08-15 22:14:53 +02003081 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003082 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003083 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003084
3085 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003086 // list slice: list is at stack-3, indexes at stack-2 and
3087 // stack-1
3088 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003089 list = tv->vval.v_list;
3090
3091 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003092 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003093 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003094
3095 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003096 {
Bram Moolenaared591872020-08-15 22:14:53 +02003097 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003098 n1 = tv->vval.v_number;
3099 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003100 }
Bram Moolenaared591872020-08-15 22:14:53 +02003101
3102 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003103 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003104 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003105 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3106 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003107 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 }
3109 break;
3110
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003111 case ISN_ANYINDEX:
3112 case ISN_ANYSLICE:
3113 {
3114 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3115 typval_T *var1, *var2;
3116 int res;
3117
3118 // index: composite is at stack-2, index at stack-1
3119 // slice: composite is at stack-3, indexes at stack-2 and
3120 // stack-1
3121 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003122 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003123 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3124 goto on_error;
3125 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3126 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003127 res = eval_index_inner(tv, is_slice, var1, var2,
3128 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003129 clear_tv(var1);
3130 if (is_slice)
3131 clear_tv(var2);
3132 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3133 if (res == FAIL)
3134 goto on_error;
3135 }
3136 break;
3137
Bram Moolenaar9af78762020-06-16 11:34:42 +02003138 case ISN_SLICE:
3139 {
3140 list_T *list;
3141 int count = iptr->isn_arg.number;
3142
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003143 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003144 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003145 list = tv->vval.v_list;
3146
3147 // no error for short list, expect it to be checked earlier
3148 if (list != NULL && list->lv_len >= count)
3149 {
3150 list_T *newlist = list_slice(list,
3151 count, list->lv_len - 1);
3152
3153 if (newlist != NULL)
3154 {
3155 list_unref(list);
3156 tv->vval.v_list = newlist;
3157 ++newlist->lv_refcount;
3158 }
3159 }
3160 }
3161 break;
3162
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003163 case ISN_GETITEM:
3164 {
3165 listitem_T *li;
3166 int index = iptr->isn_arg.number;
3167
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003168 // Get list item: list is at stack-1, push item.
3169 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003170 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003171 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003172
3173 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3174 goto failed;
3175 ++ectx.ec_stack.ga_len;
3176 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003177
3178 // Useful when used in unpack assignment. Reset at
3179 // ISN_DROP.
3180 where.wt_index = index + 1;
3181 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003182 }
3183 break;
3184
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003185 case ISN_MEMBER:
3186 {
3187 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003188 char_u *key;
3189 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003190 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003191
3192 // dict member: dict is at stack-2, key at stack-1
3193 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003194 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003195 dict = tv->vval.v_dict;
3196
3197 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003198 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003199 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003200 if (key == NULL)
3201 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003202
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003203 if ((di = dict_find(dict, key, -1)) == NULL)
3204 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003205 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003206 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003207
3208 // If :silent! is used we will continue, make sure the
3209 // stack contents makes sense.
3210 clear_tv(tv);
3211 --ectx.ec_stack.ga_len;
3212 tv = STACK_TV_BOT(-1);
3213 clear_tv(tv);
3214 tv->v_type = VAR_NUMBER;
3215 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003216 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003217 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003218 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003219 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003220 // Clear the dict only after getting the item, to avoid
3221 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003222 tv = STACK_TV_BOT(-1);
3223 temp_tv = *tv;
3224 copy_tv(&di->di_tv, tv);
3225 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003226 }
3227 break;
3228
3229 // dict member with string key
3230 case ISN_STRINGMEMBER:
3231 {
3232 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003233 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003234 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003235
3236 tv = STACK_TV_BOT(-1);
3237 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3238 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003239 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003240 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003241 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003242 }
3243 dict = tv->vval.v_dict;
3244
3245 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3246 == NULL)
3247 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003248 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003249 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003250 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003251 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003252 // Clear the dict after getting the item, to avoid that it
3253 // make the item invalid.
3254 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003255 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003256 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 }
3258 break;
3259
3260 case ISN_NEGATENR:
3261 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003262 if (tv->v_type != VAR_NUMBER
3263#ifdef FEAT_FLOAT
3264 && tv->v_type != VAR_FLOAT
3265#endif
3266 )
3267 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003268 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003269 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003270 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003271 }
3272#ifdef FEAT_FLOAT
3273 if (tv->v_type == VAR_FLOAT)
3274 tv->vval.v_float = -tv->vval.v_float;
3275 else
3276#endif
3277 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 break;
3279
3280 case ISN_CHECKNR:
3281 {
3282 int error = FALSE;
3283
3284 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003285 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003287 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003288 (void)tv_get_number_chk(tv, &error);
3289 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003290 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003291 }
3292 break;
3293
3294 case ISN_CHECKTYPE:
3295 {
3296 checktype_T *ct = &iptr->isn_arg.type;
3297
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003298 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003300 if (!where.wt_variable)
3301 where.wt_index = ct->ct_arg_idx;
3302 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003303 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003304 if (!where.wt_variable)
3305 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003306
3307 // number 0 is FALSE, number 1 is TRUE
3308 if (tv->v_type == VAR_NUMBER
3309 && ct->ct_type->tt_type == VAR_BOOL
3310 && (tv->vval.v_number == 0
3311 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003312 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003313 tv->v_type = VAR_BOOL;
3314 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003315 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003316 }
3317 }
3318 break;
3319
Bram Moolenaar9af78762020-06-16 11:34:42 +02003320 case ISN_CHECKLEN:
3321 {
3322 int min_len = iptr->isn_arg.checklen.cl_min_len;
3323 list_T *list = NULL;
3324
3325 tv = STACK_TV_BOT(-1);
3326 if (tv->v_type == VAR_LIST)
3327 list = tv->vval.v_list;
3328 if (list == NULL || list->lv_len < min_len
3329 || (list->lv_len > min_len
3330 && !iptr->isn_arg.checklen.cl_more_OK))
3331 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003332 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003333 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003334 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003335 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003336 }
3337 }
3338 break;
3339
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003340 case ISN_SETTYPE:
3341 {
3342 checktype_T *ct = &iptr->isn_arg.type;
3343
3344 tv = STACK_TV_BOT(-1);
3345 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3346 {
3347 free_type(tv->vval.v_dict->dv_type);
3348 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3349 }
3350 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3351 {
3352 free_type(tv->vval.v_list->lv_type);
3353 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3354 }
3355 }
3356 break;
3357
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003358 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003359 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003360 {
3361 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003362 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003363
3364 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003365 if (iptr->isn_type == ISN_2BOOL)
3366 {
3367 n = tv2bool(tv);
3368 if (iptr->isn_arg.number) // invert
3369 n = !n;
3370 }
3371 else
3372 {
3373 SOURCING_LNUM = iptr->isn_lnum;
3374 n = tv_get_bool_chk(tv, &error);
3375 if (error)
3376 goto on_error;
3377 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 clear_tv(tv);
3379 tv->v_type = VAR_BOOL;
3380 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3381 }
3382 break;
3383
3384 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003385 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003386 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003387 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3388 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3389 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003390 break;
3391
Bram Moolenaar08597872020-12-10 19:43:40 +01003392 case ISN_RANGE:
3393 {
3394 exarg_T ea;
3395 char *errormsg;
3396
Bram Moolenaarece0b872021-01-08 20:40:45 +01003397 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003398 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003399 ea.addr_type = ADDR_LINES;
3400 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003401 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003402 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003403 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003404
3405 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3406 goto failed;
3407 ++ectx.ec_stack.ga_len;
3408 tv = STACK_TV_BOT(-1);
3409 tv->v_type = VAR_NUMBER;
3410 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003411 if (ea.addr_count == 0)
3412 tv->vval.v_number = curwin->w_cursor.lnum;
3413 else
3414 tv->vval.v_number = ea.line2;
3415 }
3416 break;
3417
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003418 case ISN_PUT:
3419 {
3420 int regname = iptr->isn_arg.put.put_regname;
3421 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3422 char_u *expr = NULL;
3423 int dir = FORWARD;
3424
Bram Moolenaar08597872020-12-10 19:43:40 +01003425 if (lnum < -2)
3426 {
3427 // line number was put on the stack by ISN_RANGE
3428 tv = STACK_TV_BOT(-1);
3429 curwin->w_cursor.lnum = tv->vval.v_number;
3430 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3431 dir = BACKWARD;
3432 --ectx.ec_stack.ga_len;
3433 }
3434 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003435 // :put! above cursor
3436 dir = BACKWARD;
3437 else if (lnum >= 0)
3438 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003439
3440 if (regname == '=')
3441 {
3442 tv = STACK_TV_BOT(-1);
3443 if (tv->v_type == VAR_STRING)
3444 expr = tv->vval.v_string;
3445 else
3446 {
3447 expr = typval2string(tv, TRUE); // allocates value
3448 clear_tv(tv);
3449 }
3450 --ectx.ec_stack.ga_len;
3451 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003452 check_cursor();
3453 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3454 vim_free(expr);
3455 }
3456 break;
3457
Bram Moolenaar02194d22020-10-24 23:08:38 +02003458 case ISN_CMDMOD:
3459 save_cmdmod = cmdmod;
3460 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003461 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003462 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3463 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003464 break;
3465
Bram Moolenaar02194d22020-10-24 23:08:38 +02003466 case ISN_CMDMOD_REV:
3467 // filter regprog is owned by the instruction, don't free it
3468 cmdmod.cmod_filter_regmatch.regprog = NULL;
3469 undo_cmdmod(&cmdmod);
3470 cmdmod = save_cmdmod;
3471 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003472 break;
3473
Bram Moolenaar792f7862020-11-23 08:31:18 +01003474 case ISN_UNPACK:
3475 {
3476 int count = iptr->isn_arg.unpack.unp_count;
3477 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3478 list_T *l;
3479 listitem_T *li;
3480 int i;
3481
3482 // Check there is a valid list to unpack.
3483 tv = STACK_TV_BOT(-1);
3484 if (tv->v_type != VAR_LIST)
3485 {
3486 SOURCING_LNUM = iptr->isn_lnum;
3487 emsg(_(e_for_argument_must_be_sequence_of_lists));
3488 goto on_error;
3489 }
3490 l = tv->vval.v_list;
3491 if (l == NULL
3492 || l->lv_len < (semicolon ? count - 1 : count))
3493 {
3494 SOURCING_LNUM = iptr->isn_lnum;
3495 emsg(_(e_list_value_does_not_have_enough_items));
3496 goto on_error;
3497 }
3498 else if (!semicolon && l->lv_len > count)
3499 {
3500 SOURCING_LNUM = iptr->isn_lnum;
3501 emsg(_(e_list_value_has_more_items_than_targets));
3502 goto on_error;
3503 }
3504
3505 CHECK_LIST_MATERIALIZE(l);
3506 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3507 goto failed;
3508 ectx.ec_stack.ga_len += count - 1;
3509
3510 // Variable after semicolon gets a list with the remaining
3511 // items.
3512 if (semicolon)
3513 {
3514 list_T *rem_list =
3515 list_alloc_with_items(l->lv_len - count + 1);
3516
3517 if (rem_list == NULL)
3518 goto failed;
3519 tv = STACK_TV_BOT(-count);
3520 tv->vval.v_list = rem_list;
3521 ++rem_list->lv_refcount;
3522 tv->v_lock = 0;
3523 li = l->lv_first;
3524 for (i = 0; i < count - 1; ++i)
3525 li = li->li_next;
3526 for (i = 0; li != NULL; ++i)
3527 {
3528 list_set_item(rem_list, i, &li->li_tv);
3529 li = li->li_next;
3530 }
3531 --count;
3532 }
3533
3534 // Produce the values in reverse order, first item last.
3535 li = l->lv_first;
3536 for (i = 0; i < count; ++i)
3537 {
3538 tv = STACK_TV_BOT(-i - 1);
3539 copy_tv(&li->li_tv, tv);
3540 li = li->li_next;
3541 }
3542
3543 list_unref(l);
3544 }
3545 break;
3546
Bram Moolenaarb2049902021-01-24 12:53:53 +01003547 case ISN_PROF_START:
3548 case ISN_PROF_END:
3549 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003550#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003551 funccall_T cookie;
3552 ufunc_T *cur_ufunc =
3553 (((dfunc_T *)def_functions.ga_data)
3554 + ectx.ec_dfunc_idx)->df_ufunc;
3555
3556 cookie.func = cur_ufunc;
3557 if (iptr->isn_type == ISN_PROF_START)
3558 {
3559 func_line_start(&cookie, iptr->isn_lnum);
3560 // if we get here the instruction is executed
3561 func_line_exec(&cookie);
3562 }
3563 else
3564 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003565#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003566 }
3567 break;
3568
Bram Moolenaar389df252020-07-09 21:20:47 +02003569 case ISN_SHUFFLE:
3570 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003571 typval_T tmp_tv;
3572 int item = iptr->isn_arg.shuffle.shfl_item;
3573 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003574
3575 tmp_tv = *STACK_TV_BOT(-item);
3576 for ( ; up > 0 && item > 1; --up)
3577 {
3578 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3579 --item;
3580 }
3581 *STACK_TV_BOT(-item) = tmp_tv;
3582 }
3583 break;
3584
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003585 case ISN_DROP:
3586 --ectx.ec_stack.ga_len;
3587 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003588 where.wt_index = 0;
3589 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003590 break;
3591 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003592 continue;
3593
Bram Moolenaard032f342020-07-18 18:13:02 +02003594func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003595 // Restore previous function. If the frame pointer is where we started
3596 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003597 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003598 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003599
Bram Moolenaard032f342020-07-18 18:13:02 +02003600 if (func_return(&ectx) == FAIL)
3601 // only fails when out of memory
3602 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003603 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003604
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003605on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003606 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003607 // If "emsg_silent" is set then ignore the error, unless it was set
3608 // when calling the function.
3609 if (did_emsg_cumul + did_emsg == did_emsg_before
3610 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003611 {
3612 // If a sequence of instructions causes an error while ":silent!"
3613 // was used, restore the stack length and jump ahead to restoring
3614 // the cmdmod.
3615 if (restore_cmdmod)
3616 {
3617 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3618 {
3619 --ectx.ec_stack.ga_len;
3620 clear_tv(STACK_TV_BOT(0));
3621 }
3622 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3623 ++ectx.ec_iidx;
3624 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003625 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003626 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003627on_fatal_error:
3628 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003629 // If we are not inside a try-catch started here, abort execution.
3630 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003631 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003632 }
3633
3634done:
3635 // function finished, get result from the stack.
3636 tv = STACK_TV_BOT(-1);
3637 *rettv = *tv;
3638 tv->v_type = VAR_UNKNOWN;
3639 ret = OK;
3640
3641failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003642 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003643 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003644 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003645
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003646 // Deal with any remaining closures, they may be in use somewhere.
3647 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003648 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003649 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003650 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3651 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003652
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003653 estack_pop();
3654 current_sctx = save_current_sctx;
3655
Bram Moolenaar352134b2020-10-17 22:04:08 +02003656 if (*msg_list != NULL && saved_msg_list != NULL)
3657 {
3658 msglist_T **plist = saved_msg_list;
3659
3660 // Append entries from the current msg_list (uncaught exceptions) to
3661 // the saved msg_list.
3662 while (*plist != NULL)
3663 plist = &(*plist)->next;
3664
3665 *plist = *msg_list;
3666 }
3667 msg_list = saved_msg_list;
3668
Bram Moolenaar02194d22020-10-24 23:08:38 +02003669 if (restore_cmdmod)
3670 {
3671 cmdmod.cmod_filter_regmatch.regprog = NULL;
3672 undo_cmdmod(&cmdmod);
3673 cmdmod = save_cmdmod;
3674 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003675 emsg_silent_def = save_emsg_silent_def;
3676 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003677
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003678failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003679 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003680 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3681 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003682
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003683 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003684 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003685
Bram Moolenaar0186e582021-01-10 18:33:11 +01003686 while (ectx.ec_outer != NULL)
3687 {
3688 outer_T *up = ectx.ec_outer->out_up_is_copy
3689 ? NULL : ectx.ec_outer->out_up;
3690
3691 vim_free(ectx.ec_outer);
3692 ectx.ec_outer = up;
3693 }
3694
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003695 // Not sure if this is necessary.
3696 suppress_errthrow = save_suppress_errthrow;
3697
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003698 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003699 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003700 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003701 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003702 return ret;
3703}
3704
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003705/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003706 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003707 * We don't really need this at runtime, but we do have tests that require it,
3708 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003709 */
3710 void
3711ex_disassemble(exarg_T *eap)
3712{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003713 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003714 char_u *fname;
3715 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003716 dfunc_T *dfunc;
3717 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003718 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003719 int current;
3720 int line_idx = 0;
3721 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003722 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003723
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003724 if (STRNCMP(arg, "<lambda>", 8) == 0)
3725 {
3726 arg += 8;
3727 (void)getdigits(&arg);
3728 fname = vim_strnsave(eap->arg, arg - eap->arg);
3729 }
3730 else
3731 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003732 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003733 if (fname == NULL)
3734 {
3735 semsg(_(e_invarg2), eap->arg);
3736 return;
3737 }
3738
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003739 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003740 if (ufunc == NULL)
3741 {
3742 char_u *p = untrans_function_name(fname);
3743
3744 if (p != NULL)
3745 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003746 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003747 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003748 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003749 if (ufunc == NULL)
3750 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003751 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003752 return;
3753 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003754 if (func_needs_compiling(ufunc, eap->forceit)
3755 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003756 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003757 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003758 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003759 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return;
3761 }
3762 if (ufunc->uf_name_exp != NULL)
3763 msg((char *)ufunc->uf_name_exp);
3764 else
3765 msg((char *)ufunc->uf_name);
3766
3767 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003768#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003769 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3770 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3771 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003772#else
3773 instr = dfunc->df_instr;
3774 instr_count = dfunc->df_instr_count;
3775#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003776 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003777 {
3778 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003779 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003780
3781 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3782 {
3783 if (current > prev_current)
3784 {
3785 msg_puts("\n\n");
3786 prev_current = current;
3787 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003788 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3789 if (line != NULL)
3790 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003791 }
3792
3793 switch (iptr->isn_type)
3794 {
3795 case ISN_EXEC:
3796 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3797 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003798 case ISN_EXECCONCAT:
3799 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003800 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003801 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003802 case ISN_ECHO:
3803 {
3804 echo_T *echo = &iptr->isn_arg.echo;
3805
3806 smsg("%4d %s %d", current,
3807 echo->echo_with_white ? "ECHO" : "ECHON",
3808 echo->echo_count);
3809 }
3810 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003811 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003812 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003813 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003814 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003815 case ISN_ECHOMSG:
3816 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003817 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003818 break;
3819 case ISN_ECHOERR:
3820 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003821 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003822 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003823 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003824 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003825 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003826 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003827 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003828 + STACK_FRAME_SIZE));
3829 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003830 smsg("%4d LOAD $%lld", current,
3831 (varnumber_T)(iptr->isn_arg.number));
3832 }
3833 break;
3834 case ISN_LOADOUTER:
3835 {
3836 if (iptr->isn_arg.number < 0)
3837 smsg("%4d LOADOUTER level %d arg[%d]", current,
3838 iptr->isn_arg.outer.outer_depth,
3839 iptr->isn_arg.outer.outer_idx
3840 + STACK_FRAME_SIZE);
3841 else
3842 smsg("%4d LOADOUTER level %d $%d", current,
3843 iptr->isn_arg.outer.outer_depth,
3844 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003845 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003846 break;
3847 case ISN_LOADV:
3848 smsg("%4d LOADV v:%s", current,
3849 get_vim_var_name(iptr->isn_arg.number));
3850 break;
3851 case ISN_LOADSCRIPT:
3852 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003853 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3854 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003855 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003856 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003858 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3859 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003860 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003861 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003862 }
3863 break;
3864 case ISN_LOADS:
3865 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003866 scriptitem_T *si = SCRIPT_ITEM(
3867 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868
3869 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003870 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003871 }
3872 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003873 case ISN_LOADAUTO:
3874 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3875 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 case ISN_LOADG:
3877 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3878 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003879 case ISN_LOADB:
3880 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3881 break;
3882 case ISN_LOADW:
3883 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3884 break;
3885 case ISN_LOADT:
3886 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3887 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003888 case ISN_LOADGDICT:
3889 smsg("%4d LOAD g:", current);
3890 break;
3891 case ISN_LOADBDICT:
3892 smsg("%4d LOAD b:", current);
3893 break;
3894 case ISN_LOADWDICT:
3895 smsg("%4d LOAD w:", current);
3896 break;
3897 case ISN_LOADTDICT:
3898 smsg("%4d LOAD t:", current);
3899 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900 case ISN_LOADOPT:
3901 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3902 break;
3903 case ISN_LOADENV:
3904 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3905 break;
3906 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003907 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 break;
3909
3910 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003911 if (iptr->isn_arg.number < 0)
3912 smsg("%4d STORE arg[%lld]", current,
3913 iptr->isn_arg.number + STACK_FRAME_SIZE);
3914 else
3915 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3916 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003917 case ISN_STOREOUTER:
3918 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003919 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003920 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3921 iptr->isn_arg.outer.outer_depth,
3922 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003923 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003924 smsg("%4d STOREOUTER level %d $%d", current,
3925 iptr->isn_arg.outer.outer_depth,
3926 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003927 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003928 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003929 case ISN_STOREV:
3930 smsg("%4d STOREV v:%s", current,
3931 get_vim_var_name(iptr->isn_arg.number));
3932 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003933 case ISN_STOREAUTO:
3934 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3935 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003937 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3938 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003939 case ISN_STOREB:
3940 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3941 break;
3942 case ISN_STOREW:
3943 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3944 break;
3945 case ISN_STORET:
3946 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3947 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003948 case ISN_STORES:
3949 {
3950 scriptitem_T *si = SCRIPT_ITEM(
3951 iptr->isn_arg.loadstore.ls_sid);
3952
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003953 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003954 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003955 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 break;
3957 case ISN_STORESCRIPT:
3958 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003959 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3960 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003961 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003962 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003963
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003964 smsg("%4d STORESCRIPT %s-%d in %s", current,
3965 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003966 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003967 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969 break;
3970 case ISN_STOREOPT:
3971 smsg("%4d STOREOPT &%s", current,
3972 iptr->isn_arg.storeopt.so_name);
3973 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003974 case ISN_STOREENV:
3975 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3976 break;
3977 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003978 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003979 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003980 case ISN_STORENR:
3981 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003982 iptr->isn_arg.storenr.stnr_val,
3983 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003984 break;
3985
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003986 case ISN_STOREINDEX:
3987 switch (iptr->isn_arg.vartype)
3988 {
3989 case VAR_LIST:
3990 smsg("%4d STORELIST", current);
3991 break;
3992 case VAR_DICT:
3993 smsg("%4d STOREDICT", current);
3994 break;
3995 case VAR_ANY:
3996 smsg("%4d STOREINDEX", current);
3997 break;
3998 default: break;
3999 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004000 break;
4001
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004002 // constants
4003 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004004 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004005 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004006 break;
4007 case ISN_PUSHBOOL:
4008 case ISN_PUSHSPEC:
4009 smsg("%4d PUSH %s", current,
4010 get_var_special_name(iptr->isn_arg.number));
4011 break;
4012 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004013#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004015#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 break;
4017 case ISN_PUSHS:
4018 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4019 break;
4020 case ISN_PUSHBLOB:
4021 {
4022 char_u *r;
4023 char_u numbuf[NUMBUFLEN];
4024 char_u *tofree;
4025
4026 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004027 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004028 vim_free(tofree);
4029 }
4030 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004031 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004032 {
4033 char *name = (char *)iptr->isn_arg.string;
4034
4035 smsg("%4d PUSHFUNC \"%s\"", current,
4036 name == NULL ? "[none]" : name);
4037 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004038 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004039 case ISN_PUSHCHANNEL:
4040#ifdef FEAT_JOB_CHANNEL
4041 {
4042 channel_T *channel = iptr->isn_arg.channel;
4043
4044 smsg("%4d PUSHCHANNEL %d", current,
4045 channel == NULL ? 0 : channel->ch_id);
4046 }
4047#endif
4048 break;
4049 case ISN_PUSHJOB:
4050#ifdef FEAT_JOB_CHANNEL
4051 {
4052 typval_T tv;
4053 char_u *name;
4054
4055 tv.v_type = VAR_JOB;
4056 tv.vval.v_job = iptr->isn_arg.job;
4057 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004058 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004059 }
4060#endif
4061 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 case ISN_PUSHEXC:
4063 smsg("%4d PUSH v:exception", current);
4064 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004065 case ISN_UNLET:
4066 smsg("%4d UNLET%s %s", current,
4067 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4068 iptr->isn_arg.unlet.ul_name);
4069 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004070 case ISN_UNLETENV:
4071 smsg("%4d UNLETENV%s $%s", current,
4072 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4073 iptr->isn_arg.unlet.ul_name);
4074 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004075 case ISN_UNLETINDEX:
4076 smsg("%4d UNLETINDEX", current);
4077 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004078 case ISN_LOCKCONST:
4079 smsg("%4d LOCKCONST", current);
4080 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004081 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004082 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004083 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004084 break;
4085 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004086 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004087 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004088 break;
4089
4090 // function call
4091 case ISN_BCALL:
4092 {
4093 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4094
4095 smsg("%4d BCALL %s(argc %d)", current,
4096 internal_func_name(cbfunc->cbf_idx),
4097 cbfunc->cbf_argcount);
4098 }
4099 break;
4100 case ISN_DCALL:
4101 {
4102 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4103 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4104 + cdfunc->cdf_idx;
4105
4106 smsg("%4d DCALL %s(argc %d)", current,
4107 df->df_ufunc->uf_name_exp != NULL
4108 ? df->df_ufunc->uf_name_exp
4109 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4110 }
4111 break;
4112 case ISN_UCALL:
4113 {
4114 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4115
4116 smsg("%4d UCALL %s(argc %d)", current,
4117 cufunc->cuf_name, cufunc->cuf_argcount);
4118 }
4119 break;
4120 case ISN_PCALL:
4121 {
4122 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4123
4124 smsg("%4d PCALL%s (argc %d)", current,
4125 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4126 }
4127 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004128 case ISN_PCALL_END:
4129 smsg("%4d PCALL end", current);
4130 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004131 case ISN_RETURN:
4132 smsg("%4d RETURN", current);
4133 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004134 case ISN_RETURN_ZERO:
4135 smsg("%4d RETURN 0", current);
4136 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004137 case ISN_FUNCREF:
4138 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004139 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004141 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004142
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004143 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004144 }
4145 break;
4146
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004147 case ISN_NEWFUNC:
4148 {
4149 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4150
4151 smsg("%4d NEWFUNC %s %s", current,
4152 newfunc->nf_lambda, newfunc->nf_global);
4153 }
4154 break;
4155
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004156 case ISN_DEF:
4157 {
4158 char_u *name = iptr->isn_arg.string;
4159
4160 smsg("%4d DEF %s", current,
4161 name == NULL ? (char_u *)"" : name);
4162 }
4163 break;
4164
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 case ISN_JUMP:
4166 {
4167 char *when = "?";
4168
4169 switch (iptr->isn_arg.jump.jump_when)
4170 {
4171 case JUMP_ALWAYS:
4172 when = "JUMP";
4173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004174 case JUMP_AND_KEEP_IF_TRUE:
4175 when = "JUMP_AND_KEEP_IF_TRUE";
4176 break;
4177 case JUMP_IF_FALSE:
4178 when = "JUMP_IF_FALSE";
4179 break;
4180 case JUMP_AND_KEEP_IF_FALSE:
4181 when = "JUMP_AND_KEEP_IF_FALSE";
4182 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004183 case JUMP_IF_COND_FALSE:
4184 when = "JUMP_IF_COND_FALSE";
4185 break;
4186 case JUMP_IF_COND_TRUE:
4187 when = "JUMP_IF_COND_TRUE";
4188 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004189 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004190 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 iptr->isn_arg.jump.jump_where);
4192 }
4193 break;
4194
4195 case ISN_FOR:
4196 {
4197 forloop_T *forloop = &iptr->isn_arg.forloop;
4198
4199 smsg("%4d FOR $%d -> %d", current,
4200 forloop->for_idx, forloop->for_end);
4201 }
4202 break;
4203
4204 case ISN_TRY:
4205 {
4206 try_T *try = &iptr->isn_arg.try;
4207
4208 smsg("%4d TRY catch -> %d, finally -> %d", current,
4209 try->try_catch, try->try_finally);
4210 }
4211 break;
4212 case ISN_CATCH:
4213 // TODO
4214 smsg("%4d CATCH", current);
4215 break;
4216 case ISN_ENDTRY:
4217 smsg("%4d ENDTRY", current);
4218 break;
4219 case ISN_THROW:
4220 smsg("%4d THROW", current);
4221 break;
4222
4223 // expression operations on number
4224 case ISN_OPNR:
4225 case ISN_OPFLOAT:
4226 case ISN_OPANY:
4227 {
4228 char *what;
4229 char *ins;
4230
4231 switch (iptr->isn_arg.op.op_type)
4232 {
4233 case EXPR_MULT: what = "*"; break;
4234 case EXPR_DIV: what = "/"; break;
4235 case EXPR_REM: what = "%"; break;
4236 case EXPR_SUB: what = "-"; break;
4237 case EXPR_ADD: what = "+"; break;
4238 default: what = "???"; break;
4239 }
4240 switch (iptr->isn_type)
4241 {
4242 case ISN_OPNR: ins = "OPNR"; break;
4243 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4244 case ISN_OPANY: ins = "OPANY"; break;
4245 default: ins = "???"; break;
4246 }
4247 smsg("%4d %s %s", current, ins, what);
4248 }
4249 break;
4250
4251 case ISN_COMPAREBOOL:
4252 case ISN_COMPARESPECIAL:
4253 case ISN_COMPARENR:
4254 case ISN_COMPAREFLOAT:
4255 case ISN_COMPARESTRING:
4256 case ISN_COMPAREBLOB:
4257 case ISN_COMPARELIST:
4258 case ISN_COMPAREDICT:
4259 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004260 case ISN_COMPAREANY:
4261 {
4262 char *p;
4263 char buf[10];
4264 char *type;
4265
4266 switch (iptr->isn_arg.op.op_type)
4267 {
4268 case EXPR_EQUAL: p = "=="; break;
4269 case EXPR_NEQUAL: p = "!="; break;
4270 case EXPR_GREATER: p = ">"; break;
4271 case EXPR_GEQUAL: p = ">="; break;
4272 case EXPR_SMALLER: p = "<"; break;
4273 case EXPR_SEQUAL: p = "<="; break;
4274 case EXPR_MATCH: p = "=~"; break;
4275 case EXPR_IS: p = "is"; break;
4276 case EXPR_ISNOT: p = "isnot"; break;
4277 case EXPR_NOMATCH: p = "!~"; break;
4278 default: p = "???"; break;
4279 }
4280 STRCPY(buf, p);
4281 if (iptr->isn_arg.op.op_ic == TRUE)
4282 strcat(buf, "?");
4283 switch(iptr->isn_type)
4284 {
4285 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4286 case ISN_COMPARESPECIAL:
4287 type = "COMPARESPECIAL"; break;
4288 case ISN_COMPARENR: type = "COMPARENR"; break;
4289 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4290 case ISN_COMPARESTRING:
4291 type = "COMPARESTRING"; break;
4292 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4293 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4294 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4295 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004296 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4297 default: type = "???"; break;
4298 }
4299
4300 smsg("%4d %s %s", current, type, buf);
4301 }
4302 break;
4303
4304 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4305 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4306
4307 // expression operations
4308 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004309 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004310 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004311 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004312 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004313 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004314 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004315 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4316 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004317 case ISN_SLICE: smsg("%4d SLICE %lld",
4318 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004319 case ISN_GETITEM: smsg("%4d ITEM %lld",
4320 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004321 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4322 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004323 iptr->isn_arg.string); break;
4324 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4325
4326 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004327 case ISN_CHECKTYPE:
4328 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004329 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004330 char *tofree;
4331
Bram Moolenaare32e5162021-01-21 20:21:29 +01004332 if (ct->ct_arg_idx == 0)
4333 smsg("%4d CHECKTYPE %s stack[%d]", current,
4334 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004335 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004336 else
4337 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4338 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004339 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004340 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004341 vim_free(tofree);
4342 break;
4343 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004344 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4345 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4346 iptr->isn_arg.checklen.cl_min_len);
4347 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004348 case ISN_SETTYPE:
4349 {
4350 char *tofree;
4351
4352 smsg("%4d SETTYPE %s", current,
4353 type_name(iptr->isn_arg.type.ct_type, &tofree));
4354 vim_free(tofree);
4355 break;
4356 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004357 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004358 case ISN_2BOOL: if (iptr->isn_arg.number)
4359 smsg("%4d INVERT (!val)", current);
4360 else
4361 smsg("%4d 2BOOL (!!val)", current);
4362 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004363 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004364 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004365 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004366 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004367 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004368 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004369 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4370 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004371 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004372 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4373 smsg("%4d PUT %c above range",
4374 current, iptr->isn_arg.put.put_regname);
4375 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4376 smsg("%4d PUT %c range",
4377 current, iptr->isn_arg.put.put_regname);
4378 else
4379 smsg("%4d PUT %c %ld", current,
4380 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004381 (long)iptr->isn_arg.put.put_lnum);
4382 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004383
Bram Moolenaar02194d22020-10-24 23:08:38 +02004384 // TODO: summarize modifiers
4385 case ISN_CMDMOD:
4386 {
4387 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004388 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004389 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4390
4391 buf = alloc(len + 1);
4392 if (buf != NULL)
4393 {
4394 (void)produce_cmdmods(
4395 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4396 smsg("%4d CMDMOD %s", current, buf);
4397 vim_free(buf);
4398 }
4399 break;
4400 }
4401 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004402
Bram Moolenaarb2049902021-01-24 12:53:53 +01004403 case ISN_PROF_START:
4404 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4405 break;
4406
4407 case ISN_PROF_END:
4408 smsg("%4d PROFILE END", current);
4409 break;
4410
Bram Moolenaar792f7862020-11-23 08:31:18 +01004411 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4412 iptr->isn_arg.unpack.unp_count,
4413 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4414 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004415 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4416 iptr->isn_arg.shuffle.shfl_item,
4417 iptr->isn_arg.shuffle.shfl_up);
4418 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004419 case ISN_DROP: smsg("%4d DROP", current); break;
4420 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004421
4422 out_flush(); // output one line at a time
4423 ui_breakcheck();
4424 if (got_int)
4425 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427}
4428
4429/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004430 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004431 * list, etc. Mostly like what JavaScript does, except that empty list and
4432 * empty dictionary are FALSE.
4433 */
4434 int
4435tv2bool(typval_T *tv)
4436{
4437 switch (tv->v_type)
4438 {
4439 case VAR_NUMBER:
4440 return tv->vval.v_number != 0;
4441 case VAR_FLOAT:
4442#ifdef FEAT_FLOAT
4443 return tv->vval.v_float != 0.0;
4444#else
4445 break;
4446#endif
4447 case VAR_PARTIAL:
4448 return tv->vval.v_partial != NULL;
4449 case VAR_FUNC:
4450 case VAR_STRING:
4451 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4452 case VAR_LIST:
4453 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4454 case VAR_DICT:
4455 return tv->vval.v_dict != NULL
4456 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4457 case VAR_BOOL:
4458 case VAR_SPECIAL:
4459 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4460 case VAR_JOB:
4461#ifdef FEAT_JOB_CHANNEL
4462 return tv->vval.v_job != NULL;
4463#else
4464 break;
4465#endif
4466 case VAR_CHANNEL:
4467#ifdef FEAT_JOB_CHANNEL
4468 return tv->vval.v_channel != NULL;
4469#else
4470 break;
4471#endif
4472 case VAR_BLOB:
4473 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4474 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004475 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004476 case VAR_VOID:
4477 break;
4478 }
4479 return FALSE;
4480}
4481
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004482 void
4483emsg_using_string_as(typval_T *tv, int as_number)
4484{
4485 semsg(_(as_number ? e_using_string_as_number_str
4486 : e_using_string_as_bool_str),
4487 tv->vval.v_string == NULL
4488 ? (char_u *)"" : tv->vval.v_string);
4489}
4490
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004491/*
4492 * If "tv" is a string give an error and return FAIL.
4493 */
4494 int
4495check_not_string(typval_T *tv)
4496{
4497 if (tv->v_type == VAR_STRING)
4498 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004499 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004500 clear_tv(tv);
4501 return FAIL;
4502 }
4503 return OK;
4504}
4505
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506
4507#endif // FEAT_EVAL