blob: 8febf3623aa8baefd4cbe5db924c26843e40ccbd [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 Moolenaard9d77892021-02-12 21:32:47 +010027 int tcd_frame_idx; // ec_frame_idx at ISN_TRY
28 int tcd_stack_len; // size of ectx.ec_stack at ISN_TRY
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010029 int tcd_catch_idx; // instruction of the first catch
30 int tcd_finally_idx; // instruction of the finally block
31 int tcd_caught; // catch block entered
32 int tcd_return; // when TRUE return from end of :finally
33} trycmd_T;
34
35
36// A stack is used to store:
37// - arguments passed to a :def function
38// - info about the calling function, to use when returning
39// - local variables
40// - temporary values
41//
42// In detail (FP == Frame Pointer):
43// arg1 first argument from caller (if present)
44// arg2 second argument from caller (if present)
45// extra_arg1 any missing optional argument default value
46// FP -> cur_func calling function
47// current previous instruction pointer
48// frame_ptr previous Frame Pointer
49// var1 space for local variable
50// var2 space for local variable
51// .... fixed space for max. number of local variables
52// temp temporary values
53// .... flexible space for temporary values (can grow big)
54
55/*
56 * Execution context.
57 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010058struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010059 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020060 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061
Bram Moolenaar0186e582021-01-10 18:33:11 +010062 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020063
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010064 garray_T ec_trystack; // stack of trycmd_T values
65 int ec_in_catch; // when TRUE in catch or finally block
66
67 int ec_dfunc_idx; // current function index
68 isn_T *ec_instr; // array with instructions
69 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020070
71 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010072};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010073
74// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020075#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010076
Bram Moolenaar418f1df2020-08-12 21:34:49 +020077 void
78to_string_error(vartype_T vartype)
79{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020080 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020081}
82
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010084 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010085 */
86 static int
87ufunc_argcount(ufunc_T *ufunc)
88{
89 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
90}
91
92/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010093 * Set the instruction index, depending on omitted arguments, where the default
94 * values are to be computed. If all optional arguments are present, start
95 * with the function body.
96 * The expression evaluation is at the start of the instructions:
97 * 0 -> EVAL default1
98 * STORE arg[-2]
99 * 1 -> EVAL default2
100 * STORE arg[-1]
101 * 2 -> function body
102 */
103 static void
104init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
105{
106 if (ufunc->uf_def_args.ga_len == 0)
107 ectx->ec_iidx = 0;
108 else
109 {
110 int defcount = ufunc->uf_args.ga_len - argcount;
111
112 // If there is a varargs argument defcount can be negative, no defaults
113 // to evaluate then.
114 if (defcount < 0)
115 defcount = 0;
116 ectx->ec_iidx = ufunc->uf_def_arg_idx[
117 ufunc->uf_def_args.ga_len - defcount];
118 }
119}
120
121/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200122 * Create a new list from "count" items at the bottom of the stack.
123 * When "count" is zero an empty list is added to the stack.
124 */
125 static int
126exe_newlist(int count, ectx_T *ectx)
127{
128 list_T *list = list_alloc_with_items(count);
129 int idx;
130 typval_T *tv;
131
132 if (list == NULL)
133 return FAIL;
134 for (idx = 0; idx < count; ++idx)
135 list_set_item(list, idx, STACK_TV_BOT(idx - count));
136
137 if (count > 0)
138 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200139 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200140 return FAIL;
141 else
142 ++ectx->ec_stack.ga_len;
143 tv = STACK_TV_BOT(-1);
144 tv->v_type = VAR_LIST;
145 tv->vval.v_list = list;
146 ++list->lv_refcount;
147 return OK;
148}
149
150/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100151 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100152 * This adds a stack frame and sets the instruction pointer to the start of the
153 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100154 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100155 *
156 * Stack has:
157 * - current arguments (already there)
158 * - omitted optional argument (default values) added here
159 * - stack frame:
160 * - pointer to calling function
161 * - Index of next instruction in calling function
162 * - previous frame pointer
163 * - reserved space for local variables
164 */
165 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100166call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100167{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200168 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100169 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
170 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200171 int arg_to_add;
172 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200173 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200175 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176
177 if (dfunc->df_deleted)
178 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100179 // don't use ufunc->uf_name, it may have been freed
180 emsg_funcname(e_func_deleted,
181 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100182 return FAIL;
183 }
184
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100185#ifdef FEAT_PROFILE
186 // Profiling might be enabled/disabled along the way. This should not
187 // fail, since the function was compiled before and toggling profiling
188 // doesn't change any errors.
189 if (func_needs_compiling(ufunc, PROFILING(ufunc))
190 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
191 == FAIL)
192 return FAIL;
193#endif
194
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200195 if (ufunc->uf_va_name != NULL)
196 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200197 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200198 // Stack at time of call with 2 varargs:
199 // normal_arg
200 // optional_arg
201 // vararg_1
202 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200203 // After creating the list:
204 // normal_arg
205 // optional_arg
206 // vararg-list
207 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200208 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200209 // After creating the list
210 // normal_arg
211 // (space for optional_arg)
212 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200213 vararg_count = argcount - ufunc->uf_args.ga_len;
214 if (vararg_count < 0)
215 vararg_count = 0;
216 else
217 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200218 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200220
221 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200222 }
223
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200225 if (arg_to_add < 0)
226 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200227 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200228 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200229 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200230 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200231 return FAIL;
232 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200233
234 // Reserve space for:
235 // - missing arguments
236 // - stack frame
237 // - local variables
238 // - if needed: a counter for number of closures created in
239 // ectx->ec_funcrefs.
240 varcount = dfunc->df_varcount + dfunc->df_has_closure;
241 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
242 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100243 return FAIL;
244
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100245 // If depth of calling is getting too high, don't execute the function.
246 if (funcdepth_increment() == FAIL)
247 return FAIL;
248
Bram Moolenaarfe270812020-04-11 22:31:27 +0200249 // Move the vararg-list to below the missing optional arguments.
250 if (vararg_count > 0 && arg_to_add > 0)
251 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100252
253 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200254 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200255 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200256 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100257
258 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100259 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
260 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100261 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
262 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200263 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264
265 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200266 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100267 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200268 if (dfunc->df_has_closure)
269 {
270 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
271
272 tv->v_type = VAR_NUMBER;
273 tv->vval.v_number = 0;
274 }
275 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100276
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100277 if (pt != NULL || ufunc->uf_partial != NULL
278 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100279 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
281
282 if (outer == NULL)
283 return FAIL;
284 if (pt != NULL)
285 {
286 *outer = pt->pt_outer;
287 outer->out_up_is_copy = TRUE;
288 }
289 else if (ufunc->uf_partial != NULL)
290 {
291 *outer = ufunc->uf_partial->pt_outer;
292 outer->out_up_is_copy = TRUE;
293 }
294 else
295 {
296 outer->out_stack = &ectx->ec_stack;
297 outer->out_frame_idx = ectx->ec_frame_idx;
298 outer->out_up = ectx->ec_outer;
299 }
300 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100301 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100302 else
303 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100304
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100305 // Set execution state to the start of the called function.
306 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100307 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100308 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200309 if (entry != NULL)
310 {
311 // Set the script context to the script where the function was defined.
312 // TODO: save more than the SID?
313 entry->es_save_sid = current_sctx.sc_sid;
314 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
315 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100316
317 // Decide where to start execution, handles optional arguments.
318 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100319
320 return OK;
321}
322
323// Get pointer to item in the stack.
324#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
325
326/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200327 * Used when returning from a function: Check if any closure is still
328 * referenced. If so then move the arguments and variables to a separate piece
329 * of stack to be used when the closure is called.
330 * When "free_arguments" is TRUE the arguments are to be freed.
331 * Returns FAIL when out of memory.
332 */
333 static int
334handle_closure_in_use(ectx_T *ectx, int free_arguments)
335{
336 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
337 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200338 int argcount;
339 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200340 int idx;
341 typval_T *tv;
342 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200343 garray_T *gap = &ectx->ec_funcrefs;
344 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200345
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200346 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200347 return OK; // function was freed
348 if (dfunc->df_has_closure == 0)
349 return OK; // no closures
350 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
351 closure_count = tv->vval.v_number;
352 if (closure_count == 0)
353 return OK; // no funcrefs created
354
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200355 argcount = ufunc_argcount(dfunc->df_ufunc);
356 top = ectx->ec_frame_idx - argcount;
357
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200358 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200359 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200360 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200361 partial_T *pt;
362 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200363
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200364 if (off < 0)
365 continue; // count is off or already done
366 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200367 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200368 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200369 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200370 int i;
371
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200372 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200373 // unreferenced on return.
374 for (i = 0; i < dfunc->df_varcount; ++i)
375 {
376 typval_T *stv = STACK_TV(ectx->ec_frame_idx
377 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200378 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200379 --refcount;
380 }
381 if (refcount > 1)
382 {
383 closure_in_use = TRUE;
384 break;
385 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200386 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200387 }
388
389 if (closure_in_use)
390 {
391 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
392 typval_T *stack;
393
394 // A closure is using the arguments and/or local variables.
395 // Move them to the called function.
396 if (funcstack == NULL)
397 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200398 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
399 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200400 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
401 funcstack->fs_ga.ga_data = stack;
402 if (stack == NULL)
403 {
404 vim_free(funcstack);
405 return FAIL;
406 }
407
408 // Move or copy the arguments.
409 for (idx = 0; idx < argcount; ++idx)
410 {
411 tv = STACK_TV(top + idx);
412 if (free_arguments)
413 {
414 *(stack + idx) = *tv;
415 tv->v_type = VAR_UNKNOWN;
416 }
417 else
418 copy_tv(tv, stack + idx);
419 }
420 // Move the local variables.
421 for (idx = 0; idx < dfunc->df_varcount; ++idx)
422 {
423 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200424
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200425 // A partial created for a local function, that is also used as a
426 // local variable, has a reference count for the variable, thus
427 // will never go down to zero. When all these refcounts are one
428 // then the funcstack is unused. We need to count how many we have
429 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200430 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
431 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200432 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200433
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200434 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200435 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
436 gap->ga_len - closure_count + i])
437 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200438 }
439
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200440 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200441 tv->v_type = VAR_UNKNOWN;
442 }
443
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200444 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200445 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200446 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
447 - closure_count + idx];
448 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200449 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200450 ++funcstack->fs_refcount;
451 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100452 pt->pt_outer.out_stack = &funcstack->fs_ga;
453 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
454 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200455 }
456 }
457 }
458
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200459 for (idx = 0; idx < closure_count; ++idx)
460 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
461 - closure_count + idx]);
462 gap->ga_len -= closure_count;
463 if (gap->ga_len == 0)
464 ga_clear(gap);
465
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 return OK;
467}
468
469/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200470 * Called when a partial is freed or its reference count goes down to one. The
471 * funcstack may be the only reference to the partials in the local variables.
472 * Go over all of them, the funcref and can be freed if all partials
473 * referencing the funcstack have a reference count of one.
474 */
475 void
476funcstack_check_refcount(funcstack_T *funcstack)
477{
478 int i;
479 garray_T *gap = &funcstack->fs_ga;
480 int done = 0;
481
482 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
483 return;
484 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
485 {
486 typval_T *tv = ((typval_T *)gap->ga_data) + i;
487
488 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
489 && tv->vval.v_partial->pt_funcstack == funcstack
490 && tv->vval.v_partial->pt_refcount == 1)
491 ++done;
492 }
493 if (done == funcstack->fs_min_refcount)
494 {
495 typval_T *stack = gap->ga_data;
496
497 // All partials referencing the funcstack have a reference count of
498 // one, thus the funcstack is no longer of use.
499 for (i = 0; i < gap->ga_len; ++i)
500 clear_tv(stack + i);
501 vim_free(stack);
502 vim_free(funcstack);
503 }
504}
505
506/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100507 * Return from the current function.
508 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200509 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100510func_return(ectx_T *ectx)
511{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100512 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100513 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200514 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
515 + ectx->ec_dfunc_idx;
516 int argcount = ufunc_argcount(dfunc->df_ufunc);
517 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200518 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100519
520 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200521 entry = estack_pop();
522 if (entry != NULL)
523 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100524
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200525 if (handle_closure_in_use(ectx, TRUE) == FAIL)
526 return FAIL;
527
528 // Clear the arguments.
529 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
530 clear_tv(STACK_TV(idx));
531
532 // Clear local variables and temp values, but not the return value.
533 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100534 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100536
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100537 // The return value should be on top of the stack. However, when aborting
538 // it may not be there and ec_frame_idx is the top of the stack.
539 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100540 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100541 ret_idx = 0;
542
Bram Moolenaar0186e582021-01-10 18:33:11 +0100543 vim_free(ectx->ec_outer);
544
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100545 // Restore the previous frame.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100546 ectx->ec_dfunc_idx = STACK_TV(ectx->ec_frame_idx
547 + STACK_FRAME_FUNC_OFF)->vval.v_number;
548 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
549 + STACK_FRAME_IIDX_OFF)->vval.v_number;
550 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
551 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200552 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100553 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
554 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100555 dfunc = ((dfunc_T *)def_functions.ga_data) + ectx->ec_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100556 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100557
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100558 if (ret_idx > 0)
559 {
560 // Reset the stack to the position before the call, with a spot for the
561 // return value, moved there from above the frame.
562 ectx->ec_stack.ga_len = top + 1;
563 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
564 }
565 else
566 // Reset the stack to the position before the call.
567 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200568
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100569 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200570 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100571}
572
573#undef STACK_TV
574
575/*
576 * Prepare arguments and rettv for calling a builtin or user function.
577 */
578 static int
579call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
580{
581 int idx;
582 typval_T *tv;
583
584 // Move arguments from bottom of the stack to argvars[] and add terminator.
585 for (idx = 0; idx < argcount; ++idx)
586 argvars[idx] = *STACK_TV_BOT(idx - argcount);
587 argvars[argcount].v_type = VAR_UNKNOWN;
588
589 // Result replaces the arguments on the stack.
590 if (argcount > 0)
591 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200592 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100593 return FAIL;
594 else
595 ++ectx->ec_stack.ga_len;
596
597 // Default return value is zero.
598 tv = STACK_TV_BOT(-1);
599 tv->v_type = VAR_NUMBER;
600 tv->vval.v_number = 0;
601
602 return OK;
603}
604
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200605// Ugly global to avoid passing the execution context around through many
606// layers.
607static ectx_T *current_ectx = NULL;
608
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100609/*
610 * Call a builtin function by index.
611 */
612 static int
613call_bfunc(int func_idx, int argcount, ectx_T *ectx)
614{
615 typval_T argvars[MAX_FUNC_ARGS];
616 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100617 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200618 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100619
620 if (call_prepare(argcount, argvars, ectx) == FAIL)
621 return FAIL;
622
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200623 // Call the builtin function. Set "current_ectx" so that when it
624 // recursively invokes call_def_function() a closure context can be set.
625 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100626 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200627 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100628
629 // Clear the arguments.
630 for (idx = 0; idx < argcount; ++idx)
631 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200632
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100633 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200634 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 return OK;
636}
637
638/*
639 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100640 * If the function is compiled this will add a stack frame and set the
641 * instruction pointer at the start of the function.
642 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100643 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100644 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100645 */
646 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100647call_ufunc(
648 ufunc_T *ufunc,
649 partial_T *pt,
650 int argcount,
651 ectx_T *ectx,
652 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100653{
654 typval_T argvars[MAX_FUNC_ARGS];
655 funcexe_T funcexe;
656 int error;
657 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100658 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100659#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100660 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100661#else
662# define profiling FALSE
663#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100664
Bram Moolenaarb2049902021-01-24 12:53:53 +0100665 if (func_needs_compiling(ufunc, profiling)
666 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200667 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200668 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100669 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100670 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100671 if (error != FCERR_UNKNOWN)
672 {
673 if (error == FCERR_TOOMANY)
674 semsg(_(e_toomanyarg), ufunc->uf_name);
675 else
676 semsg(_(e_toofewarg), ufunc->uf_name);
677 return FAIL;
678 }
679
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100680 // The function has been compiled, can call it quickly. For a function
681 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100682 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100683 if (iptr != NULL)
684 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100685 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100686 iptr->isn_type = ISN_DCALL;
687 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
688 iptr->isn_arg.dfunc.cdf_argcount = argcount;
689 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100690 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100691 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100692
693 if (call_prepare(argcount, argvars, ectx) == FAIL)
694 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200695 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100696 funcexe.evaluate = TRUE;
697
698 // Call the user function. Result goes in last position on the stack.
699 // TODO: add selfdict if there is one
700 error = call_user_func_check(ufunc, argcount, argvars,
701 STACK_TV_BOT(-1), &funcexe, NULL);
702
703 // Clear the arguments.
704 for (idx = 0; idx < argcount; ++idx)
705 clear_tv(&argvars[idx]);
706
707 if (error != FCERR_NONE)
708 {
709 user_func_error(error, ufunc->uf_name);
710 return FAIL;
711 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100712 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200713 // Error other than from calling the function itself.
714 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100715 return OK;
716}
717
718/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200719 * Return TRUE if an error was given or CTRL-C was pressed.
720 */
721 static int
722vim9_aborting(int prev_called_emsg)
723{
724 return called_emsg > prev_called_emsg || got_int || did_throw;
725}
726
727/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100728 * Execute a function by "name".
729 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100730 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100731 * Returns FAIL if not found without an error message.
732 */
733 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100734call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100735{
736 ufunc_T *ufunc;
737
738 if (builtin_function(name, -1))
739 {
740 int func_idx = find_internal_func(name);
741
742 if (func_idx < 0)
743 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200744 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100745 return FAIL;
746 return call_bfunc(func_idx, argcount, ectx);
747 }
748
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200749 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200750
751 if (ufunc == NULL)
752 {
753 int called_emsg_before = called_emsg;
754
755 if (script_autoload(name, TRUE))
756 // loaded a package, search for the function again
757 ufunc = find_func(name, FALSE, NULL);
758 if (vim9_aborting(called_emsg_before))
759 return FAIL; // bail out if loading the script caused an error
760 }
761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100762 if (ufunc != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100763 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100764
765 return FAIL;
766}
767
768 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200769call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200771 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200772 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100774 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100775
776 if (tv->v_type == VAR_PARTIAL)
777 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200778 partial_T *pt = tv->vval.v_partial;
779 int i;
780
781 if (pt->pt_argc > 0)
782 {
783 // Make space for arguments from the partial, shift the "argcount"
784 // arguments up.
785 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
786 return FAIL;
787 for (i = 1; i <= argcount; ++i)
788 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
789 ectx->ec_stack.ga_len += pt->pt_argc;
790 argcount += pt->pt_argc;
791
792 // copy the arguments from the partial onto the stack
793 for (i = 0; i < pt->pt_argc; ++i)
794 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
795 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100796
797 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100798 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200799
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100800 name = pt->pt_name;
801 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200802 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100803 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200804 if (name != NULL)
805 {
806 char_u fname_buf[FLEN_FIXED + 1];
807 char_u *tofree = NULL;
808 int error = FCERR_NONE;
809 char_u *fname;
810
811 // May need to translate <SNR>123_ to K_SNR.
812 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
813 if (error != FCERR_NONE)
814 res = FAIL;
815 else
816 res = call_by_name(fname, argcount, ectx, NULL);
817 vim_free(tofree);
818 }
819
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100820 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821 {
822 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200823 semsg(_(e_unknownfunc),
824 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100825 return FAIL;
826 }
827 return OK;
828}
829
830/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200831 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
832 * TRUE.
833 */
834 static int
835error_if_locked(int lock, char *error)
836{
837 if (lock & (VAR_LOCKED | VAR_FIXED))
838 {
839 emsg(_(error));
840 return TRUE;
841 }
842 return FALSE;
843}
844
845/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100846 * Store "tv" in variable "name".
847 * This is for s: and g: variables.
848 */
849 static void
850store_var(char_u *name, typval_T *tv)
851{
852 funccal_entry_T entry;
853
854 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100855 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100856 restore_funccal();
857}
858
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100859/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100860 * Convert "tv" to a string.
861 * Return FAIL if not allowed.
862 */
863 static int
864do_2string(typval_T *tv, int is_2string_any)
865{
866 if (tv->v_type != VAR_STRING)
867 {
868 char_u *str;
869
870 if (is_2string_any)
871 {
872 switch (tv->v_type)
873 {
874 case VAR_SPECIAL:
875 case VAR_BOOL:
876 case VAR_NUMBER:
877 case VAR_FLOAT:
878 case VAR_BLOB: break;
879 default: to_string_error(tv->v_type);
880 return FAIL;
881 }
882 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100883 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100884 clear_tv(tv);
885 tv->v_type = VAR_STRING;
886 tv->vval.v_string = str;
887 }
888 return OK;
889}
890
891/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100892 * When the value of "sv" is a null list of dict, allocate it.
893 */
894 static void
895allocate_if_null(typval_T *tv)
896{
897 switch (tv->v_type)
898 {
899 case VAR_LIST:
900 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100901 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100902 break;
903 case VAR_DICT:
904 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100905 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100906 break;
907 default:
908 break;
909 }
910}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200911
Bram Moolenaare7525c52021-01-09 13:20:37 +0100912/*
913 * Return the character "str[index]" where "index" is the character index. If
914 * "index" is out of range NULL is returned.
915 */
916 char_u *
917char_from_string(char_u *str, varnumber_T index)
918{
919 size_t nbyte = 0;
920 varnumber_T nchar = index;
921 size_t slen;
922
923 if (str == NULL)
924 return NULL;
925 slen = STRLEN(str);
926
927 // do the same as for a list: a negative index counts from the end
928 if (index < 0)
929 {
930 int clen = 0;
931
932 for (nbyte = 0; nbyte < slen; ++clen)
933 nbyte += MB_CPTR2LEN(str + nbyte);
934 nchar = clen + index;
935 if (nchar < 0)
936 // unlike list: index out of range results in empty string
937 return NULL;
938 }
939
940 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
941 nbyte += MB_CPTR2LEN(str + nbyte);
942 if (nbyte >= slen)
943 return NULL;
944 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
945}
946
947/*
948 * Get the byte index for character index "idx" in string "str" with length
949 * "str_len".
950 * If going over the end return "str_len".
951 * If "idx" is negative count from the end, -1 is the last character.
952 * When going over the start return -1.
953 */
954 static long
955char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
956{
957 varnumber_T nchar = idx;
958 size_t nbyte = 0;
959
960 if (nchar >= 0)
961 {
962 while (nchar > 0 && nbyte < str_len)
963 {
964 nbyte += MB_CPTR2LEN(str + nbyte);
965 --nchar;
966 }
967 }
968 else
969 {
970 nbyte = str_len;
971 while (nchar < 0 && nbyte > 0)
972 {
973 --nbyte;
974 nbyte -= mb_head_off(str, str + nbyte);
975 ++nchar;
976 }
977 if (nchar < 0)
978 return -1;
979 }
980 return (long)nbyte;
981}
982
983/*
984 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +0100985 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +0100986 * Return NULL when the result is empty.
987 */
988 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +0100989string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +0100990{
991 long start_byte, end_byte;
992 size_t slen;
993
994 if (str == NULL)
995 return NULL;
996 slen = STRLEN(str);
997 start_byte = char_idx2byte(str, slen, first);
998 if (start_byte < 0)
999 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001000 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001001 end_byte = (long)slen;
1002 else
1003 {
1004 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001005 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001006 // end index is inclusive
1007 end_byte += MB_CPTR2LEN(str + end_byte);
1008 }
1009
1010 if (start_byte >= (long)slen || end_byte <= start_byte)
1011 return NULL;
1012 return vim_strnsave(str + start_byte, end_byte - start_byte);
1013}
1014
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001015 static svar_T *
1016get_script_svar(scriptref_T *sref, ectx_T *ectx)
1017{
1018 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1019 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1020 + ectx->ec_dfunc_idx;
1021 svar_T *sv;
1022
1023 if (sref->sref_seq != si->sn_script_seq)
1024 {
1025 // The script was reloaded after the function was
1026 // compiled, the script_idx may not be valid.
1027 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1028 dfunc->df_ufunc->uf_name_exp);
1029 return NULL;
1030 }
1031 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1032 if (!equal_type(sv->sv_type, sref->sref_type))
1033 {
1034 emsg(_(e_script_variable_type_changed));
1035 return NULL;
1036 }
1037 return sv;
1038}
1039
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001040/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001041 * Execute a function by "name".
1042 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001043 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001044 */
1045 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001046call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001047{
Bram Moolenaared677f52020-08-12 16:38:10 +02001048 int called_emsg_before = called_emsg;
1049 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001050
Bram Moolenaared677f52020-08-12 16:38:10 +02001051 res = call_by_name(name, argcount, ectx, iptr);
1052 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001053 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001054 dictitem_T *v;
1055
1056 v = find_var(name, NULL, FALSE);
1057 if (v == NULL)
1058 {
1059 semsg(_(e_unknownfunc), name);
1060 return FAIL;
1061 }
1062 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1063 {
1064 semsg(_(e_unknownfunc), name);
1065 return FAIL;
1066 }
1067 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001068 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001069 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001070}
1071
1072/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001073 * When a function reference is used, fill a partial with the information
1074 * needed, especially when it is used as a closure.
1075 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001076 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001077fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1078{
1079 pt->pt_func = ufunc;
1080 pt->pt_refcount = 1;
1081
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001082 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001083 {
1084 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1085 + ectx->ec_dfunc_idx;
1086
1087 // The closure needs to find arguments and local
1088 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001089 pt->pt_outer.out_stack = &ectx->ec_stack;
1090 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1091 pt->pt_outer.out_up = ectx->ec_outer;
1092 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001093
1094 // If this function returns and the closure is still
1095 // being used, we need to make a copy of the context
1096 // (arguments and local variables). Store a reference
1097 // to the partial so we can handle that.
1098 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1099 {
1100 vim_free(pt);
1101 return FAIL;
1102 }
1103 // Extra variable keeps the count of closures created
1104 // in the current function call.
1105 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1106 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1107
1108 ((partial_T **)ectx->ec_funcrefs.ga_data)
1109 [ectx->ec_funcrefs.ga_len] = pt;
1110 ++pt->pt_refcount;
1111 ++ectx->ec_funcrefs.ga_len;
1112 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001113 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001114 return OK;
1115}
1116
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001117
Bram Moolenaarf112f302020-12-20 17:47:52 +01001118/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119 * Call a "def" function from old Vim script.
1120 * Return OK or FAIL.
1121 */
1122 int
1123call_def_function(
1124 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001125 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001127 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001128 typval_T *rettv) // return value
1129{
1130 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001131 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001132 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001133 typval_T *tv;
1134 int idx;
1135 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001136 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001137 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001138 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001139 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001140 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001141 msglist_T **saved_msg_list = NULL;
1142 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001143 cmdmod_T save_cmdmod;
1144 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001145 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001146 int save_emsg_silent_def = emsg_silent_def;
1147 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001148 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001149 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001150 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001151
1152// Get pointer to item in the stack.
1153#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1154
1155// Get pointer to item at the bottom of the stack, -1 is the bottom.
1156#undef STACK_TV_BOT
1157#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1158
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001159// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001160#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 +01001161
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001162 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001163 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1164 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001165 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001166 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001167 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001168 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001169 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001170 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001171 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001172
Bram Moolenaar09689a02020-05-09 22:50:08 +02001173 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001174 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001175 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1176 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001177 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001178 {
1179 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001180 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001181 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001182 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001183
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001184 // If depth of calling is getting too high, don't execute the function.
1185 orig_funcdepth = funcdepth_get();
1186 if (funcdepth_increment() == FAIL)
1187 return FAIL;
1188
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001189 CLEAR_FIELD(ectx);
1190 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1191 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1192 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001193 {
1194 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001195 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001196 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001197 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001198 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001199
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001200 // Put arguments on the stack, but no more than what the function expects.
1201 // A lambda can be called with more arguments than it uses.
1202 for (idx = 0; idx < argc
1203 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1204 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001206 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001207 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001208 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001209 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 copy_tv(&argv[idx], STACK_TV_BOT(0));
1211 ++ectx.ec_stack.ga_len;
1212 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001213
1214 // Turn varargs into a list. Empty list if no args.
1215 if (ufunc->uf_va_name != NULL)
1216 {
1217 int vararg_count = argc - ufunc->uf_args.ga_len;
1218
1219 if (vararg_count < 0)
1220 vararg_count = 0;
1221 else
1222 argc -= vararg_count;
1223 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001224 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001225
1226 // Check the type of the list items.
1227 tv = STACK_TV_BOT(-1);
1228 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001229 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001230 && ufunc->uf_va_type->tt_member != &t_any
1231 && tv->vval.v_list != NULL)
1232 {
1233 type_T *expected = ufunc->uf_va_type->tt_member;
1234 listitem_T *li = tv->vval.v_list->lv_first;
1235
1236 for (idx = 0; idx < vararg_count; ++idx)
1237 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001238 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001239 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001240 goto failed_early;
1241 li = li->li_next;
1242 }
1243 }
1244
Bram Moolenaar23e03252020-04-12 22:22:31 +02001245 if (defcount > 0)
1246 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001247 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001248 --ectx.ec_stack.ga_len;
1249 }
1250
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001251 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001252 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001253 if (defcount > 0)
1254 for (idx = 0; idx < defcount; ++idx)
1255 {
1256 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1257 ++ectx.ec_stack.ga_len;
1258 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001259 if (ufunc->uf_va_name != NULL)
1260 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001261
1262 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001263 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1264 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001266 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001267 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1268 + ufunc->uf_dfunc_idx;
1269 ufunc_T *base_ufunc = dfunc->df_ufunc;
1270
1271 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1272 // by copy_func().
1273 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001274 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001275 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1276 if (ectx.ec_outer == NULL)
1277 goto failed_early;
1278 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001279 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001280 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1281 {
1282 if (current_ectx->ec_outer != NULL)
1283 *ectx.ec_outer = *current_ectx->ec_outer;
1284 }
1285 else
1286 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001287 }
1288 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001289 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1290 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001291 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001292 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001293
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001294 // dummy frame entries
1295 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1296 {
1297 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1298 ++ectx.ec_stack.ga_len;
1299 }
1300
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001301 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001302 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001303 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1304 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001305
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001306 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001307 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001308 ectx.ec_stack.ga_len += dfunc->df_varcount;
1309 if (dfunc->df_has_closure)
1310 {
1311 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1312 STACK_TV_VAR(idx)->vval.v_number = 0;
1313 ++ectx.ec_stack.ga_len;
1314 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001315
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001316 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001317 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001318
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001319 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001320 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001321 estack_push_ufunc(ufunc, 1);
1322 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001323 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1324
Bram Moolenaar352134b2020-10-17 22:04:08 +02001325 // Use a specific location for storing error messages to be converted to an
1326 // exception.
1327 saved_msg_list = msg_list;
1328 msg_list = &private_msg_list;
1329
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001330 // Do turn errors into exceptions.
1331 suppress_errthrow = FALSE;
1332
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001333 // When ":silent!" was used before calling then we still abort the
1334 // function. If ":silent!" is used in the function then we don't.
1335 emsg_silent_def = emsg_silent;
1336 did_emsg_def = 0;
1337
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001338 where.wt_index = 0;
1339 where.wt_variable = FALSE;
1340
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001341 // Decide where to start execution, handles optional arguments.
1342 init_instr_idx(ufunc, argc, &ectx);
1343
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001344 for (;;)
1345 {
1346 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001347
Bram Moolenaar270d0382020-05-15 21:42:53 +02001348 if (++breakcheck_count >= 100)
1349 {
1350 line_breakcheck();
1351 breakcheck_count = 0;
1352 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001353 if (got_int)
1354 {
1355 // Turn CTRL-C into an exception.
1356 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001357 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001358 goto failed;
1359 did_throw = TRUE;
1360 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001361
Bram Moolenaara26b9702020-04-18 19:53:28 +02001362 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1363 {
1364 // Turn an error message into an exception.
1365 did_emsg = FALSE;
1366 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1367 goto failed;
1368 did_throw = TRUE;
1369 *msg_list = NULL;
1370 }
1371
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001372 if (did_throw && !ectx.ec_in_catch)
1373 {
1374 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001375 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376
1377 // An exception jumps to the first catch, finally, or returns from
1378 // the current function.
1379 if (trystack->ga_len > 0)
1380 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001381 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382 {
1383 // jump to ":catch" or ":finally"
1384 ectx.ec_in_catch = TRUE;
1385 ectx.ec_iidx = trycmd->tcd_catch_idx;
1386 }
1387 else
1388 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001389 // Not inside try or need to return from current functions.
1390 // Push a dummy return value.
1391 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1392 goto failed;
1393 tv = STACK_TV_BOT(0);
1394 tv->v_type = VAR_NUMBER;
1395 tv->vval.v_number = 0;
1396 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001397 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001398 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001399 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001400 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001401 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1402 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001403 goto done;
1404 }
1405
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001406 if (func_return(&ectx) == FAIL)
1407 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001408 }
1409 continue;
1410 }
1411
1412 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1413 switch (iptr->isn_type)
1414 {
1415 // execute Ex command line
1416 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001417 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001418 source_cookie_T cookie;
1419
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001420 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001421 // Pass getsourceline to get an error for a missing ":end"
1422 // command.
1423 CLEAR_FIELD(cookie);
1424 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1425 if (do_cmdline(iptr->isn_arg.string,
1426 getsourceline, &cookie,
1427 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1428 == FAIL
1429 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001430 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001431 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001432 break;
1433
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001434 // execute Ex command from pieces on the stack
1435 case ISN_EXECCONCAT:
1436 {
1437 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001438 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001439 int pass;
1440 int i;
1441 char_u *cmd = NULL;
1442 char_u *str;
1443
1444 for (pass = 1; pass <= 2; ++pass)
1445 {
1446 for (i = 0; i < count; ++i)
1447 {
1448 tv = STACK_TV_BOT(i - count);
1449 str = tv->vval.v_string;
1450 if (str != NULL && *str != NUL)
1451 {
1452 if (pass == 2)
1453 STRCPY(cmd + len, str);
1454 len += STRLEN(str);
1455 }
1456 if (pass == 2)
1457 clear_tv(tv);
1458 }
1459 if (pass == 1)
1460 {
1461 cmd = alloc(len + 1);
1462 if (cmd == NULL)
1463 goto failed;
1464 len = 0;
1465 }
1466 }
1467
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001468 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001469 do_cmdline_cmd(cmd);
1470 vim_free(cmd);
1471 }
1472 break;
1473
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001474 // execute :echo {string} ...
1475 case ISN_ECHO:
1476 {
1477 int count = iptr->isn_arg.echo.echo_count;
1478 int atstart = TRUE;
1479 int needclr = TRUE;
1480
1481 for (idx = 0; idx < count; ++idx)
1482 {
1483 tv = STACK_TV_BOT(idx - count);
1484 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1485 &atstart, &needclr);
1486 clear_tv(tv);
1487 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001488 if (needclr)
1489 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001490 ectx.ec_stack.ga_len -= count;
1491 }
1492 break;
1493
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001494 // :execute {string} ...
1495 // :echomsg {string} ...
1496 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001497 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001498 case ISN_ECHOMSG:
1499 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001500 {
1501 int count = iptr->isn_arg.number;
1502 garray_T ga;
1503 char_u buf[NUMBUFLEN];
1504 char_u *p;
1505 int len;
1506 int failed = FALSE;
1507
1508 ga_init2(&ga, 1, 80);
1509 for (idx = 0; idx < count; ++idx)
1510 {
1511 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001512 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001513 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001514 if (tv->v_type == VAR_CHANNEL
1515 || tv->v_type == VAR_JOB)
1516 {
1517 SOURCING_LNUM = iptr->isn_lnum;
1518 emsg(_(e_inval_string));
1519 break;
1520 }
1521 else
1522 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001523 }
1524 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001525 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001526
1527 len = (int)STRLEN(p);
1528 if (ga_grow(&ga, len + 2) == FAIL)
1529 failed = TRUE;
1530 else
1531 {
1532 if (ga.ga_len > 0)
1533 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1534 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1535 ga.ga_len += len;
1536 }
1537 clear_tv(tv);
1538 }
1539 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001540 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001541 {
1542 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001543 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001544 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001545
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001546 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001547 {
1548 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001549 {
1550 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001551 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001552 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001553 {
1554 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001555 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001556 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001557 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001558 else
1559 {
1560 msg_sb_eol();
1561 if (iptr->isn_type == ISN_ECHOMSG)
1562 {
1563 msg_attr(ga.ga_data, echo_attr);
1564 out_flush();
1565 }
1566 else
1567 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001568 SOURCING_LNUM = iptr->isn_lnum;
1569 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001570 }
1571 }
1572 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001573 ga_clear(&ga);
1574 }
1575 break;
1576
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001577 // load local variable or argument
1578 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001579 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001580 goto failed;
1581 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1582 ++ectx.ec_stack.ga_len;
1583 break;
1584
1585 // load v: variable
1586 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001587 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001588 goto failed;
1589 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1590 ++ectx.ec_stack.ga_len;
1591 break;
1592
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001593 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001594 case ISN_LOADSCRIPT:
1595 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001596 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001597 svar_T *sv;
1598
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001599 sv = get_script_svar(sref, &ectx);
1600 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001601 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001602 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001603 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001604 goto failed;
1605 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1606 ++ectx.ec_stack.ga_len;
1607 }
1608 break;
1609
1610 // load s: variable in old script
1611 case ISN_LOADS:
1612 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001613 hashtab_T *ht = &SCRIPT_VARS(
1614 iptr->isn_arg.loadstore.ls_sid);
1615 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001616 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001617
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001618 if (di == NULL)
1619 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001620 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001621 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001622 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001623 }
1624 else
1625 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001626 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001627 goto failed;
1628 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1629 ++ectx.ec_stack.ga_len;
1630 }
1631 }
1632 break;
1633
Bram Moolenaard3aac292020-04-19 14:32:17 +02001634 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001635 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001636 case ISN_LOADB:
1637 case ISN_LOADW:
1638 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001639 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001640 dictitem_T *di = NULL;
1641 hashtab_T *ht = NULL;
1642 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001643
Bram Moolenaard3aac292020-04-19 14:32:17 +02001644 switch (iptr->isn_type)
1645 {
1646 case ISN_LOADG:
1647 ht = get_globvar_ht();
1648 namespace = 'g';
1649 break;
1650 case ISN_LOADB:
1651 ht = &curbuf->b_vars->dv_hashtab;
1652 namespace = 'b';
1653 break;
1654 case ISN_LOADW:
1655 ht = &curwin->w_vars->dv_hashtab;
1656 namespace = 'w';
1657 break;
1658 case ISN_LOADT:
1659 ht = &curtab->tp_vars->dv_hashtab;
1660 namespace = 't';
1661 break;
1662 default: // Cannot reach here
1663 goto failed;
1664 }
1665 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001666
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001667 if (di == NULL)
1668 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001669 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001670 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001671 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001672 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 }
1674 else
1675 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001676 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001677 goto failed;
1678 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1679 ++ectx.ec_stack.ga_len;
1680 }
1681 }
1682 break;
1683
Bram Moolenaar03290b82020-12-19 16:30:44 +01001684 // load autoload variable
1685 case ISN_LOADAUTO:
1686 {
1687 char_u *name = iptr->isn_arg.string;
1688
1689 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1690 goto failed;
1691 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001692 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001693 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1694 goto on_error;
1695 ++ectx.ec_stack.ga_len;
1696 }
1697 break;
1698
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001699 // load g:/b:/w:/t: namespace
1700 case ISN_LOADGDICT:
1701 case ISN_LOADBDICT:
1702 case ISN_LOADWDICT:
1703 case ISN_LOADTDICT:
1704 {
1705 dict_T *d = NULL;
1706
1707 switch (iptr->isn_type)
1708 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001709 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1710 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1711 case ISN_LOADWDICT: d = curwin->w_vars; break;
1712 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001713 default: // Cannot reach here
1714 goto failed;
1715 }
1716 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1717 goto failed;
1718 tv = STACK_TV_BOT(0);
1719 tv->v_type = VAR_DICT;
1720 tv->v_lock = 0;
1721 tv->vval.v_dict = d;
1722 ++ectx.ec_stack.ga_len;
1723 }
1724 break;
1725
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001726 // load &option
1727 case ISN_LOADOPT:
1728 {
1729 typval_T optval;
1730 char_u *name = iptr->isn_arg.string;
1731
Bram Moolenaara8c17702020-04-01 21:17:24 +02001732 // This is not expected to fail, name is checked during
1733 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001734 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001735 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001736 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001737 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001738 *STACK_TV_BOT(0) = optval;
1739 ++ectx.ec_stack.ga_len;
1740 }
1741 break;
1742
1743 // load $ENV
1744 case ISN_LOADENV:
1745 {
1746 typval_T optval;
1747 char_u *name = iptr->isn_arg.string;
1748
Bram Moolenaar270d0382020-05-15 21:42:53 +02001749 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001751 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001752 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001753 *STACK_TV_BOT(0) = optval;
1754 ++ectx.ec_stack.ga_len;
1755 }
1756 break;
1757
1758 // load @register
1759 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001760 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001761 goto failed;
1762 tv = STACK_TV_BOT(0);
1763 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001764 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001765 // This may result in NULL, which should be equivalent to an
1766 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001767 tv->vval.v_string = get_reg_contents(
1768 iptr->isn_arg.number, GREG_EXPR_SRC);
1769 ++ectx.ec_stack.ga_len;
1770 break;
1771
1772 // store local variable
1773 case ISN_STORE:
1774 --ectx.ec_stack.ga_len;
1775 tv = STACK_TV_VAR(iptr->isn_arg.number);
1776 clear_tv(tv);
1777 *tv = *STACK_TV_BOT(0);
1778 break;
1779
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001780 // store s: variable in old script
1781 case ISN_STORES:
1782 {
1783 hashtab_T *ht = &SCRIPT_VARS(
1784 iptr->isn_arg.loadstore.ls_sid);
1785 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001786 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001787
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001788 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001789 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001790 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001791 else
1792 {
1793 clear_tv(&di->di_tv);
1794 di->di_tv = *STACK_TV_BOT(0);
1795 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001796 }
1797 break;
1798
1799 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001800 case ISN_STORESCRIPT:
1801 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001802 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1803 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001805 sv = get_script_svar(sref, &ectx);
1806 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001807 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 --ectx.ec_stack.ga_len;
1809 clear_tv(sv->sv_tv);
1810 *sv->sv_tv = *STACK_TV_BOT(0);
1811 }
1812 break;
1813
1814 // store option
1815 case ISN_STOREOPT:
1816 {
1817 long n = 0;
1818 char_u *s = NULL;
1819 char *msg;
1820
1821 --ectx.ec_stack.ga_len;
1822 tv = STACK_TV_BOT(0);
1823 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001824 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001825 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001826 if (s == NULL)
1827 s = (char_u *)"";
1828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001829 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001830 // must be VAR_NUMBER, CHECKTYPE makes sure
1831 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001832 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1833 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001834 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001835 if (msg != NULL)
1836 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001837 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001838 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001839 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001841 }
1842 break;
1843
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001844 // store $ENV
1845 case ISN_STOREENV:
1846 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001847 tv = STACK_TV_BOT(0);
1848 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1849 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001850 break;
1851
1852 // store @r
1853 case ISN_STOREREG:
1854 {
1855 int reg = iptr->isn_arg.number;
1856
1857 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001858 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001859 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001860 tv_get_string(tv), -1, FALSE);
1861 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001862 }
1863 break;
1864
1865 // store v: variable
1866 case ISN_STOREV:
1867 --ectx.ec_stack.ga_len;
1868 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1869 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001870 // should not happen, type is checked when compiling
1871 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001872 break;
1873
Bram Moolenaard3aac292020-04-19 14:32:17 +02001874 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001875 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001876 case ISN_STOREB:
1877 case ISN_STOREW:
1878 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001879 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001880 dictitem_T *di;
1881 hashtab_T *ht;
1882 char_u *name = iptr->isn_arg.string + 2;
1883
Bram Moolenaard3aac292020-04-19 14:32:17 +02001884 switch (iptr->isn_type)
1885 {
1886 case ISN_STOREG:
1887 ht = get_globvar_ht();
1888 break;
1889 case ISN_STOREB:
1890 ht = &curbuf->b_vars->dv_hashtab;
1891 break;
1892 case ISN_STOREW:
1893 ht = &curwin->w_vars->dv_hashtab;
1894 break;
1895 case ISN_STORET:
1896 ht = &curtab->tp_vars->dv_hashtab;
1897 break;
1898 default: // Cannot reach here
1899 goto failed;
1900 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001901
1902 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001903 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001904 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001905 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001906 else
1907 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001908 SOURCING_LNUM = iptr->isn_lnum;
1909 if (var_check_permission(di, name) == FAIL)
1910 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 clear_tv(&di->di_tv);
1912 di->di_tv = *STACK_TV_BOT(0);
1913 }
1914 }
1915 break;
1916
Bram Moolenaar03290b82020-12-19 16:30:44 +01001917 // store an autoload variable
1918 case ISN_STOREAUTO:
1919 SOURCING_LNUM = iptr->isn_lnum;
1920 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1921 clear_tv(STACK_TV_BOT(-1));
1922 --ectx.ec_stack.ga_len;
1923 break;
1924
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001925 // store number in local variable
1926 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001927 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001928 clear_tv(tv);
1929 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001930 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 break;
1932
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001933 // store value in list or dict variable
1934 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001935 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001936 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001937 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001938 typval_T *tv_dest = STACK_TV_BOT(-1);
1939 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001940
Bram Moolenaar752fc692021-01-04 21:57:11 +01001941 // Stack contains:
1942 // -3 value to be stored
1943 // -2 index
1944 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001945 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001946 SOURCING_LNUM = iptr->isn_lnum;
1947 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001948 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001949 dest_type = tv_dest->v_type;
1950 if (dest_type == VAR_DICT)
1951 status = do_2string(tv_idx, TRUE);
1952 else if (dest_type == VAR_LIST
1953 && tv_idx->v_type != VAR_NUMBER)
1954 {
1955 emsg(_(e_number_exp));
1956 status = FAIL;
1957 }
1958 }
1959 else if (dest_type != tv_dest->v_type)
1960 {
1961 // just in case, should be OK
1962 semsg(_(e_expected_str_but_got_str),
1963 vartype_name(dest_type),
1964 vartype_name(tv_dest->v_type));
1965 status = FAIL;
1966 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001967
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001968 if (status == OK && dest_type == VAR_LIST)
1969 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01001970 long lidx = (long)tv_idx->vval.v_number;
1971 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001972
1973 if (list == NULL)
1974 {
1975 emsg(_(e_list_not_set));
1976 goto on_error;
1977 }
1978 if (lidx < 0 && list->lv_len + lidx >= 0)
1979 // negative index is relative to the end
1980 lidx = list->lv_len + lidx;
1981 if (lidx < 0 || lidx > list->lv_len)
1982 {
1983 semsg(_(e_listidx), lidx);
1984 goto on_error;
1985 }
1986 if (lidx < list->lv_len)
1987 {
1988 listitem_T *li = list_find(list, lidx);
1989
1990 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02001991 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001992 goto on_error;
1993 // overwrite existing list item
1994 clear_tv(&li->li_tv);
1995 li->li_tv = *tv;
1996 }
1997 else
1998 {
1999 if (error_if_locked(list->lv_lock,
2000 e_cannot_change_list))
2001 goto on_error;
2002 // append to list, only fails when out of memory
2003 if (list_append_tv(list, tv) == FAIL)
2004 goto failed;
2005 clear_tv(tv);
2006 }
2007 }
2008 else if (status == OK && dest_type == VAR_DICT)
2009 {
2010 char_u *key = tv_idx->vval.v_string;
2011 dict_T *dict = tv_dest->vval.v_dict;
2012 dictitem_T *di;
2013
2014 SOURCING_LNUM = iptr->isn_lnum;
2015 if (dict == NULL)
2016 {
2017 emsg(_(e_dictionary_not_set));
2018 goto on_error;
2019 }
2020 if (key == NULL)
2021 key = (char_u *)"";
2022 di = dict_find(dict, key, -1);
2023 if (di != NULL)
2024 {
2025 if (error_if_locked(di->di_tv.v_lock,
2026 e_cannot_change_dict_item))
2027 goto on_error;
2028 // overwrite existing value
2029 clear_tv(&di->di_tv);
2030 di->di_tv = *tv;
2031 }
2032 else
2033 {
2034 if (error_if_locked(dict->dv_lock,
2035 e_cannot_change_dict))
2036 goto on_error;
2037 // add to dict, only fails when out of memory
2038 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2039 goto failed;
2040 clear_tv(tv);
2041 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002042 }
2043 else
2044 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002045 status = FAIL;
2046 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002047 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002048
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002049 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002050 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002051 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002052 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002053 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002054 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002055 goto on_error;
2056 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002057 }
2058 break;
2059
Bram Moolenaar0186e582021-01-10 18:33:11 +01002060 // load or store variable or argument from outer scope
2061 case ISN_LOADOUTER:
2062 case ISN_STOREOUTER:
2063 {
2064 int depth = iptr->isn_arg.outer.outer_depth;
2065 outer_T *outer = ectx.ec_outer;
2066
2067 while (depth > 1 && outer != NULL)
2068 {
2069 outer = outer->out_up;
2070 --depth;
2071 }
2072 if (outer == NULL)
2073 {
2074 SOURCING_LNUM = iptr->isn_lnum;
2075 iemsg("LOADOUTER depth more than scope levels");
2076 goto failed;
2077 }
2078 tv = ((typval_T *)outer->out_stack->ga_data)
2079 + outer->out_frame_idx + STACK_FRAME_SIZE
2080 + iptr->isn_arg.outer.outer_idx;
2081 if (iptr->isn_type == ISN_LOADOUTER)
2082 {
2083 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2084 goto failed;
2085 copy_tv(tv, STACK_TV_BOT(0));
2086 ++ectx.ec_stack.ga_len;
2087 }
2088 else
2089 {
2090 --ectx.ec_stack.ga_len;
2091 clear_tv(tv);
2092 *tv = *STACK_TV_BOT(0);
2093 }
2094 }
2095 break;
2096
Bram Moolenaar752fc692021-01-04 21:57:11 +01002097 // unlet item in list or dict variable
2098 case ISN_UNLETINDEX:
2099 {
2100 typval_T *tv_idx = STACK_TV_BOT(-2);
2101 typval_T *tv_dest = STACK_TV_BOT(-1);
2102 int status = OK;
2103
2104 // Stack contains:
2105 // -2 index
2106 // -1 dict or list
2107 if (tv_dest->v_type == VAR_DICT)
2108 {
2109 // unlet a dict item, index must be a string
2110 if (tv_idx->v_type != VAR_STRING)
2111 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002113 semsg(_(e_expected_str_but_got_str),
2114 vartype_name(VAR_STRING),
2115 vartype_name(tv_idx->v_type));
2116 status = FAIL;
2117 }
2118 else
2119 {
2120 dict_T *d = tv_dest->vval.v_dict;
2121 char_u *key = tv_idx->vval.v_string;
2122 dictitem_T *di = NULL;
2123
2124 if (key == NULL)
2125 key = (char_u *)"";
2126 if (d != NULL)
2127 di = dict_find(d, key, (int)STRLEN(key));
2128 if (di == NULL)
2129 {
2130 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002131 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002132 semsg(_(e_dictkey), key);
2133 status = FAIL;
2134 }
2135 else
2136 {
2137 // TODO: check for dict or item locked
2138 dictitem_remove(d, di);
2139 }
2140 }
2141 }
2142 else if (tv_dest->v_type == VAR_LIST)
2143 {
2144 // unlet a List item, index must be a number
2145 if (tv_idx->v_type != VAR_NUMBER)
2146 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002147 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002148 semsg(_(e_expected_str_but_got_str),
2149 vartype_name(VAR_NUMBER),
2150 vartype_name(tv_idx->v_type));
2151 status = FAIL;
2152 }
2153 else
2154 {
2155 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002156 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002157 listitem_T *li = NULL;
2158
2159 li = list_find(l, n);
2160 if (li == NULL)
2161 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002162 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002163 semsg(_(e_listidx), n);
2164 status = FAIL;
2165 }
2166 else
2167 // TODO: check for list or item locked
2168 listitem_remove(l, li);
2169 }
2170 }
2171 else
2172 {
2173 status = FAIL;
2174 semsg(_(e_cannot_index_str),
2175 vartype_name(tv_dest->v_type));
2176 }
2177
2178 clear_tv(tv_idx);
2179 clear_tv(tv_dest);
2180 ectx.ec_stack.ga_len -= 2;
2181 if (status == FAIL)
2182 goto on_error;
2183 }
2184 break;
2185
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002186 // push constant
2187 case ISN_PUSHNR:
2188 case ISN_PUSHBOOL:
2189 case ISN_PUSHSPEC:
2190 case ISN_PUSHF:
2191 case ISN_PUSHS:
2192 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002193 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002194 case ISN_PUSHCHANNEL:
2195 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002196 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002197 goto failed;
2198 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002199 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002200 ++ectx.ec_stack.ga_len;
2201 switch (iptr->isn_type)
2202 {
2203 case ISN_PUSHNR:
2204 tv->v_type = VAR_NUMBER;
2205 tv->vval.v_number = iptr->isn_arg.number;
2206 break;
2207 case ISN_PUSHBOOL:
2208 tv->v_type = VAR_BOOL;
2209 tv->vval.v_number = iptr->isn_arg.number;
2210 break;
2211 case ISN_PUSHSPEC:
2212 tv->v_type = VAR_SPECIAL;
2213 tv->vval.v_number = iptr->isn_arg.number;
2214 break;
2215#ifdef FEAT_FLOAT
2216 case ISN_PUSHF:
2217 tv->v_type = VAR_FLOAT;
2218 tv->vval.v_float = iptr->isn_arg.fnumber;
2219 break;
2220#endif
2221 case ISN_PUSHBLOB:
2222 blob_copy(iptr->isn_arg.blob, tv);
2223 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002224 case ISN_PUSHFUNC:
2225 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002226 if (iptr->isn_arg.string == NULL)
2227 tv->vval.v_string = NULL;
2228 else
2229 tv->vval.v_string =
2230 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002231 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002232 case ISN_PUSHCHANNEL:
2233#ifdef FEAT_JOB_CHANNEL
2234 tv->v_type = VAR_CHANNEL;
2235 tv->vval.v_channel = iptr->isn_arg.channel;
2236 if (tv->vval.v_channel != NULL)
2237 ++tv->vval.v_channel->ch_refcount;
2238#endif
2239 break;
2240 case ISN_PUSHJOB:
2241#ifdef FEAT_JOB_CHANNEL
2242 tv->v_type = VAR_JOB;
2243 tv->vval.v_job = iptr->isn_arg.job;
2244 if (tv->vval.v_job != NULL)
2245 ++tv->vval.v_job->jv_refcount;
2246#endif
2247 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002248 default:
2249 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002250 tv->vval.v_string = vim_strsave(
2251 iptr->isn_arg.string == NULL
2252 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002253 }
2254 break;
2255
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002256 case ISN_UNLET:
2257 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2258 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002259 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002260 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002261 case ISN_UNLETENV:
2262 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2263 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002264
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002265 case ISN_LOCKCONST:
2266 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2267 break;
2268
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002269 // create a list from items on the stack; uses a single allocation
2270 // for the list header and the items
2271 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002272 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2273 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002274 break;
2275
2276 // create a dict from items on the stack
2277 case ISN_NEWDICT:
2278 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002279 int count = iptr->isn_arg.number;
2280 dict_T *dict = dict_alloc();
2281 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002282 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002283
2284 if (dict == NULL)
2285 goto failed;
2286 for (idx = 0; idx < count; ++idx)
2287 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002288 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002289 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002290 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002291 key = tv->vval.v_string == NULL
2292 ? (char_u *)"" : tv->vval.v_string;
2293 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002294 if (item != NULL)
2295 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002296 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002297 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002298 dict_unref(dict);
2299 goto on_error;
2300 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002301 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002302 clear_tv(tv);
2303 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002304 {
2305 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002306 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002307 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2309 item->di_tv.v_lock = 0;
2310 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002311 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002312 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002313 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002314 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002315 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002316 }
2317
2318 if (count > 0)
2319 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002320 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 goto failed;
2322 else
2323 ++ectx.ec_stack.ga_len;
2324 tv = STACK_TV_BOT(-1);
2325 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002326 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 tv->vval.v_dict = dict;
2328 ++dict->dv_refcount;
2329 }
2330 break;
2331
2332 // call a :def function
2333 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002334 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002335 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002336 iptr->isn_arg.dfunc.cdf_argcount,
2337 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002338 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002339 break;
2340
2341 // call a builtin function
2342 case ISN_BCALL:
2343 SOURCING_LNUM = iptr->isn_lnum;
2344 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2345 iptr->isn_arg.bfunc.cbf_argcount,
2346 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002347 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002348 break;
2349
2350 // call a funcref or partial
2351 case ISN_PCALL:
2352 {
2353 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2354 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002355 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002356
2357 SOURCING_LNUM = iptr->isn_lnum;
2358 if (pfunc->cpf_top)
2359 {
2360 // funcref is above the arguments
2361 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2362 }
2363 else
2364 {
2365 // Get the funcref from the stack.
2366 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002367 partial_tv = *STACK_TV_BOT(0);
2368 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002369 }
2370 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002371 if (tv == &partial_tv)
2372 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002373 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002374 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 }
2376 break;
2377
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002378 case ISN_PCALL_END:
2379 // PCALL finished, arguments have been consumed and replaced by
2380 // the return value. Now clear the funcref from the stack,
2381 // and move the return value in its place.
2382 --ectx.ec_stack.ga_len;
2383 clear_tv(STACK_TV_BOT(-1));
2384 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2385 break;
2386
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002387 // call a user defined function or funcref/partial
2388 case ISN_UCALL:
2389 {
2390 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2391
2392 SOURCING_LNUM = iptr->isn_lnum;
2393 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002394 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002395 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 }
2397 break;
2398
2399 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002400 case ISN_RETURN_ZERO:
2401 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2402 goto failed;
2403 tv = STACK_TV_BOT(0);
2404 ++ectx.ec_stack.ga_len;
2405 tv->v_type = VAR_NUMBER;
2406 tv->vval.v_number = 0;
2407 tv->v_lock = 0;
2408 // FALLTHROUGH
2409
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410 case ISN_RETURN:
2411 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002412 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002413 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002414
2415 if (trystack->ga_len > 0)
2416 trycmd = ((trycmd_T *)trystack->ga_data)
2417 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002418 if (trycmd != NULL
2419 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002420 && trycmd->tcd_finally_idx != 0)
2421 {
2422 // jump to ":finally"
2423 ectx.ec_iidx = trycmd->tcd_finally_idx;
2424 trycmd->tcd_return = TRUE;
2425 }
2426 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002427 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 }
2429 break;
2430
2431 // push a function reference to a compiled function
2432 case ISN_FUNCREF:
2433 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002434 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2435 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2436 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 if (pt == NULL)
2439 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002440 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002441 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002442 vim_free(pt);
2443 goto failed;
2444 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002445 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2446 &ectx) == FAIL)
2447 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 tv = STACK_TV_BOT(0);
2450 ++ectx.ec_stack.ga_len;
2451 tv->vval.v_partial = pt;
2452 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002453 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 }
2455 break;
2456
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002457 // Create a global function from a lambda.
2458 case ISN_NEWFUNC:
2459 {
2460 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2461
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002462 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002463 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002464 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002465 }
2466 break;
2467
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002468 // List functions
2469 case ISN_DEF:
2470 if (iptr->isn_arg.string == NULL)
2471 list_functions(NULL);
2472 else
2473 {
2474 exarg_T ea;
2475
2476 CLEAR_FIELD(ea);
2477 ea.cmd = ea.arg = iptr->isn_arg.string;
2478 define_function(&ea, NULL);
2479 }
2480 break;
2481
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002482 // jump if a condition is met
2483 case ISN_JUMP:
2484 {
2485 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002486 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002487 int jump = TRUE;
2488
2489 if (when != JUMP_ALWAYS)
2490 {
2491 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002492 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002493 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002494 || when == JUMP_IF_COND_TRUE)
2495 {
2496 SOURCING_LNUM = iptr->isn_lnum;
2497 jump = tv_get_bool_chk(tv, &error);
2498 if (error)
2499 goto on_error;
2500 }
2501 else
2502 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002503 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002504 || when == JUMP_AND_KEEP_IF_FALSE
2505 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002506 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002507 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 {
2509 // drop the value from the stack
2510 clear_tv(tv);
2511 --ectx.ec_stack.ga_len;
2512 }
2513 }
2514 if (jump)
2515 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2516 }
2517 break;
2518
2519 // top of a for loop
2520 case ISN_FOR:
2521 {
2522 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2523 typval_T *idxtv =
2524 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2525
2526 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002527 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002528 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002529 ++idxtv->vval.v_number;
2530 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 // past the end of the list, jump to "endfor"
2532 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2533 else if (list->lv_first == &range_list_item)
2534 {
2535 // non-materialized range() list
2536 tv = STACK_TV_BOT(0);
2537 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002538 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 tv->vval.v_number = list_find_nr(
2540 list, idxtv->vval.v_number, NULL);
2541 ++ectx.ec_stack.ga_len;
2542 }
2543 else
2544 {
2545 listitem_T *li = list_find(list, idxtv->vval.v_number);
2546
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2548 ++ectx.ec_stack.ga_len;
2549 }
2550 }
2551 break;
2552
2553 // start of ":try" block
2554 case ISN_TRY:
2555 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002556 trycmd_T *trycmd = NULL;
2557
Bram Moolenaar270d0382020-05-15 21:42:53 +02002558 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002559 goto failed;
2560 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2561 + ectx.ec_trystack.ga_len;
2562 ++ectx.ec_trystack.ga_len;
2563 ++trylevel;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002564 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002565 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_catch;
2567 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_finally;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002568 trycmd->tcd_caught = FALSE;
Bram Moolenaar9939f572020-09-16 22:29:52 +02002569 trycmd->tcd_return = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002570 }
2571 break;
2572
2573 case ISN_PUSHEXC:
2574 if (current_exception == NULL)
2575 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002576 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002577 iemsg("Evaluating catch while current_exception is NULL");
2578 goto failed;
2579 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002580 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002581 goto failed;
2582 tv = STACK_TV_BOT(0);
2583 ++ectx.ec_stack.ga_len;
2584 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002585 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 tv->vval.v_string = vim_strsave(
2587 (char_u *)current_exception->value);
2588 break;
2589
2590 case ISN_CATCH:
2591 {
2592 garray_T *trystack = &ectx.ec_trystack;
2593
Bram Moolenaar20a76292020-12-25 19:47:24 +01002594 if (restore_cmdmod)
2595 {
2596 cmdmod.cmod_filter_regmatch.regprog = NULL;
2597 undo_cmdmod(&cmdmod);
2598 cmdmod = save_cmdmod;
2599 restore_cmdmod = FALSE;
2600 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002601 if (trystack->ga_len > 0)
2602 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002603 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 + trystack->ga_len - 1;
2605 trycmd->tcd_caught = TRUE;
2606 }
2607 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002608 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 catch_exception(current_exception);
2610 }
2611 break;
2612
2613 // end of ":try" block
2614 case ISN_ENDTRY:
2615 {
2616 garray_T *trystack = &ectx.ec_trystack;
2617
2618 if (trystack->ga_len > 0)
2619 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002620 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002621
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002622 --trystack->ga_len;
2623 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002624 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002625 trycmd = ((trycmd_T *)trystack->ga_data)
2626 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002627 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002628 {
2629 // discard the exception
2630 if (caught_stack == current_exception)
2631 caught_stack = caught_stack->caught;
2632 discard_current_exception();
2633 }
2634
2635 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002636 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002637
2638 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2639 {
2640 --ectx.ec_stack.ga_len;
2641 clear_tv(STACK_TV_BOT(0));
2642 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 }
2644 }
2645 break;
2646
2647 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002648 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002649 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002650
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002651 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2652 {
2653 // throwing an exception while using "silent!" causes
2654 // the function to abort but not display an error.
2655 tv = STACK_TV_BOT(-1);
2656 clear_tv(tv);
2657 tv->v_type = VAR_NUMBER;
2658 tv->vval.v_number = 0;
2659 goto done;
2660 }
2661 --ectx.ec_stack.ga_len;
2662 tv = STACK_TV_BOT(0);
2663 if (tv->vval.v_string == NULL
2664 || *skipwhite(tv->vval.v_string) == NUL)
2665 {
2666 vim_free(tv->vval.v_string);
2667 SOURCING_LNUM = iptr->isn_lnum;
2668 emsg(_(e_throw_with_empty_string));
2669 goto failed;
2670 }
2671
2672 // Inside a "catch" we need to first discard the caught
2673 // exception.
2674 if (trystack->ga_len > 0)
2675 {
2676 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2677 + trystack->ga_len - 1;
2678 if (trycmd->tcd_caught && current_exception != NULL)
2679 {
2680 // discard the exception
2681 if (caught_stack == current_exception)
2682 caught_stack = caught_stack->caught;
2683 discard_current_exception();
2684 trycmd->tcd_caught = FALSE;
2685 }
2686 }
2687
2688 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2689 == FAIL)
2690 {
2691 vim_free(tv->vval.v_string);
2692 goto failed;
2693 }
2694 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002695 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 break;
2697
2698 // compare with special values
2699 case ISN_COMPAREBOOL:
2700 case ISN_COMPARESPECIAL:
2701 {
2702 typval_T *tv1 = STACK_TV_BOT(-2);
2703 typval_T *tv2 = STACK_TV_BOT(-1);
2704 varnumber_T arg1 = tv1->vval.v_number;
2705 varnumber_T arg2 = tv2->vval.v_number;
2706 int res;
2707
2708 switch (iptr->isn_arg.op.op_type)
2709 {
2710 case EXPR_EQUAL: res = arg1 == arg2; break;
2711 case EXPR_NEQUAL: res = arg1 != arg2; break;
2712 default: res = 0; break;
2713 }
2714
2715 --ectx.ec_stack.ga_len;
2716 tv1->v_type = VAR_BOOL;
2717 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2718 }
2719 break;
2720
2721 // Operation with two number arguments
2722 case ISN_OPNR:
2723 case ISN_COMPARENR:
2724 {
2725 typval_T *tv1 = STACK_TV_BOT(-2);
2726 typval_T *tv2 = STACK_TV_BOT(-1);
2727 varnumber_T arg1 = tv1->vval.v_number;
2728 varnumber_T arg2 = tv2->vval.v_number;
2729 varnumber_T res;
2730
2731 switch (iptr->isn_arg.op.op_type)
2732 {
2733 case EXPR_MULT: res = arg1 * arg2; break;
2734 case EXPR_DIV: res = arg1 / arg2; break;
2735 case EXPR_REM: res = arg1 % arg2; break;
2736 case EXPR_SUB: res = arg1 - arg2; break;
2737 case EXPR_ADD: res = arg1 + arg2; break;
2738
2739 case EXPR_EQUAL: res = arg1 == arg2; break;
2740 case EXPR_NEQUAL: res = arg1 != arg2; break;
2741 case EXPR_GREATER: res = arg1 > arg2; break;
2742 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2743 case EXPR_SMALLER: res = arg1 < arg2; break;
2744 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2745 default: res = 0; break;
2746 }
2747
2748 --ectx.ec_stack.ga_len;
2749 if (iptr->isn_type == ISN_COMPARENR)
2750 {
2751 tv1->v_type = VAR_BOOL;
2752 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2753 }
2754 else
2755 tv1->vval.v_number = res;
2756 }
2757 break;
2758
2759 // Computation with two float arguments
2760 case ISN_OPFLOAT:
2761 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002762#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002763 {
2764 typval_T *tv1 = STACK_TV_BOT(-2);
2765 typval_T *tv2 = STACK_TV_BOT(-1);
2766 float_T arg1 = tv1->vval.v_float;
2767 float_T arg2 = tv2->vval.v_float;
2768 float_T res = 0;
2769 int cmp = FALSE;
2770
2771 switch (iptr->isn_arg.op.op_type)
2772 {
2773 case EXPR_MULT: res = arg1 * arg2; break;
2774 case EXPR_DIV: res = arg1 / arg2; break;
2775 case EXPR_SUB: res = arg1 - arg2; break;
2776 case EXPR_ADD: res = arg1 + arg2; break;
2777
2778 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2779 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2780 case EXPR_GREATER: cmp = arg1 > arg2; break;
2781 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2782 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2783 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2784 default: cmp = 0; break;
2785 }
2786 --ectx.ec_stack.ga_len;
2787 if (iptr->isn_type == ISN_COMPAREFLOAT)
2788 {
2789 tv1->v_type = VAR_BOOL;
2790 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2791 }
2792 else
2793 tv1->vval.v_float = res;
2794 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002795#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002796 break;
2797
2798 case ISN_COMPARELIST:
2799 {
2800 typval_T *tv1 = STACK_TV_BOT(-2);
2801 typval_T *tv2 = STACK_TV_BOT(-1);
2802 list_T *arg1 = tv1->vval.v_list;
2803 list_T *arg2 = tv2->vval.v_list;
2804 int cmp = FALSE;
2805 int ic = iptr->isn_arg.op.op_ic;
2806
2807 switch (iptr->isn_arg.op.op_type)
2808 {
2809 case EXPR_EQUAL: cmp =
2810 list_equal(arg1, arg2, ic, FALSE); break;
2811 case EXPR_NEQUAL: cmp =
2812 !list_equal(arg1, arg2, ic, FALSE); break;
2813 case EXPR_IS: cmp = arg1 == arg2; break;
2814 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2815 default: cmp = 0; break;
2816 }
2817 --ectx.ec_stack.ga_len;
2818 clear_tv(tv1);
2819 clear_tv(tv2);
2820 tv1->v_type = VAR_BOOL;
2821 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2822 }
2823 break;
2824
2825 case ISN_COMPAREBLOB:
2826 {
2827 typval_T *tv1 = STACK_TV_BOT(-2);
2828 typval_T *tv2 = STACK_TV_BOT(-1);
2829 blob_T *arg1 = tv1->vval.v_blob;
2830 blob_T *arg2 = tv2->vval.v_blob;
2831 int cmp = FALSE;
2832
2833 switch (iptr->isn_arg.op.op_type)
2834 {
2835 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2836 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2837 case EXPR_IS: cmp = arg1 == arg2; break;
2838 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2839 default: cmp = 0; break;
2840 }
2841 --ectx.ec_stack.ga_len;
2842 clear_tv(tv1);
2843 clear_tv(tv2);
2844 tv1->v_type = VAR_BOOL;
2845 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2846 }
2847 break;
2848
2849 // TODO: handle separately
2850 case ISN_COMPARESTRING:
2851 case ISN_COMPAREDICT:
2852 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002853 case ISN_COMPAREANY:
2854 {
2855 typval_T *tv1 = STACK_TV_BOT(-2);
2856 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01002857 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002858 int ic = iptr->isn_arg.op.op_ic;
2859
Bram Moolenaareb26f432020-09-14 16:50:05 +02002860 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01002861 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002862 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002863 --ectx.ec_stack.ga_len;
2864 }
2865 break;
2866
2867 case ISN_ADDLIST:
2868 case ISN_ADDBLOB:
2869 {
2870 typval_T *tv1 = STACK_TV_BOT(-2);
2871 typval_T *tv2 = STACK_TV_BOT(-1);
2872
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002873 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002874 if (iptr->isn_type == ISN_ADDLIST)
2875 eval_addlist(tv1, tv2);
2876 else
2877 eval_addblob(tv1, tv2);
2878 clear_tv(tv2);
2879 --ectx.ec_stack.ga_len;
2880 }
2881 break;
2882
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002883 case ISN_LISTAPPEND:
2884 {
2885 typval_T *tv1 = STACK_TV_BOT(-2);
2886 typval_T *tv2 = STACK_TV_BOT(-1);
2887 list_T *l = tv1->vval.v_list;
2888
2889 // add an item to a list
2890 if (l == NULL)
2891 {
2892 SOURCING_LNUM = iptr->isn_lnum;
2893 emsg(_(e_cannot_add_to_null_list));
2894 goto on_error;
2895 }
2896 if (list_append_tv(l, tv2) == FAIL)
2897 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02002898 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02002899 --ectx.ec_stack.ga_len;
2900 }
2901 break;
2902
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02002903 case ISN_BLOBAPPEND:
2904 {
2905 typval_T *tv1 = STACK_TV_BOT(-2);
2906 typval_T *tv2 = STACK_TV_BOT(-1);
2907 blob_T *b = tv1->vval.v_blob;
2908 int error = FALSE;
2909 varnumber_T n;
2910
2911 // add a number to a blob
2912 if (b == NULL)
2913 {
2914 SOURCING_LNUM = iptr->isn_lnum;
2915 emsg(_(e_cannot_add_to_null_blob));
2916 goto on_error;
2917 }
2918 n = tv_get_number_chk(tv2, &error);
2919 if (error)
2920 goto on_error;
2921 ga_append(&b->bv_ga, (int)n);
2922 --ectx.ec_stack.ga_len;
2923 }
2924 break;
2925
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002926 // Computation with two arguments of unknown type
2927 case ISN_OPANY:
2928 {
2929 typval_T *tv1 = STACK_TV_BOT(-2);
2930 typval_T *tv2 = STACK_TV_BOT(-1);
2931 varnumber_T n1, n2;
2932#ifdef FEAT_FLOAT
2933 float_T f1 = 0, f2 = 0;
2934#endif
2935 int error = FALSE;
2936
2937 if (iptr->isn_arg.op.op_type == EXPR_ADD)
2938 {
2939 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
2940 {
2941 eval_addlist(tv1, tv2);
2942 clear_tv(tv2);
2943 --ectx.ec_stack.ga_len;
2944 break;
2945 }
2946 else if (tv1->v_type == VAR_BLOB
2947 && tv2->v_type == VAR_BLOB)
2948 {
2949 eval_addblob(tv1, tv2);
2950 clear_tv(tv2);
2951 --ectx.ec_stack.ga_len;
2952 break;
2953 }
2954 }
2955#ifdef FEAT_FLOAT
2956 if (tv1->v_type == VAR_FLOAT)
2957 {
2958 f1 = tv1->vval.v_float;
2959 n1 = 0;
2960 }
2961 else
2962#endif
2963 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01002964 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 n1 = tv_get_number_chk(tv1, &error);
2966 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002967 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002968#ifdef FEAT_FLOAT
2969 if (tv2->v_type == VAR_FLOAT)
2970 f1 = n1;
2971#endif
2972 }
2973#ifdef FEAT_FLOAT
2974 if (tv2->v_type == VAR_FLOAT)
2975 {
2976 f2 = tv2->vval.v_float;
2977 n2 = 0;
2978 }
2979 else
2980#endif
2981 {
2982 n2 = tv_get_number_chk(tv2, &error);
2983 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02002984 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002985#ifdef FEAT_FLOAT
2986 if (tv1->v_type == VAR_FLOAT)
2987 f2 = n2;
2988#endif
2989 }
2990#ifdef FEAT_FLOAT
2991 // if there is a float on either side the result is a float
2992 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
2993 {
2994 switch (iptr->isn_arg.op.op_type)
2995 {
2996 case EXPR_MULT: f1 = f1 * f2; break;
2997 case EXPR_DIV: f1 = f1 / f2; break;
2998 case EXPR_SUB: f1 = f1 - f2; break;
2999 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003000 default: SOURCING_LNUM = iptr->isn_lnum;
3001 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003002 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003003 }
3004 clear_tv(tv1);
3005 clear_tv(tv2);
3006 tv1->v_type = VAR_FLOAT;
3007 tv1->vval.v_float = f1;
3008 --ectx.ec_stack.ga_len;
3009 }
3010 else
3011#endif
3012 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003013 int failed = FALSE;
3014
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003015 switch (iptr->isn_arg.op.op_type)
3016 {
3017 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003018 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3019 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003020 goto on_error;
3021 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 case EXPR_SUB: n1 = n1 - n2; break;
3023 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003024 default: n1 = num_modulus(n1, n2, &failed);
3025 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003026 goto on_error;
3027 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003028 }
3029 clear_tv(tv1);
3030 clear_tv(tv2);
3031 tv1->v_type = VAR_NUMBER;
3032 tv1->vval.v_number = n1;
3033 --ectx.ec_stack.ga_len;
3034 }
3035 }
3036 break;
3037
3038 case ISN_CONCAT:
3039 {
3040 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3041 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3042 char_u *res;
3043
3044 res = concat_str(str1, str2);
3045 clear_tv(STACK_TV_BOT(-2));
3046 clear_tv(STACK_TV_BOT(-1));
3047 --ectx.ec_stack.ga_len;
3048 STACK_TV_BOT(-1)->vval.v_string = res;
3049 }
3050 break;
3051
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003052 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003053 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003054 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003055 int is_slice = iptr->isn_type == ISN_STRSLICE;
3056 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003057 char_u *res;
3058
3059 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003060 // string slice: string is at stack-3, first index at
3061 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003062 if (is_slice)
3063 {
3064 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003065 n1 = tv->vval.v_number;
3066 }
3067
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003068 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003069 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003070
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003071 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003072 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003073 if (is_slice)
3074 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003075 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003076 else
3077 // Index: The resulting variable is a string of a
3078 // single character. If the index is too big or
3079 // negative the result is empty.
3080 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003081 vim_free(tv->vval.v_string);
3082 tv->vval.v_string = res;
3083 }
3084 break;
3085
3086 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003087 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003088 {
Bram Moolenaared591872020-08-15 22:14:53 +02003089 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003090 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003091 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003092
3093 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003094 // list slice: list is at stack-3, indexes at stack-2 and
3095 // stack-1
3096 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003097 list = tv->vval.v_list;
3098
3099 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003100 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003101 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003102
3103 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003104 {
Bram Moolenaared591872020-08-15 22:14:53 +02003105 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003106 n1 = tv->vval.v_number;
3107 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003108 }
Bram Moolenaared591872020-08-15 22:14:53 +02003109
3110 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003111 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003112 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003113 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3114 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003115 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003116 }
3117 break;
3118
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003119 case ISN_ANYINDEX:
3120 case ISN_ANYSLICE:
3121 {
3122 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3123 typval_T *var1, *var2;
3124 int res;
3125
3126 // index: composite is at stack-2, index at stack-1
3127 // slice: composite is at stack-3, indexes at stack-2 and
3128 // stack-1
3129 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003130 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003131 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3132 goto on_error;
3133 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3134 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003135 res = eval_index_inner(tv, is_slice, var1, var2,
3136 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003137 clear_tv(var1);
3138 if (is_slice)
3139 clear_tv(var2);
3140 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3141 if (res == FAIL)
3142 goto on_error;
3143 }
3144 break;
3145
Bram Moolenaar9af78762020-06-16 11:34:42 +02003146 case ISN_SLICE:
3147 {
3148 list_T *list;
3149 int count = iptr->isn_arg.number;
3150
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003151 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003152 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003153 list = tv->vval.v_list;
3154
3155 // no error for short list, expect it to be checked earlier
3156 if (list != NULL && list->lv_len >= count)
3157 {
3158 list_T *newlist = list_slice(list,
3159 count, list->lv_len - 1);
3160
3161 if (newlist != NULL)
3162 {
3163 list_unref(list);
3164 tv->vval.v_list = newlist;
3165 ++newlist->lv_refcount;
3166 }
3167 }
3168 }
3169 break;
3170
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003171 case ISN_GETITEM:
3172 {
3173 listitem_T *li;
3174 int index = iptr->isn_arg.number;
3175
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003176 // Get list item: list is at stack-1, push item.
3177 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003178 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003179 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003180
3181 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3182 goto failed;
3183 ++ectx.ec_stack.ga_len;
3184 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003185
3186 // Useful when used in unpack assignment. Reset at
3187 // ISN_DROP.
3188 where.wt_index = index + 1;
3189 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003190 }
3191 break;
3192
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003193 case ISN_MEMBER:
3194 {
3195 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003196 char_u *key;
3197 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003198 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003199
3200 // dict member: dict is at stack-2, key at stack-1
3201 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003202 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003203 dict = tv->vval.v_dict;
3204
3205 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003206 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003207 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003208 if (key == NULL)
3209 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003210
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003211 if ((di = dict_find(dict, key, -1)) == NULL)
3212 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003213 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003214 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003215
3216 // If :silent! is used we will continue, make sure the
3217 // stack contents makes sense.
3218 clear_tv(tv);
3219 --ectx.ec_stack.ga_len;
3220 tv = STACK_TV_BOT(-1);
3221 clear_tv(tv);
3222 tv->v_type = VAR_NUMBER;
3223 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003224 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003225 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003226 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003227 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003228 // Clear the dict only after getting the item, to avoid
3229 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003230 tv = STACK_TV_BOT(-1);
3231 temp_tv = *tv;
3232 copy_tv(&di->di_tv, tv);
3233 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003234 }
3235 break;
3236
3237 // dict member with string key
3238 case ISN_STRINGMEMBER:
3239 {
3240 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003241 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003242 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243
3244 tv = STACK_TV_BOT(-1);
3245 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3246 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003247 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003248 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003249 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 }
3251 dict = tv->vval.v_dict;
3252
3253 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3254 == NULL)
3255 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003256 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003258 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003260 // Clear the dict after getting the item, to avoid that it
3261 // make the item invalid.
3262 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003263 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003264 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 }
3266 break;
3267
3268 case ISN_NEGATENR:
3269 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003270 if (tv->v_type != VAR_NUMBER
3271#ifdef FEAT_FLOAT
3272 && tv->v_type != VAR_FLOAT
3273#endif
3274 )
3275 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003277 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003278 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003279 }
3280#ifdef FEAT_FLOAT
3281 if (tv->v_type == VAR_FLOAT)
3282 tv->vval.v_float = -tv->vval.v_float;
3283 else
3284#endif
3285 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 break;
3287
3288 case ISN_CHECKNR:
3289 {
3290 int error = FALSE;
3291
3292 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003293 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003294 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003295 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003296 (void)tv_get_number_chk(tv, &error);
3297 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003298 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003299 }
3300 break;
3301
3302 case ISN_CHECKTYPE:
3303 {
3304 checktype_T *ct = &iptr->isn_arg.type;
3305
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003306 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003308 if (!where.wt_variable)
3309 where.wt_index = ct->ct_arg_idx;
3310 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003311 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003312 if (!where.wt_variable)
3313 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003314
3315 // number 0 is FALSE, number 1 is TRUE
3316 if (tv->v_type == VAR_NUMBER
3317 && ct->ct_type->tt_type == VAR_BOOL
3318 && (tv->vval.v_number == 0
3319 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003320 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003321 tv->v_type = VAR_BOOL;
3322 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003323 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003324 }
3325 }
3326 break;
3327
Bram Moolenaar9af78762020-06-16 11:34:42 +02003328 case ISN_CHECKLEN:
3329 {
3330 int min_len = iptr->isn_arg.checklen.cl_min_len;
3331 list_T *list = NULL;
3332
3333 tv = STACK_TV_BOT(-1);
3334 if (tv->v_type == VAR_LIST)
3335 list = tv->vval.v_list;
3336 if (list == NULL || list->lv_len < min_len
3337 || (list->lv_len > min_len
3338 && !iptr->isn_arg.checklen.cl_more_OK))
3339 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003340 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003341 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003342 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003343 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003344 }
3345 }
3346 break;
3347
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003348 case ISN_SETTYPE:
3349 {
3350 checktype_T *ct = &iptr->isn_arg.type;
3351
3352 tv = STACK_TV_BOT(-1);
3353 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3354 {
3355 free_type(tv->vval.v_dict->dv_type);
3356 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3357 }
3358 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3359 {
3360 free_type(tv->vval.v_list->lv_type);
3361 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3362 }
3363 }
3364 break;
3365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003366 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003367 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003368 {
3369 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003370 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003371
3372 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003373 if (iptr->isn_type == ISN_2BOOL)
3374 {
3375 n = tv2bool(tv);
3376 if (iptr->isn_arg.number) // invert
3377 n = !n;
3378 }
3379 else
3380 {
3381 SOURCING_LNUM = iptr->isn_lnum;
3382 n = tv_get_bool_chk(tv, &error);
3383 if (error)
3384 goto on_error;
3385 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003386 clear_tv(tv);
3387 tv->v_type = VAR_BOOL;
3388 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3389 }
3390 break;
3391
3392 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003393 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003394 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003395 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3396 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3397 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003398 break;
3399
Bram Moolenaar08597872020-12-10 19:43:40 +01003400 case ISN_RANGE:
3401 {
3402 exarg_T ea;
3403 char *errormsg;
3404
Bram Moolenaarece0b872021-01-08 20:40:45 +01003405 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003406 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003407 ea.addr_type = ADDR_LINES;
3408 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003409 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003410 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003411 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003412
3413 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3414 goto failed;
3415 ++ectx.ec_stack.ga_len;
3416 tv = STACK_TV_BOT(-1);
3417 tv->v_type = VAR_NUMBER;
3418 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003419 if (ea.addr_count == 0)
3420 tv->vval.v_number = curwin->w_cursor.lnum;
3421 else
3422 tv->vval.v_number = ea.line2;
3423 }
3424 break;
3425
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003426 case ISN_PUT:
3427 {
3428 int regname = iptr->isn_arg.put.put_regname;
3429 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3430 char_u *expr = NULL;
3431 int dir = FORWARD;
3432
Bram Moolenaar08597872020-12-10 19:43:40 +01003433 if (lnum < -2)
3434 {
3435 // line number was put on the stack by ISN_RANGE
3436 tv = STACK_TV_BOT(-1);
3437 curwin->w_cursor.lnum = tv->vval.v_number;
3438 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3439 dir = BACKWARD;
3440 --ectx.ec_stack.ga_len;
3441 }
3442 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003443 // :put! above cursor
3444 dir = BACKWARD;
3445 else if (lnum >= 0)
3446 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003447
3448 if (regname == '=')
3449 {
3450 tv = STACK_TV_BOT(-1);
3451 if (tv->v_type == VAR_STRING)
3452 expr = tv->vval.v_string;
3453 else
3454 {
3455 expr = typval2string(tv, TRUE); // allocates value
3456 clear_tv(tv);
3457 }
3458 --ectx.ec_stack.ga_len;
3459 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003460 check_cursor();
3461 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3462 vim_free(expr);
3463 }
3464 break;
3465
Bram Moolenaar02194d22020-10-24 23:08:38 +02003466 case ISN_CMDMOD:
3467 save_cmdmod = cmdmod;
3468 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003469 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003470 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3471 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003472 break;
3473
Bram Moolenaar02194d22020-10-24 23:08:38 +02003474 case ISN_CMDMOD_REV:
3475 // filter regprog is owned by the instruction, don't free it
3476 cmdmod.cmod_filter_regmatch.regprog = NULL;
3477 undo_cmdmod(&cmdmod);
3478 cmdmod = save_cmdmod;
3479 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003480 break;
3481
Bram Moolenaar792f7862020-11-23 08:31:18 +01003482 case ISN_UNPACK:
3483 {
3484 int count = iptr->isn_arg.unpack.unp_count;
3485 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3486 list_T *l;
3487 listitem_T *li;
3488 int i;
3489
3490 // Check there is a valid list to unpack.
3491 tv = STACK_TV_BOT(-1);
3492 if (tv->v_type != VAR_LIST)
3493 {
3494 SOURCING_LNUM = iptr->isn_lnum;
3495 emsg(_(e_for_argument_must_be_sequence_of_lists));
3496 goto on_error;
3497 }
3498 l = tv->vval.v_list;
3499 if (l == NULL
3500 || l->lv_len < (semicolon ? count - 1 : count))
3501 {
3502 SOURCING_LNUM = iptr->isn_lnum;
3503 emsg(_(e_list_value_does_not_have_enough_items));
3504 goto on_error;
3505 }
3506 else if (!semicolon && l->lv_len > count)
3507 {
3508 SOURCING_LNUM = iptr->isn_lnum;
3509 emsg(_(e_list_value_has_more_items_than_targets));
3510 goto on_error;
3511 }
3512
3513 CHECK_LIST_MATERIALIZE(l);
3514 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3515 goto failed;
3516 ectx.ec_stack.ga_len += count - 1;
3517
3518 // Variable after semicolon gets a list with the remaining
3519 // items.
3520 if (semicolon)
3521 {
3522 list_T *rem_list =
3523 list_alloc_with_items(l->lv_len - count + 1);
3524
3525 if (rem_list == NULL)
3526 goto failed;
3527 tv = STACK_TV_BOT(-count);
3528 tv->vval.v_list = rem_list;
3529 ++rem_list->lv_refcount;
3530 tv->v_lock = 0;
3531 li = l->lv_first;
3532 for (i = 0; i < count - 1; ++i)
3533 li = li->li_next;
3534 for (i = 0; li != NULL; ++i)
3535 {
3536 list_set_item(rem_list, i, &li->li_tv);
3537 li = li->li_next;
3538 }
3539 --count;
3540 }
3541
3542 // Produce the values in reverse order, first item last.
3543 li = l->lv_first;
3544 for (i = 0; i < count; ++i)
3545 {
3546 tv = STACK_TV_BOT(-i - 1);
3547 copy_tv(&li->li_tv, tv);
3548 li = li->li_next;
3549 }
3550
3551 list_unref(l);
3552 }
3553 break;
3554
Bram Moolenaarb2049902021-01-24 12:53:53 +01003555 case ISN_PROF_START:
3556 case ISN_PROF_END:
3557 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003558#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003559 funccall_T cookie;
3560 ufunc_T *cur_ufunc =
3561 (((dfunc_T *)def_functions.ga_data)
3562 + ectx.ec_dfunc_idx)->df_ufunc;
3563
3564 cookie.func = cur_ufunc;
3565 if (iptr->isn_type == ISN_PROF_START)
3566 {
3567 func_line_start(&cookie, iptr->isn_lnum);
3568 // if we get here the instruction is executed
3569 func_line_exec(&cookie);
3570 }
3571 else
3572 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003573#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003574 }
3575 break;
3576
Bram Moolenaar389df252020-07-09 21:20:47 +02003577 case ISN_SHUFFLE:
3578 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003579 typval_T tmp_tv;
3580 int item = iptr->isn_arg.shuffle.shfl_item;
3581 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003582
3583 tmp_tv = *STACK_TV_BOT(-item);
3584 for ( ; up > 0 && item > 1; --up)
3585 {
3586 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3587 --item;
3588 }
3589 *STACK_TV_BOT(-item) = tmp_tv;
3590 }
3591 break;
3592
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003593 case ISN_DROP:
3594 --ectx.ec_stack.ga_len;
3595 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003596 where.wt_index = 0;
3597 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003598 break;
3599 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003600 continue;
3601
Bram Moolenaard032f342020-07-18 18:13:02 +02003602func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003603 // Restore previous function. If the frame pointer is where we started
3604 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003605 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003606 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003607
Bram Moolenaard032f342020-07-18 18:13:02 +02003608 if (func_return(&ectx) == FAIL)
3609 // only fails when out of memory
3610 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003611 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003612
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003613on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003614 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003615 // If "emsg_silent" is set then ignore the error, unless it was set
3616 // when calling the function.
3617 if (did_emsg_cumul + did_emsg == did_emsg_before
3618 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003619 {
3620 // If a sequence of instructions causes an error while ":silent!"
3621 // was used, restore the stack length and jump ahead to restoring
3622 // the cmdmod.
3623 if (restore_cmdmod)
3624 {
3625 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3626 {
3627 --ectx.ec_stack.ga_len;
3628 clear_tv(STACK_TV_BOT(0));
3629 }
3630 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3631 ++ectx.ec_iidx;
3632 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003633 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003634 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003635on_fatal_error:
3636 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003637 // If we are not inside a try-catch started here, abort execution.
3638 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003639 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003640 }
3641
3642done:
3643 // function finished, get result from the stack.
3644 tv = STACK_TV_BOT(-1);
3645 *rettv = *tv;
3646 tv->v_type = VAR_UNKNOWN;
3647 ret = OK;
3648
3649failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003650 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003651 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003652 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003653
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003654 // Deal with any remaining closures, they may be in use somewhere.
3655 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003656 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003657 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003658 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3659 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003660
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003661 estack_pop();
3662 current_sctx = save_current_sctx;
3663
Bram Moolenaar352134b2020-10-17 22:04:08 +02003664 if (*msg_list != NULL && saved_msg_list != NULL)
3665 {
3666 msglist_T **plist = saved_msg_list;
3667
3668 // Append entries from the current msg_list (uncaught exceptions) to
3669 // the saved msg_list.
3670 while (*plist != NULL)
3671 plist = &(*plist)->next;
3672
3673 *plist = *msg_list;
3674 }
3675 msg_list = saved_msg_list;
3676
Bram Moolenaar02194d22020-10-24 23:08:38 +02003677 if (restore_cmdmod)
3678 {
3679 cmdmod.cmod_filter_regmatch.regprog = NULL;
3680 undo_cmdmod(&cmdmod);
3681 cmdmod = save_cmdmod;
3682 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003683 emsg_silent_def = save_emsg_silent_def;
3684 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003685
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003686failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003687 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003688 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3689 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003690
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003691 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003692 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003693
Bram Moolenaar0186e582021-01-10 18:33:11 +01003694 while (ectx.ec_outer != NULL)
3695 {
3696 outer_T *up = ectx.ec_outer->out_up_is_copy
3697 ? NULL : ectx.ec_outer->out_up;
3698
3699 vim_free(ectx.ec_outer);
3700 ectx.ec_outer = up;
3701 }
3702
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003703 // Not sure if this is necessary.
3704 suppress_errthrow = save_suppress_errthrow;
3705
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003706 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003707 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003708 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003709 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003710 return ret;
3711}
3712
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003713/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003714 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003715 * We don't really need this at runtime, but we do have tests that require it,
3716 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003717 */
3718 void
3719ex_disassemble(exarg_T *eap)
3720{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003721 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003722 char_u *fname;
3723 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003724 dfunc_T *dfunc;
3725 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003726 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003727 int current;
3728 int line_idx = 0;
3729 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003730 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003731
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003732 if (STRNCMP(arg, "<lambda>", 8) == 0)
3733 {
3734 arg += 8;
3735 (void)getdigits(&arg);
3736 fname = vim_strnsave(eap->arg, arg - eap->arg);
3737 }
3738 else
3739 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003740 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003741 if (fname == NULL)
3742 {
3743 semsg(_(e_invarg2), eap->arg);
3744 return;
3745 }
3746
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003747 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003748 if (ufunc == NULL)
3749 {
3750 char_u *p = untrans_function_name(fname);
3751
3752 if (p != NULL)
3753 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003754 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003755 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003756 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003757 if (ufunc == NULL)
3758 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003759 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003760 return;
3761 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003762 if (func_needs_compiling(ufunc, eap->forceit)
3763 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003764 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003765 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003766 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003767 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003768 return;
3769 }
3770 if (ufunc->uf_name_exp != NULL)
3771 msg((char *)ufunc->uf_name_exp);
3772 else
3773 msg((char *)ufunc->uf_name);
3774
3775 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003776#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003777 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3778 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3779 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003780#else
3781 instr = dfunc->df_instr;
3782 instr_count = dfunc->df_instr_count;
3783#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003784 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003785 {
3786 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003787 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003788
3789 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3790 {
3791 if (current > prev_current)
3792 {
3793 msg_puts("\n\n");
3794 prev_current = current;
3795 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003796 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3797 if (line != NULL)
3798 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003799 }
3800
3801 switch (iptr->isn_type)
3802 {
3803 case ISN_EXEC:
3804 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3805 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003806 case ISN_EXECCONCAT:
3807 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003808 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003809 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003810 case ISN_ECHO:
3811 {
3812 echo_T *echo = &iptr->isn_arg.echo;
3813
3814 smsg("%4d %s %d", current,
3815 echo->echo_with_white ? "ECHO" : "ECHON",
3816 echo->echo_count);
3817 }
3818 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003819 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003820 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003821 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003822 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003823 case ISN_ECHOMSG:
3824 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003825 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003826 break;
3827 case ISN_ECHOERR:
3828 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003829 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003830 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003831 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003832 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003833 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003834 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003835 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003836 + STACK_FRAME_SIZE));
3837 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003838 smsg("%4d LOAD $%lld", current,
3839 (varnumber_T)(iptr->isn_arg.number));
3840 }
3841 break;
3842 case ISN_LOADOUTER:
3843 {
3844 if (iptr->isn_arg.number < 0)
3845 smsg("%4d LOADOUTER level %d arg[%d]", current,
3846 iptr->isn_arg.outer.outer_depth,
3847 iptr->isn_arg.outer.outer_idx
3848 + STACK_FRAME_SIZE);
3849 else
3850 smsg("%4d LOADOUTER level %d $%d", current,
3851 iptr->isn_arg.outer.outer_depth,
3852 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003853 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003854 break;
3855 case ISN_LOADV:
3856 smsg("%4d LOADV v:%s", current,
3857 get_vim_var_name(iptr->isn_arg.number));
3858 break;
3859 case ISN_LOADSCRIPT:
3860 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003861 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3862 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003864 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003866 smsg("%4d LOADSCRIPT %s-%d from %s", current,
3867 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003868 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003869 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 }
3871 break;
3872 case ISN_LOADS:
3873 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003874 scriptitem_T *si = SCRIPT_ITEM(
3875 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876
3877 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003878 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 }
3880 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003881 case ISN_LOADAUTO:
3882 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
3883 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003884 case ISN_LOADG:
3885 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
3886 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003887 case ISN_LOADB:
3888 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
3889 break;
3890 case ISN_LOADW:
3891 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
3892 break;
3893 case ISN_LOADT:
3894 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
3895 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02003896 case ISN_LOADGDICT:
3897 smsg("%4d LOAD g:", current);
3898 break;
3899 case ISN_LOADBDICT:
3900 smsg("%4d LOAD b:", current);
3901 break;
3902 case ISN_LOADWDICT:
3903 smsg("%4d LOAD w:", current);
3904 break;
3905 case ISN_LOADTDICT:
3906 smsg("%4d LOAD t:", current);
3907 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908 case ISN_LOADOPT:
3909 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
3910 break;
3911 case ISN_LOADENV:
3912 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
3913 break;
3914 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003915 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003916 break;
3917
3918 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01003919 if (iptr->isn_arg.number < 0)
3920 smsg("%4d STORE arg[%lld]", current,
3921 iptr->isn_arg.number + STACK_FRAME_SIZE);
3922 else
3923 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
3924 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003925 case ISN_STOREOUTER:
3926 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003927 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003928 smsg("%4d STOREOUTEr level %d arg[%d]", current,
3929 iptr->isn_arg.outer.outer_depth,
3930 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01003931 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003932 smsg("%4d STOREOUTER level %d $%d", current,
3933 iptr->isn_arg.outer.outer_depth,
3934 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02003935 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003936 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003937 case ISN_STOREV:
3938 smsg("%4d STOREV v:%s", current,
3939 get_vim_var_name(iptr->isn_arg.number));
3940 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01003941 case ISN_STOREAUTO:
3942 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
3943 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003944 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003945 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
3946 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02003947 case ISN_STOREB:
3948 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
3949 break;
3950 case ISN_STOREW:
3951 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
3952 break;
3953 case ISN_STORET:
3954 smsg("%4d STORET %s", current, iptr->isn_arg.string);
3955 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003956 case ISN_STORES:
3957 {
3958 scriptitem_T *si = SCRIPT_ITEM(
3959 iptr->isn_arg.loadstore.ls_sid);
3960
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01003961 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02003962 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003963 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003964 break;
3965 case ISN_STORESCRIPT:
3966 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003967 scriptref_T *sref = iptr->isn_arg.script.scriptref;
3968 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003969 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003970 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003971
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003972 smsg("%4d STORESCRIPT %s-%d in %s", current,
3973 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01003974 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02003975 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 }
3977 break;
3978 case ISN_STOREOPT:
3979 smsg("%4d STOREOPT &%s", current,
3980 iptr->isn_arg.storeopt.so_name);
3981 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003982 case ISN_STOREENV:
3983 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
3984 break;
3985 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01003986 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01003987 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003988 case ISN_STORENR:
3989 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01003990 iptr->isn_arg.storenr.stnr_val,
3991 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003992 break;
3993
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003994 case ISN_STOREINDEX:
3995 switch (iptr->isn_arg.vartype)
3996 {
3997 case VAR_LIST:
3998 smsg("%4d STORELIST", current);
3999 break;
4000 case VAR_DICT:
4001 smsg("%4d STOREDICT", current);
4002 break;
4003 case VAR_ANY:
4004 smsg("%4d STOREINDEX", current);
4005 break;
4006 default: break;
4007 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004008 break;
4009
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004010 // constants
4011 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004012 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004013 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004014 break;
4015 case ISN_PUSHBOOL:
4016 case ISN_PUSHSPEC:
4017 smsg("%4d PUSH %s", current,
4018 get_var_special_name(iptr->isn_arg.number));
4019 break;
4020 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004021#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004023#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004024 break;
4025 case ISN_PUSHS:
4026 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4027 break;
4028 case ISN_PUSHBLOB:
4029 {
4030 char_u *r;
4031 char_u numbuf[NUMBUFLEN];
4032 char_u *tofree;
4033
4034 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004035 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004036 vim_free(tofree);
4037 }
4038 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004039 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004040 {
4041 char *name = (char *)iptr->isn_arg.string;
4042
4043 smsg("%4d PUSHFUNC \"%s\"", current,
4044 name == NULL ? "[none]" : name);
4045 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004046 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004047 case ISN_PUSHCHANNEL:
4048#ifdef FEAT_JOB_CHANNEL
4049 {
4050 channel_T *channel = iptr->isn_arg.channel;
4051
4052 smsg("%4d PUSHCHANNEL %d", current,
4053 channel == NULL ? 0 : channel->ch_id);
4054 }
4055#endif
4056 break;
4057 case ISN_PUSHJOB:
4058#ifdef FEAT_JOB_CHANNEL
4059 {
4060 typval_T tv;
4061 char_u *name;
4062
4063 tv.v_type = VAR_JOB;
4064 tv.vval.v_job = iptr->isn_arg.job;
4065 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004066 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004067 }
4068#endif
4069 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004070 case ISN_PUSHEXC:
4071 smsg("%4d PUSH v:exception", current);
4072 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004073 case ISN_UNLET:
4074 smsg("%4d UNLET%s %s", current,
4075 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4076 iptr->isn_arg.unlet.ul_name);
4077 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004078 case ISN_UNLETENV:
4079 smsg("%4d UNLETENV%s $%s", current,
4080 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4081 iptr->isn_arg.unlet.ul_name);
4082 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004083 case ISN_UNLETINDEX:
4084 smsg("%4d UNLETINDEX", current);
4085 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004086 case ISN_LOCKCONST:
4087 smsg("%4d LOCKCONST", current);
4088 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004089 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004090 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004091 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004092 break;
4093 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004094 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004095 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 break;
4097
4098 // function call
4099 case ISN_BCALL:
4100 {
4101 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4102
4103 smsg("%4d BCALL %s(argc %d)", current,
4104 internal_func_name(cbfunc->cbf_idx),
4105 cbfunc->cbf_argcount);
4106 }
4107 break;
4108 case ISN_DCALL:
4109 {
4110 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4111 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4112 + cdfunc->cdf_idx;
4113
4114 smsg("%4d DCALL %s(argc %d)", current,
4115 df->df_ufunc->uf_name_exp != NULL
4116 ? df->df_ufunc->uf_name_exp
4117 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4118 }
4119 break;
4120 case ISN_UCALL:
4121 {
4122 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4123
4124 smsg("%4d UCALL %s(argc %d)", current,
4125 cufunc->cuf_name, cufunc->cuf_argcount);
4126 }
4127 break;
4128 case ISN_PCALL:
4129 {
4130 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4131
4132 smsg("%4d PCALL%s (argc %d)", current,
4133 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4134 }
4135 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004136 case ISN_PCALL_END:
4137 smsg("%4d PCALL end", current);
4138 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004139 case ISN_RETURN:
4140 smsg("%4d RETURN", current);
4141 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004142 case ISN_RETURN_ZERO:
4143 smsg("%4d RETURN 0", current);
4144 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 case ISN_FUNCREF:
4146 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004147 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004149 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004150
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004151 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 }
4153 break;
4154
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004155 case ISN_NEWFUNC:
4156 {
4157 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4158
4159 smsg("%4d NEWFUNC %s %s", current,
4160 newfunc->nf_lambda, newfunc->nf_global);
4161 }
4162 break;
4163
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004164 case ISN_DEF:
4165 {
4166 char_u *name = iptr->isn_arg.string;
4167
4168 smsg("%4d DEF %s", current,
4169 name == NULL ? (char_u *)"" : name);
4170 }
4171 break;
4172
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004173 case ISN_JUMP:
4174 {
4175 char *when = "?";
4176
4177 switch (iptr->isn_arg.jump.jump_when)
4178 {
4179 case JUMP_ALWAYS:
4180 when = "JUMP";
4181 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 case JUMP_AND_KEEP_IF_TRUE:
4183 when = "JUMP_AND_KEEP_IF_TRUE";
4184 break;
4185 case JUMP_IF_FALSE:
4186 when = "JUMP_IF_FALSE";
4187 break;
4188 case JUMP_AND_KEEP_IF_FALSE:
4189 when = "JUMP_AND_KEEP_IF_FALSE";
4190 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004191 case JUMP_IF_COND_FALSE:
4192 when = "JUMP_IF_COND_FALSE";
4193 break;
4194 case JUMP_IF_COND_TRUE:
4195 when = "JUMP_IF_COND_TRUE";
4196 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004197 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004198 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 iptr->isn_arg.jump.jump_where);
4200 }
4201 break;
4202
4203 case ISN_FOR:
4204 {
4205 forloop_T *forloop = &iptr->isn_arg.forloop;
4206
4207 smsg("%4d FOR $%d -> %d", current,
4208 forloop->for_idx, forloop->for_end);
4209 }
4210 break;
4211
4212 case ISN_TRY:
4213 {
4214 try_T *try = &iptr->isn_arg.try;
4215
4216 smsg("%4d TRY catch -> %d, finally -> %d", current,
4217 try->try_catch, try->try_finally);
4218 }
4219 break;
4220 case ISN_CATCH:
4221 // TODO
4222 smsg("%4d CATCH", current);
4223 break;
4224 case ISN_ENDTRY:
4225 smsg("%4d ENDTRY", current);
4226 break;
4227 case ISN_THROW:
4228 smsg("%4d THROW", current);
4229 break;
4230
4231 // expression operations on number
4232 case ISN_OPNR:
4233 case ISN_OPFLOAT:
4234 case ISN_OPANY:
4235 {
4236 char *what;
4237 char *ins;
4238
4239 switch (iptr->isn_arg.op.op_type)
4240 {
4241 case EXPR_MULT: what = "*"; break;
4242 case EXPR_DIV: what = "/"; break;
4243 case EXPR_REM: what = "%"; break;
4244 case EXPR_SUB: what = "-"; break;
4245 case EXPR_ADD: what = "+"; break;
4246 default: what = "???"; break;
4247 }
4248 switch (iptr->isn_type)
4249 {
4250 case ISN_OPNR: ins = "OPNR"; break;
4251 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4252 case ISN_OPANY: ins = "OPANY"; break;
4253 default: ins = "???"; break;
4254 }
4255 smsg("%4d %s %s", current, ins, what);
4256 }
4257 break;
4258
4259 case ISN_COMPAREBOOL:
4260 case ISN_COMPARESPECIAL:
4261 case ISN_COMPARENR:
4262 case ISN_COMPAREFLOAT:
4263 case ISN_COMPARESTRING:
4264 case ISN_COMPAREBLOB:
4265 case ISN_COMPARELIST:
4266 case ISN_COMPAREDICT:
4267 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 case ISN_COMPAREANY:
4269 {
4270 char *p;
4271 char buf[10];
4272 char *type;
4273
4274 switch (iptr->isn_arg.op.op_type)
4275 {
4276 case EXPR_EQUAL: p = "=="; break;
4277 case EXPR_NEQUAL: p = "!="; break;
4278 case EXPR_GREATER: p = ">"; break;
4279 case EXPR_GEQUAL: p = ">="; break;
4280 case EXPR_SMALLER: p = "<"; break;
4281 case EXPR_SEQUAL: p = "<="; break;
4282 case EXPR_MATCH: p = "=~"; break;
4283 case EXPR_IS: p = "is"; break;
4284 case EXPR_ISNOT: p = "isnot"; break;
4285 case EXPR_NOMATCH: p = "!~"; break;
4286 default: p = "???"; break;
4287 }
4288 STRCPY(buf, p);
4289 if (iptr->isn_arg.op.op_ic == TRUE)
4290 strcat(buf, "?");
4291 switch(iptr->isn_type)
4292 {
4293 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4294 case ISN_COMPARESPECIAL:
4295 type = "COMPARESPECIAL"; break;
4296 case ISN_COMPARENR: type = "COMPARENR"; break;
4297 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4298 case ISN_COMPARESTRING:
4299 type = "COMPARESTRING"; break;
4300 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4301 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4302 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4303 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004304 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4305 default: type = "???"; break;
4306 }
4307
4308 smsg("%4d %s %s", current, type, buf);
4309 }
4310 break;
4311
4312 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4313 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4314
4315 // expression operations
4316 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004317 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004318 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004319 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004320 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004321 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004322 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004323 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4324 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004325 case ISN_SLICE: smsg("%4d SLICE %lld",
4326 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004327 case ISN_GETITEM: smsg("%4d ITEM %lld",
4328 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004329 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4330 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 iptr->isn_arg.string); break;
4332 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4333
4334 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004335 case ISN_CHECKTYPE:
4336 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004337 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004338 char *tofree;
4339
Bram Moolenaare32e5162021-01-21 20:21:29 +01004340 if (ct->ct_arg_idx == 0)
4341 smsg("%4d CHECKTYPE %s stack[%d]", current,
4342 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004343 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004344 else
4345 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4346 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004347 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004348 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004349 vim_free(tofree);
4350 break;
4351 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004352 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4353 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4354 iptr->isn_arg.checklen.cl_min_len);
4355 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004356 case ISN_SETTYPE:
4357 {
4358 char *tofree;
4359
4360 smsg("%4d SETTYPE %s", current,
4361 type_name(iptr->isn_arg.type.ct_type, &tofree));
4362 vim_free(tofree);
4363 break;
4364 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004365 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004366 case ISN_2BOOL: if (iptr->isn_arg.number)
4367 smsg("%4d INVERT (!val)", current);
4368 else
4369 smsg("%4d 2BOOL (!!val)", current);
4370 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004371 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004372 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004373 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004374 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004375 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004376 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004377 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4378 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004379 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004380 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4381 smsg("%4d PUT %c above range",
4382 current, iptr->isn_arg.put.put_regname);
4383 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4384 smsg("%4d PUT %c range",
4385 current, iptr->isn_arg.put.put_regname);
4386 else
4387 smsg("%4d PUT %c %ld", current,
4388 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004389 (long)iptr->isn_arg.put.put_lnum);
4390 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004391
Bram Moolenaar02194d22020-10-24 23:08:38 +02004392 // TODO: summarize modifiers
4393 case ISN_CMDMOD:
4394 {
4395 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004396 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004397 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4398
4399 buf = alloc(len + 1);
4400 if (buf != NULL)
4401 {
4402 (void)produce_cmdmods(
4403 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4404 smsg("%4d CMDMOD %s", current, buf);
4405 vim_free(buf);
4406 }
4407 break;
4408 }
4409 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004410
Bram Moolenaarb2049902021-01-24 12:53:53 +01004411 case ISN_PROF_START:
4412 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4413 break;
4414
4415 case ISN_PROF_END:
4416 smsg("%4d PROFILE END", current);
4417 break;
4418
Bram Moolenaar792f7862020-11-23 08:31:18 +01004419 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4420 iptr->isn_arg.unpack.unp_count,
4421 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4422 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004423 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4424 iptr->isn_arg.shuffle.shfl_item,
4425 iptr->isn_arg.shuffle.shfl_up);
4426 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004427 case ISN_DROP: smsg("%4d DROP", current); break;
4428 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004429
4430 out_flush(); // output one line at a time
4431 ui_breakcheck();
4432 if (got_int)
4433 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004435}
4436
4437/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004438 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 * list, etc. Mostly like what JavaScript does, except that empty list and
4440 * empty dictionary are FALSE.
4441 */
4442 int
4443tv2bool(typval_T *tv)
4444{
4445 switch (tv->v_type)
4446 {
4447 case VAR_NUMBER:
4448 return tv->vval.v_number != 0;
4449 case VAR_FLOAT:
4450#ifdef FEAT_FLOAT
4451 return tv->vval.v_float != 0.0;
4452#else
4453 break;
4454#endif
4455 case VAR_PARTIAL:
4456 return tv->vval.v_partial != NULL;
4457 case VAR_FUNC:
4458 case VAR_STRING:
4459 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4460 case VAR_LIST:
4461 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4462 case VAR_DICT:
4463 return tv->vval.v_dict != NULL
4464 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4465 case VAR_BOOL:
4466 case VAR_SPECIAL:
4467 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4468 case VAR_JOB:
4469#ifdef FEAT_JOB_CHANNEL
4470 return tv->vval.v_job != NULL;
4471#else
4472 break;
4473#endif
4474 case VAR_CHANNEL:
4475#ifdef FEAT_JOB_CHANNEL
4476 return tv->vval.v_channel != NULL;
4477#else
4478 break;
4479#endif
4480 case VAR_BLOB:
4481 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4482 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004483 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004484 case VAR_VOID:
4485 break;
4486 }
4487 return FALSE;
4488}
4489
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004490 void
4491emsg_using_string_as(typval_T *tv, int as_number)
4492{
4493 semsg(_(as_number ? e_using_string_as_number_str
4494 : e_using_string_as_bool_str),
4495 tv->vval.v_string == NULL
4496 ? (char_u *)"" : tv->vval.v_string);
4497}
4498
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004499/*
4500 * If "tv" is a string give an error and return FAIL.
4501 */
4502 int
4503check_not_string(typval_T *tv)
4504{
4505 if (tv->v_type == VAR_STRING)
4506 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004507 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004508 clear_tv(tv);
4509 return FAIL;
4510 }
4511 return OK;
4512}
4513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004514
4515#endif // FEAT_EVAL