blob: 2e7e204e3c120be9686551760a4a298fbee05eea [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 Moolenaar7e82c5f2021-02-21 21:32:45 +010029 int tcd_catch_idx; // instruction of the first :catch or :finally
30 int tcd_finally_idx; // instruction of the :finally block or zero
31 int tcd_endtry_idx; // instruction of the :endtry
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010032 int tcd_caught; // catch block entered
Bram Moolenaarc150c092021-02-13 15:02:46 +010033 int tcd_cont; // :continue encountered, jump here
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#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 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100100 * Set the instruction index, depending on omitted arguments, where the default
101 * values are to be computed. If all optional arguments are present, start
102 * with the function body.
103 * The expression evaluation is at the start of the instructions:
104 * 0 -> EVAL default1
105 * STORE arg[-2]
106 * 1 -> EVAL default2
107 * STORE arg[-1]
108 * 2 -> function body
109 */
110 static void
111init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
112{
113 if (ufunc->uf_def_args.ga_len == 0)
114 ectx->ec_iidx = 0;
115 else
116 {
117 int defcount = ufunc->uf_args.ga_len - argcount;
118
119 // If there is a varargs argument defcount can be negative, no defaults
120 // to evaluate then.
121 if (defcount < 0)
122 defcount = 0;
123 ectx->ec_iidx = ufunc->uf_def_arg_idx[
124 ufunc->uf_def_args.ga_len - defcount];
125 }
126}
127
128/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200129 * Create a new list from "count" items at the bottom of the stack.
130 * When "count" is zero an empty list is added to the stack.
131 */
132 static int
133exe_newlist(int count, ectx_T *ectx)
134{
135 list_T *list = list_alloc_with_items(count);
136 int idx;
137 typval_T *tv;
138
139 if (list == NULL)
140 return FAIL;
141 for (idx = 0; idx < count; ++idx)
142 list_set_item(list, idx, STACK_TV_BOT(idx - count));
143
144 if (count > 0)
145 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200146 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200147 return FAIL;
148 else
149 ++ectx->ec_stack.ga_len;
150 tv = STACK_TV_BOT(-1);
151 tv->v_type = VAR_LIST;
152 tv->vval.v_list = list;
153 ++list->lv_refcount;
154 return OK;
155}
156
157/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100159 * This adds a stack frame and sets the instruction pointer to the start of the
160 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100161 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 *
163 * Stack has:
164 * - current arguments (already there)
165 * - omitted optional argument (default values) added here
166 * - stack frame:
167 * - pointer to calling function
168 * - Index of next instruction in calling function
169 * - previous frame pointer
170 * - reserved space for local variables
171 */
172 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100173call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200175 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
177 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200178 int arg_to_add;
179 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200180 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200182 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
184 if (dfunc->df_deleted)
185 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100186 // don't use ufunc->uf_name, it may have been freed
187 emsg_funcname(e_func_deleted,
188 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 return FAIL;
190 }
191
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100192#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100193 if (do_profiling == PROF_YES)
194 {
195 if (ga_grow(&profile_info_ga, 1) == OK)
196 {
197 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
198 + profile_info_ga.ga_len;
199 ++profile_info_ga.ga_len;
200 CLEAR_POINTER(info);
201 profile_may_start_func(info, ufunc,
202 (((dfunc_T *)def_functions.ga_data)
203 + ectx->ec_dfunc_idx)->df_ufunc);
204 }
205
206 // Profiling might be enabled/disabled along the way. This should not
207 // fail, since the function was compiled before and toggling profiling
208 // doesn't change any errors.
209 if (func_needs_compiling(ufunc, PROFILING(ufunc))
210 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100211 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100212 return FAIL;
213 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100214#endif
215
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 if (ufunc->uf_va_name != NULL)
217 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200218 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // Stack at time of call with 2 varargs:
220 // normal_arg
221 // optional_arg
222 // vararg_1
223 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 // After creating the list:
225 // normal_arg
226 // optional_arg
227 // vararg-list
228 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200229 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // After creating the list
231 // normal_arg
232 // (space for optional_arg)
233 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200234 vararg_count = argcount - ufunc->uf_args.ga_len;
235 if (vararg_count < 0)
236 vararg_count = 0;
237 else
238 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200239 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200240 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200241
242 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 }
244
Bram Moolenaarfe270812020-04-11 22:31:27 +0200245 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (arg_to_add < 0)
247 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200248 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200249 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200250 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200251 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 return FAIL;
253 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254
255 // Reserve space for:
256 // - missing arguments
257 // - stack frame
258 // - local variables
259 // - if needed: a counter for number of closures created in
260 // ectx->ec_funcrefs.
261 varcount = dfunc->df_varcount + dfunc->df_has_closure;
262 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
263 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 return FAIL;
265
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100266 // If depth of calling is getting too high, don't execute the function.
267 if (funcdepth_increment() == FAIL)
268 return FAIL;
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100282 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
283 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200284 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
286 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200287 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200289 if (dfunc->df_has_closure)
290 {
291 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
292
293 tv->v_type = VAR_NUMBER;
294 tv->vval.v_number = 0;
295 }
296 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100298 if (pt != NULL || ufunc->uf_partial != NULL
299 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100300 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100301 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
302
303 if (outer == NULL)
304 return FAIL;
305 if (pt != NULL)
306 {
307 *outer = pt->pt_outer;
308 outer->out_up_is_copy = TRUE;
309 }
310 else if (ufunc->uf_partial != NULL)
311 {
312 *outer = ufunc->uf_partial->pt_outer;
313 outer->out_up_is_copy = TRUE;
314 }
315 else
316 {
317 outer->out_stack = &ectx->ec_stack;
318 outer->out_frame_idx = ectx->ec_frame_idx;
319 outer->out_up = ectx->ec_outer;
320 }
321 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100322 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100323 else
324 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 // Set execution state to the start of the called function.
327 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100328 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100329 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200330 if (entry != NULL)
331 {
332 // Set the script context to the script where the function was defined.
333 // TODO: save more than the SID?
334 entry->es_save_sid = current_sctx.sc_sid;
335 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
336 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100337
338 // Decide where to start execution, handles optional arguments.
339 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
341 return OK;
342}
343
344// Get pointer to item in the stack.
345#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
346
347/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 * Used when returning from a function: Check if any closure is still
349 * referenced. If so then move the arguments and variables to a separate piece
350 * of stack to be used when the closure is called.
351 * When "free_arguments" is TRUE the arguments are to be freed.
352 * Returns FAIL when out of memory.
353 */
354 static int
355handle_closure_in_use(ectx_T *ectx, int free_arguments)
356{
357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
358 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200359 int argcount;
360 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 int idx;
362 typval_T *tv;
363 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200364 garray_T *gap = &ectx->ec_funcrefs;
365 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200367 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 return OK; // function was freed
369 if (dfunc->df_has_closure == 0)
370 return OK; // no closures
371 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
372 closure_count = tv->vval.v_number;
373 if (closure_count == 0)
374 return OK; // no funcrefs created
375
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200376 argcount = ufunc_argcount(dfunc->df_ufunc);
377 top = ectx->ec_frame_idx - argcount;
378
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200380 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200382 partial_T *pt;
383 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200384
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200385 if (off < 0)
386 continue; // count is off or already done
387 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200389 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200391 int i;
392
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200393 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200394 // unreferenced on return.
395 for (i = 0; i < dfunc->df_varcount; ++i)
396 {
397 typval_T *stv = STACK_TV(ectx->ec_frame_idx
398 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200400 --refcount;
401 }
402 if (refcount > 1)
403 {
404 closure_in_use = TRUE;
405 break;
406 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200407 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409
410 if (closure_in_use)
411 {
412 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
413 typval_T *stack;
414
415 // A closure is using the arguments and/or local variables.
416 // Move them to the called function.
417 if (funcstack == NULL)
418 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200419 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
420 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
422 funcstack->fs_ga.ga_data = stack;
423 if (stack == NULL)
424 {
425 vim_free(funcstack);
426 return FAIL;
427 }
428
429 // Move or copy the arguments.
430 for (idx = 0; idx < argcount; ++idx)
431 {
432 tv = STACK_TV(top + idx);
433 if (free_arguments)
434 {
435 *(stack + idx) = *tv;
436 tv->v_type = VAR_UNKNOWN;
437 }
438 else
439 copy_tv(tv, stack + idx);
440 }
441 // Move the local variables.
442 for (idx = 0; idx < dfunc->df_varcount; ++idx)
443 {
444 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200445
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200446 // A partial created for a local function, that is also used as a
447 // local variable, has a reference count for the variable, thus
448 // will never go down to zero. When all these refcounts are one
449 // then the funcstack is unused. We need to count how many we have
450 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200451 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
452 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200453 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200454
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200456 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
457 gap->ga_len - closure_count + i])
458 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200459 }
460
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200461 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200462 tv->v_type = VAR_UNKNOWN;
463 }
464
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200465 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200467 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
468 - closure_count + idx];
469 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200471 ++funcstack->fs_refcount;
472 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100473 pt->pt_outer.out_stack = &funcstack->fs_ga;
474 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
475 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 }
477 }
478 }
479
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200480 for (idx = 0; idx < closure_count; ++idx)
481 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
482 - closure_count + idx]);
483 gap->ga_len -= closure_count;
484 if (gap->ga_len == 0)
485 ga_clear(gap);
486
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200487 return OK;
488}
489
490/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200491 * Called when a partial is freed or its reference count goes down to one. The
492 * funcstack may be the only reference to the partials in the local variables.
493 * Go over all of them, the funcref and can be freed if all partials
494 * referencing the funcstack have a reference count of one.
495 */
496 void
497funcstack_check_refcount(funcstack_T *funcstack)
498{
499 int i;
500 garray_T *gap = &funcstack->fs_ga;
501 int done = 0;
502
503 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
504 return;
505 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
506 {
507 typval_T *tv = ((typval_T *)gap->ga_data) + i;
508
509 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
510 && tv->vval.v_partial->pt_funcstack == funcstack
511 && tv->vval.v_partial->pt_refcount == 1)
512 ++done;
513 }
514 if (done == funcstack->fs_min_refcount)
515 {
516 typval_T *stack = gap->ga_data;
517
518 // All partials referencing the funcstack have a reference count of
519 // one, thus the funcstack is no longer of use.
520 for (i = 0; i < gap->ga_len; ++i)
521 clear_tv(stack + i);
522 vim_free(stack);
523 vim_free(funcstack);
524 }
525}
526
527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 * Return from the current function.
529 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531func_return(ectx_T *ectx)
532{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100534 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
536 + ectx->ec_dfunc_idx;
537 int argcount = ufunc_argcount(dfunc->df_ufunc);
538 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200539 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100540 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
541 + STACK_FRAME_FUNC_OFF)->vval.v_number;
542 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
543 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar12d26532021-02-19 19:13:21 +0100545#ifdef FEAT_PROFILE
546 if (do_profiling == PROF_YES)
547 {
548 ufunc_T *caller = prev_dfunc->df_ufunc;
549
550 if (dfunc->df_ufunc->uf_profiling
551 || (caller != NULL && caller->uf_profiling))
552 {
553 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
554 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
555 --profile_info_ga.ga_len;
556 }
557 }
558#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200560 entry = estack_pop();
561 if (entry != NULL)
562 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200564 if (handle_closure_in_use(ectx, TRUE) == FAIL)
565 return FAIL;
566
567 // Clear the arguments.
568 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
569 clear_tv(STACK_TV(idx));
570
571 // Clear local variables and temp values, but not the return value.
572 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100573 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100575
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100576 // The return value should be on top of the stack. However, when aborting
577 // it may not be there and ec_frame_idx is the top of the stack.
578 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100580 ret_idx = 0;
581
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 vim_free(ectx->ec_outer);
583
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100584 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100585 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100586 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
587 + STACK_FRAME_IIDX_OFF)->vval.v_number;
588 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
589 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200590 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100593 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100594
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100595 if (ret_idx > 0)
596 {
597 // Reset the stack to the position before the call, with a spot for the
598 // return value, moved there from above the frame.
599 ectx->ec_stack.ga_len = top + 1;
600 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
601 }
602 else
603 // Reset the stack to the position before the call.
604 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200605
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100606 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200607 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608}
609
610#undef STACK_TV
611
612/*
613 * Prepare arguments and rettv for calling a builtin or user function.
614 */
615 static int
616call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
617{
618 int idx;
619 typval_T *tv;
620
621 // Move arguments from bottom of the stack to argvars[] and add terminator.
622 for (idx = 0; idx < argcount; ++idx)
623 argvars[idx] = *STACK_TV_BOT(idx - argcount);
624 argvars[argcount].v_type = VAR_UNKNOWN;
625
626 // Result replaces the arguments on the stack.
627 if (argcount > 0)
628 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200629 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 return FAIL;
631 else
632 ++ectx->ec_stack.ga_len;
633
634 // Default return value is zero.
635 tv = STACK_TV_BOT(-1);
636 tv->v_type = VAR_NUMBER;
637 tv->vval.v_number = 0;
638
639 return OK;
640}
641
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200642// Ugly global to avoid passing the execution context around through many
643// layers.
644static ectx_T *current_ectx = NULL;
645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646/*
647 * Call a builtin function by index.
648 */
649 static int
650call_bfunc(int func_idx, int argcount, ectx_T *ectx)
651{
652 typval_T argvars[MAX_FUNC_ARGS];
653 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100654 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200655 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 if (call_prepare(argcount, argvars, ectx) == FAIL)
658 return FAIL;
659
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200660 // Call the builtin function. Set "current_ectx" so that when it
661 // recursively invokes call_def_function() a closure context can be set.
662 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200664 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
666 // Clear the arguments.
667 for (idx = 0; idx < argcount; ++idx)
668 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200669
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100670 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return OK;
673}
674
675/*
676 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100677 * If the function is compiled this will add a stack frame and set the
678 * instruction pointer at the start of the function.
679 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100680 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 */
683 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100684call_ufunc(
685 ufunc_T *ufunc,
686 partial_T *pt,
687 int argcount,
688 ectx_T *ectx,
689 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690{
691 typval_T argvars[MAX_FUNC_ARGS];
692 funcexe_T funcexe;
693 int error;
694 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100696#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100697 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100698#else
699# define profiling FALSE
700#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
Bram Moolenaarb2049902021-01-24 12:53:53 +0100702 if (func_needs_compiling(ufunc, profiling)
703 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200704 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200705 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100706 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100707 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100708 if (error != FCERR_UNKNOWN)
709 {
710 if (error == FCERR_TOOMANY)
711 semsg(_(e_toomanyarg), ufunc->uf_name);
712 else
713 semsg(_(e_toofewarg), ufunc->uf_name);
714 return FAIL;
715 }
716
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717 // The function has been compiled, can call it quickly. For a function
718 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100719 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100720 if (iptr != NULL)
721 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100722 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 iptr->isn_type = ISN_DCALL;
724 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
725 iptr->isn_arg.dfunc.cdf_argcount = argcount;
726 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100727 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730 if (call_prepare(argcount, argvars, ectx) == FAIL)
731 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 funcexe.evaluate = TRUE;
734
735 // Call the user function. Result goes in last position on the stack.
736 // TODO: add selfdict if there is one
737 error = call_user_func_check(ufunc, argcount, argvars,
738 STACK_TV_BOT(-1), &funcexe, NULL);
739
740 // Clear the arguments.
741 for (idx = 0; idx < argcount; ++idx)
742 clear_tv(&argvars[idx]);
743
744 if (error != FCERR_NONE)
745 {
746 user_func_error(error, ufunc->uf_name);
747 return FAIL;
748 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200750 // Error other than from calling the function itself.
751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return OK;
753}
754
755/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200756 * Return TRUE if an error was given or CTRL-C was pressed.
757 */
758 static int
759vim9_aborting(int prev_called_emsg)
760{
761 return called_emsg > prev_called_emsg || got_int || did_throw;
762}
763
764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 * Execute a function by "name".
766 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100767 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 * Returns FAIL if not found without an error message.
769 */
770 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100771call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772{
773 ufunc_T *ufunc;
774
775 if (builtin_function(name, -1))
776 {
777 int func_idx = find_internal_func(name);
778
779 if (func_idx < 0)
780 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200781 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 return FAIL;
783 return call_bfunc(func_idx, argcount, ectx);
784 }
785
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200786 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200787
788 if (ufunc == NULL)
789 {
790 int called_emsg_before = called_emsg;
791
792 if (script_autoload(name, TRUE))
793 // loaded a package, search for the function again
794 ufunc = find_func(name, FALSE, NULL);
795 if (vim9_aborting(called_emsg_before))
796 return FAIL; // bail out if loading the script caused an error
797 }
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100800 {
801 if (ufunc->uf_arg_types != NULL)
802 {
803 int i;
804 typval_T *argv = STACK_TV_BOT(0) - argcount;
805
806 // The function can change at runtime, check that the argument
807 // types are correct.
808 for (i = 0; i < argcount; ++i)
809 {
810 type_T *type = i < ufunc->uf_args.ga_len
811 ? ufunc->uf_arg_types[i] : ufunc->uf_va_type;
812
813 if (type != NULL && check_typval_arg_type(type,
814 &argv[i], i + 1) == FAIL)
815 return FAIL;
816 }
817 }
818
Bram Moolenaar0186e582021-01-10 18:33:11 +0100819 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100820 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100821
822 return FAIL;
823}
824
825 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200826call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100827{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200828 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200829 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100831 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100832
833 if (tv->v_type == VAR_PARTIAL)
834 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200835 partial_T *pt = tv->vval.v_partial;
836 int i;
837
838 if (pt->pt_argc > 0)
839 {
840 // Make space for arguments from the partial, shift the "argcount"
841 // arguments up.
842 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
843 return FAIL;
844 for (i = 1; i <= argcount; ++i)
845 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
846 ectx->ec_stack.ga_len += pt->pt_argc;
847 argcount += pt->pt_argc;
848
849 // copy the arguments from the partial onto the stack
850 for (i = 0; i < pt->pt_argc; ++i)
851 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
852 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100853
854 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100855 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200856
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100857 name = pt->pt_name;
858 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200859 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200861 if (name != NULL)
862 {
863 char_u fname_buf[FLEN_FIXED + 1];
864 char_u *tofree = NULL;
865 int error = FCERR_NONE;
866 char_u *fname;
867
868 // May need to translate <SNR>123_ to K_SNR.
869 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
870 if (error != FCERR_NONE)
871 res = FAIL;
872 else
873 res = call_by_name(fname, argcount, ectx, NULL);
874 vim_free(tofree);
875 }
876
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100877 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100878 {
879 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200880 semsg(_(e_unknownfunc),
881 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100882 return FAIL;
883 }
884 return OK;
885}
886
887/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200888 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
889 * TRUE.
890 */
891 static int
892error_if_locked(int lock, char *error)
893{
894 if (lock & (VAR_LOCKED | VAR_FIXED))
895 {
896 emsg(_(error));
897 return TRUE;
898 }
899 return FALSE;
900}
901
902/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100903 * Give an error if "tv" is not a number and return FAIL.
904 */
905 static int
906check_for_number(typval_T *tv)
907{
908 if (tv->v_type != VAR_NUMBER)
909 {
910 semsg(_(e_expected_str_but_got_str),
911 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
912 return FAIL;
913 }
914 return OK;
915}
916
917/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100918 * Store "tv" in variable "name".
919 * This is for s: and g: variables.
920 */
921 static void
922store_var(char_u *name, typval_T *tv)
923{
924 funccal_entry_T entry;
925
926 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100927 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100928 restore_funccal();
929}
930
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100931/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100932 * Convert "tv" to a string.
933 * Return FAIL if not allowed.
934 */
935 static int
936do_2string(typval_T *tv, int is_2string_any)
937{
938 if (tv->v_type != VAR_STRING)
939 {
940 char_u *str;
941
942 if (is_2string_any)
943 {
944 switch (tv->v_type)
945 {
946 case VAR_SPECIAL:
947 case VAR_BOOL:
948 case VAR_NUMBER:
949 case VAR_FLOAT:
950 case VAR_BLOB: break;
951 default: to_string_error(tv->v_type);
952 return FAIL;
953 }
954 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100955 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100956 clear_tv(tv);
957 tv->v_type = VAR_STRING;
958 tv->vval.v_string = str;
959 }
960 return OK;
961}
962
963/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100964 * When the value of "sv" is a null list of dict, allocate it.
965 */
966 static void
967allocate_if_null(typval_T *tv)
968{
969 switch (tv->v_type)
970 {
971 case VAR_LIST:
972 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100973 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100974 break;
975 case VAR_DICT:
976 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100977 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100978 break;
979 default:
980 break;
981 }
982}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200983
Bram Moolenaare7525c52021-01-09 13:20:37 +0100984/*
985 * Return the character "str[index]" where "index" is the character index. If
986 * "index" is out of range NULL is returned.
987 */
988 char_u *
989char_from_string(char_u *str, varnumber_T index)
990{
991 size_t nbyte = 0;
992 varnumber_T nchar = index;
993 size_t slen;
994
995 if (str == NULL)
996 return NULL;
997 slen = STRLEN(str);
998
999 // do the same as for a list: a negative index counts from the end
1000 if (index < 0)
1001 {
1002 int clen = 0;
1003
1004 for (nbyte = 0; nbyte < slen; ++clen)
1005 nbyte += MB_CPTR2LEN(str + nbyte);
1006 nchar = clen + index;
1007 if (nchar < 0)
1008 // unlike list: index out of range results in empty string
1009 return NULL;
1010 }
1011
1012 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
1013 nbyte += MB_CPTR2LEN(str + nbyte);
1014 if (nbyte >= slen)
1015 return NULL;
1016 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
1017}
1018
1019/*
1020 * Get the byte index for character index "idx" in string "str" with length
1021 * "str_len".
1022 * If going over the end return "str_len".
1023 * If "idx" is negative count from the end, -1 is the last character.
1024 * When going over the start return -1.
1025 */
1026 static long
1027char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1028{
1029 varnumber_T nchar = idx;
1030 size_t nbyte = 0;
1031
1032 if (nchar >= 0)
1033 {
1034 while (nchar > 0 && nbyte < str_len)
1035 {
1036 nbyte += MB_CPTR2LEN(str + nbyte);
1037 --nchar;
1038 }
1039 }
1040 else
1041 {
1042 nbyte = str_len;
1043 while (nchar < 0 && nbyte > 0)
1044 {
1045 --nbyte;
1046 nbyte -= mb_head_off(str, str + nbyte);
1047 ++nchar;
1048 }
1049 if (nchar < 0)
1050 return -1;
1051 }
1052 return (long)nbyte;
1053}
1054
1055/*
1056 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001057 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001058 * Return NULL when the result is empty.
1059 */
1060 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001061string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001062{
1063 long start_byte, end_byte;
1064 size_t slen;
1065
1066 if (str == NULL)
1067 return NULL;
1068 slen = STRLEN(str);
1069 start_byte = char_idx2byte(str, slen, first);
1070 if (start_byte < 0)
1071 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001072 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001073 end_byte = (long)slen;
1074 else
1075 {
1076 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001077 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001078 // end index is inclusive
1079 end_byte += MB_CPTR2LEN(str + end_byte);
1080 }
1081
1082 if (start_byte >= (long)slen || end_byte <= start_byte)
1083 return NULL;
1084 return vim_strnsave(str + start_byte, end_byte - start_byte);
1085}
1086
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001087 static svar_T *
1088get_script_svar(scriptref_T *sref, ectx_T *ectx)
1089{
1090 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1091 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1092 + ectx->ec_dfunc_idx;
1093 svar_T *sv;
1094
1095 if (sref->sref_seq != si->sn_script_seq)
1096 {
1097 // The script was reloaded after the function was
1098 // compiled, the script_idx may not be valid.
1099 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1100 dfunc->df_ufunc->uf_name_exp);
1101 return NULL;
1102 }
1103 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1104 if (!equal_type(sv->sv_type, sref->sref_type))
1105 {
1106 emsg(_(e_script_variable_type_changed));
1107 return NULL;
1108 }
1109 return sv;
1110}
1111
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001112/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001113 * Execute a function by "name".
1114 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001115 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001116 */
1117 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001118call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001119{
Bram Moolenaared677f52020-08-12 16:38:10 +02001120 int called_emsg_before = called_emsg;
1121 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122
Bram Moolenaared677f52020-08-12 16:38:10 +02001123 res = call_by_name(name, argcount, ectx, iptr);
1124 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001125 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001126 dictitem_T *v;
1127
1128 v = find_var(name, NULL, FALSE);
1129 if (v == NULL)
1130 {
1131 semsg(_(e_unknownfunc), name);
1132 return FAIL;
1133 }
1134 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1135 {
1136 semsg(_(e_unknownfunc), name);
1137 return FAIL;
1138 }
1139 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001140 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001141 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001142}
1143
1144/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001145 * When a function reference is used, fill a partial with the information
1146 * needed, especially when it is used as a closure.
1147 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001148 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001149fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1150{
1151 pt->pt_func = ufunc;
1152 pt->pt_refcount = 1;
1153
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001154 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001155 {
1156 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1157 + ectx->ec_dfunc_idx;
1158
1159 // The closure needs to find arguments and local
1160 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001161 pt->pt_outer.out_stack = &ectx->ec_stack;
1162 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1163 pt->pt_outer.out_up = ectx->ec_outer;
1164 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001165
1166 // If this function returns and the closure is still
1167 // being used, we need to make a copy of the context
1168 // (arguments and local variables). Store a reference
1169 // to the partial so we can handle that.
1170 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1171 {
1172 vim_free(pt);
1173 return FAIL;
1174 }
1175 // Extra variable keeps the count of closures created
1176 // in the current function call.
1177 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1178 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1179
1180 ((partial_T **)ectx->ec_funcrefs.ga_data)
1181 [ectx->ec_funcrefs.ga_len] = pt;
1182 ++pt->pt_refcount;
1183 ++ectx->ec_funcrefs.ga_len;
1184 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001185 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001186 return OK;
1187}
1188
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001189
Bram Moolenaarf112f302020-12-20 17:47:52 +01001190/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001191 * Call a "def" function from old Vim script.
1192 * Return OK or FAIL.
1193 */
1194 int
1195call_def_function(
1196 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001197 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001198 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001199 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001200 typval_T *rettv) // return value
1201{
1202 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001203 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001204 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 typval_T *tv;
1206 int idx;
1207 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001208 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001209 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001210 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001211 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001212 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001213 msglist_T **saved_msg_list = NULL;
1214 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001215 cmdmod_T save_cmdmod;
1216 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001217 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001218 int save_emsg_silent_def = emsg_silent_def;
1219 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001220 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001221 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001222 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001223
1224// Get pointer to item in the stack.
1225#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1226
1227// Get pointer to item at the bottom of the stack, -1 is the bottom.
1228#undef STACK_TV_BOT
1229#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1230
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001231// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001232#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 +01001233
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001234 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001235 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1236 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001237 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001238 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001239 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001240 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001241 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001242 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001244
Bram Moolenaar09689a02020-05-09 22:50:08 +02001245 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001246 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001247 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1248 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001249 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001250 {
1251 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001252 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001253 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001254 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001255
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001256 // If depth of calling is getting too high, don't execute the function.
1257 orig_funcdepth = funcdepth_get();
1258 if (funcdepth_increment() == FAIL)
1259 return FAIL;
1260
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001261 CLEAR_FIELD(ectx);
1262 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1263 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1264 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001265 {
1266 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001267 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001268 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001269 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001270 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001271
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001272 // Put arguments on the stack, but no more than what the function expects.
1273 // A lambda can be called with more arguments than it uses.
1274 for (idx = 0; idx < argc
1275 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1276 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001277 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001278 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001279 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001280 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001281 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 copy_tv(&argv[idx], STACK_TV_BOT(0));
1283 ++ectx.ec_stack.ga_len;
1284 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001285
1286 // Turn varargs into a list. Empty list if no args.
1287 if (ufunc->uf_va_name != NULL)
1288 {
1289 int vararg_count = argc - ufunc->uf_args.ga_len;
1290
1291 if (vararg_count < 0)
1292 vararg_count = 0;
1293 else
1294 argc -= vararg_count;
1295 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001296 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001297
1298 // Check the type of the list items.
1299 tv = STACK_TV_BOT(-1);
1300 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001301 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001302 && ufunc->uf_va_type->tt_member != &t_any
1303 && tv->vval.v_list != NULL)
1304 {
1305 type_T *expected = ufunc->uf_va_type->tt_member;
1306 listitem_T *li = tv->vval.v_list->lv_first;
1307
1308 for (idx = 0; idx < vararg_count; ++idx)
1309 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001310 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001311 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001312 goto failed_early;
1313 li = li->li_next;
1314 }
1315 }
1316
Bram Moolenaar23e03252020-04-12 22:22:31 +02001317 if (defcount > 0)
1318 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001319 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001320 --ectx.ec_stack.ga_len;
1321 }
1322
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001323 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001324 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001325 if (defcount > 0)
1326 for (idx = 0; idx < defcount; ++idx)
1327 {
1328 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1329 ++ectx.ec_stack.ga_len;
1330 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001331 if (ufunc->uf_va_name != NULL)
1332 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001333
1334 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001335 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1336 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001337
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001338 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001339 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1340 + ufunc->uf_dfunc_idx;
1341 ufunc_T *base_ufunc = dfunc->df_ufunc;
1342
1343 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1344 // by copy_func().
1345 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001346 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001347 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1348 if (ectx.ec_outer == NULL)
1349 goto failed_early;
1350 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001351 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001352 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1353 {
1354 if (current_ectx->ec_outer != NULL)
1355 *ectx.ec_outer = *current_ectx->ec_outer;
1356 }
1357 else
1358 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001359 }
1360 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001361 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1362 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001363 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001364 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001365
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001366 // dummy frame entries
1367 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1368 {
1369 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1370 ++ectx.ec_stack.ga_len;
1371 }
1372
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001373 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001374 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001375 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1376 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001377
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001378 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001379 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001380 ectx.ec_stack.ga_len += dfunc->df_varcount;
1381 if (dfunc->df_has_closure)
1382 {
1383 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1384 STACK_TV_VAR(idx)->vval.v_number = 0;
1385 ++ectx.ec_stack.ga_len;
1386 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001387
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001388 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001389 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001390
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001391 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001392 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001393 estack_push_ufunc(ufunc, 1);
1394 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001395 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1396
Bram Moolenaar352134b2020-10-17 22:04:08 +02001397 // Use a specific location for storing error messages to be converted to an
1398 // exception.
1399 saved_msg_list = msg_list;
1400 msg_list = &private_msg_list;
1401
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001402 // Do turn errors into exceptions.
1403 suppress_errthrow = FALSE;
1404
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001405 // When ":silent!" was used before calling then we still abort the
1406 // function. If ":silent!" is used in the function then we don't.
1407 emsg_silent_def = emsg_silent;
1408 did_emsg_def = 0;
1409
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001410 where.wt_index = 0;
1411 where.wt_variable = FALSE;
1412
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001413 // Decide where to start execution, handles optional arguments.
1414 init_instr_idx(ufunc, argc, &ectx);
1415
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001416 for (;;)
1417 {
1418 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001419
Bram Moolenaar270d0382020-05-15 21:42:53 +02001420 if (++breakcheck_count >= 100)
1421 {
1422 line_breakcheck();
1423 breakcheck_count = 0;
1424 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001425 if (got_int)
1426 {
1427 // Turn CTRL-C into an exception.
1428 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001429 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001430 goto failed;
1431 did_throw = TRUE;
1432 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001433
Bram Moolenaara26b9702020-04-18 19:53:28 +02001434 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1435 {
1436 // Turn an error message into an exception.
1437 did_emsg = FALSE;
1438 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1439 goto failed;
1440 did_throw = TRUE;
1441 *msg_list = NULL;
1442 }
1443
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001444 if (did_throw && !ectx.ec_in_catch)
1445 {
1446 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001447 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001448
1449 // An exception jumps to the first catch, finally, or returns from
1450 // the current function.
1451 if (trystack->ga_len > 0)
1452 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001453 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001454 {
1455 // jump to ":catch" or ":finally"
1456 ectx.ec_in_catch = TRUE;
1457 ectx.ec_iidx = trycmd->tcd_catch_idx;
1458 }
1459 else
1460 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001461 // Not inside try or need to return from current functions.
1462 // Push a dummy return value.
1463 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1464 goto failed;
1465 tv = STACK_TV_BOT(0);
1466 tv->v_type = VAR_NUMBER;
1467 tv->vval.v_number = 0;
1468 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001469 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001470 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001471 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001472 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001473 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1474 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 goto done;
1476 }
1477
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001478 if (func_return(&ectx) == FAIL)
1479 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 }
1481 continue;
1482 }
1483
1484 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1485 switch (iptr->isn_type)
1486 {
1487 // execute Ex command line
1488 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001489 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001490 source_cookie_T cookie;
1491
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001493 // Pass getsourceline to get an error for a missing ":end"
1494 // command.
1495 CLEAR_FIELD(cookie);
1496 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1497 if (do_cmdline(iptr->isn_arg.string,
1498 getsourceline, &cookie,
1499 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1500 == FAIL
1501 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001502 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001503 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001504 break;
1505
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001506 // execute Ex command from pieces on the stack
1507 case ISN_EXECCONCAT:
1508 {
1509 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001510 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001511 int pass;
1512 int i;
1513 char_u *cmd = NULL;
1514 char_u *str;
1515
1516 for (pass = 1; pass <= 2; ++pass)
1517 {
1518 for (i = 0; i < count; ++i)
1519 {
1520 tv = STACK_TV_BOT(i - count);
1521 str = tv->vval.v_string;
1522 if (str != NULL && *str != NUL)
1523 {
1524 if (pass == 2)
1525 STRCPY(cmd + len, str);
1526 len += STRLEN(str);
1527 }
1528 if (pass == 2)
1529 clear_tv(tv);
1530 }
1531 if (pass == 1)
1532 {
1533 cmd = alloc(len + 1);
1534 if (cmd == NULL)
1535 goto failed;
1536 len = 0;
1537 }
1538 }
1539
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001541 do_cmdline_cmd(cmd);
1542 vim_free(cmd);
1543 }
1544 break;
1545
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001546 // execute :echo {string} ...
1547 case ISN_ECHO:
1548 {
1549 int count = iptr->isn_arg.echo.echo_count;
1550 int atstart = TRUE;
1551 int needclr = TRUE;
1552
1553 for (idx = 0; idx < count; ++idx)
1554 {
1555 tv = STACK_TV_BOT(idx - count);
1556 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1557 &atstart, &needclr);
1558 clear_tv(tv);
1559 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001560 if (needclr)
1561 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001562 ectx.ec_stack.ga_len -= count;
1563 }
1564 break;
1565
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001566 // :execute {string} ...
1567 // :echomsg {string} ...
1568 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001569 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001570 case ISN_ECHOMSG:
1571 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001572 {
1573 int count = iptr->isn_arg.number;
1574 garray_T ga;
1575 char_u buf[NUMBUFLEN];
1576 char_u *p;
1577 int len;
1578 int failed = FALSE;
1579
1580 ga_init2(&ga, 1, 80);
1581 for (idx = 0; idx < count; ++idx)
1582 {
1583 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001584 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001585 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001586 if (tv->v_type == VAR_CHANNEL
1587 || tv->v_type == VAR_JOB)
1588 {
1589 SOURCING_LNUM = iptr->isn_lnum;
1590 emsg(_(e_inval_string));
1591 break;
1592 }
1593 else
1594 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001595 }
1596 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001597 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001598
1599 len = (int)STRLEN(p);
1600 if (ga_grow(&ga, len + 2) == FAIL)
1601 failed = TRUE;
1602 else
1603 {
1604 if (ga.ga_len > 0)
1605 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1606 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1607 ga.ga_len += len;
1608 }
1609 clear_tv(tv);
1610 }
1611 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001612 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001613 {
1614 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001615 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001616 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001617
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001618 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001619 {
1620 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001621 {
1622 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001623 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001624 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001625 {
1626 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001627 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001628 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001629 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001630 else
1631 {
1632 msg_sb_eol();
1633 if (iptr->isn_type == ISN_ECHOMSG)
1634 {
1635 msg_attr(ga.ga_data, echo_attr);
1636 out_flush();
1637 }
1638 else
1639 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001640 SOURCING_LNUM = iptr->isn_lnum;
1641 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001642 }
1643 }
1644 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001645 ga_clear(&ga);
1646 }
1647 break;
1648
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 // load local variable or argument
1650 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001651 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001652 goto failed;
1653 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1654 ++ectx.ec_stack.ga_len;
1655 break;
1656
1657 // load v: variable
1658 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001659 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001660 goto failed;
1661 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1662 ++ectx.ec_stack.ga_len;
1663 break;
1664
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001665 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001666 case ISN_LOADSCRIPT:
1667 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001668 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001669 svar_T *sv;
1670
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001671 sv = get_script_svar(sref, &ectx);
1672 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001673 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001674 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001675 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001676 goto failed;
1677 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1678 ++ectx.ec_stack.ga_len;
1679 }
1680 break;
1681
1682 // load s: variable in old script
1683 case ISN_LOADS:
1684 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001685 hashtab_T *ht = &SCRIPT_VARS(
1686 iptr->isn_arg.loadstore.ls_sid);
1687 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001688 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001689
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001690 if (di == NULL)
1691 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001692 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001693 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001694 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 }
1696 else
1697 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001698 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001699 goto failed;
1700 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1701 ++ectx.ec_stack.ga_len;
1702 }
1703 }
1704 break;
1705
Bram Moolenaard3aac292020-04-19 14:32:17 +02001706 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001707 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001708 case ISN_LOADB:
1709 case ISN_LOADW:
1710 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001711 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001712 dictitem_T *di = NULL;
1713 hashtab_T *ht = NULL;
1714 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001715
Bram Moolenaard3aac292020-04-19 14:32:17 +02001716 switch (iptr->isn_type)
1717 {
1718 case ISN_LOADG:
1719 ht = get_globvar_ht();
1720 namespace = 'g';
1721 break;
1722 case ISN_LOADB:
1723 ht = &curbuf->b_vars->dv_hashtab;
1724 namespace = 'b';
1725 break;
1726 case ISN_LOADW:
1727 ht = &curwin->w_vars->dv_hashtab;
1728 namespace = 'w';
1729 break;
1730 case ISN_LOADT:
1731 ht = &curtab->tp_vars->dv_hashtab;
1732 namespace = 't';
1733 break;
1734 default: // Cannot reach here
1735 goto failed;
1736 }
1737 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001739 if (di == NULL)
1740 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001741 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001742 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001743 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001744 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001745 }
1746 else
1747 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001748 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001749 goto failed;
1750 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1751 ++ectx.ec_stack.ga_len;
1752 }
1753 }
1754 break;
1755
Bram Moolenaar03290b82020-12-19 16:30:44 +01001756 // load autoload variable
1757 case ISN_LOADAUTO:
1758 {
1759 char_u *name = iptr->isn_arg.string;
1760
1761 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1762 goto failed;
1763 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001764 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001765 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1766 goto on_error;
1767 ++ectx.ec_stack.ga_len;
1768 }
1769 break;
1770
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001771 // load g:/b:/w:/t: namespace
1772 case ISN_LOADGDICT:
1773 case ISN_LOADBDICT:
1774 case ISN_LOADWDICT:
1775 case ISN_LOADTDICT:
1776 {
1777 dict_T *d = NULL;
1778
1779 switch (iptr->isn_type)
1780 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001781 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1782 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1783 case ISN_LOADWDICT: d = curwin->w_vars; break;
1784 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001785 default: // Cannot reach here
1786 goto failed;
1787 }
1788 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1789 goto failed;
1790 tv = STACK_TV_BOT(0);
1791 tv->v_type = VAR_DICT;
1792 tv->v_lock = 0;
1793 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001794 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001795 ++ectx.ec_stack.ga_len;
1796 }
1797 break;
1798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001799 // load &option
1800 case ISN_LOADOPT:
1801 {
1802 typval_T optval;
1803 char_u *name = iptr->isn_arg.string;
1804
Bram Moolenaara8c17702020-04-01 21:17:24 +02001805 // This is not expected to fail, name is checked during
1806 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001807 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001808 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001809 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001810 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001811 *STACK_TV_BOT(0) = optval;
1812 ++ectx.ec_stack.ga_len;
1813 }
1814 break;
1815
1816 // load $ENV
1817 case ISN_LOADENV:
1818 {
1819 typval_T optval;
1820 char_u *name = iptr->isn_arg.string;
1821
Bram Moolenaar270d0382020-05-15 21:42:53 +02001822 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001823 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001824 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001825 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001826 *STACK_TV_BOT(0) = optval;
1827 ++ectx.ec_stack.ga_len;
1828 }
1829 break;
1830
1831 // load @register
1832 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001833 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001834 goto failed;
1835 tv = STACK_TV_BOT(0);
1836 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001837 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001838 // This may result in NULL, which should be equivalent to an
1839 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001840 tv->vval.v_string = get_reg_contents(
1841 iptr->isn_arg.number, GREG_EXPR_SRC);
1842 ++ectx.ec_stack.ga_len;
1843 break;
1844
1845 // store local variable
1846 case ISN_STORE:
1847 --ectx.ec_stack.ga_len;
1848 tv = STACK_TV_VAR(iptr->isn_arg.number);
1849 clear_tv(tv);
1850 *tv = *STACK_TV_BOT(0);
1851 break;
1852
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001853 // store s: variable in old script
1854 case ISN_STORES:
1855 {
1856 hashtab_T *ht = &SCRIPT_VARS(
1857 iptr->isn_arg.loadstore.ls_sid);
1858 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001859 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001860
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001861 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001862 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001863 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001864 else
1865 {
1866 clear_tv(&di->di_tv);
1867 di->di_tv = *STACK_TV_BOT(0);
1868 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001869 }
1870 break;
1871
1872 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001873 case ISN_STORESCRIPT:
1874 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001875 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1876 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001878 sv = get_script_svar(sref, &ectx);
1879 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001880 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 --ectx.ec_stack.ga_len;
1882 clear_tv(sv->sv_tv);
1883 *sv->sv_tv = *STACK_TV_BOT(0);
1884 }
1885 break;
1886
1887 // store option
1888 case ISN_STOREOPT:
1889 {
1890 long n = 0;
1891 char_u *s = NULL;
1892 char *msg;
1893
1894 --ectx.ec_stack.ga_len;
1895 tv = STACK_TV_BOT(0);
1896 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001897 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001898 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001899 if (s == NULL)
1900 s = (char_u *)"";
1901 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001902 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001903 // must be VAR_NUMBER, CHECKTYPE makes sure
1904 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001905 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1906 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001907 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001908 if (msg != NULL)
1909 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001910 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001912 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001914 }
1915 break;
1916
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001917 // store $ENV
1918 case ISN_STOREENV:
1919 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001920 tv = STACK_TV_BOT(0);
1921 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1922 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001923 break;
1924
1925 // store @r
1926 case ISN_STOREREG:
1927 {
1928 int reg = iptr->isn_arg.number;
1929
1930 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001931 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001932 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001933 tv_get_string(tv), -1, FALSE);
1934 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001935 }
1936 break;
1937
1938 // store v: variable
1939 case ISN_STOREV:
1940 --ectx.ec_stack.ga_len;
1941 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1942 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001943 // should not happen, type is checked when compiling
1944 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001945 break;
1946
Bram Moolenaard3aac292020-04-19 14:32:17 +02001947 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001948 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001949 case ISN_STOREB:
1950 case ISN_STOREW:
1951 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001952 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001953 dictitem_T *di;
1954 hashtab_T *ht;
1955 char_u *name = iptr->isn_arg.string + 2;
1956
Bram Moolenaard3aac292020-04-19 14:32:17 +02001957 switch (iptr->isn_type)
1958 {
1959 case ISN_STOREG:
1960 ht = get_globvar_ht();
1961 break;
1962 case ISN_STOREB:
1963 ht = &curbuf->b_vars->dv_hashtab;
1964 break;
1965 case ISN_STOREW:
1966 ht = &curwin->w_vars->dv_hashtab;
1967 break;
1968 case ISN_STORET:
1969 ht = &curtab->tp_vars->dv_hashtab;
1970 break;
1971 default: // Cannot reach here
1972 goto failed;
1973 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001974
1975 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001976 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001978 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979 else
1980 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001981 SOURCING_LNUM = iptr->isn_lnum;
1982 if (var_check_permission(di, name) == FAIL)
1983 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 clear_tv(&di->di_tv);
1985 di->di_tv = *STACK_TV_BOT(0);
1986 }
1987 }
1988 break;
1989
Bram Moolenaar03290b82020-12-19 16:30:44 +01001990 // store an autoload variable
1991 case ISN_STOREAUTO:
1992 SOURCING_LNUM = iptr->isn_lnum;
1993 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1994 clear_tv(STACK_TV_BOT(-1));
1995 --ectx.ec_stack.ga_len;
1996 break;
1997
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001998 // store number in local variable
1999 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002000 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002001 clear_tv(tv);
2002 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002003 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002004 break;
2005
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002006 // store value in list or dict variable
2007 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002008 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002009 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002010 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002011 typval_T *tv_dest = STACK_TV_BOT(-1);
2012 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002013
Bram Moolenaar752fc692021-01-04 21:57:11 +01002014 // Stack contains:
2015 // -3 value to be stored
2016 // -2 index
2017 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002018 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002019 SOURCING_LNUM = iptr->isn_lnum;
2020 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002021 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002022 dest_type = tv_dest->v_type;
2023 if (dest_type == VAR_DICT)
2024 status = do_2string(tv_idx, TRUE);
2025 else if (dest_type == VAR_LIST
2026 && tv_idx->v_type != VAR_NUMBER)
2027 {
2028 emsg(_(e_number_exp));
2029 status = FAIL;
2030 }
2031 }
2032 else if (dest_type != tv_dest->v_type)
2033 {
2034 // just in case, should be OK
2035 semsg(_(e_expected_str_but_got_str),
2036 vartype_name(dest_type),
2037 vartype_name(tv_dest->v_type));
2038 status = FAIL;
2039 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002040
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002041 if (status == OK && dest_type == VAR_LIST)
2042 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002043 long lidx = (long)tv_idx->vval.v_number;
2044 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002045
2046 if (list == NULL)
2047 {
2048 emsg(_(e_list_not_set));
2049 goto on_error;
2050 }
2051 if (lidx < 0 && list->lv_len + lidx >= 0)
2052 // negative index is relative to the end
2053 lidx = list->lv_len + lidx;
2054 if (lidx < 0 || lidx > list->lv_len)
2055 {
2056 semsg(_(e_listidx), lidx);
2057 goto on_error;
2058 }
2059 if (lidx < list->lv_len)
2060 {
2061 listitem_T *li = list_find(list, lidx);
2062
2063 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002064 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002065 goto on_error;
2066 // overwrite existing list item
2067 clear_tv(&li->li_tv);
2068 li->li_tv = *tv;
2069 }
2070 else
2071 {
2072 if (error_if_locked(list->lv_lock,
2073 e_cannot_change_list))
2074 goto on_error;
2075 // append to list, only fails when out of memory
2076 if (list_append_tv(list, tv) == FAIL)
2077 goto failed;
2078 clear_tv(tv);
2079 }
2080 }
2081 else if (status == OK && dest_type == VAR_DICT)
2082 {
2083 char_u *key = tv_idx->vval.v_string;
2084 dict_T *dict = tv_dest->vval.v_dict;
2085 dictitem_T *di;
2086
2087 SOURCING_LNUM = iptr->isn_lnum;
2088 if (dict == NULL)
2089 {
2090 emsg(_(e_dictionary_not_set));
2091 goto on_error;
2092 }
2093 if (key == NULL)
2094 key = (char_u *)"";
2095 di = dict_find(dict, key, -1);
2096 if (di != NULL)
2097 {
2098 if (error_if_locked(di->di_tv.v_lock,
2099 e_cannot_change_dict_item))
2100 goto on_error;
2101 // overwrite existing value
2102 clear_tv(&di->di_tv);
2103 di->di_tv = *tv;
2104 }
2105 else
2106 {
2107 if (error_if_locked(dict->dv_lock,
2108 e_cannot_change_dict))
2109 goto on_error;
2110 // add to dict, only fails when out of memory
2111 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2112 goto failed;
2113 clear_tv(tv);
2114 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002115 }
2116 else
2117 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002118 status = FAIL;
2119 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002120 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002121
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002122 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002123 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002124 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002125 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002126 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002127 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002128 goto on_error;
2129 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002130 }
2131 break;
2132
Bram Moolenaar0186e582021-01-10 18:33:11 +01002133 // load or store variable or argument from outer scope
2134 case ISN_LOADOUTER:
2135 case ISN_STOREOUTER:
2136 {
2137 int depth = iptr->isn_arg.outer.outer_depth;
2138 outer_T *outer = ectx.ec_outer;
2139
2140 while (depth > 1 && outer != NULL)
2141 {
2142 outer = outer->out_up;
2143 --depth;
2144 }
2145 if (outer == NULL)
2146 {
2147 SOURCING_LNUM = iptr->isn_lnum;
2148 iemsg("LOADOUTER depth more than scope levels");
2149 goto failed;
2150 }
2151 tv = ((typval_T *)outer->out_stack->ga_data)
2152 + outer->out_frame_idx + STACK_FRAME_SIZE
2153 + iptr->isn_arg.outer.outer_idx;
2154 if (iptr->isn_type == ISN_LOADOUTER)
2155 {
2156 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2157 goto failed;
2158 copy_tv(tv, STACK_TV_BOT(0));
2159 ++ectx.ec_stack.ga_len;
2160 }
2161 else
2162 {
2163 --ectx.ec_stack.ga_len;
2164 clear_tv(tv);
2165 *tv = *STACK_TV_BOT(0);
2166 }
2167 }
2168 break;
2169
Bram Moolenaar752fc692021-01-04 21:57:11 +01002170 // unlet item in list or dict variable
2171 case ISN_UNLETINDEX:
2172 {
2173 typval_T *tv_idx = STACK_TV_BOT(-2);
2174 typval_T *tv_dest = STACK_TV_BOT(-1);
2175 int status = OK;
2176
2177 // Stack contains:
2178 // -2 index
2179 // -1 dict or list
2180 if (tv_dest->v_type == VAR_DICT)
2181 {
2182 // unlet a dict item, index must be a string
2183 if (tv_idx->v_type != VAR_STRING)
2184 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002185 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002186 semsg(_(e_expected_str_but_got_str),
2187 vartype_name(VAR_STRING),
2188 vartype_name(tv_idx->v_type));
2189 status = FAIL;
2190 }
2191 else
2192 {
2193 dict_T *d = tv_dest->vval.v_dict;
2194 char_u *key = tv_idx->vval.v_string;
2195 dictitem_T *di = NULL;
2196
2197 if (key == NULL)
2198 key = (char_u *)"";
2199 if (d != NULL)
2200 di = dict_find(d, key, (int)STRLEN(key));
2201 if (di == NULL)
2202 {
2203 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002204 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002205 semsg(_(e_dictkey), key);
2206 status = FAIL;
2207 }
2208 else
2209 {
2210 // TODO: check for dict or item locked
2211 dictitem_remove(d, di);
2212 }
2213 }
2214 }
2215 else if (tv_dest->v_type == VAR_LIST)
2216 {
2217 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002218 SOURCING_LNUM = iptr->isn_lnum;
2219 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002220 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002221 status = FAIL;
2222 }
2223 else
2224 {
2225 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002226 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002227 listitem_T *li = NULL;
2228
2229 li = list_find(l, n);
2230 if (li == NULL)
2231 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002232 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002233 semsg(_(e_listidx), n);
2234 status = FAIL;
2235 }
2236 else
2237 // TODO: check for list or item locked
2238 listitem_remove(l, li);
2239 }
2240 }
2241 else
2242 {
2243 status = FAIL;
2244 semsg(_(e_cannot_index_str),
2245 vartype_name(tv_dest->v_type));
2246 }
2247
2248 clear_tv(tv_idx);
2249 clear_tv(tv_dest);
2250 ectx.ec_stack.ga_len -= 2;
2251 if (status == FAIL)
2252 goto on_error;
2253 }
2254 break;
2255
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002256 // unlet range of items in list variable
2257 case ISN_UNLETRANGE:
2258 {
2259 // Stack contains:
2260 // -3 index1
2261 // -2 index2
2262 // -1 dict or list
2263 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2264 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2265 typval_T *tv_dest = STACK_TV_BOT(-1);
2266 int status = OK;
2267
2268 if (tv_dest->v_type == VAR_LIST)
2269 {
2270 // indexes must be a number
2271 SOURCING_LNUM = iptr->isn_lnum;
2272 if (check_for_number(tv_idx1) == FAIL
2273 || check_for_number(tv_idx2) == FAIL)
2274 {
2275 status = FAIL;
2276 }
2277 else
2278 {
2279 list_T *l = tv_dest->vval.v_list;
2280 long n1 = (long)tv_idx1->vval.v_number;
2281 long n2 = (long)tv_idx2->vval.v_number;
2282 listitem_T *li;
2283
2284 li = list_find_index(l, &n1);
2285 if (li == NULL
2286 || list_unlet_range(l, li, NULL, n1,
2287 TRUE, n2) == FAIL)
2288 status = FAIL;
2289 }
2290 }
2291 else
2292 {
2293 status = FAIL;
2294 SOURCING_LNUM = iptr->isn_lnum;
2295 semsg(_(e_cannot_index_str),
2296 vartype_name(tv_dest->v_type));
2297 }
2298
2299 clear_tv(tv_idx1);
2300 clear_tv(tv_idx2);
2301 clear_tv(tv_dest);
2302 ectx.ec_stack.ga_len -= 3;
2303 if (status == FAIL)
2304 goto on_error;
2305 }
2306 break;
2307
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002308 // push constant
2309 case ISN_PUSHNR:
2310 case ISN_PUSHBOOL:
2311 case ISN_PUSHSPEC:
2312 case ISN_PUSHF:
2313 case ISN_PUSHS:
2314 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002315 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002316 case ISN_PUSHCHANNEL:
2317 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002318 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002319 goto failed;
2320 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002321 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002322 ++ectx.ec_stack.ga_len;
2323 switch (iptr->isn_type)
2324 {
2325 case ISN_PUSHNR:
2326 tv->v_type = VAR_NUMBER;
2327 tv->vval.v_number = iptr->isn_arg.number;
2328 break;
2329 case ISN_PUSHBOOL:
2330 tv->v_type = VAR_BOOL;
2331 tv->vval.v_number = iptr->isn_arg.number;
2332 break;
2333 case ISN_PUSHSPEC:
2334 tv->v_type = VAR_SPECIAL;
2335 tv->vval.v_number = iptr->isn_arg.number;
2336 break;
2337#ifdef FEAT_FLOAT
2338 case ISN_PUSHF:
2339 tv->v_type = VAR_FLOAT;
2340 tv->vval.v_float = iptr->isn_arg.fnumber;
2341 break;
2342#endif
2343 case ISN_PUSHBLOB:
2344 blob_copy(iptr->isn_arg.blob, tv);
2345 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002346 case ISN_PUSHFUNC:
2347 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002348 if (iptr->isn_arg.string == NULL)
2349 tv->vval.v_string = NULL;
2350 else
2351 tv->vval.v_string =
2352 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002353 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002354 case ISN_PUSHCHANNEL:
2355#ifdef FEAT_JOB_CHANNEL
2356 tv->v_type = VAR_CHANNEL;
2357 tv->vval.v_channel = iptr->isn_arg.channel;
2358 if (tv->vval.v_channel != NULL)
2359 ++tv->vval.v_channel->ch_refcount;
2360#endif
2361 break;
2362 case ISN_PUSHJOB:
2363#ifdef FEAT_JOB_CHANNEL
2364 tv->v_type = VAR_JOB;
2365 tv->vval.v_job = iptr->isn_arg.job;
2366 if (tv->vval.v_job != NULL)
2367 ++tv->vval.v_job->jv_refcount;
2368#endif
2369 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 default:
2371 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002372 tv->vval.v_string = vim_strsave(
2373 iptr->isn_arg.string == NULL
2374 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 }
2376 break;
2377
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002378 case ISN_UNLET:
2379 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2380 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002381 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002382 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002383 case ISN_UNLETENV:
2384 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2385 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002386
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002387 case ISN_LOCKCONST:
2388 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2389 break;
2390
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002391 // create a list from items on the stack; uses a single allocation
2392 // for the list header and the items
2393 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002394 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2395 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 break;
2397
2398 // create a dict from items on the stack
2399 case ISN_NEWDICT:
2400 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002401 int count = iptr->isn_arg.number;
2402 dict_T *dict = dict_alloc();
2403 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002404 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002405
2406 if (dict == NULL)
2407 goto failed;
2408 for (idx = 0; idx < count; ++idx)
2409 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002410 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002411 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002412 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002413 key = tv->vval.v_string == NULL
2414 ? (char_u *)"" : tv->vval.v_string;
2415 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002416 if (item != NULL)
2417 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002418 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002419 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002420 dict_unref(dict);
2421 goto on_error;
2422 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002423 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 clear_tv(tv);
2425 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002426 {
2427 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002429 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002430 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2431 item->di_tv.v_lock = 0;
2432 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002433 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002434 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002435 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002436 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002438 }
2439
2440 if (count > 0)
2441 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002442 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 goto failed;
2444 else
2445 ++ectx.ec_stack.ga_len;
2446 tv = STACK_TV_BOT(-1);
2447 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002448 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 tv->vval.v_dict = dict;
2450 ++dict->dv_refcount;
2451 }
2452 break;
2453
2454 // call a :def function
2455 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002456 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002457 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002458 iptr->isn_arg.dfunc.cdf_argcount,
2459 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002460 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002461 break;
2462
2463 // call a builtin function
2464 case ISN_BCALL:
2465 SOURCING_LNUM = iptr->isn_lnum;
2466 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2467 iptr->isn_arg.bfunc.cbf_argcount,
2468 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002469 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 break;
2471
2472 // call a funcref or partial
2473 case ISN_PCALL:
2474 {
2475 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2476 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002477 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002478
2479 SOURCING_LNUM = iptr->isn_lnum;
2480 if (pfunc->cpf_top)
2481 {
2482 // funcref is above the arguments
2483 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2484 }
2485 else
2486 {
2487 // Get the funcref from the stack.
2488 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002489 partial_tv = *STACK_TV_BOT(0);
2490 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491 }
2492 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002493 if (tv == &partial_tv)
2494 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002495 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002496 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498 break;
2499
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002500 case ISN_PCALL_END:
2501 // PCALL finished, arguments have been consumed and replaced by
2502 // the return value. Now clear the funcref from the stack,
2503 // and move the return value in its place.
2504 --ectx.ec_stack.ga_len;
2505 clear_tv(STACK_TV_BOT(-1));
2506 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2507 break;
2508
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002509 // call a user defined function or funcref/partial
2510 case ISN_UCALL:
2511 {
2512 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2513
2514 SOURCING_LNUM = iptr->isn_lnum;
2515 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002516 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002517 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002518 }
2519 break;
2520
2521 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002522 case ISN_RETURN_ZERO:
2523 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2524 goto failed;
2525 tv = STACK_TV_BOT(0);
2526 ++ectx.ec_stack.ga_len;
2527 tv->v_type = VAR_NUMBER;
2528 tv->vval.v_number = 0;
2529 tv->v_lock = 0;
2530 // FALLTHROUGH
2531
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002532 case ISN_RETURN:
2533 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002534 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002535 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002536
2537 if (trystack->ga_len > 0)
2538 trycmd = ((trycmd_T *)trystack->ga_data)
2539 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002540 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002541 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002542 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002543 // jump to ":finally" or ":endtry"
2544 if (trycmd->tcd_finally_idx != 0)
2545 ectx.ec_iidx = trycmd->tcd_finally_idx;
2546 else
2547 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002548 trycmd->tcd_return = TRUE;
2549 }
2550 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002551 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002552 }
2553 break;
2554
2555 // push a function reference to a compiled function
2556 case ISN_FUNCREF:
2557 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002558 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2559 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2560 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002562 if (pt == NULL)
2563 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002564 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002565 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002566 vim_free(pt);
2567 goto failed;
2568 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002569 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2570 &ectx) == FAIL)
2571 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002572
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002573 tv = STACK_TV_BOT(0);
2574 ++ectx.ec_stack.ga_len;
2575 tv->vval.v_partial = pt;
2576 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002577 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 }
2579 break;
2580
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002581 // Create a global function from a lambda.
2582 case ISN_NEWFUNC:
2583 {
2584 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2585
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002586 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002587 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002588 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002589 }
2590 break;
2591
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002592 // List functions
2593 case ISN_DEF:
2594 if (iptr->isn_arg.string == NULL)
2595 list_functions(NULL);
2596 else
2597 {
2598 exarg_T ea;
2599
2600 CLEAR_FIELD(ea);
2601 ea.cmd = ea.arg = iptr->isn_arg.string;
2602 define_function(&ea, NULL);
2603 }
2604 break;
2605
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002606 // jump if a condition is met
2607 case ISN_JUMP:
2608 {
2609 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002610 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 int jump = TRUE;
2612
2613 if (when != JUMP_ALWAYS)
2614 {
2615 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002616 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002617 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002618 || when == JUMP_IF_COND_TRUE)
2619 {
2620 SOURCING_LNUM = iptr->isn_lnum;
2621 jump = tv_get_bool_chk(tv, &error);
2622 if (error)
2623 goto on_error;
2624 }
2625 else
2626 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002627 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002628 || when == JUMP_AND_KEEP_IF_FALSE
2629 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002630 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002631 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 {
2633 // drop the value from the stack
2634 clear_tv(tv);
2635 --ectx.ec_stack.ga_len;
2636 }
2637 }
2638 if (jump)
2639 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2640 }
2641 break;
2642
2643 // top of a for loop
2644 case ISN_FOR:
2645 {
2646 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2647 typval_T *idxtv =
2648 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2649
2650 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002651 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002652 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002653 ++idxtv->vval.v_number;
2654 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002655 // past the end of the list, jump to "endfor"
2656 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2657 else if (list->lv_first == &range_list_item)
2658 {
2659 // non-materialized range() list
2660 tv = STACK_TV_BOT(0);
2661 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002662 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002663 tv->vval.v_number = list_find_nr(
2664 list, idxtv->vval.v_number, NULL);
2665 ++ectx.ec_stack.ga_len;
2666 }
2667 else
2668 {
2669 listitem_T *li = list_find(list, idxtv->vval.v_number);
2670
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2672 ++ectx.ec_stack.ga_len;
2673 }
2674 }
2675 break;
2676
2677 // start of ":try" block
2678 case ISN_TRY:
2679 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002680 trycmd_T *trycmd = NULL;
2681
Bram Moolenaar270d0382020-05-15 21:42:53 +02002682 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002683 goto failed;
2684 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2685 + ectx.ec_trystack.ga_len;
2686 ++ectx.ec_trystack.ga_len;
2687 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002688 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002689 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002690 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002691 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2692 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2693 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002694 }
2695 break;
2696
2697 case ISN_PUSHEXC:
2698 if (current_exception == NULL)
2699 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002700 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002701 iemsg("Evaluating catch while current_exception is NULL");
2702 goto failed;
2703 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002704 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 goto failed;
2706 tv = STACK_TV_BOT(0);
2707 ++ectx.ec_stack.ga_len;
2708 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002709 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 tv->vval.v_string = vim_strsave(
2711 (char_u *)current_exception->value);
2712 break;
2713
2714 case ISN_CATCH:
2715 {
2716 garray_T *trystack = &ectx.ec_trystack;
2717
Bram Moolenaar20a76292020-12-25 19:47:24 +01002718 if (restore_cmdmod)
2719 {
2720 cmdmod.cmod_filter_regmatch.regprog = NULL;
2721 undo_cmdmod(&cmdmod);
2722 cmdmod = save_cmdmod;
2723 restore_cmdmod = FALSE;
2724 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002725 if (trystack->ga_len > 0)
2726 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002727 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002728 + trystack->ga_len - 1;
2729 trycmd->tcd_caught = TRUE;
2730 }
2731 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002732 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 catch_exception(current_exception);
2734 }
2735 break;
2736
Bram Moolenaarc150c092021-02-13 15:02:46 +01002737 case ISN_TRYCONT:
2738 {
2739 garray_T *trystack = &ectx.ec_trystack;
2740 trycont_T *trycont = &iptr->isn_arg.trycont;
2741 int i;
2742 trycmd_T *trycmd;
2743 int iidx = trycont->tct_where;
2744
2745 if (trystack->ga_len < trycont->tct_levels)
2746 {
2747 siemsg("TRYCONT: expected %d levels, found %d",
2748 trycont->tct_levels, trystack->ga_len);
2749 goto failed;
2750 }
2751 // Make :endtry jump to any outer try block and the last
2752 // :endtry inside the loop to the loop start.
2753 for (i = trycont->tct_levels; i > 0; --i)
2754 {
2755 trycmd = ((trycmd_T *)trystack->ga_data)
2756 + trystack->ga_len - i;
2757 trycmd->tcd_cont = iidx;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002758 iidx = trycmd->tcd_finally_idx == 0
2759 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002760 }
2761 // jump to :finally or :endtry of current try statement
2762 ectx.ec_iidx = iidx;
2763 }
2764 break;
2765
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002766 case ISN_FINALLY:
2767 {
2768 garray_T *trystack = &ectx.ec_trystack;
2769 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2770 + trystack->ga_len - 1;
2771
2772 // Reset the index to avoid a return statement jumps here
2773 // again.
2774 trycmd->tcd_finally_idx = 0;
2775 break;
2776 }
2777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002778 // end of ":try" block
2779 case ISN_ENDTRY:
2780 {
2781 garray_T *trystack = &ectx.ec_trystack;
2782
2783 if (trystack->ga_len > 0)
2784 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002785 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002786
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002787 --trystack->ga_len;
2788 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002789 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002790 trycmd = ((trycmd_T *)trystack->ga_data)
2791 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002792 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 {
2794 // discard the exception
2795 if (caught_stack == current_exception)
2796 caught_stack = caught_stack->caught;
2797 discard_current_exception();
2798 }
2799
2800 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002801 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002802
2803 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2804 {
2805 --ectx.ec_stack.ga_len;
2806 clear_tv(STACK_TV_BOT(0));
2807 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002808 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002809 // handling :continue: jump to outer try block or
2810 // start of the loop
2811 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002812 }
2813 }
2814 break;
2815
2816 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002817 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002818 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002819
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002820 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2821 {
2822 // throwing an exception while using "silent!" causes
2823 // the function to abort but not display an error.
2824 tv = STACK_TV_BOT(-1);
2825 clear_tv(tv);
2826 tv->v_type = VAR_NUMBER;
2827 tv->vval.v_number = 0;
2828 goto done;
2829 }
2830 --ectx.ec_stack.ga_len;
2831 tv = STACK_TV_BOT(0);
2832 if (tv->vval.v_string == NULL
2833 || *skipwhite(tv->vval.v_string) == NUL)
2834 {
2835 vim_free(tv->vval.v_string);
2836 SOURCING_LNUM = iptr->isn_lnum;
2837 emsg(_(e_throw_with_empty_string));
2838 goto failed;
2839 }
2840
2841 // Inside a "catch" we need to first discard the caught
2842 // exception.
2843 if (trystack->ga_len > 0)
2844 {
2845 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2846 + trystack->ga_len - 1;
2847 if (trycmd->tcd_caught && current_exception != NULL)
2848 {
2849 // discard the exception
2850 if (caught_stack == current_exception)
2851 caught_stack = caught_stack->caught;
2852 discard_current_exception();
2853 trycmd->tcd_caught = FALSE;
2854 }
2855 }
2856
2857 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2858 == FAIL)
2859 {
2860 vim_free(tv->vval.v_string);
2861 goto failed;
2862 }
2863 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002864 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002865 break;
2866
2867 // compare with special values
2868 case ISN_COMPAREBOOL:
2869 case ISN_COMPARESPECIAL:
2870 {
2871 typval_T *tv1 = STACK_TV_BOT(-2);
2872 typval_T *tv2 = STACK_TV_BOT(-1);
2873 varnumber_T arg1 = tv1->vval.v_number;
2874 varnumber_T arg2 = tv2->vval.v_number;
2875 int res;
2876
2877 switch (iptr->isn_arg.op.op_type)
2878 {
2879 case EXPR_EQUAL: res = arg1 == arg2; break;
2880 case EXPR_NEQUAL: res = arg1 != arg2; break;
2881 default: res = 0; break;
2882 }
2883
2884 --ectx.ec_stack.ga_len;
2885 tv1->v_type = VAR_BOOL;
2886 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2887 }
2888 break;
2889
2890 // Operation with two number arguments
2891 case ISN_OPNR:
2892 case ISN_COMPARENR:
2893 {
2894 typval_T *tv1 = STACK_TV_BOT(-2);
2895 typval_T *tv2 = STACK_TV_BOT(-1);
2896 varnumber_T arg1 = tv1->vval.v_number;
2897 varnumber_T arg2 = tv2->vval.v_number;
2898 varnumber_T res;
2899
2900 switch (iptr->isn_arg.op.op_type)
2901 {
2902 case EXPR_MULT: res = arg1 * arg2; break;
2903 case EXPR_DIV: res = arg1 / arg2; break;
2904 case EXPR_REM: res = arg1 % arg2; break;
2905 case EXPR_SUB: res = arg1 - arg2; break;
2906 case EXPR_ADD: res = arg1 + arg2; break;
2907
2908 case EXPR_EQUAL: res = arg1 == arg2; break;
2909 case EXPR_NEQUAL: res = arg1 != arg2; break;
2910 case EXPR_GREATER: res = arg1 > arg2; break;
2911 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2912 case EXPR_SMALLER: res = arg1 < arg2; break;
2913 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2914 default: res = 0; break;
2915 }
2916
2917 --ectx.ec_stack.ga_len;
2918 if (iptr->isn_type == ISN_COMPARENR)
2919 {
2920 tv1->v_type = VAR_BOOL;
2921 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2922 }
2923 else
2924 tv1->vval.v_number = res;
2925 }
2926 break;
2927
2928 // Computation with two float arguments
2929 case ISN_OPFLOAT:
2930 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002931#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002932 {
2933 typval_T *tv1 = STACK_TV_BOT(-2);
2934 typval_T *tv2 = STACK_TV_BOT(-1);
2935 float_T arg1 = tv1->vval.v_float;
2936 float_T arg2 = tv2->vval.v_float;
2937 float_T res = 0;
2938 int cmp = FALSE;
2939
2940 switch (iptr->isn_arg.op.op_type)
2941 {
2942 case EXPR_MULT: res = arg1 * arg2; break;
2943 case EXPR_DIV: res = arg1 / arg2; break;
2944 case EXPR_SUB: res = arg1 - arg2; break;
2945 case EXPR_ADD: res = arg1 + arg2; break;
2946
2947 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2948 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2949 case EXPR_GREATER: cmp = arg1 > arg2; break;
2950 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2951 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2952 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2953 default: cmp = 0; break;
2954 }
2955 --ectx.ec_stack.ga_len;
2956 if (iptr->isn_type == ISN_COMPAREFLOAT)
2957 {
2958 tv1->v_type = VAR_BOOL;
2959 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2960 }
2961 else
2962 tv1->vval.v_float = res;
2963 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002964#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002965 break;
2966
2967 case ISN_COMPARELIST:
2968 {
2969 typval_T *tv1 = STACK_TV_BOT(-2);
2970 typval_T *tv2 = STACK_TV_BOT(-1);
2971 list_T *arg1 = tv1->vval.v_list;
2972 list_T *arg2 = tv2->vval.v_list;
2973 int cmp = FALSE;
2974 int ic = iptr->isn_arg.op.op_ic;
2975
2976 switch (iptr->isn_arg.op.op_type)
2977 {
2978 case EXPR_EQUAL: cmp =
2979 list_equal(arg1, arg2, ic, FALSE); break;
2980 case EXPR_NEQUAL: cmp =
2981 !list_equal(arg1, arg2, ic, FALSE); break;
2982 case EXPR_IS: cmp = arg1 == arg2; break;
2983 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2984 default: cmp = 0; break;
2985 }
2986 --ectx.ec_stack.ga_len;
2987 clear_tv(tv1);
2988 clear_tv(tv2);
2989 tv1->v_type = VAR_BOOL;
2990 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2991 }
2992 break;
2993
2994 case ISN_COMPAREBLOB:
2995 {
2996 typval_T *tv1 = STACK_TV_BOT(-2);
2997 typval_T *tv2 = STACK_TV_BOT(-1);
2998 blob_T *arg1 = tv1->vval.v_blob;
2999 blob_T *arg2 = tv2->vval.v_blob;
3000 int cmp = FALSE;
3001
3002 switch (iptr->isn_arg.op.op_type)
3003 {
3004 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3005 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3006 case EXPR_IS: cmp = arg1 == arg2; break;
3007 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3008 default: cmp = 0; break;
3009 }
3010 --ectx.ec_stack.ga_len;
3011 clear_tv(tv1);
3012 clear_tv(tv2);
3013 tv1->v_type = VAR_BOOL;
3014 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3015 }
3016 break;
3017
3018 // TODO: handle separately
3019 case ISN_COMPARESTRING:
3020 case ISN_COMPAREDICT:
3021 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003022 case ISN_COMPAREANY:
3023 {
3024 typval_T *tv1 = STACK_TV_BOT(-2);
3025 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003026 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003027 int ic = iptr->isn_arg.op.op_ic;
3028
Bram Moolenaareb26f432020-09-14 16:50:05 +02003029 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003030 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003031 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003032 --ectx.ec_stack.ga_len;
3033 }
3034 break;
3035
3036 case ISN_ADDLIST:
3037 case ISN_ADDBLOB:
3038 {
3039 typval_T *tv1 = STACK_TV_BOT(-2);
3040 typval_T *tv2 = STACK_TV_BOT(-1);
3041
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003042 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003043 if (iptr->isn_type == ISN_ADDLIST)
3044 eval_addlist(tv1, tv2);
3045 else
3046 eval_addblob(tv1, tv2);
3047 clear_tv(tv2);
3048 --ectx.ec_stack.ga_len;
3049 }
3050 break;
3051
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003052 case ISN_LISTAPPEND:
3053 {
3054 typval_T *tv1 = STACK_TV_BOT(-2);
3055 typval_T *tv2 = STACK_TV_BOT(-1);
3056 list_T *l = tv1->vval.v_list;
3057
3058 // add an item to a list
3059 if (l == NULL)
3060 {
3061 SOURCING_LNUM = iptr->isn_lnum;
3062 emsg(_(e_cannot_add_to_null_list));
3063 goto on_error;
3064 }
3065 if (list_append_tv(l, tv2) == FAIL)
3066 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003067 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003068 --ectx.ec_stack.ga_len;
3069 }
3070 break;
3071
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003072 case ISN_BLOBAPPEND:
3073 {
3074 typval_T *tv1 = STACK_TV_BOT(-2);
3075 typval_T *tv2 = STACK_TV_BOT(-1);
3076 blob_T *b = tv1->vval.v_blob;
3077 int error = FALSE;
3078 varnumber_T n;
3079
3080 // add a number to a blob
3081 if (b == NULL)
3082 {
3083 SOURCING_LNUM = iptr->isn_lnum;
3084 emsg(_(e_cannot_add_to_null_blob));
3085 goto on_error;
3086 }
3087 n = tv_get_number_chk(tv2, &error);
3088 if (error)
3089 goto on_error;
3090 ga_append(&b->bv_ga, (int)n);
3091 --ectx.ec_stack.ga_len;
3092 }
3093 break;
3094
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003095 // Computation with two arguments of unknown type
3096 case ISN_OPANY:
3097 {
3098 typval_T *tv1 = STACK_TV_BOT(-2);
3099 typval_T *tv2 = STACK_TV_BOT(-1);
3100 varnumber_T n1, n2;
3101#ifdef FEAT_FLOAT
3102 float_T f1 = 0, f2 = 0;
3103#endif
3104 int error = FALSE;
3105
3106 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3107 {
3108 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3109 {
3110 eval_addlist(tv1, tv2);
3111 clear_tv(tv2);
3112 --ectx.ec_stack.ga_len;
3113 break;
3114 }
3115 else if (tv1->v_type == VAR_BLOB
3116 && tv2->v_type == VAR_BLOB)
3117 {
3118 eval_addblob(tv1, tv2);
3119 clear_tv(tv2);
3120 --ectx.ec_stack.ga_len;
3121 break;
3122 }
3123 }
3124#ifdef FEAT_FLOAT
3125 if (tv1->v_type == VAR_FLOAT)
3126 {
3127 f1 = tv1->vval.v_float;
3128 n1 = 0;
3129 }
3130 else
3131#endif
3132 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003133 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003134 n1 = tv_get_number_chk(tv1, &error);
3135 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003136 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003137#ifdef FEAT_FLOAT
3138 if (tv2->v_type == VAR_FLOAT)
3139 f1 = n1;
3140#endif
3141 }
3142#ifdef FEAT_FLOAT
3143 if (tv2->v_type == VAR_FLOAT)
3144 {
3145 f2 = tv2->vval.v_float;
3146 n2 = 0;
3147 }
3148 else
3149#endif
3150 {
3151 n2 = tv_get_number_chk(tv2, &error);
3152 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003153 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003154#ifdef FEAT_FLOAT
3155 if (tv1->v_type == VAR_FLOAT)
3156 f2 = n2;
3157#endif
3158 }
3159#ifdef FEAT_FLOAT
3160 // if there is a float on either side the result is a float
3161 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3162 {
3163 switch (iptr->isn_arg.op.op_type)
3164 {
3165 case EXPR_MULT: f1 = f1 * f2; break;
3166 case EXPR_DIV: f1 = f1 / f2; break;
3167 case EXPR_SUB: f1 = f1 - f2; break;
3168 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003169 default: SOURCING_LNUM = iptr->isn_lnum;
3170 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003171 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003172 }
3173 clear_tv(tv1);
3174 clear_tv(tv2);
3175 tv1->v_type = VAR_FLOAT;
3176 tv1->vval.v_float = f1;
3177 --ectx.ec_stack.ga_len;
3178 }
3179 else
3180#endif
3181 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003182 int failed = FALSE;
3183
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003184 switch (iptr->isn_arg.op.op_type)
3185 {
3186 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003187 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3188 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003189 goto on_error;
3190 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 case EXPR_SUB: n1 = n1 - n2; break;
3192 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003193 default: n1 = num_modulus(n1, n2, &failed);
3194 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003195 goto on_error;
3196 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003197 }
3198 clear_tv(tv1);
3199 clear_tv(tv2);
3200 tv1->v_type = VAR_NUMBER;
3201 tv1->vval.v_number = n1;
3202 --ectx.ec_stack.ga_len;
3203 }
3204 }
3205 break;
3206
3207 case ISN_CONCAT:
3208 {
3209 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3210 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3211 char_u *res;
3212
3213 res = concat_str(str1, str2);
3214 clear_tv(STACK_TV_BOT(-2));
3215 clear_tv(STACK_TV_BOT(-1));
3216 --ectx.ec_stack.ga_len;
3217 STACK_TV_BOT(-1)->vval.v_string = res;
3218 }
3219 break;
3220
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003221 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003222 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003223 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003224 int is_slice = iptr->isn_type == ISN_STRSLICE;
3225 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003226 char_u *res;
3227
3228 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003229 // string slice: string is at stack-3, first index at
3230 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003231 if (is_slice)
3232 {
3233 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003234 n1 = tv->vval.v_number;
3235 }
3236
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003237 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003238 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003239
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003240 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003241 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003242 if (is_slice)
3243 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003244 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003245 else
3246 // Index: The resulting variable is a string of a
3247 // single character. If the index is too big or
3248 // negative the result is empty.
3249 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003250 vim_free(tv->vval.v_string);
3251 tv->vval.v_string = res;
3252 }
3253 break;
3254
3255 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003256 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003257 {
Bram Moolenaared591872020-08-15 22:14:53 +02003258 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003259 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003260 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003261
3262 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003263 // list slice: list is at stack-3, indexes at stack-2 and
3264 // stack-1
3265 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003266 list = tv->vval.v_list;
3267
3268 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003269 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003270 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003271
3272 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
Bram Moolenaared591872020-08-15 22:14:53 +02003274 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003275 n1 = tv->vval.v_number;
3276 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277 }
Bram Moolenaared591872020-08-15 22:14:53 +02003278
3279 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003280 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003281 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003282 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3283 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003284 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 }
3286 break;
3287
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003288 case ISN_ANYINDEX:
3289 case ISN_ANYSLICE:
3290 {
3291 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3292 typval_T *var1, *var2;
3293 int res;
3294
3295 // index: composite is at stack-2, index at stack-1
3296 // slice: composite is at stack-3, indexes at stack-2 and
3297 // stack-1
3298 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003299 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003300 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3301 goto on_error;
3302 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3303 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003304 res = eval_index_inner(tv, is_slice, var1, var2,
3305 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003306 clear_tv(var1);
3307 if (is_slice)
3308 clear_tv(var2);
3309 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3310 if (res == FAIL)
3311 goto on_error;
3312 }
3313 break;
3314
Bram Moolenaar9af78762020-06-16 11:34:42 +02003315 case ISN_SLICE:
3316 {
3317 list_T *list;
3318 int count = iptr->isn_arg.number;
3319
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003320 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003321 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003322 list = tv->vval.v_list;
3323
3324 // no error for short list, expect it to be checked earlier
3325 if (list != NULL && list->lv_len >= count)
3326 {
3327 list_T *newlist = list_slice(list,
3328 count, list->lv_len - 1);
3329
3330 if (newlist != NULL)
3331 {
3332 list_unref(list);
3333 tv->vval.v_list = newlist;
3334 ++newlist->lv_refcount;
3335 }
3336 }
3337 }
3338 break;
3339
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003340 case ISN_GETITEM:
3341 {
3342 listitem_T *li;
3343 int index = iptr->isn_arg.number;
3344
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003345 // Get list item: list is at stack-1, push item.
3346 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003347 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003348 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003349
3350 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3351 goto failed;
3352 ++ectx.ec_stack.ga_len;
3353 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003354
3355 // Useful when used in unpack assignment. Reset at
3356 // ISN_DROP.
3357 where.wt_index = index + 1;
3358 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003359 }
3360 break;
3361
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003362 case ISN_MEMBER:
3363 {
3364 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003365 char_u *key;
3366 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003367 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003368
3369 // dict member: dict is at stack-2, key at stack-1
3370 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003371 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003372 dict = tv->vval.v_dict;
3373
3374 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003375 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003376 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003377 if (key == NULL)
3378 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003379
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003380 if ((di = dict_find(dict, key, -1)) == NULL)
3381 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003382 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003383 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003384
3385 // If :silent! is used we will continue, make sure the
3386 // stack contents makes sense.
3387 clear_tv(tv);
3388 --ectx.ec_stack.ga_len;
3389 tv = STACK_TV_BOT(-1);
3390 clear_tv(tv);
3391 tv->v_type = VAR_NUMBER;
3392 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003393 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003394 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003395 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003396 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003397 // Clear the dict only after getting the item, to avoid
3398 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003399 tv = STACK_TV_BOT(-1);
3400 temp_tv = *tv;
3401 copy_tv(&di->di_tv, tv);
3402 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003403 }
3404 break;
3405
3406 // dict member with string key
3407 case ISN_STRINGMEMBER:
3408 {
3409 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003410 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003411 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003412
3413 tv = STACK_TV_BOT(-1);
3414 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3415 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003416 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003417 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003418 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003419 }
3420 dict = tv->vval.v_dict;
3421
3422 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3423 == NULL)
3424 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003425 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003427 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003429 // Clear the dict after getting the item, to avoid that it
3430 // make the item invalid.
3431 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003433 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 }
3435 break;
3436
3437 case ISN_NEGATENR:
3438 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003439 if (tv->v_type != VAR_NUMBER
3440#ifdef FEAT_FLOAT
3441 && tv->v_type != VAR_FLOAT
3442#endif
3443 )
3444 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003445 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003446 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003447 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003448 }
3449#ifdef FEAT_FLOAT
3450 if (tv->v_type == VAR_FLOAT)
3451 tv->vval.v_float = -tv->vval.v_float;
3452 else
3453#endif
3454 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003455 break;
3456
3457 case ISN_CHECKNR:
3458 {
3459 int error = FALSE;
3460
3461 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003462 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003464 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003465 (void)tv_get_number_chk(tv, &error);
3466 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003467 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003468 }
3469 break;
3470
3471 case ISN_CHECKTYPE:
3472 {
3473 checktype_T *ct = &iptr->isn_arg.type;
3474
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003475 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003476 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003477 if (!where.wt_variable)
3478 where.wt_index = ct->ct_arg_idx;
3479 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003480 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003481 if (!where.wt_variable)
3482 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003483
3484 // number 0 is FALSE, number 1 is TRUE
3485 if (tv->v_type == VAR_NUMBER
3486 && ct->ct_type->tt_type == VAR_BOOL
3487 && (tv->vval.v_number == 0
3488 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003489 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003490 tv->v_type = VAR_BOOL;
3491 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003492 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003493 }
3494 }
3495 break;
3496
Bram Moolenaar9af78762020-06-16 11:34:42 +02003497 case ISN_CHECKLEN:
3498 {
3499 int min_len = iptr->isn_arg.checklen.cl_min_len;
3500 list_T *list = NULL;
3501
3502 tv = STACK_TV_BOT(-1);
3503 if (tv->v_type == VAR_LIST)
3504 list = tv->vval.v_list;
3505 if (list == NULL || list->lv_len < min_len
3506 || (list->lv_len > min_len
3507 && !iptr->isn_arg.checklen.cl_more_OK))
3508 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003509 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003510 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003511 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003512 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003513 }
3514 }
3515 break;
3516
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003517 case ISN_SETTYPE:
3518 {
3519 checktype_T *ct = &iptr->isn_arg.type;
3520
3521 tv = STACK_TV_BOT(-1);
3522 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3523 {
3524 free_type(tv->vval.v_dict->dv_type);
3525 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3526 }
3527 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3528 {
3529 free_type(tv->vval.v_list->lv_type);
3530 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3531 }
3532 }
3533 break;
3534
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003535 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003536 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003537 {
3538 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003539 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003540
3541 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003542 if (iptr->isn_type == ISN_2BOOL)
3543 {
3544 n = tv2bool(tv);
3545 if (iptr->isn_arg.number) // invert
3546 n = !n;
3547 }
3548 else
3549 {
3550 SOURCING_LNUM = iptr->isn_lnum;
3551 n = tv_get_bool_chk(tv, &error);
3552 if (error)
3553 goto on_error;
3554 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003555 clear_tv(tv);
3556 tv->v_type = VAR_BOOL;
3557 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3558 }
3559 break;
3560
3561 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003562 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003563 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003564 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3565 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3566 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003567 break;
3568
Bram Moolenaar08597872020-12-10 19:43:40 +01003569 case ISN_RANGE:
3570 {
3571 exarg_T ea;
3572 char *errormsg;
3573
Bram Moolenaarece0b872021-01-08 20:40:45 +01003574 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003575 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003576 ea.addr_type = ADDR_LINES;
3577 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003578 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003579 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003580 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003581
3582 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3583 goto failed;
3584 ++ectx.ec_stack.ga_len;
3585 tv = STACK_TV_BOT(-1);
3586 tv->v_type = VAR_NUMBER;
3587 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003588 if (ea.addr_count == 0)
3589 tv->vval.v_number = curwin->w_cursor.lnum;
3590 else
3591 tv->vval.v_number = ea.line2;
3592 }
3593 break;
3594
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003595 case ISN_PUT:
3596 {
3597 int regname = iptr->isn_arg.put.put_regname;
3598 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3599 char_u *expr = NULL;
3600 int dir = FORWARD;
3601
Bram Moolenaar08597872020-12-10 19:43:40 +01003602 if (lnum < -2)
3603 {
3604 // line number was put on the stack by ISN_RANGE
3605 tv = STACK_TV_BOT(-1);
3606 curwin->w_cursor.lnum = tv->vval.v_number;
3607 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3608 dir = BACKWARD;
3609 --ectx.ec_stack.ga_len;
3610 }
3611 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003612 // :put! above cursor
3613 dir = BACKWARD;
3614 else if (lnum >= 0)
3615 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003616
3617 if (regname == '=')
3618 {
3619 tv = STACK_TV_BOT(-1);
3620 if (tv->v_type == VAR_STRING)
3621 expr = tv->vval.v_string;
3622 else
3623 {
3624 expr = typval2string(tv, TRUE); // allocates value
3625 clear_tv(tv);
3626 }
3627 --ectx.ec_stack.ga_len;
3628 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003629 check_cursor();
3630 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3631 vim_free(expr);
3632 }
3633 break;
3634
Bram Moolenaar02194d22020-10-24 23:08:38 +02003635 case ISN_CMDMOD:
3636 save_cmdmod = cmdmod;
3637 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003638 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003639 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3640 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003641 break;
3642
Bram Moolenaar02194d22020-10-24 23:08:38 +02003643 case ISN_CMDMOD_REV:
3644 // filter regprog is owned by the instruction, don't free it
3645 cmdmod.cmod_filter_regmatch.regprog = NULL;
3646 undo_cmdmod(&cmdmod);
3647 cmdmod = save_cmdmod;
3648 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003649 break;
3650
Bram Moolenaar792f7862020-11-23 08:31:18 +01003651 case ISN_UNPACK:
3652 {
3653 int count = iptr->isn_arg.unpack.unp_count;
3654 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3655 list_T *l;
3656 listitem_T *li;
3657 int i;
3658
3659 // Check there is a valid list to unpack.
3660 tv = STACK_TV_BOT(-1);
3661 if (tv->v_type != VAR_LIST)
3662 {
3663 SOURCING_LNUM = iptr->isn_lnum;
3664 emsg(_(e_for_argument_must_be_sequence_of_lists));
3665 goto on_error;
3666 }
3667 l = tv->vval.v_list;
3668 if (l == NULL
3669 || l->lv_len < (semicolon ? count - 1 : count))
3670 {
3671 SOURCING_LNUM = iptr->isn_lnum;
3672 emsg(_(e_list_value_does_not_have_enough_items));
3673 goto on_error;
3674 }
3675 else if (!semicolon && l->lv_len > count)
3676 {
3677 SOURCING_LNUM = iptr->isn_lnum;
3678 emsg(_(e_list_value_has_more_items_than_targets));
3679 goto on_error;
3680 }
3681
3682 CHECK_LIST_MATERIALIZE(l);
3683 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3684 goto failed;
3685 ectx.ec_stack.ga_len += count - 1;
3686
3687 // Variable after semicolon gets a list with the remaining
3688 // items.
3689 if (semicolon)
3690 {
3691 list_T *rem_list =
3692 list_alloc_with_items(l->lv_len - count + 1);
3693
3694 if (rem_list == NULL)
3695 goto failed;
3696 tv = STACK_TV_BOT(-count);
3697 tv->vval.v_list = rem_list;
3698 ++rem_list->lv_refcount;
3699 tv->v_lock = 0;
3700 li = l->lv_first;
3701 for (i = 0; i < count - 1; ++i)
3702 li = li->li_next;
3703 for (i = 0; li != NULL; ++i)
3704 {
3705 list_set_item(rem_list, i, &li->li_tv);
3706 li = li->li_next;
3707 }
3708 --count;
3709 }
3710
3711 // Produce the values in reverse order, first item last.
3712 li = l->lv_first;
3713 for (i = 0; i < count; ++i)
3714 {
3715 tv = STACK_TV_BOT(-i - 1);
3716 copy_tv(&li->li_tv, tv);
3717 li = li->li_next;
3718 }
3719
3720 list_unref(l);
3721 }
3722 break;
3723
Bram Moolenaarb2049902021-01-24 12:53:53 +01003724 case ISN_PROF_START:
3725 case ISN_PROF_END:
3726 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003727#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003728 funccall_T cookie;
3729 ufunc_T *cur_ufunc =
3730 (((dfunc_T *)def_functions.ga_data)
3731 + ectx.ec_dfunc_idx)->df_ufunc;
3732
3733 cookie.func = cur_ufunc;
3734 if (iptr->isn_type == ISN_PROF_START)
3735 {
3736 func_line_start(&cookie, iptr->isn_lnum);
3737 // if we get here the instruction is executed
3738 func_line_exec(&cookie);
3739 }
3740 else
3741 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003742#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003743 }
3744 break;
3745
Bram Moolenaar389df252020-07-09 21:20:47 +02003746 case ISN_SHUFFLE:
3747 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003748 typval_T tmp_tv;
3749 int item = iptr->isn_arg.shuffle.shfl_item;
3750 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003751
3752 tmp_tv = *STACK_TV_BOT(-item);
3753 for ( ; up > 0 && item > 1; --up)
3754 {
3755 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3756 --item;
3757 }
3758 *STACK_TV_BOT(-item) = tmp_tv;
3759 }
3760 break;
3761
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003762 case ISN_DROP:
3763 --ectx.ec_stack.ga_len;
3764 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003765 where.wt_index = 0;
3766 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003767 break;
3768 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003769 continue;
3770
Bram Moolenaard032f342020-07-18 18:13:02 +02003771func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003772 // Restore previous function. If the frame pointer is where we started
3773 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003774 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003775 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003776
Bram Moolenaard032f342020-07-18 18:13:02 +02003777 if (func_return(&ectx) == FAIL)
3778 // only fails when out of memory
3779 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003780 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003781
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003782on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003783 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003784 // If "emsg_silent" is set then ignore the error, unless it was set
3785 // when calling the function.
3786 if (did_emsg_cumul + did_emsg == did_emsg_before
3787 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003788 {
3789 // If a sequence of instructions causes an error while ":silent!"
3790 // was used, restore the stack length and jump ahead to restoring
3791 // the cmdmod.
3792 if (restore_cmdmod)
3793 {
3794 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3795 {
3796 --ectx.ec_stack.ga_len;
3797 clear_tv(STACK_TV_BOT(0));
3798 }
3799 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3800 ++ectx.ec_iidx;
3801 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003802 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003803 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003804on_fatal_error:
3805 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003806 // If we are not inside a try-catch started here, abort execution.
3807 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003808 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003809 }
3810
3811done:
3812 // function finished, get result from the stack.
3813 tv = STACK_TV_BOT(-1);
3814 *rettv = *tv;
3815 tv->v_type = VAR_UNKNOWN;
3816 ret = OK;
3817
3818failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003819 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003820 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003821 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003822
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003823 // Deal with any remaining closures, they may be in use somewhere.
3824 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003825 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003826 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003827 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3828 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003829
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003830 estack_pop();
3831 current_sctx = save_current_sctx;
3832
Bram Moolenaar352134b2020-10-17 22:04:08 +02003833 if (*msg_list != NULL && saved_msg_list != NULL)
3834 {
3835 msglist_T **plist = saved_msg_list;
3836
3837 // Append entries from the current msg_list (uncaught exceptions) to
3838 // the saved msg_list.
3839 while (*plist != NULL)
3840 plist = &(*plist)->next;
3841
3842 *plist = *msg_list;
3843 }
3844 msg_list = saved_msg_list;
3845
Bram Moolenaar02194d22020-10-24 23:08:38 +02003846 if (restore_cmdmod)
3847 {
3848 cmdmod.cmod_filter_regmatch.regprog = NULL;
3849 undo_cmdmod(&cmdmod);
3850 cmdmod = save_cmdmod;
3851 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003852 emsg_silent_def = save_emsg_silent_def;
3853 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003854
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003855failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003856 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003857 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3858 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003860 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003861 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003862
Bram Moolenaar0186e582021-01-10 18:33:11 +01003863 while (ectx.ec_outer != NULL)
3864 {
3865 outer_T *up = ectx.ec_outer->out_up_is_copy
3866 ? NULL : ectx.ec_outer->out_up;
3867
3868 vim_free(ectx.ec_outer);
3869 ectx.ec_outer = up;
3870 }
3871
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003872 // Not sure if this is necessary.
3873 suppress_errthrow = save_suppress_errthrow;
3874
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003875 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003876 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003877 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003878 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 return ret;
3880}
3881
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003882/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003883 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003884 * We don't really need this at runtime, but we do have tests that require it,
3885 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003886 */
3887 void
3888ex_disassemble(exarg_T *eap)
3889{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003890 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003891 char_u *fname;
3892 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003893 dfunc_T *dfunc;
3894 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003895 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003896 int current;
3897 int line_idx = 0;
3898 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003899 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003900
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003901 if (STRNCMP(arg, "<lambda>", 8) == 0)
3902 {
3903 arg += 8;
3904 (void)getdigits(&arg);
3905 fname = vim_strnsave(eap->arg, arg - eap->arg);
3906 }
3907 else
3908 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003909 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003910 if (fname == NULL)
3911 {
3912 semsg(_(e_invarg2), eap->arg);
3913 return;
3914 }
3915
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003916 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003917 if (ufunc == NULL)
3918 {
3919 char_u *p = untrans_function_name(fname);
3920
3921 if (p != NULL)
3922 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003923 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003924 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003925 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003926 if (ufunc == NULL)
3927 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003928 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003929 return;
3930 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003931 if (func_needs_compiling(ufunc, eap->forceit)
3932 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003933 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003934 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003936 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 return;
3938 }
3939 if (ufunc->uf_name_exp != NULL)
3940 msg((char *)ufunc->uf_name_exp);
3941 else
3942 msg((char *)ufunc->uf_name);
3943
3944 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003945#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003946 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3947 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3948 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003949#else
3950 instr = dfunc->df_instr;
3951 instr_count = dfunc->df_instr_count;
3952#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003953 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 {
3955 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003956 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003957
3958 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3959 {
3960 if (current > prev_current)
3961 {
3962 msg_puts("\n\n");
3963 prev_current = current;
3964 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003965 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3966 if (line != NULL)
3967 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003968 }
3969
3970 switch (iptr->isn_type)
3971 {
3972 case ISN_EXEC:
3973 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3974 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003975 case ISN_EXECCONCAT:
3976 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003977 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003978 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003979 case ISN_ECHO:
3980 {
3981 echo_T *echo = &iptr->isn_arg.echo;
3982
3983 smsg("%4d %s %d", current,
3984 echo->echo_with_white ? "ECHO" : "ECHON",
3985 echo->echo_count);
3986 }
3987 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003988 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003989 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003990 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003991 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003992 case ISN_ECHOMSG:
3993 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003994 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003995 break;
3996 case ISN_ECHOERR:
3997 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003998 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003999 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004001 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004002 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004003 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004004 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004005 + STACK_FRAME_SIZE));
4006 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004007 smsg("%4d LOAD $%lld", current,
4008 (varnumber_T)(iptr->isn_arg.number));
4009 }
4010 break;
4011 case ISN_LOADOUTER:
4012 {
4013 if (iptr->isn_arg.number < 0)
4014 smsg("%4d LOADOUTER level %d arg[%d]", current,
4015 iptr->isn_arg.outer.outer_depth,
4016 iptr->isn_arg.outer.outer_idx
4017 + STACK_FRAME_SIZE);
4018 else
4019 smsg("%4d LOADOUTER level %d $%d", current,
4020 iptr->isn_arg.outer.outer_depth,
4021 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004022 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004023 break;
4024 case ISN_LOADV:
4025 smsg("%4d LOADV v:%s", current,
4026 get_vim_var_name(iptr->isn_arg.number));
4027 break;
4028 case ISN_LOADSCRIPT:
4029 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004030 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4031 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004032 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004033 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004034
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004035 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4036 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004037 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004038 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004039 }
4040 break;
4041 case ISN_LOADS:
4042 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004043 scriptitem_T *si = SCRIPT_ITEM(
4044 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004045
4046 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004047 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004048 }
4049 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004050 case ISN_LOADAUTO:
4051 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4052 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053 case ISN_LOADG:
4054 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4055 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004056 case ISN_LOADB:
4057 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4058 break;
4059 case ISN_LOADW:
4060 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4061 break;
4062 case ISN_LOADT:
4063 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4064 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004065 case ISN_LOADGDICT:
4066 smsg("%4d LOAD g:", current);
4067 break;
4068 case ISN_LOADBDICT:
4069 smsg("%4d LOAD b:", current);
4070 break;
4071 case ISN_LOADWDICT:
4072 smsg("%4d LOAD w:", current);
4073 break;
4074 case ISN_LOADTDICT:
4075 smsg("%4d LOAD t:", current);
4076 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004077 case ISN_LOADOPT:
4078 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4079 break;
4080 case ISN_LOADENV:
4081 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4082 break;
4083 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004084 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 break;
4086
4087 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004088 if (iptr->isn_arg.number < 0)
4089 smsg("%4d STORE arg[%lld]", current,
4090 iptr->isn_arg.number + STACK_FRAME_SIZE);
4091 else
4092 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4093 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004094 case ISN_STOREOUTER:
4095 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004096 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004097 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4098 iptr->isn_arg.outer.outer_depth,
4099 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004100 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004101 smsg("%4d STOREOUTER level %d $%d", current,
4102 iptr->isn_arg.outer.outer_depth,
4103 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004104 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004105 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004106 case ISN_STOREV:
4107 smsg("%4d STOREV v:%s", current,
4108 get_vim_var_name(iptr->isn_arg.number));
4109 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004110 case ISN_STOREAUTO:
4111 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4112 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004114 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4115 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004116 case ISN_STOREB:
4117 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4118 break;
4119 case ISN_STOREW:
4120 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4121 break;
4122 case ISN_STORET:
4123 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4124 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004125 case ISN_STORES:
4126 {
4127 scriptitem_T *si = SCRIPT_ITEM(
4128 iptr->isn_arg.loadstore.ls_sid);
4129
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004130 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004131 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004132 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004133 break;
4134 case ISN_STORESCRIPT:
4135 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004136 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4137 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004139 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004140
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004141 smsg("%4d STORESCRIPT %s-%d in %s", current,
4142 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004143 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004144 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004145 }
4146 break;
4147 case ISN_STOREOPT:
4148 smsg("%4d STOREOPT &%s", current,
4149 iptr->isn_arg.storeopt.so_name);
4150 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004151 case ISN_STOREENV:
4152 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4153 break;
4154 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004155 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004156 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 case ISN_STORENR:
4158 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004159 iptr->isn_arg.storenr.stnr_val,
4160 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004161 break;
4162
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004163 case ISN_STOREINDEX:
4164 switch (iptr->isn_arg.vartype)
4165 {
4166 case VAR_LIST:
4167 smsg("%4d STORELIST", current);
4168 break;
4169 case VAR_DICT:
4170 smsg("%4d STOREDICT", current);
4171 break;
4172 case VAR_ANY:
4173 smsg("%4d STOREINDEX", current);
4174 break;
4175 default: break;
4176 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004177 break;
4178
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004179 // constants
4180 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004181 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004182 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004183 break;
4184 case ISN_PUSHBOOL:
4185 case ISN_PUSHSPEC:
4186 smsg("%4d PUSH %s", current,
4187 get_var_special_name(iptr->isn_arg.number));
4188 break;
4189 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004190#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004192#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004193 break;
4194 case ISN_PUSHS:
4195 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4196 break;
4197 case ISN_PUSHBLOB:
4198 {
4199 char_u *r;
4200 char_u numbuf[NUMBUFLEN];
4201 char_u *tofree;
4202
4203 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004204 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004205 vim_free(tofree);
4206 }
4207 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004208 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004209 {
4210 char *name = (char *)iptr->isn_arg.string;
4211
4212 smsg("%4d PUSHFUNC \"%s\"", current,
4213 name == NULL ? "[none]" : name);
4214 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004215 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004216 case ISN_PUSHCHANNEL:
4217#ifdef FEAT_JOB_CHANNEL
4218 {
4219 channel_T *channel = iptr->isn_arg.channel;
4220
4221 smsg("%4d PUSHCHANNEL %d", current,
4222 channel == NULL ? 0 : channel->ch_id);
4223 }
4224#endif
4225 break;
4226 case ISN_PUSHJOB:
4227#ifdef FEAT_JOB_CHANNEL
4228 {
4229 typval_T tv;
4230 char_u *name;
4231
4232 tv.v_type = VAR_JOB;
4233 tv.vval.v_job = iptr->isn_arg.job;
4234 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004235 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004236 }
4237#endif
4238 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004239 case ISN_PUSHEXC:
4240 smsg("%4d PUSH v:exception", current);
4241 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004242 case ISN_UNLET:
4243 smsg("%4d UNLET%s %s", current,
4244 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4245 iptr->isn_arg.unlet.ul_name);
4246 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004247 case ISN_UNLETENV:
4248 smsg("%4d UNLETENV%s $%s", current,
4249 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4250 iptr->isn_arg.unlet.ul_name);
4251 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004252 case ISN_UNLETINDEX:
4253 smsg("%4d UNLETINDEX", current);
4254 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004255 case ISN_UNLETRANGE:
4256 smsg("%4d UNLETRANGE", current);
4257 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004258 case ISN_LOCKCONST:
4259 smsg("%4d LOCKCONST", current);
4260 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004261 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004262 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004263 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004264 break;
4265 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004266 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004267 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004268 break;
4269
4270 // function call
4271 case ISN_BCALL:
4272 {
4273 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4274
4275 smsg("%4d BCALL %s(argc %d)", current,
4276 internal_func_name(cbfunc->cbf_idx),
4277 cbfunc->cbf_argcount);
4278 }
4279 break;
4280 case ISN_DCALL:
4281 {
4282 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4283 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4284 + cdfunc->cdf_idx;
4285
4286 smsg("%4d DCALL %s(argc %d)", current,
4287 df->df_ufunc->uf_name_exp != NULL
4288 ? df->df_ufunc->uf_name_exp
4289 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4290 }
4291 break;
4292 case ISN_UCALL:
4293 {
4294 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4295
4296 smsg("%4d UCALL %s(argc %d)", current,
4297 cufunc->cuf_name, cufunc->cuf_argcount);
4298 }
4299 break;
4300 case ISN_PCALL:
4301 {
4302 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4303
4304 smsg("%4d PCALL%s (argc %d)", current,
4305 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4306 }
4307 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004308 case ISN_PCALL_END:
4309 smsg("%4d PCALL end", current);
4310 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004311 case ISN_RETURN:
4312 smsg("%4d RETURN", current);
4313 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004314 case ISN_RETURN_ZERO:
4315 smsg("%4d RETURN 0", current);
4316 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004317 case ISN_FUNCREF:
4318 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004319 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004320 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004321 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004323 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004324 }
4325 break;
4326
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004327 case ISN_NEWFUNC:
4328 {
4329 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4330
4331 smsg("%4d NEWFUNC %s %s", current,
4332 newfunc->nf_lambda, newfunc->nf_global);
4333 }
4334 break;
4335
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004336 case ISN_DEF:
4337 {
4338 char_u *name = iptr->isn_arg.string;
4339
4340 smsg("%4d DEF %s", current,
4341 name == NULL ? (char_u *)"" : name);
4342 }
4343 break;
4344
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004345 case ISN_JUMP:
4346 {
4347 char *when = "?";
4348
4349 switch (iptr->isn_arg.jump.jump_when)
4350 {
4351 case JUMP_ALWAYS:
4352 when = "JUMP";
4353 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004354 case JUMP_AND_KEEP_IF_TRUE:
4355 when = "JUMP_AND_KEEP_IF_TRUE";
4356 break;
4357 case JUMP_IF_FALSE:
4358 when = "JUMP_IF_FALSE";
4359 break;
4360 case JUMP_AND_KEEP_IF_FALSE:
4361 when = "JUMP_AND_KEEP_IF_FALSE";
4362 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004363 case JUMP_IF_COND_FALSE:
4364 when = "JUMP_IF_COND_FALSE";
4365 break;
4366 case JUMP_IF_COND_TRUE:
4367 when = "JUMP_IF_COND_TRUE";
4368 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004369 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004370 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004371 iptr->isn_arg.jump.jump_where);
4372 }
4373 break;
4374
4375 case ISN_FOR:
4376 {
4377 forloop_T *forloop = &iptr->isn_arg.forloop;
4378
4379 smsg("%4d FOR $%d -> %d", current,
4380 forloop->for_idx, forloop->for_end);
4381 }
4382 break;
4383
4384 case ISN_TRY:
4385 {
4386 try_T *try = &iptr->isn_arg.try;
4387
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004388 if (try->try_ref->try_finally == 0)
4389 smsg("%4d TRY catch -> %d, endtry -> %d",
4390 current,
4391 try->try_ref->try_catch,
4392 try->try_ref->try_endtry);
4393 else
4394 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4395 current,
4396 try->try_ref->try_catch,
4397 try->try_ref->try_finally,
4398 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004399 }
4400 break;
4401 case ISN_CATCH:
4402 // TODO
4403 smsg("%4d CATCH", current);
4404 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004405 case ISN_TRYCONT:
4406 {
4407 trycont_T *trycont = &iptr->isn_arg.trycont;
4408
4409 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4410 trycont->tct_levels,
4411 trycont->tct_levels == 1 ? "" : "s",
4412 trycont->tct_where);
4413 }
4414 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004415 case ISN_FINALLY:
4416 smsg("%4d FINALLY", current);
4417 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 case ISN_ENDTRY:
4419 smsg("%4d ENDTRY", current);
4420 break;
4421 case ISN_THROW:
4422 smsg("%4d THROW", current);
4423 break;
4424
4425 // expression operations on number
4426 case ISN_OPNR:
4427 case ISN_OPFLOAT:
4428 case ISN_OPANY:
4429 {
4430 char *what;
4431 char *ins;
4432
4433 switch (iptr->isn_arg.op.op_type)
4434 {
4435 case EXPR_MULT: what = "*"; break;
4436 case EXPR_DIV: what = "/"; break;
4437 case EXPR_REM: what = "%"; break;
4438 case EXPR_SUB: what = "-"; break;
4439 case EXPR_ADD: what = "+"; break;
4440 default: what = "???"; break;
4441 }
4442 switch (iptr->isn_type)
4443 {
4444 case ISN_OPNR: ins = "OPNR"; break;
4445 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4446 case ISN_OPANY: ins = "OPANY"; break;
4447 default: ins = "???"; break;
4448 }
4449 smsg("%4d %s %s", current, ins, what);
4450 }
4451 break;
4452
4453 case ISN_COMPAREBOOL:
4454 case ISN_COMPARESPECIAL:
4455 case ISN_COMPARENR:
4456 case ISN_COMPAREFLOAT:
4457 case ISN_COMPARESTRING:
4458 case ISN_COMPAREBLOB:
4459 case ISN_COMPARELIST:
4460 case ISN_COMPAREDICT:
4461 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004462 case ISN_COMPAREANY:
4463 {
4464 char *p;
4465 char buf[10];
4466 char *type;
4467
4468 switch (iptr->isn_arg.op.op_type)
4469 {
4470 case EXPR_EQUAL: p = "=="; break;
4471 case EXPR_NEQUAL: p = "!="; break;
4472 case EXPR_GREATER: p = ">"; break;
4473 case EXPR_GEQUAL: p = ">="; break;
4474 case EXPR_SMALLER: p = "<"; break;
4475 case EXPR_SEQUAL: p = "<="; break;
4476 case EXPR_MATCH: p = "=~"; break;
4477 case EXPR_IS: p = "is"; break;
4478 case EXPR_ISNOT: p = "isnot"; break;
4479 case EXPR_NOMATCH: p = "!~"; break;
4480 default: p = "???"; break;
4481 }
4482 STRCPY(buf, p);
4483 if (iptr->isn_arg.op.op_ic == TRUE)
4484 strcat(buf, "?");
4485 switch(iptr->isn_type)
4486 {
4487 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4488 case ISN_COMPARESPECIAL:
4489 type = "COMPARESPECIAL"; break;
4490 case ISN_COMPARENR: type = "COMPARENR"; break;
4491 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4492 case ISN_COMPARESTRING:
4493 type = "COMPARESTRING"; break;
4494 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4495 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4496 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4497 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004498 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4499 default: type = "???"; break;
4500 }
4501
4502 smsg("%4d %s %s", current, type, buf);
4503 }
4504 break;
4505
4506 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4507 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4508
4509 // expression operations
4510 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004511 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004512 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004513 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004514 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004515 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004516 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004517 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4518 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004519 case ISN_SLICE: smsg("%4d SLICE %lld",
4520 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004521 case ISN_GETITEM: smsg("%4d ITEM %lld",
4522 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004523 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4524 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004525 iptr->isn_arg.string); break;
4526 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4527
4528 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004529 case ISN_CHECKTYPE:
4530 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004531 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004532 char *tofree;
4533
Bram Moolenaare32e5162021-01-21 20:21:29 +01004534 if (ct->ct_arg_idx == 0)
4535 smsg("%4d CHECKTYPE %s stack[%d]", current,
4536 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004537 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004538 else
4539 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4540 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004541 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004542 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004543 vim_free(tofree);
4544 break;
4545 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004546 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4547 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4548 iptr->isn_arg.checklen.cl_min_len);
4549 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004550 case ISN_SETTYPE:
4551 {
4552 char *tofree;
4553
4554 smsg("%4d SETTYPE %s", current,
4555 type_name(iptr->isn_arg.type.ct_type, &tofree));
4556 vim_free(tofree);
4557 break;
4558 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004559 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004560 case ISN_2BOOL: if (iptr->isn_arg.number)
4561 smsg("%4d INVERT (!val)", current);
4562 else
4563 smsg("%4d 2BOOL (!!val)", current);
4564 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004565 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004566 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004567 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004568 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004569 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004570 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004571 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4572 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004573 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004574 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4575 smsg("%4d PUT %c above range",
4576 current, iptr->isn_arg.put.put_regname);
4577 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4578 smsg("%4d PUT %c range",
4579 current, iptr->isn_arg.put.put_regname);
4580 else
4581 smsg("%4d PUT %c %ld", current,
4582 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004583 (long)iptr->isn_arg.put.put_lnum);
4584 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004585
Bram Moolenaar02194d22020-10-24 23:08:38 +02004586 // TODO: summarize modifiers
4587 case ISN_CMDMOD:
4588 {
4589 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004590 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004591 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4592
4593 buf = alloc(len + 1);
4594 if (buf != NULL)
4595 {
4596 (void)produce_cmdmods(
4597 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4598 smsg("%4d CMDMOD %s", current, buf);
4599 vim_free(buf);
4600 }
4601 break;
4602 }
4603 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004604
Bram Moolenaarb2049902021-01-24 12:53:53 +01004605 case ISN_PROF_START:
4606 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4607 break;
4608
4609 case ISN_PROF_END:
4610 smsg("%4d PROFILE END", current);
4611 break;
4612
Bram Moolenaar792f7862020-11-23 08:31:18 +01004613 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4614 iptr->isn_arg.unpack.unp_count,
4615 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4616 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004617 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4618 iptr->isn_arg.shuffle.shfl_item,
4619 iptr->isn_arg.shuffle.shfl_up);
4620 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004621 case ISN_DROP: smsg("%4d DROP", current); break;
4622 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004623
4624 out_flush(); // output one line at a time
4625 ui_breakcheck();
4626 if (got_int)
4627 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004628 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629}
4630
4631/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004632 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004633 * list, etc. Mostly like what JavaScript does, except that empty list and
4634 * empty dictionary are FALSE.
4635 */
4636 int
4637tv2bool(typval_T *tv)
4638{
4639 switch (tv->v_type)
4640 {
4641 case VAR_NUMBER:
4642 return tv->vval.v_number != 0;
4643 case VAR_FLOAT:
4644#ifdef FEAT_FLOAT
4645 return tv->vval.v_float != 0.0;
4646#else
4647 break;
4648#endif
4649 case VAR_PARTIAL:
4650 return tv->vval.v_partial != NULL;
4651 case VAR_FUNC:
4652 case VAR_STRING:
4653 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4654 case VAR_LIST:
4655 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4656 case VAR_DICT:
4657 return tv->vval.v_dict != NULL
4658 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4659 case VAR_BOOL:
4660 case VAR_SPECIAL:
4661 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4662 case VAR_JOB:
4663#ifdef FEAT_JOB_CHANNEL
4664 return tv->vval.v_job != NULL;
4665#else
4666 break;
4667#endif
4668 case VAR_CHANNEL:
4669#ifdef FEAT_JOB_CHANNEL
4670 return tv->vval.v_channel != NULL;
4671#else
4672 break;
4673#endif
4674 case VAR_BLOB:
4675 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4676 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004677 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004678 case VAR_VOID:
4679 break;
4680 }
4681 return FALSE;
4682}
4683
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004684 void
4685emsg_using_string_as(typval_T *tv, int as_number)
4686{
4687 semsg(_(as_number ? e_using_string_as_number_str
4688 : e_using_string_as_bool_str),
4689 tv->vval.v_string == NULL
4690 ? (char_u *)"" : tv->vval.v_string);
4691}
4692
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004693/*
4694 * If "tv" is a string give an error and return FAIL.
4695 */
4696 int
4697check_not_string(typval_T *tv)
4698{
4699 if (tv->v_type == VAR_STRING)
4700 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004701 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004702 clear_tv(tv);
4703 return FAIL;
4704 }
4705 return OK;
4706}
4707
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004708
4709#endif // FEAT_EVAL