blob: d2cc62f9ae267f83c6924f4e18abb4f1f218274b [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 Moolenaar0186e582021-01-10 18:33:11 +0100800 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100801
802 return FAIL;
803}
804
805 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200806call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100807{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200808 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200809 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100810 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100811 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100812
813 if (tv->v_type == VAR_PARTIAL)
814 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200815 partial_T *pt = tv->vval.v_partial;
816 int i;
817
818 if (pt->pt_argc > 0)
819 {
820 // Make space for arguments from the partial, shift the "argcount"
821 // arguments up.
822 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
823 return FAIL;
824 for (i = 1; i <= argcount; ++i)
825 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
826 ectx->ec_stack.ga_len += pt->pt_argc;
827 argcount += pt->pt_argc;
828
829 // copy the arguments from the partial onto the stack
830 for (i = 0; i < pt->pt_argc; ++i)
831 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
832 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833
834 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100835 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100837 name = pt->pt_name;
838 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200839 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200841 if (name != NULL)
842 {
843 char_u fname_buf[FLEN_FIXED + 1];
844 char_u *tofree = NULL;
845 int error = FCERR_NONE;
846 char_u *fname;
847
848 // May need to translate <SNR>123_ to K_SNR.
849 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
850 if (error != FCERR_NONE)
851 res = FAIL;
852 else
853 res = call_by_name(fname, argcount, ectx, NULL);
854 vim_free(tofree);
855 }
856
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100857 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100858 {
859 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200860 semsg(_(e_unknownfunc),
861 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100862 return FAIL;
863 }
864 return OK;
865}
866
867/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200868 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
869 * TRUE.
870 */
871 static int
872error_if_locked(int lock, char *error)
873{
874 if (lock & (VAR_LOCKED | VAR_FIXED))
875 {
876 emsg(_(error));
877 return TRUE;
878 }
879 return FALSE;
880}
881
882/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100883 * Give an error if "tv" is not a number and return FAIL.
884 */
885 static int
886check_for_number(typval_T *tv)
887{
888 if (tv->v_type != VAR_NUMBER)
889 {
890 semsg(_(e_expected_str_but_got_str),
891 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
892 return FAIL;
893 }
894 return OK;
895}
896
897/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100898 * Store "tv" in variable "name".
899 * This is for s: and g: variables.
900 */
901 static void
902store_var(char_u *name, typval_T *tv)
903{
904 funccal_entry_T entry;
905
906 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100907 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100908 restore_funccal();
909}
910
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100911/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100912 * Convert "tv" to a string.
913 * Return FAIL if not allowed.
914 */
915 static int
916do_2string(typval_T *tv, int is_2string_any)
917{
918 if (tv->v_type != VAR_STRING)
919 {
920 char_u *str;
921
922 if (is_2string_any)
923 {
924 switch (tv->v_type)
925 {
926 case VAR_SPECIAL:
927 case VAR_BOOL:
928 case VAR_NUMBER:
929 case VAR_FLOAT:
930 case VAR_BLOB: break;
931 default: to_string_error(tv->v_type);
932 return FAIL;
933 }
934 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100935 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100936 clear_tv(tv);
937 tv->v_type = VAR_STRING;
938 tv->vval.v_string = str;
939 }
940 return OK;
941}
942
943/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100944 * When the value of "sv" is a null list of dict, allocate it.
945 */
946 static void
947allocate_if_null(typval_T *tv)
948{
949 switch (tv->v_type)
950 {
951 case VAR_LIST:
952 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100953 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100954 break;
955 case VAR_DICT:
956 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100957 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100958 break;
959 default:
960 break;
961 }
962}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200963
Bram Moolenaare7525c52021-01-09 13:20:37 +0100964/*
965 * Return the character "str[index]" where "index" is the character index. If
966 * "index" is out of range NULL is returned.
967 */
968 char_u *
969char_from_string(char_u *str, varnumber_T index)
970{
971 size_t nbyte = 0;
972 varnumber_T nchar = index;
973 size_t slen;
974
975 if (str == NULL)
976 return NULL;
977 slen = STRLEN(str);
978
979 // do the same as for a list: a negative index counts from the end
980 if (index < 0)
981 {
982 int clen = 0;
983
984 for (nbyte = 0; nbyte < slen; ++clen)
985 nbyte += MB_CPTR2LEN(str + nbyte);
986 nchar = clen + index;
987 if (nchar < 0)
988 // unlike list: index out of range results in empty string
989 return NULL;
990 }
991
992 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
993 nbyte += MB_CPTR2LEN(str + nbyte);
994 if (nbyte >= slen)
995 return NULL;
996 return vim_strnsave(str + nbyte, MB_CPTR2LEN(str + nbyte));
997}
998
999/*
1000 * Get the byte index for character index "idx" in string "str" with length
1001 * "str_len".
1002 * If going over the end return "str_len".
1003 * If "idx" is negative count from the end, -1 is the last character.
1004 * When going over the start return -1.
1005 */
1006 static long
1007char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1008{
1009 varnumber_T nchar = idx;
1010 size_t nbyte = 0;
1011
1012 if (nchar >= 0)
1013 {
1014 while (nchar > 0 && nbyte < str_len)
1015 {
1016 nbyte += MB_CPTR2LEN(str + nbyte);
1017 --nchar;
1018 }
1019 }
1020 else
1021 {
1022 nbyte = str_len;
1023 while (nchar < 0 && nbyte > 0)
1024 {
1025 --nbyte;
1026 nbyte -= mb_head_off(str, str + nbyte);
1027 ++nchar;
1028 }
1029 if (nchar < 0)
1030 return -1;
1031 }
1032 return (long)nbyte;
1033}
1034
1035/*
1036 * Return the slice "str[first:last]" using character indexes.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001037 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001038 * Return NULL when the result is empty.
1039 */
1040 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001041string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001042{
1043 long start_byte, end_byte;
1044 size_t slen;
1045
1046 if (str == NULL)
1047 return NULL;
1048 slen = STRLEN(str);
1049 start_byte = char_idx2byte(str, slen, first);
1050 if (start_byte < 0)
1051 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001052 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001053 end_byte = (long)slen;
1054 else
1055 {
1056 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001057 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001058 // end index is inclusive
1059 end_byte += MB_CPTR2LEN(str + end_byte);
1060 }
1061
1062 if (start_byte >= (long)slen || end_byte <= start_byte)
1063 return NULL;
1064 return vim_strnsave(str + start_byte, end_byte - start_byte);
1065}
1066
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001067 static svar_T *
1068get_script_svar(scriptref_T *sref, ectx_T *ectx)
1069{
1070 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1071 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1072 + ectx->ec_dfunc_idx;
1073 svar_T *sv;
1074
1075 if (sref->sref_seq != si->sn_script_seq)
1076 {
1077 // The script was reloaded after the function was
1078 // compiled, the script_idx may not be valid.
1079 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1080 dfunc->df_ufunc->uf_name_exp);
1081 return NULL;
1082 }
1083 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1084 if (!equal_type(sv->sv_type, sref->sref_type))
1085 {
1086 emsg(_(e_script_variable_type_changed));
1087 return NULL;
1088 }
1089 return sv;
1090}
1091
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001092/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001093 * Execute a function by "name".
1094 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001095 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001096 */
1097 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001098call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001099{
Bram Moolenaared677f52020-08-12 16:38:10 +02001100 int called_emsg_before = called_emsg;
1101 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001102
Bram Moolenaared677f52020-08-12 16:38:10 +02001103 res = call_by_name(name, argcount, ectx, iptr);
1104 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001105 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001106 dictitem_T *v;
1107
1108 v = find_var(name, NULL, FALSE);
1109 if (v == NULL)
1110 {
1111 semsg(_(e_unknownfunc), name);
1112 return FAIL;
1113 }
1114 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1115 {
1116 semsg(_(e_unknownfunc), name);
1117 return FAIL;
1118 }
1119 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001120 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001121 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001122}
1123
1124/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001125 * When a function reference is used, fill a partial with the information
1126 * needed, especially when it is used as a closure.
1127 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001128 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001129fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1130{
1131 pt->pt_func = ufunc;
1132 pt->pt_refcount = 1;
1133
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001134 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001135 {
1136 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1137 + ectx->ec_dfunc_idx;
1138
1139 // The closure needs to find arguments and local
1140 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001141 pt->pt_outer.out_stack = &ectx->ec_stack;
1142 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1143 pt->pt_outer.out_up = ectx->ec_outer;
1144 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001145
1146 // If this function returns and the closure is still
1147 // being used, we need to make a copy of the context
1148 // (arguments and local variables). Store a reference
1149 // to the partial so we can handle that.
1150 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1151 {
1152 vim_free(pt);
1153 return FAIL;
1154 }
1155 // Extra variable keeps the count of closures created
1156 // in the current function call.
1157 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1158 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1159
1160 ((partial_T **)ectx->ec_funcrefs.ga_data)
1161 [ectx->ec_funcrefs.ga_len] = pt;
1162 ++pt->pt_refcount;
1163 ++ectx->ec_funcrefs.ga_len;
1164 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001165 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001166 return OK;
1167}
1168
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001169
Bram Moolenaarf112f302020-12-20 17:47:52 +01001170/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001171 * Call a "def" function from old Vim script.
1172 * Return OK or FAIL.
1173 */
1174 int
1175call_def_function(
1176 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001177 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001178 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001179 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001180 typval_T *rettv) // return value
1181{
1182 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001183 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001184 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001185 typval_T *tv;
1186 int idx;
1187 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001188 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001189 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001190 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001191 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001192 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001193 msglist_T **saved_msg_list = NULL;
1194 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001195 cmdmod_T save_cmdmod;
1196 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001197 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001198 int save_emsg_silent_def = emsg_silent_def;
1199 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001200 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001201 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001202 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203
1204// Get pointer to item in the stack.
1205#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1206
1207// Get pointer to item at the bottom of the stack, -1 is the bottom.
1208#undef STACK_TV_BOT
1209#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1210
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001211// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001212#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 +01001213
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001214 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001215 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1216 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001217 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001218 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001219 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001220 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001221 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001222 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001223 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001224
Bram Moolenaar09689a02020-05-09 22:50:08 +02001225 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001226 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001227 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1228 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001229 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001230 {
1231 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001232 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001233 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001234 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001235
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001236 // If depth of calling is getting too high, don't execute the function.
1237 orig_funcdepth = funcdepth_get();
1238 if (funcdepth_increment() == FAIL)
1239 return FAIL;
1240
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001241 CLEAR_FIELD(ectx);
1242 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1243 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1244 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001245 {
1246 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001247 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001248 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001249 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001250 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001251
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001252 // Put arguments on the stack, but no more than what the function expects.
1253 // A lambda can be called with more arguments than it uses.
1254 for (idx = 0; idx < argc
1255 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1256 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001257 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001258 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001259 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001260 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001261 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001262 copy_tv(&argv[idx], STACK_TV_BOT(0));
1263 ++ectx.ec_stack.ga_len;
1264 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001265
1266 // Turn varargs into a list. Empty list if no args.
1267 if (ufunc->uf_va_name != NULL)
1268 {
1269 int vararg_count = argc - ufunc->uf_args.ga_len;
1270
1271 if (vararg_count < 0)
1272 vararg_count = 0;
1273 else
1274 argc -= vararg_count;
1275 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001276 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001277
1278 // Check the type of the list items.
1279 tv = STACK_TV_BOT(-1);
1280 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001281 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001282 && ufunc->uf_va_type->tt_member != &t_any
1283 && tv->vval.v_list != NULL)
1284 {
1285 type_T *expected = ufunc->uf_va_type->tt_member;
1286 listitem_T *li = tv->vval.v_list->lv_first;
1287
1288 for (idx = 0; idx < vararg_count; ++idx)
1289 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001290 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001291 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001292 goto failed_early;
1293 li = li->li_next;
1294 }
1295 }
1296
Bram Moolenaar23e03252020-04-12 22:22:31 +02001297 if (defcount > 0)
1298 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001299 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001300 --ectx.ec_stack.ga_len;
1301 }
1302
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001303 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001304 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001305 if (defcount > 0)
1306 for (idx = 0; idx < defcount; ++idx)
1307 {
1308 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1309 ++ectx.ec_stack.ga_len;
1310 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001311 if (ufunc->uf_va_name != NULL)
1312 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001313
1314 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001315 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1316 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001317
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001318 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001319 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1320 + ufunc->uf_dfunc_idx;
1321 ufunc_T *base_ufunc = dfunc->df_ufunc;
1322
1323 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1324 // by copy_func().
1325 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001326 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001327 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1328 if (ectx.ec_outer == NULL)
1329 goto failed_early;
1330 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001331 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001332 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1333 {
1334 if (current_ectx->ec_outer != NULL)
1335 *ectx.ec_outer = *current_ectx->ec_outer;
1336 }
1337 else
1338 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001339 }
1340 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001341 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1342 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001343 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001344 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001345
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001346 // dummy frame entries
1347 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1348 {
1349 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1350 ++ectx.ec_stack.ga_len;
1351 }
1352
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001353 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001354 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001355 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1356 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001357
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001358 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001359 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001360 ectx.ec_stack.ga_len += dfunc->df_varcount;
1361 if (dfunc->df_has_closure)
1362 {
1363 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1364 STACK_TV_VAR(idx)->vval.v_number = 0;
1365 ++ectx.ec_stack.ga_len;
1366 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001367
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001368 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001369 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001370
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001371 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001372 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001373 estack_push_ufunc(ufunc, 1);
1374 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001375 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1376
Bram Moolenaar352134b2020-10-17 22:04:08 +02001377 // Use a specific location for storing error messages to be converted to an
1378 // exception.
1379 saved_msg_list = msg_list;
1380 msg_list = &private_msg_list;
1381
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001382 // Do turn errors into exceptions.
1383 suppress_errthrow = FALSE;
1384
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001385 // When ":silent!" was used before calling then we still abort the
1386 // function. If ":silent!" is used in the function then we don't.
1387 emsg_silent_def = emsg_silent;
1388 did_emsg_def = 0;
1389
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001390 where.wt_index = 0;
1391 where.wt_variable = FALSE;
1392
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001393 // Decide where to start execution, handles optional arguments.
1394 init_instr_idx(ufunc, argc, &ectx);
1395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001396 for (;;)
1397 {
1398 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001399
Bram Moolenaar270d0382020-05-15 21:42:53 +02001400 if (++breakcheck_count >= 100)
1401 {
1402 line_breakcheck();
1403 breakcheck_count = 0;
1404 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001405 if (got_int)
1406 {
1407 // Turn CTRL-C into an exception.
1408 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001409 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001410 goto failed;
1411 did_throw = TRUE;
1412 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001413
Bram Moolenaara26b9702020-04-18 19:53:28 +02001414 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1415 {
1416 // Turn an error message into an exception.
1417 did_emsg = FALSE;
1418 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1419 goto failed;
1420 did_throw = TRUE;
1421 *msg_list = NULL;
1422 }
1423
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001424 if (did_throw && !ectx.ec_in_catch)
1425 {
1426 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001427 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001428
1429 // An exception jumps to the first catch, finally, or returns from
1430 // the current function.
1431 if (trystack->ga_len > 0)
1432 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001433 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001434 {
1435 // jump to ":catch" or ":finally"
1436 ectx.ec_in_catch = TRUE;
1437 ectx.ec_iidx = trycmd->tcd_catch_idx;
1438 }
1439 else
1440 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001441 // Not inside try or need to return from current functions.
1442 // Push a dummy return value.
1443 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1444 goto failed;
1445 tv = STACK_TV_BOT(0);
1446 tv->v_type = VAR_NUMBER;
1447 tv->vval.v_number = 0;
1448 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001449 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001450 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001451 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001452 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001453 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1454 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001455 goto done;
1456 }
1457
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001458 if (func_return(&ectx) == FAIL)
1459 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001460 }
1461 continue;
1462 }
1463
1464 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1465 switch (iptr->isn_type)
1466 {
1467 // execute Ex command line
1468 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001469 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001470 source_cookie_T cookie;
1471
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001472 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001473 // Pass getsourceline to get an error for a missing ":end"
1474 // command.
1475 CLEAR_FIELD(cookie);
1476 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1477 if (do_cmdline(iptr->isn_arg.string,
1478 getsourceline, &cookie,
1479 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1480 == FAIL
1481 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001482 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001483 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001484 break;
1485
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001486 // execute Ex command from pieces on the stack
1487 case ISN_EXECCONCAT:
1488 {
1489 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001490 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001491 int pass;
1492 int i;
1493 char_u *cmd = NULL;
1494 char_u *str;
1495
1496 for (pass = 1; pass <= 2; ++pass)
1497 {
1498 for (i = 0; i < count; ++i)
1499 {
1500 tv = STACK_TV_BOT(i - count);
1501 str = tv->vval.v_string;
1502 if (str != NULL && *str != NUL)
1503 {
1504 if (pass == 2)
1505 STRCPY(cmd + len, str);
1506 len += STRLEN(str);
1507 }
1508 if (pass == 2)
1509 clear_tv(tv);
1510 }
1511 if (pass == 1)
1512 {
1513 cmd = alloc(len + 1);
1514 if (cmd == NULL)
1515 goto failed;
1516 len = 0;
1517 }
1518 }
1519
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001520 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001521 do_cmdline_cmd(cmd);
1522 vim_free(cmd);
1523 }
1524 break;
1525
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001526 // execute :echo {string} ...
1527 case ISN_ECHO:
1528 {
1529 int count = iptr->isn_arg.echo.echo_count;
1530 int atstart = TRUE;
1531 int needclr = TRUE;
1532
1533 for (idx = 0; idx < count; ++idx)
1534 {
1535 tv = STACK_TV_BOT(idx - count);
1536 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1537 &atstart, &needclr);
1538 clear_tv(tv);
1539 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001540 if (needclr)
1541 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001542 ectx.ec_stack.ga_len -= count;
1543 }
1544 break;
1545
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001546 // :execute {string} ...
1547 // :echomsg {string} ...
1548 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001549 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001550 case ISN_ECHOMSG:
1551 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001552 {
1553 int count = iptr->isn_arg.number;
1554 garray_T ga;
1555 char_u buf[NUMBUFLEN];
1556 char_u *p;
1557 int len;
1558 int failed = FALSE;
1559
1560 ga_init2(&ga, 1, 80);
1561 for (idx = 0; idx < count; ++idx)
1562 {
1563 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001564 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001565 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001566 if (tv->v_type == VAR_CHANNEL
1567 || tv->v_type == VAR_JOB)
1568 {
1569 SOURCING_LNUM = iptr->isn_lnum;
1570 emsg(_(e_inval_string));
1571 break;
1572 }
1573 else
1574 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001575 }
1576 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001577 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001578
1579 len = (int)STRLEN(p);
1580 if (ga_grow(&ga, len + 2) == FAIL)
1581 failed = TRUE;
1582 else
1583 {
1584 if (ga.ga_len > 0)
1585 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1586 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1587 ga.ga_len += len;
1588 }
1589 clear_tv(tv);
1590 }
1591 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001592 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001593 {
1594 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001595 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001596 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001597
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001598 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001599 {
1600 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001601 {
1602 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001603 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001604 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001605 {
1606 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001607 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001608 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001609 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001610 else
1611 {
1612 msg_sb_eol();
1613 if (iptr->isn_type == ISN_ECHOMSG)
1614 {
1615 msg_attr(ga.ga_data, echo_attr);
1616 out_flush();
1617 }
1618 else
1619 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001620 SOURCING_LNUM = iptr->isn_lnum;
1621 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001622 }
1623 }
1624 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001625 ga_clear(&ga);
1626 }
1627 break;
1628
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001629 // load local variable or argument
1630 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001631 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001632 goto failed;
1633 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1634 ++ectx.ec_stack.ga_len;
1635 break;
1636
1637 // load v: variable
1638 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001639 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001640 goto failed;
1641 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1642 ++ectx.ec_stack.ga_len;
1643 break;
1644
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001645 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001646 case ISN_LOADSCRIPT:
1647 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001648 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001649 svar_T *sv;
1650
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001651 sv = get_script_svar(sref, &ectx);
1652 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001653 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001654 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001655 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001656 goto failed;
1657 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1658 ++ectx.ec_stack.ga_len;
1659 }
1660 break;
1661
1662 // load s: variable in old script
1663 case ISN_LOADS:
1664 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001665 hashtab_T *ht = &SCRIPT_VARS(
1666 iptr->isn_arg.loadstore.ls_sid);
1667 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001668 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001670 if (di == NULL)
1671 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001672 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001673 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001674 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001675 }
1676 else
1677 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001678 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 goto failed;
1680 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1681 ++ectx.ec_stack.ga_len;
1682 }
1683 }
1684 break;
1685
Bram Moolenaard3aac292020-04-19 14:32:17 +02001686 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001687 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001688 case ISN_LOADB:
1689 case ISN_LOADW:
1690 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001691 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001692 dictitem_T *di = NULL;
1693 hashtab_T *ht = NULL;
1694 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001695
Bram Moolenaard3aac292020-04-19 14:32:17 +02001696 switch (iptr->isn_type)
1697 {
1698 case ISN_LOADG:
1699 ht = get_globvar_ht();
1700 namespace = 'g';
1701 break;
1702 case ISN_LOADB:
1703 ht = &curbuf->b_vars->dv_hashtab;
1704 namespace = 'b';
1705 break;
1706 case ISN_LOADW:
1707 ht = &curwin->w_vars->dv_hashtab;
1708 namespace = 'w';
1709 break;
1710 case ISN_LOADT:
1711 ht = &curtab->tp_vars->dv_hashtab;
1712 namespace = 't';
1713 break;
1714 default: // Cannot reach here
1715 goto failed;
1716 }
1717 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001718
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001719 if (di == NULL)
1720 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001721 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001722 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001723 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001724 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001725 }
1726 else
1727 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001728 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001729 goto failed;
1730 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1731 ++ectx.ec_stack.ga_len;
1732 }
1733 }
1734 break;
1735
Bram Moolenaar03290b82020-12-19 16:30:44 +01001736 // load autoload variable
1737 case ISN_LOADAUTO:
1738 {
1739 char_u *name = iptr->isn_arg.string;
1740
1741 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1742 goto failed;
1743 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001744 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaar03290b82020-12-19 16:30:44 +01001745 STACK_TV_BOT(0), NULL, TRUE, FALSE) == FAIL)
1746 goto on_error;
1747 ++ectx.ec_stack.ga_len;
1748 }
1749 break;
1750
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001751 // load g:/b:/w:/t: namespace
1752 case ISN_LOADGDICT:
1753 case ISN_LOADBDICT:
1754 case ISN_LOADWDICT:
1755 case ISN_LOADTDICT:
1756 {
1757 dict_T *d = NULL;
1758
1759 switch (iptr->isn_type)
1760 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001761 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1762 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1763 case ISN_LOADWDICT: d = curwin->w_vars; break;
1764 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001765 default: // Cannot reach here
1766 goto failed;
1767 }
1768 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1769 goto failed;
1770 tv = STACK_TV_BOT(0);
1771 tv->v_type = VAR_DICT;
1772 tv->v_lock = 0;
1773 tv->vval.v_dict = d;
1774 ++ectx.ec_stack.ga_len;
1775 }
1776 break;
1777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001778 // load &option
1779 case ISN_LOADOPT:
1780 {
1781 typval_T optval;
1782 char_u *name = iptr->isn_arg.string;
1783
Bram Moolenaara8c17702020-04-01 21:17:24 +02001784 // This is not expected to fail, name is checked during
1785 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001786 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001787 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001788 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001789 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001790 *STACK_TV_BOT(0) = optval;
1791 ++ectx.ec_stack.ga_len;
1792 }
1793 break;
1794
1795 // load $ENV
1796 case ISN_LOADENV:
1797 {
1798 typval_T optval;
1799 char_u *name = iptr->isn_arg.string;
1800
Bram Moolenaar270d0382020-05-15 21:42:53 +02001801 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001802 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001803 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001804 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001805 *STACK_TV_BOT(0) = optval;
1806 ++ectx.ec_stack.ga_len;
1807 }
1808 break;
1809
1810 // load @register
1811 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001812 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001813 goto failed;
1814 tv = STACK_TV_BOT(0);
1815 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001816 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001817 // This may result in NULL, which should be equivalent to an
1818 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001819 tv->vval.v_string = get_reg_contents(
1820 iptr->isn_arg.number, GREG_EXPR_SRC);
1821 ++ectx.ec_stack.ga_len;
1822 break;
1823
1824 // store local variable
1825 case ISN_STORE:
1826 --ectx.ec_stack.ga_len;
1827 tv = STACK_TV_VAR(iptr->isn_arg.number);
1828 clear_tv(tv);
1829 *tv = *STACK_TV_BOT(0);
1830 break;
1831
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001832 // store s: variable in old script
1833 case ISN_STORES:
1834 {
1835 hashtab_T *ht = &SCRIPT_VARS(
1836 iptr->isn_arg.loadstore.ls_sid);
1837 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001838 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001839
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001840 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001841 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001842 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001843 else
1844 {
1845 clear_tv(&di->di_tv);
1846 di->di_tv = *STACK_TV_BOT(0);
1847 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001848 }
1849 break;
1850
1851 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001852 case ISN_STORESCRIPT:
1853 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001854 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1855 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001856
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001857 sv = get_script_svar(sref, &ectx);
1858 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001859 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001860 --ectx.ec_stack.ga_len;
1861 clear_tv(sv->sv_tv);
1862 *sv->sv_tv = *STACK_TV_BOT(0);
1863 }
1864 break;
1865
1866 // store option
1867 case ISN_STOREOPT:
1868 {
1869 long n = 0;
1870 char_u *s = NULL;
1871 char *msg;
1872
1873 --ectx.ec_stack.ga_len;
1874 tv = STACK_TV_BOT(0);
1875 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001876 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001877 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001878 if (s == NULL)
1879 s = (char_u *)"";
1880 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001881 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001882 // must be VAR_NUMBER, CHECKTYPE makes sure
1883 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001884 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1885 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001886 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001887 if (msg != NULL)
1888 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001889 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001891 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001892 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001893 }
1894 break;
1895
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001896 // store $ENV
1897 case ISN_STOREENV:
1898 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001899 tv = STACK_TV_BOT(0);
1900 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1901 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001902 break;
1903
1904 // store @r
1905 case ISN_STOREREG:
1906 {
1907 int reg = iptr->isn_arg.number;
1908
1909 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001910 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001911 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001912 tv_get_string(tv), -1, FALSE);
1913 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001914 }
1915 break;
1916
1917 // store v: variable
1918 case ISN_STOREV:
1919 --ectx.ec_stack.ga_len;
1920 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1921 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001922 // should not happen, type is checked when compiling
1923 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001924 break;
1925
Bram Moolenaard3aac292020-04-19 14:32:17 +02001926 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001928 case ISN_STOREB:
1929 case ISN_STOREW:
1930 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001931 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001932 dictitem_T *di;
1933 hashtab_T *ht;
1934 char_u *name = iptr->isn_arg.string + 2;
1935
Bram Moolenaard3aac292020-04-19 14:32:17 +02001936 switch (iptr->isn_type)
1937 {
1938 case ISN_STOREG:
1939 ht = get_globvar_ht();
1940 break;
1941 case ISN_STOREB:
1942 ht = &curbuf->b_vars->dv_hashtab;
1943 break;
1944 case ISN_STOREW:
1945 ht = &curwin->w_vars->dv_hashtab;
1946 break;
1947 case ISN_STORET:
1948 ht = &curtab->tp_vars->dv_hashtab;
1949 break;
1950 default: // Cannot reach here
1951 goto failed;
1952 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953
1954 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001955 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001956 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001957 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001958 else
1959 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001960 SOURCING_LNUM = iptr->isn_lnum;
1961 if (var_check_permission(di, name) == FAIL)
1962 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001963 clear_tv(&di->di_tv);
1964 di->di_tv = *STACK_TV_BOT(0);
1965 }
1966 }
1967 break;
1968
Bram Moolenaar03290b82020-12-19 16:30:44 +01001969 // store an autoload variable
1970 case ISN_STOREAUTO:
1971 SOURCING_LNUM = iptr->isn_lnum;
1972 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1973 clear_tv(STACK_TV_BOT(-1));
1974 --ectx.ec_stack.ga_len;
1975 break;
1976
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001977 // store number in local variable
1978 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01001979 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001980 clear_tv(tv);
1981 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01001982 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001983 break;
1984
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001985 // store value in list or dict variable
1986 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001987 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001988 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001989 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001990 typval_T *tv_dest = STACK_TV_BOT(-1);
1991 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001992
Bram Moolenaar752fc692021-01-04 21:57:11 +01001993 // Stack contains:
1994 // -3 value to be stored
1995 // -2 index
1996 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02001997 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01001998 SOURCING_LNUM = iptr->isn_lnum;
1999 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002000 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002001 dest_type = tv_dest->v_type;
2002 if (dest_type == VAR_DICT)
2003 status = do_2string(tv_idx, TRUE);
2004 else if (dest_type == VAR_LIST
2005 && tv_idx->v_type != VAR_NUMBER)
2006 {
2007 emsg(_(e_number_exp));
2008 status = FAIL;
2009 }
2010 }
2011 else if (dest_type != tv_dest->v_type)
2012 {
2013 // just in case, should be OK
2014 semsg(_(e_expected_str_but_got_str),
2015 vartype_name(dest_type),
2016 vartype_name(tv_dest->v_type));
2017 status = FAIL;
2018 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002019
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002020 if (status == OK && dest_type == VAR_LIST)
2021 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002022 long lidx = (long)tv_idx->vval.v_number;
2023 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002024
2025 if (list == NULL)
2026 {
2027 emsg(_(e_list_not_set));
2028 goto on_error;
2029 }
2030 if (lidx < 0 && list->lv_len + lidx >= 0)
2031 // negative index is relative to the end
2032 lidx = list->lv_len + lidx;
2033 if (lidx < 0 || lidx > list->lv_len)
2034 {
2035 semsg(_(e_listidx), lidx);
2036 goto on_error;
2037 }
2038 if (lidx < list->lv_len)
2039 {
2040 listitem_T *li = list_find(list, lidx);
2041
2042 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002043 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002044 goto on_error;
2045 // overwrite existing list item
2046 clear_tv(&li->li_tv);
2047 li->li_tv = *tv;
2048 }
2049 else
2050 {
2051 if (error_if_locked(list->lv_lock,
2052 e_cannot_change_list))
2053 goto on_error;
2054 // append to list, only fails when out of memory
2055 if (list_append_tv(list, tv) == FAIL)
2056 goto failed;
2057 clear_tv(tv);
2058 }
2059 }
2060 else if (status == OK && dest_type == VAR_DICT)
2061 {
2062 char_u *key = tv_idx->vval.v_string;
2063 dict_T *dict = tv_dest->vval.v_dict;
2064 dictitem_T *di;
2065
2066 SOURCING_LNUM = iptr->isn_lnum;
2067 if (dict == NULL)
2068 {
2069 emsg(_(e_dictionary_not_set));
2070 goto on_error;
2071 }
2072 if (key == NULL)
2073 key = (char_u *)"";
2074 di = dict_find(dict, key, -1);
2075 if (di != NULL)
2076 {
2077 if (error_if_locked(di->di_tv.v_lock,
2078 e_cannot_change_dict_item))
2079 goto on_error;
2080 // overwrite existing value
2081 clear_tv(&di->di_tv);
2082 di->di_tv = *tv;
2083 }
2084 else
2085 {
2086 if (error_if_locked(dict->dv_lock,
2087 e_cannot_change_dict))
2088 goto on_error;
2089 // add to dict, only fails when out of memory
2090 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2091 goto failed;
2092 clear_tv(tv);
2093 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002094 }
2095 else
2096 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002097 status = FAIL;
2098 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002099 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002100
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002101 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002102 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002103 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002104 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002105 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002106 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002107 goto on_error;
2108 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002109 }
2110 break;
2111
Bram Moolenaar0186e582021-01-10 18:33:11 +01002112 // load or store variable or argument from outer scope
2113 case ISN_LOADOUTER:
2114 case ISN_STOREOUTER:
2115 {
2116 int depth = iptr->isn_arg.outer.outer_depth;
2117 outer_T *outer = ectx.ec_outer;
2118
2119 while (depth > 1 && outer != NULL)
2120 {
2121 outer = outer->out_up;
2122 --depth;
2123 }
2124 if (outer == NULL)
2125 {
2126 SOURCING_LNUM = iptr->isn_lnum;
2127 iemsg("LOADOUTER depth more than scope levels");
2128 goto failed;
2129 }
2130 tv = ((typval_T *)outer->out_stack->ga_data)
2131 + outer->out_frame_idx + STACK_FRAME_SIZE
2132 + iptr->isn_arg.outer.outer_idx;
2133 if (iptr->isn_type == ISN_LOADOUTER)
2134 {
2135 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2136 goto failed;
2137 copy_tv(tv, STACK_TV_BOT(0));
2138 ++ectx.ec_stack.ga_len;
2139 }
2140 else
2141 {
2142 --ectx.ec_stack.ga_len;
2143 clear_tv(tv);
2144 *tv = *STACK_TV_BOT(0);
2145 }
2146 }
2147 break;
2148
Bram Moolenaar752fc692021-01-04 21:57:11 +01002149 // unlet item in list or dict variable
2150 case ISN_UNLETINDEX:
2151 {
2152 typval_T *tv_idx = STACK_TV_BOT(-2);
2153 typval_T *tv_dest = STACK_TV_BOT(-1);
2154 int status = OK;
2155
2156 // Stack contains:
2157 // -2 index
2158 // -1 dict or list
2159 if (tv_dest->v_type == VAR_DICT)
2160 {
2161 // unlet a dict item, index must be a string
2162 if (tv_idx->v_type != VAR_STRING)
2163 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002164 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002165 semsg(_(e_expected_str_but_got_str),
2166 vartype_name(VAR_STRING),
2167 vartype_name(tv_idx->v_type));
2168 status = FAIL;
2169 }
2170 else
2171 {
2172 dict_T *d = tv_dest->vval.v_dict;
2173 char_u *key = tv_idx->vval.v_string;
2174 dictitem_T *di = NULL;
2175
2176 if (key == NULL)
2177 key = (char_u *)"";
2178 if (d != NULL)
2179 di = dict_find(d, key, (int)STRLEN(key));
2180 if (di == NULL)
2181 {
2182 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002183 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002184 semsg(_(e_dictkey), key);
2185 status = FAIL;
2186 }
2187 else
2188 {
2189 // TODO: check for dict or item locked
2190 dictitem_remove(d, di);
2191 }
2192 }
2193 }
2194 else if (tv_dest->v_type == VAR_LIST)
2195 {
2196 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002197 SOURCING_LNUM = iptr->isn_lnum;
2198 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002199 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002200 status = FAIL;
2201 }
2202 else
2203 {
2204 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002205 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002206 listitem_T *li = NULL;
2207
2208 li = list_find(l, n);
2209 if (li == NULL)
2210 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002211 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002212 semsg(_(e_listidx), n);
2213 status = FAIL;
2214 }
2215 else
2216 // TODO: check for list or item locked
2217 listitem_remove(l, li);
2218 }
2219 }
2220 else
2221 {
2222 status = FAIL;
2223 semsg(_(e_cannot_index_str),
2224 vartype_name(tv_dest->v_type));
2225 }
2226
2227 clear_tv(tv_idx);
2228 clear_tv(tv_dest);
2229 ectx.ec_stack.ga_len -= 2;
2230 if (status == FAIL)
2231 goto on_error;
2232 }
2233 break;
2234
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002235 // unlet range of items in list variable
2236 case ISN_UNLETRANGE:
2237 {
2238 // Stack contains:
2239 // -3 index1
2240 // -2 index2
2241 // -1 dict or list
2242 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2243 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2244 typval_T *tv_dest = STACK_TV_BOT(-1);
2245 int status = OK;
2246
2247 if (tv_dest->v_type == VAR_LIST)
2248 {
2249 // indexes must be a number
2250 SOURCING_LNUM = iptr->isn_lnum;
2251 if (check_for_number(tv_idx1) == FAIL
2252 || check_for_number(tv_idx2) == FAIL)
2253 {
2254 status = FAIL;
2255 }
2256 else
2257 {
2258 list_T *l = tv_dest->vval.v_list;
2259 long n1 = (long)tv_idx1->vval.v_number;
2260 long n2 = (long)tv_idx2->vval.v_number;
2261 listitem_T *li;
2262
2263 li = list_find_index(l, &n1);
2264 if (li == NULL
2265 || list_unlet_range(l, li, NULL, n1,
2266 TRUE, n2) == FAIL)
2267 status = FAIL;
2268 }
2269 }
2270 else
2271 {
2272 status = FAIL;
2273 SOURCING_LNUM = iptr->isn_lnum;
2274 semsg(_(e_cannot_index_str),
2275 vartype_name(tv_dest->v_type));
2276 }
2277
2278 clear_tv(tv_idx1);
2279 clear_tv(tv_idx2);
2280 clear_tv(tv_dest);
2281 ectx.ec_stack.ga_len -= 3;
2282 if (status == FAIL)
2283 goto on_error;
2284 }
2285 break;
2286
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002287 // push constant
2288 case ISN_PUSHNR:
2289 case ISN_PUSHBOOL:
2290 case ISN_PUSHSPEC:
2291 case ISN_PUSHF:
2292 case ISN_PUSHS:
2293 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002294 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002295 case ISN_PUSHCHANNEL:
2296 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002297 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002298 goto failed;
2299 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002300 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002301 ++ectx.ec_stack.ga_len;
2302 switch (iptr->isn_type)
2303 {
2304 case ISN_PUSHNR:
2305 tv->v_type = VAR_NUMBER;
2306 tv->vval.v_number = iptr->isn_arg.number;
2307 break;
2308 case ISN_PUSHBOOL:
2309 tv->v_type = VAR_BOOL;
2310 tv->vval.v_number = iptr->isn_arg.number;
2311 break;
2312 case ISN_PUSHSPEC:
2313 tv->v_type = VAR_SPECIAL;
2314 tv->vval.v_number = iptr->isn_arg.number;
2315 break;
2316#ifdef FEAT_FLOAT
2317 case ISN_PUSHF:
2318 tv->v_type = VAR_FLOAT;
2319 tv->vval.v_float = iptr->isn_arg.fnumber;
2320 break;
2321#endif
2322 case ISN_PUSHBLOB:
2323 blob_copy(iptr->isn_arg.blob, tv);
2324 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002325 case ISN_PUSHFUNC:
2326 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002327 if (iptr->isn_arg.string == NULL)
2328 tv->vval.v_string = NULL;
2329 else
2330 tv->vval.v_string =
2331 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002332 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002333 case ISN_PUSHCHANNEL:
2334#ifdef FEAT_JOB_CHANNEL
2335 tv->v_type = VAR_CHANNEL;
2336 tv->vval.v_channel = iptr->isn_arg.channel;
2337 if (tv->vval.v_channel != NULL)
2338 ++tv->vval.v_channel->ch_refcount;
2339#endif
2340 break;
2341 case ISN_PUSHJOB:
2342#ifdef FEAT_JOB_CHANNEL
2343 tv->v_type = VAR_JOB;
2344 tv->vval.v_job = iptr->isn_arg.job;
2345 if (tv->vval.v_job != NULL)
2346 ++tv->vval.v_job->jv_refcount;
2347#endif
2348 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002349 default:
2350 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002351 tv->vval.v_string = vim_strsave(
2352 iptr->isn_arg.string == NULL
2353 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002354 }
2355 break;
2356
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002357 case ISN_UNLET:
2358 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2359 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002360 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002361 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002362 case ISN_UNLETENV:
2363 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2364 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002365
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002366 case ISN_LOCKCONST:
2367 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2368 break;
2369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002370 // create a list from items on the stack; uses a single allocation
2371 // for the list header and the items
2372 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002373 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2374 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 break;
2376
2377 // create a dict from items on the stack
2378 case ISN_NEWDICT:
2379 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002380 int count = iptr->isn_arg.number;
2381 dict_T *dict = dict_alloc();
2382 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002383 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002384
2385 if (dict == NULL)
2386 goto failed;
2387 for (idx = 0; idx < count; ++idx)
2388 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002389 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002390 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002391 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002392 key = tv->vval.v_string == NULL
2393 ? (char_u *)"" : tv->vval.v_string;
2394 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002395 if (item != NULL)
2396 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002397 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002398 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002399 dict_unref(dict);
2400 goto on_error;
2401 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002402 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002403 clear_tv(tv);
2404 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002405 {
2406 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002407 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002408 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2410 item->di_tv.v_lock = 0;
2411 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002412 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002413 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002414 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002415 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002416 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002417 }
2418
2419 if (count > 0)
2420 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002421 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002422 goto failed;
2423 else
2424 ++ectx.ec_stack.ga_len;
2425 tv = STACK_TV_BOT(-1);
2426 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002427 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002428 tv->vval.v_dict = dict;
2429 ++dict->dv_refcount;
2430 }
2431 break;
2432
2433 // call a :def function
2434 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002435 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002436 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 iptr->isn_arg.dfunc.cdf_argcount,
2438 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002439 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002440 break;
2441
2442 // call a builtin function
2443 case ISN_BCALL:
2444 SOURCING_LNUM = iptr->isn_lnum;
2445 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2446 iptr->isn_arg.bfunc.cbf_argcount,
2447 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002448 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 break;
2450
2451 // call a funcref or partial
2452 case ISN_PCALL:
2453 {
2454 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2455 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002456 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002457
2458 SOURCING_LNUM = iptr->isn_lnum;
2459 if (pfunc->cpf_top)
2460 {
2461 // funcref is above the arguments
2462 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2463 }
2464 else
2465 {
2466 // Get the funcref from the stack.
2467 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002468 partial_tv = *STACK_TV_BOT(0);
2469 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002470 }
2471 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002472 if (tv == &partial_tv)
2473 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002476 }
2477 break;
2478
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002479 case ISN_PCALL_END:
2480 // PCALL finished, arguments have been consumed and replaced by
2481 // the return value. Now clear the funcref from the stack,
2482 // and move the return value in its place.
2483 --ectx.ec_stack.ga_len;
2484 clear_tv(STACK_TV_BOT(-1));
2485 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2486 break;
2487
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002488 // call a user defined function or funcref/partial
2489 case ISN_UCALL:
2490 {
2491 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2492
2493 SOURCING_LNUM = iptr->isn_lnum;
2494 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002495 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002496 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002497 }
2498 break;
2499
2500 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002501 case ISN_RETURN_ZERO:
2502 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2503 goto failed;
2504 tv = STACK_TV_BOT(0);
2505 ++ectx.ec_stack.ga_len;
2506 tv->v_type = VAR_NUMBER;
2507 tv->vval.v_number = 0;
2508 tv->v_lock = 0;
2509 // FALLTHROUGH
2510
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002511 case ISN_RETURN:
2512 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002513 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002514 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002515
2516 if (trystack->ga_len > 0)
2517 trycmd = ((trycmd_T *)trystack->ga_data)
2518 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002519 if (trycmd != NULL
2520 && trycmd->tcd_frame_idx == ectx.ec_frame_idx
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002521 && trycmd->tcd_finally_idx != 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 {
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002523 // jump to ":finally" once
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002524 ectx.ec_iidx = trycmd->tcd_finally_idx;
2525 trycmd->tcd_return = TRUE;
2526 }
2527 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002528 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002529 }
2530 break;
2531
2532 // push a function reference to a compiled function
2533 case ISN_FUNCREF:
2534 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002535 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2536 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2537 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002538
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002539 if (pt == NULL)
2540 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002541 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002542 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002543 vim_free(pt);
2544 goto failed;
2545 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002546 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2547 &ectx) == FAIL)
2548 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002549
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002550 tv = STACK_TV_BOT(0);
2551 ++ectx.ec_stack.ga_len;
2552 tv->vval.v_partial = pt;
2553 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002554 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 }
2556 break;
2557
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002558 // Create a global function from a lambda.
2559 case ISN_NEWFUNC:
2560 {
2561 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2562
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002563 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002564 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002565 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002566 }
2567 break;
2568
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002569 // List functions
2570 case ISN_DEF:
2571 if (iptr->isn_arg.string == NULL)
2572 list_functions(NULL);
2573 else
2574 {
2575 exarg_T ea;
2576
2577 CLEAR_FIELD(ea);
2578 ea.cmd = ea.arg = iptr->isn_arg.string;
2579 define_function(&ea, NULL);
2580 }
2581 break;
2582
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 // jump if a condition is met
2584 case ISN_JUMP:
2585 {
2586 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002587 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002588 int jump = TRUE;
2589
2590 if (when != JUMP_ALWAYS)
2591 {
2592 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002593 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002594 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002595 || when == JUMP_IF_COND_TRUE)
2596 {
2597 SOURCING_LNUM = iptr->isn_lnum;
2598 jump = tv_get_bool_chk(tv, &error);
2599 if (error)
2600 goto on_error;
2601 }
2602 else
2603 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002604 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002605 || when == JUMP_AND_KEEP_IF_FALSE
2606 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002607 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002608 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002609 {
2610 // drop the value from the stack
2611 clear_tv(tv);
2612 --ectx.ec_stack.ga_len;
2613 }
2614 }
2615 if (jump)
2616 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2617 }
2618 break;
2619
2620 // top of a for loop
2621 case ISN_FOR:
2622 {
2623 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2624 typval_T *idxtv =
2625 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2626
2627 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002628 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002629 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002630 ++idxtv->vval.v_number;
2631 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 // past the end of the list, jump to "endfor"
2633 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2634 else if (list->lv_first == &range_list_item)
2635 {
2636 // non-materialized range() list
2637 tv = STACK_TV_BOT(0);
2638 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002639 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 tv->vval.v_number = list_find_nr(
2641 list, idxtv->vval.v_number, NULL);
2642 ++ectx.ec_stack.ga_len;
2643 }
2644 else
2645 {
2646 listitem_T *li = list_find(list, idxtv->vval.v_number);
2647
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002648 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2649 ++ectx.ec_stack.ga_len;
2650 }
2651 }
2652 break;
2653
2654 // start of ":try" block
2655 case ISN_TRY:
2656 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002657 trycmd_T *trycmd = NULL;
2658
Bram Moolenaar270d0382020-05-15 21:42:53 +02002659 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 goto failed;
2661 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2662 + ectx.ec_trystack.ga_len;
2663 ++ectx.ec_trystack.ga_len;
2664 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002665 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002666 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002667 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002668 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2669 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2670 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002671 }
2672 break;
2673
2674 case ISN_PUSHEXC:
2675 if (current_exception == NULL)
2676 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002677 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002678 iemsg("Evaluating catch while current_exception is NULL");
2679 goto failed;
2680 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002681 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002682 goto failed;
2683 tv = STACK_TV_BOT(0);
2684 ++ectx.ec_stack.ga_len;
2685 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002686 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002687 tv->vval.v_string = vim_strsave(
2688 (char_u *)current_exception->value);
2689 break;
2690
2691 case ISN_CATCH:
2692 {
2693 garray_T *trystack = &ectx.ec_trystack;
2694
Bram Moolenaar20a76292020-12-25 19:47:24 +01002695 if (restore_cmdmod)
2696 {
2697 cmdmod.cmod_filter_regmatch.regprog = NULL;
2698 undo_cmdmod(&cmdmod);
2699 cmdmod = save_cmdmod;
2700 restore_cmdmod = FALSE;
2701 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002702 if (trystack->ga_len > 0)
2703 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002704 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002705 + trystack->ga_len - 1;
2706 trycmd->tcd_caught = TRUE;
2707 }
2708 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002709 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 catch_exception(current_exception);
2711 }
2712 break;
2713
Bram Moolenaarc150c092021-02-13 15:02:46 +01002714 case ISN_TRYCONT:
2715 {
2716 garray_T *trystack = &ectx.ec_trystack;
2717 trycont_T *trycont = &iptr->isn_arg.trycont;
2718 int i;
2719 trycmd_T *trycmd;
2720 int iidx = trycont->tct_where;
2721
2722 if (trystack->ga_len < trycont->tct_levels)
2723 {
2724 siemsg("TRYCONT: expected %d levels, found %d",
2725 trycont->tct_levels, trystack->ga_len);
2726 goto failed;
2727 }
2728 // Make :endtry jump to any outer try block and the last
2729 // :endtry inside the loop to the loop start.
2730 for (i = trycont->tct_levels; i > 0; --i)
2731 {
2732 trycmd = ((trycmd_T *)trystack->ga_data)
2733 + trystack->ga_len - i;
2734 trycmd->tcd_cont = iidx;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002735 iidx = trycmd->tcd_finally_idx == 0
2736 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002737 }
2738 // jump to :finally or :endtry of current try statement
2739 ectx.ec_iidx = iidx;
2740 }
2741 break;
2742
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002743 case ISN_FINALLY:
2744 {
2745 garray_T *trystack = &ectx.ec_trystack;
2746 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2747 + trystack->ga_len - 1;
2748
2749 // Reset the index to avoid a return statement jumps here
2750 // again.
2751 trycmd->tcd_finally_idx = 0;
2752 break;
2753 }
2754
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002755 // end of ":try" block
2756 case ISN_ENDTRY:
2757 {
2758 garray_T *trystack = &ectx.ec_trystack;
2759
2760 if (trystack->ga_len > 0)
2761 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002762 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002763
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002764 --trystack->ga_len;
2765 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002766 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002767 trycmd = ((trycmd_T *)trystack->ga_data)
2768 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002769 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002770 {
2771 // discard the exception
2772 if (caught_stack == current_exception)
2773 caught_stack = caught_stack->caught;
2774 discard_current_exception();
2775 }
2776
2777 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002778 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002779
2780 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2781 {
2782 --ectx.ec_stack.ga_len;
2783 clear_tv(STACK_TV_BOT(0));
2784 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002785 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002786 // handling :continue: jump to outer try block or
2787 // start of the loop
2788 ectx.ec_iidx = trycmd->tcd_cont;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002789 }
2790 }
2791 break;
2792
2793 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002794 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002795 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002796
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002797 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2798 {
2799 // throwing an exception while using "silent!" causes
2800 // the function to abort but not display an error.
2801 tv = STACK_TV_BOT(-1);
2802 clear_tv(tv);
2803 tv->v_type = VAR_NUMBER;
2804 tv->vval.v_number = 0;
2805 goto done;
2806 }
2807 --ectx.ec_stack.ga_len;
2808 tv = STACK_TV_BOT(0);
2809 if (tv->vval.v_string == NULL
2810 || *skipwhite(tv->vval.v_string) == NUL)
2811 {
2812 vim_free(tv->vval.v_string);
2813 SOURCING_LNUM = iptr->isn_lnum;
2814 emsg(_(e_throw_with_empty_string));
2815 goto failed;
2816 }
2817
2818 // Inside a "catch" we need to first discard the caught
2819 // exception.
2820 if (trystack->ga_len > 0)
2821 {
2822 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2823 + trystack->ga_len - 1;
2824 if (trycmd->tcd_caught && current_exception != NULL)
2825 {
2826 // discard the exception
2827 if (caught_stack == current_exception)
2828 caught_stack = caught_stack->caught;
2829 discard_current_exception();
2830 trycmd->tcd_caught = FALSE;
2831 }
2832 }
2833
2834 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2835 == FAIL)
2836 {
2837 vim_free(tv->vval.v_string);
2838 goto failed;
2839 }
2840 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002841 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002842 break;
2843
2844 // compare with special values
2845 case ISN_COMPAREBOOL:
2846 case ISN_COMPARESPECIAL:
2847 {
2848 typval_T *tv1 = STACK_TV_BOT(-2);
2849 typval_T *tv2 = STACK_TV_BOT(-1);
2850 varnumber_T arg1 = tv1->vval.v_number;
2851 varnumber_T arg2 = tv2->vval.v_number;
2852 int res;
2853
2854 switch (iptr->isn_arg.op.op_type)
2855 {
2856 case EXPR_EQUAL: res = arg1 == arg2; break;
2857 case EXPR_NEQUAL: res = arg1 != arg2; break;
2858 default: res = 0; break;
2859 }
2860
2861 --ectx.ec_stack.ga_len;
2862 tv1->v_type = VAR_BOOL;
2863 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2864 }
2865 break;
2866
2867 // Operation with two number arguments
2868 case ISN_OPNR:
2869 case ISN_COMPARENR:
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 varnumber_T res;
2876
2877 switch (iptr->isn_arg.op.op_type)
2878 {
2879 case EXPR_MULT: res = arg1 * arg2; break;
2880 case EXPR_DIV: res = arg1 / arg2; break;
2881 case EXPR_REM: res = arg1 % arg2; break;
2882 case EXPR_SUB: res = arg1 - arg2; break;
2883 case EXPR_ADD: res = arg1 + arg2; break;
2884
2885 case EXPR_EQUAL: res = arg1 == arg2; break;
2886 case EXPR_NEQUAL: res = arg1 != arg2; break;
2887 case EXPR_GREATER: res = arg1 > arg2; break;
2888 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2889 case EXPR_SMALLER: res = arg1 < arg2; break;
2890 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2891 default: res = 0; break;
2892 }
2893
2894 --ectx.ec_stack.ga_len;
2895 if (iptr->isn_type == ISN_COMPARENR)
2896 {
2897 tv1->v_type = VAR_BOOL;
2898 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2899 }
2900 else
2901 tv1->vval.v_number = res;
2902 }
2903 break;
2904
2905 // Computation with two float arguments
2906 case ISN_OPFLOAT:
2907 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002908#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002909 {
2910 typval_T *tv1 = STACK_TV_BOT(-2);
2911 typval_T *tv2 = STACK_TV_BOT(-1);
2912 float_T arg1 = tv1->vval.v_float;
2913 float_T arg2 = tv2->vval.v_float;
2914 float_T res = 0;
2915 int cmp = FALSE;
2916
2917 switch (iptr->isn_arg.op.op_type)
2918 {
2919 case EXPR_MULT: res = arg1 * arg2; break;
2920 case EXPR_DIV: res = arg1 / arg2; break;
2921 case EXPR_SUB: res = arg1 - arg2; break;
2922 case EXPR_ADD: res = arg1 + arg2; break;
2923
2924 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2925 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2926 case EXPR_GREATER: cmp = arg1 > arg2; break;
2927 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2928 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2929 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2930 default: cmp = 0; break;
2931 }
2932 --ectx.ec_stack.ga_len;
2933 if (iptr->isn_type == ISN_COMPAREFLOAT)
2934 {
2935 tv1->v_type = VAR_BOOL;
2936 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2937 }
2938 else
2939 tv1->vval.v_float = res;
2940 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002941#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002942 break;
2943
2944 case ISN_COMPARELIST:
2945 {
2946 typval_T *tv1 = STACK_TV_BOT(-2);
2947 typval_T *tv2 = STACK_TV_BOT(-1);
2948 list_T *arg1 = tv1->vval.v_list;
2949 list_T *arg2 = tv2->vval.v_list;
2950 int cmp = FALSE;
2951 int ic = iptr->isn_arg.op.op_ic;
2952
2953 switch (iptr->isn_arg.op.op_type)
2954 {
2955 case EXPR_EQUAL: cmp =
2956 list_equal(arg1, arg2, ic, FALSE); break;
2957 case EXPR_NEQUAL: cmp =
2958 !list_equal(arg1, arg2, ic, FALSE); break;
2959 case EXPR_IS: cmp = arg1 == arg2; break;
2960 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2961 default: cmp = 0; break;
2962 }
2963 --ectx.ec_stack.ga_len;
2964 clear_tv(tv1);
2965 clear_tv(tv2);
2966 tv1->v_type = VAR_BOOL;
2967 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2968 }
2969 break;
2970
2971 case ISN_COMPAREBLOB:
2972 {
2973 typval_T *tv1 = STACK_TV_BOT(-2);
2974 typval_T *tv2 = STACK_TV_BOT(-1);
2975 blob_T *arg1 = tv1->vval.v_blob;
2976 blob_T *arg2 = tv2->vval.v_blob;
2977 int cmp = FALSE;
2978
2979 switch (iptr->isn_arg.op.op_type)
2980 {
2981 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
2982 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
2983 case EXPR_IS: cmp = arg1 == arg2; break;
2984 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2985 default: cmp = 0; break;
2986 }
2987 --ectx.ec_stack.ga_len;
2988 clear_tv(tv1);
2989 clear_tv(tv2);
2990 tv1->v_type = VAR_BOOL;
2991 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2992 }
2993 break;
2994
2995 // TODO: handle separately
2996 case ISN_COMPARESTRING:
2997 case ISN_COMPAREDICT:
2998 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002999 case ISN_COMPAREANY:
3000 {
3001 typval_T *tv1 = STACK_TV_BOT(-2);
3002 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003003 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003004 int ic = iptr->isn_arg.op.op_ic;
3005
Bram Moolenaareb26f432020-09-14 16:50:05 +02003006 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003007 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003008 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003009 --ectx.ec_stack.ga_len;
3010 }
3011 break;
3012
3013 case ISN_ADDLIST:
3014 case ISN_ADDBLOB:
3015 {
3016 typval_T *tv1 = STACK_TV_BOT(-2);
3017 typval_T *tv2 = STACK_TV_BOT(-1);
3018
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003019 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003020 if (iptr->isn_type == ISN_ADDLIST)
3021 eval_addlist(tv1, tv2);
3022 else
3023 eval_addblob(tv1, tv2);
3024 clear_tv(tv2);
3025 --ectx.ec_stack.ga_len;
3026 }
3027 break;
3028
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003029 case ISN_LISTAPPEND:
3030 {
3031 typval_T *tv1 = STACK_TV_BOT(-2);
3032 typval_T *tv2 = STACK_TV_BOT(-1);
3033 list_T *l = tv1->vval.v_list;
3034
3035 // add an item to a list
3036 if (l == NULL)
3037 {
3038 SOURCING_LNUM = iptr->isn_lnum;
3039 emsg(_(e_cannot_add_to_null_list));
3040 goto on_error;
3041 }
3042 if (list_append_tv(l, tv2) == FAIL)
3043 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003044 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003045 --ectx.ec_stack.ga_len;
3046 }
3047 break;
3048
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003049 case ISN_BLOBAPPEND:
3050 {
3051 typval_T *tv1 = STACK_TV_BOT(-2);
3052 typval_T *tv2 = STACK_TV_BOT(-1);
3053 blob_T *b = tv1->vval.v_blob;
3054 int error = FALSE;
3055 varnumber_T n;
3056
3057 // add a number to a blob
3058 if (b == NULL)
3059 {
3060 SOURCING_LNUM = iptr->isn_lnum;
3061 emsg(_(e_cannot_add_to_null_blob));
3062 goto on_error;
3063 }
3064 n = tv_get_number_chk(tv2, &error);
3065 if (error)
3066 goto on_error;
3067 ga_append(&b->bv_ga, (int)n);
3068 --ectx.ec_stack.ga_len;
3069 }
3070 break;
3071
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003072 // Computation with two arguments of unknown type
3073 case ISN_OPANY:
3074 {
3075 typval_T *tv1 = STACK_TV_BOT(-2);
3076 typval_T *tv2 = STACK_TV_BOT(-1);
3077 varnumber_T n1, n2;
3078#ifdef FEAT_FLOAT
3079 float_T f1 = 0, f2 = 0;
3080#endif
3081 int error = FALSE;
3082
3083 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3084 {
3085 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3086 {
3087 eval_addlist(tv1, tv2);
3088 clear_tv(tv2);
3089 --ectx.ec_stack.ga_len;
3090 break;
3091 }
3092 else if (tv1->v_type == VAR_BLOB
3093 && tv2->v_type == VAR_BLOB)
3094 {
3095 eval_addblob(tv1, tv2);
3096 clear_tv(tv2);
3097 --ectx.ec_stack.ga_len;
3098 break;
3099 }
3100 }
3101#ifdef FEAT_FLOAT
3102 if (tv1->v_type == VAR_FLOAT)
3103 {
3104 f1 = tv1->vval.v_float;
3105 n1 = 0;
3106 }
3107 else
3108#endif
3109 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003110 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003111 n1 = tv_get_number_chk(tv1, &error);
3112 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003113 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003114#ifdef FEAT_FLOAT
3115 if (tv2->v_type == VAR_FLOAT)
3116 f1 = n1;
3117#endif
3118 }
3119#ifdef FEAT_FLOAT
3120 if (tv2->v_type == VAR_FLOAT)
3121 {
3122 f2 = tv2->vval.v_float;
3123 n2 = 0;
3124 }
3125 else
3126#endif
3127 {
3128 n2 = tv_get_number_chk(tv2, &error);
3129 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003130 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003131#ifdef FEAT_FLOAT
3132 if (tv1->v_type == VAR_FLOAT)
3133 f2 = n2;
3134#endif
3135 }
3136#ifdef FEAT_FLOAT
3137 // if there is a float on either side the result is a float
3138 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3139 {
3140 switch (iptr->isn_arg.op.op_type)
3141 {
3142 case EXPR_MULT: f1 = f1 * f2; break;
3143 case EXPR_DIV: f1 = f1 / f2; break;
3144 case EXPR_SUB: f1 = f1 - f2; break;
3145 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003146 default: SOURCING_LNUM = iptr->isn_lnum;
3147 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003148 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 }
3150 clear_tv(tv1);
3151 clear_tv(tv2);
3152 tv1->v_type = VAR_FLOAT;
3153 tv1->vval.v_float = f1;
3154 --ectx.ec_stack.ga_len;
3155 }
3156 else
3157#endif
3158 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003159 int failed = FALSE;
3160
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161 switch (iptr->isn_arg.op.op_type)
3162 {
3163 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003164 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3165 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003166 goto on_error;
3167 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003168 case EXPR_SUB: n1 = n1 - n2; break;
3169 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003170 default: n1 = num_modulus(n1, n2, &failed);
3171 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003172 goto on_error;
3173 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003174 }
3175 clear_tv(tv1);
3176 clear_tv(tv2);
3177 tv1->v_type = VAR_NUMBER;
3178 tv1->vval.v_number = n1;
3179 --ectx.ec_stack.ga_len;
3180 }
3181 }
3182 break;
3183
3184 case ISN_CONCAT:
3185 {
3186 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3187 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3188 char_u *res;
3189
3190 res = concat_str(str1, str2);
3191 clear_tv(STACK_TV_BOT(-2));
3192 clear_tv(STACK_TV_BOT(-1));
3193 --ectx.ec_stack.ga_len;
3194 STACK_TV_BOT(-1)->vval.v_string = res;
3195 }
3196 break;
3197
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003198 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003199 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003200 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003201 int is_slice = iptr->isn_type == ISN_STRSLICE;
3202 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003203 char_u *res;
3204
3205 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003206 // string slice: string is at stack-3, first index at
3207 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003208 if (is_slice)
3209 {
3210 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003211 n1 = tv->vval.v_number;
3212 }
3213
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003214 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003215 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003216
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003217 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003218 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003219 if (is_slice)
3220 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003221 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003222 else
3223 // Index: The resulting variable is a string of a
3224 // single character. If the index is too big or
3225 // negative the result is empty.
3226 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003227 vim_free(tv->vval.v_string);
3228 tv->vval.v_string = res;
3229 }
3230 break;
3231
3232 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003233 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003234 {
Bram Moolenaared591872020-08-15 22:14:53 +02003235 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003236 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003237 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003238
3239 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003240 // list slice: list is at stack-3, indexes at stack-2 and
3241 // stack-1
3242 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003243 list = tv->vval.v_list;
3244
3245 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003246 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003247 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003248
3249 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003250 {
Bram Moolenaared591872020-08-15 22:14:53 +02003251 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003252 n1 = tv->vval.v_number;
3253 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003254 }
Bram Moolenaared591872020-08-15 22:14:53 +02003255
3256 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003257 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003258 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003259 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3260 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003261 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003262 }
3263 break;
3264
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003265 case ISN_ANYINDEX:
3266 case ISN_ANYSLICE:
3267 {
3268 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3269 typval_T *var1, *var2;
3270 int res;
3271
3272 // index: composite is at stack-2, index at stack-1
3273 // slice: composite is at stack-3, indexes at stack-2 and
3274 // stack-1
3275 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003276 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003277 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3278 goto on_error;
3279 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3280 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003281 res = eval_index_inner(tv, is_slice, var1, var2,
3282 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003283 clear_tv(var1);
3284 if (is_slice)
3285 clear_tv(var2);
3286 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3287 if (res == FAIL)
3288 goto on_error;
3289 }
3290 break;
3291
Bram Moolenaar9af78762020-06-16 11:34:42 +02003292 case ISN_SLICE:
3293 {
3294 list_T *list;
3295 int count = iptr->isn_arg.number;
3296
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003297 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003298 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003299 list = tv->vval.v_list;
3300
3301 // no error for short list, expect it to be checked earlier
3302 if (list != NULL && list->lv_len >= count)
3303 {
3304 list_T *newlist = list_slice(list,
3305 count, list->lv_len - 1);
3306
3307 if (newlist != NULL)
3308 {
3309 list_unref(list);
3310 tv->vval.v_list = newlist;
3311 ++newlist->lv_refcount;
3312 }
3313 }
3314 }
3315 break;
3316
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003317 case ISN_GETITEM:
3318 {
3319 listitem_T *li;
3320 int index = iptr->isn_arg.number;
3321
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003322 // Get list item: list is at stack-1, push item.
3323 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003324 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003325 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003326
3327 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3328 goto failed;
3329 ++ectx.ec_stack.ga_len;
3330 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003331
3332 // Useful when used in unpack assignment. Reset at
3333 // ISN_DROP.
3334 where.wt_index = index + 1;
3335 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003336 }
3337 break;
3338
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003339 case ISN_MEMBER:
3340 {
3341 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003342 char_u *key;
3343 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003344 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003345
3346 // dict member: dict is at stack-2, key at stack-1
3347 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003348 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003349 dict = tv->vval.v_dict;
3350
3351 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003352 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003353 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003354 if (key == NULL)
3355 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003356
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003357 if ((di = dict_find(dict, key, -1)) == NULL)
3358 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003359 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003360 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003361
3362 // If :silent! is used we will continue, make sure the
3363 // stack contents makes sense.
3364 clear_tv(tv);
3365 --ectx.ec_stack.ga_len;
3366 tv = STACK_TV_BOT(-1);
3367 clear_tv(tv);
3368 tv->v_type = VAR_NUMBER;
3369 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003370 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003371 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003372 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003373 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003374 // Clear the dict only after getting the item, to avoid
3375 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003376 tv = STACK_TV_BOT(-1);
3377 temp_tv = *tv;
3378 copy_tv(&di->di_tv, tv);
3379 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003380 }
3381 break;
3382
3383 // dict member with string key
3384 case ISN_STRINGMEMBER:
3385 {
3386 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003387 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003388 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003389
3390 tv = STACK_TV_BOT(-1);
3391 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3392 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003393 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003394 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003395 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003396 }
3397 dict = tv->vval.v_dict;
3398
3399 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3400 == NULL)
3401 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003402 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003403 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003404 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003405 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003406 // Clear the dict after getting the item, to avoid that it
3407 // make the item invalid.
3408 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003409 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003410 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003411 }
3412 break;
3413
3414 case ISN_NEGATENR:
3415 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003416 if (tv->v_type != VAR_NUMBER
3417#ifdef FEAT_FLOAT
3418 && tv->v_type != VAR_FLOAT
3419#endif
3420 )
3421 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003422 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003423 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003424 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003425 }
3426#ifdef FEAT_FLOAT
3427 if (tv->v_type == VAR_FLOAT)
3428 tv->vval.v_float = -tv->vval.v_float;
3429 else
3430#endif
3431 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003432 break;
3433
3434 case ISN_CHECKNR:
3435 {
3436 int error = FALSE;
3437
3438 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003439 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003441 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 (void)tv_get_number_chk(tv, &error);
3443 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003444 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003445 }
3446 break;
3447
3448 case ISN_CHECKTYPE:
3449 {
3450 checktype_T *ct = &iptr->isn_arg.type;
3451
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003452 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003454 if (!where.wt_variable)
3455 where.wt_index = ct->ct_arg_idx;
3456 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003457 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003458 if (!where.wt_variable)
3459 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003460
3461 // number 0 is FALSE, number 1 is TRUE
3462 if (tv->v_type == VAR_NUMBER
3463 && ct->ct_type->tt_type == VAR_BOOL
3464 && (tv->vval.v_number == 0
3465 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003466 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003467 tv->v_type = VAR_BOOL;
3468 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003469 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003470 }
3471 }
3472 break;
3473
Bram Moolenaar9af78762020-06-16 11:34:42 +02003474 case ISN_CHECKLEN:
3475 {
3476 int min_len = iptr->isn_arg.checklen.cl_min_len;
3477 list_T *list = NULL;
3478
3479 tv = STACK_TV_BOT(-1);
3480 if (tv->v_type == VAR_LIST)
3481 list = tv->vval.v_list;
3482 if (list == NULL || list->lv_len < min_len
3483 || (list->lv_len > min_len
3484 && !iptr->isn_arg.checklen.cl_more_OK))
3485 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003486 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003487 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003488 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003489 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003490 }
3491 }
3492 break;
3493
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003494 case ISN_SETTYPE:
3495 {
3496 checktype_T *ct = &iptr->isn_arg.type;
3497
3498 tv = STACK_TV_BOT(-1);
3499 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3500 {
3501 free_type(tv->vval.v_dict->dv_type);
3502 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3503 }
3504 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3505 {
3506 free_type(tv->vval.v_list->lv_type);
3507 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3508 }
3509 }
3510 break;
3511
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003512 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003513 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003514 {
3515 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003516 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003517
3518 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003519 if (iptr->isn_type == ISN_2BOOL)
3520 {
3521 n = tv2bool(tv);
3522 if (iptr->isn_arg.number) // invert
3523 n = !n;
3524 }
3525 else
3526 {
3527 SOURCING_LNUM = iptr->isn_lnum;
3528 n = tv_get_bool_chk(tv, &error);
3529 if (error)
3530 goto on_error;
3531 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003532 clear_tv(tv);
3533 tv->v_type = VAR_BOOL;
3534 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3535 }
3536 break;
3537
3538 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003539 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003540 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003541 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3542 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3543 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003544 break;
3545
Bram Moolenaar08597872020-12-10 19:43:40 +01003546 case ISN_RANGE:
3547 {
3548 exarg_T ea;
3549 char *errormsg;
3550
Bram Moolenaarece0b872021-01-08 20:40:45 +01003551 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003552 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003553 ea.addr_type = ADDR_LINES;
3554 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003555 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003556 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003557 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003558
3559 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3560 goto failed;
3561 ++ectx.ec_stack.ga_len;
3562 tv = STACK_TV_BOT(-1);
3563 tv->v_type = VAR_NUMBER;
3564 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003565 if (ea.addr_count == 0)
3566 tv->vval.v_number = curwin->w_cursor.lnum;
3567 else
3568 tv->vval.v_number = ea.line2;
3569 }
3570 break;
3571
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003572 case ISN_PUT:
3573 {
3574 int regname = iptr->isn_arg.put.put_regname;
3575 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3576 char_u *expr = NULL;
3577 int dir = FORWARD;
3578
Bram Moolenaar08597872020-12-10 19:43:40 +01003579 if (lnum < -2)
3580 {
3581 // line number was put on the stack by ISN_RANGE
3582 tv = STACK_TV_BOT(-1);
3583 curwin->w_cursor.lnum = tv->vval.v_number;
3584 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3585 dir = BACKWARD;
3586 --ectx.ec_stack.ga_len;
3587 }
3588 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003589 // :put! above cursor
3590 dir = BACKWARD;
3591 else if (lnum >= 0)
3592 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003593
3594 if (regname == '=')
3595 {
3596 tv = STACK_TV_BOT(-1);
3597 if (tv->v_type == VAR_STRING)
3598 expr = tv->vval.v_string;
3599 else
3600 {
3601 expr = typval2string(tv, TRUE); // allocates value
3602 clear_tv(tv);
3603 }
3604 --ectx.ec_stack.ga_len;
3605 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003606 check_cursor();
3607 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3608 vim_free(expr);
3609 }
3610 break;
3611
Bram Moolenaar02194d22020-10-24 23:08:38 +02003612 case ISN_CMDMOD:
3613 save_cmdmod = cmdmod;
3614 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003615 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003616 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3617 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003618 break;
3619
Bram Moolenaar02194d22020-10-24 23:08:38 +02003620 case ISN_CMDMOD_REV:
3621 // filter regprog is owned by the instruction, don't free it
3622 cmdmod.cmod_filter_regmatch.regprog = NULL;
3623 undo_cmdmod(&cmdmod);
3624 cmdmod = save_cmdmod;
3625 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003626 break;
3627
Bram Moolenaar792f7862020-11-23 08:31:18 +01003628 case ISN_UNPACK:
3629 {
3630 int count = iptr->isn_arg.unpack.unp_count;
3631 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3632 list_T *l;
3633 listitem_T *li;
3634 int i;
3635
3636 // Check there is a valid list to unpack.
3637 tv = STACK_TV_BOT(-1);
3638 if (tv->v_type != VAR_LIST)
3639 {
3640 SOURCING_LNUM = iptr->isn_lnum;
3641 emsg(_(e_for_argument_must_be_sequence_of_lists));
3642 goto on_error;
3643 }
3644 l = tv->vval.v_list;
3645 if (l == NULL
3646 || l->lv_len < (semicolon ? count - 1 : count))
3647 {
3648 SOURCING_LNUM = iptr->isn_lnum;
3649 emsg(_(e_list_value_does_not_have_enough_items));
3650 goto on_error;
3651 }
3652 else if (!semicolon && l->lv_len > count)
3653 {
3654 SOURCING_LNUM = iptr->isn_lnum;
3655 emsg(_(e_list_value_has_more_items_than_targets));
3656 goto on_error;
3657 }
3658
3659 CHECK_LIST_MATERIALIZE(l);
3660 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3661 goto failed;
3662 ectx.ec_stack.ga_len += count - 1;
3663
3664 // Variable after semicolon gets a list with the remaining
3665 // items.
3666 if (semicolon)
3667 {
3668 list_T *rem_list =
3669 list_alloc_with_items(l->lv_len - count + 1);
3670
3671 if (rem_list == NULL)
3672 goto failed;
3673 tv = STACK_TV_BOT(-count);
3674 tv->vval.v_list = rem_list;
3675 ++rem_list->lv_refcount;
3676 tv->v_lock = 0;
3677 li = l->lv_first;
3678 for (i = 0; i < count - 1; ++i)
3679 li = li->li_next;
3680 for (i = 0; li != NULL; ++i)
3681 {
3682 list_set_item(rem_list, i, &li->li_tv);
3683 li = li->li_next;
3684 }
3685 --count;
3686 }
3687
3688 // Produce the values in reverse order, first item last.
3689 li = l->lv_first;
3690 for (i = 0; i < count; ++i)
3691 {
3692 tv = STACK_TV_BOT(-i - 1);
3693 copy_tv(&li->li_tv, tv);
3694 li = li->li_next;
3695 }
3696
3697 list_unref(l);
3698 }
3699 break;
3700
Bram Moolenaarb2049902021-01-24 12:53:53 +01003701 case ISN_PROF_START:
3702 case ISN_PROF_END:
3703 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003704#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003705 funccall_T cookie;
3706 ufunc_T *cur_ufunc =
3707 (((dfunc_T *)def_functions.ga_data)
3708 + ectx.ec_dfunc_idx)->df_ufunc;
3709
3710 cookie.func = cur_ufunc;
3711 if (iptr->isn_type == ISN_PROF_START)
3712 {
3713 func_line_start(&cookie, iptr->isn_lnum);
3714 // if we get here the instruction is executed
3715 func_line_exec(&cookie);
3716 }
3717 else
3718 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003719#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003720 }
3721 break;
3722
Bram Moolenaar389df252020-07-09 21:20:47 +02003723 case ISN_SHUFFLE:
3724 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003725 typval_T tmp_tv;
3726 int item = iptr->isn_arg.shuffle.shfl_item;
3727 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003728
3729 tmp_tv = *STACK_TV_BOT(-item);
3730 for ( ; up > 0 && item > 1; --up)
3731 {
3732 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3733 --item;
3734 }
3735 *STACK_TV_BOT(-item) = tmp_tv;
3736 }
3737 break;
3738
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003739 case ISN_DROP:
3740 --ectx.ec_stack.ga_len;
3741 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003742 where.wt_index = 0;
3743 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003744 break;
3745 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003746 continue;
3747
Bram Moolenaard032f342020-07-18 18:13:02 +02003748func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003749 // Restore previous function. If the frame pointer is where we started
3750 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003751 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003752 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003753
Bram Moolenaard032f342020-07-18 18:13:02 +02003754 if (func_return(&ectx) == FAIL)
3755 // only fails when out of memory
3756 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003757 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003758
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003759on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003760 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003761 // If "emsg_silent" is set then ignore the error, unless it was set
3762 // when calling the function.
3763 if (did_emsg_cumul + did_emsg == did_emsg_before
3764 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003765 {
3766 // If a sequence of instructions causes an error while ":silent!"
3767 // was used, restore the stack length and jump ahead to restoring
3768 // the cmdmod.
3769 if (restore_cmdmod)
3770 {
3771 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3772 {
3773 --ectx.ec_stack.ga_len;
3774 clear_tv(STACK_TV_BOT(0));
3775 }
3776 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3777 ++ectx.ec_iidx;
3778 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003779 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003780 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003781on_fatal_error:
3782 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003783 // If we are not inside a try-catch started here, abort execution.
3784 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003785 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003786 }
3787
3788done:
3789 // function finished, get result from the stack.
3790 tv = STACK_TV_BOT(-1);
3791 *rettv = *tv;
3792 tv->v_type = VAR_UNKNOWN;
3793 ret = OK;
3794
3795failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003796 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003797 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003798 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003799
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003800 // Deal with any remaining closures, they may be in use somewhere.
3801 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003802 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003803 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003804 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3805 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003806
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003807 estack_pop();
3808 current_sctx = save_current_sctx;
3809
Bram Moolenaar352134b2020-10-17 22:04:08 +02003810 if (*msg_list != NULL && saved_msg_list != NULL)
3811 {
3812 msglist_T **plist = saved_msg_list;
3813
3814 // Append entries from the current msg_list (uncaught exceptions) to
3815 // the saved msg_list.
3816 while (*plist != NULL)
3817 plist = &(*plist)->next;
3818
3819 *plist = *msg_list;
3820 }
3821 msg_list = saved_msg_list;
3822
Bram Moolenaar02194d22020-10-24 23:08:38 +02003823 if (restore_cmdmod)
3824 {
3825 cmdmod.cmod_filter_regmatch.regprog = NULL;
3826 undo_cmdmod(&cmdmod);
3827 cmdmod = save_cmdmod;
3828 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003829 emsg_silent_def = save_emsg_silent_def;
3830 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003831
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003832failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003833 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003834 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3835 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003836
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003837 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003838 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003839
Bram Moolenaar0186e582021-01-10 18:33:11 +01003840 while (ectx.ec_outer != NULL)
3841 {
3842 outer_T *up = ectx.ec_outer->out_up_is_copy
3843 ? NULL : ectx.ec_outer->out_up;
3844
3845 vim_free(ectx.ec_outer);
3846 ectx.ec_outer = up;
3847 }
3848
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003849 // Not sure if this is necessary.
3850 suppress_errthrow = save_suppress_errthrow;
3851
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003852 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003853 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003854 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003855 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003856 return ret;
3857}
3858
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003859/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003860 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003861 * We don't really need this at runtime, but we do have tests that require it,
3862 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003863 */
3864 void
3865ex_disassemble(exarg_T *eap)
3866{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003867 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003868 char_u *fname;
3869 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003870 dfunc_T *dfunc;
3871 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003872 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003873 int current;
3874 int line_idx = 0;
3875 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003876 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003877
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003878 if (STRNCMP(arg, "<lambda>", 8) == 0)
3879 {
3880 arg += 8;
3881 (void)getdigits(&arg);
3882 fname = vim_strnsave(eap->arg, arg - eap->arg);
3883 }
3884 else
3885 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003886 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003887 if (fname == NULL)
3888 {
3889 semsg(_(e_invarg2), eap->arg);
3890 return;
3891 }
3892
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003893 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003894 if (ufunc == NULL)
3895 {
3896 char_u *p = untrans_function_name(fname);
3897
3898 if (p != NULL)
3899 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003900 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003901 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003902 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003903 if (ufunc == NULL)
3904 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003905 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003906 return;
3907 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003908 if (func_needs_compiling(ufunc, eap->forceit)
3909 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003910 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003911 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003913 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003914 return;
3915 }
3916 if (ufunc->uf_name_exp != NULL)
3917 msg((char *)ufunc->uf_name_exp);
3918 else
3919 msg((char *)ufunc->uf_name);
3920
3921 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003922#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003923 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3924 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3925 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003926#else
3927 instr = dfunc->df_instr;
3928 instr_count = dfunc->df_instr_count;
3929#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003930 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003931 {
3932 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003933 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934
3935 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3936 {
3937 if (current > prev_current)
3938 {
3939 msg_puts("\n\n");
3940 prev_current = current;
3941 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003942 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3943 if (line != NULL)
3944 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 }
3946
3947 switch (iptr->isn_type)
3948 {
3949 case ISN_EXEC:
3950 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3951 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003952 case ISN_EXECCONCAT:
3953 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003954 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003955 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 case ISN_ECHO:
3957 {
3958 echo_T *echo = &iptr->isn_arg.echo;
3959
3960 smsg("%4d %s %d", current,
3961 echo->echo_with_white ? "ECHO" : "ECHON",
3962 echo->echo_count);
3963 }
3964 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003965 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003966 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003967 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003968 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003969 case ISN_ECHOMSG:
3970 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003971 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003972 break;
3973 case ISN_ECHOERR:
3974 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003975 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02003976 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003977 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003978 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003979 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01003980 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003981 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003982 + STACK_FRAME_SIZE));
3983 else
Bram Moolenaarab360522021-01-10 14:02:28 +01003984 smsg("%4d LOAD $%lld", current,
3985 (varnumber_T)(iptr->isn_arg.number));
3986 }
3987 break;
3988 case ISN_LOADOUTER:
3989 {
3990 if (iptr->isn_arg.number < 0)
3991 smsg("%4d LOADOUTER level %d arg[%d]", current,
3992 iptr->isn_arg.outer.outer_depth,
3993 iptr->isn_arg.outer.outer_idx
3994 + STACK_FRAME_SIZE);
3995 else
3996 smsg("%4d LOADOUTER level %d $%d", current,
3997 iptr->isn_arg.outer.outer_depth,
3998 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02003999 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004000 break;
4001 case ISN_LOADV:
4002 smsg("%4d LOADV v:%s", current,
4003 get_vim_var_name(iptr->isn_arg.number));
4004 break;
4005 case ISN_LOADSCRIPT:
4006 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004007 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4008 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004009 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004010 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004011
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004012 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4013 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004014 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004015 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004016 }
4017 break;
4018 case ISN_LOADS:
4019 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004020 scriptitem_T *si = SCRIPT_ITEM(
4021 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004022
4023 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004024 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004025 }
4026 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004027 case ISN_LOADAUTO:
4028 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4029 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004030 case ISN_LOADG:
4031 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4032 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004033 case ISN_LOADB:
4034 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4035 break;
4036 case ISN_LOADW:
4037 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4038 break;
4039 case ISN_LOADT:
4040 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4041 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004042 case ISN_LOADGDICT:
4043 smsg("%4d LOAD g:", current);
4044 break;
4045 case ISN_LOADBDICT:
4046 smsg("%4d LOAD b:", current);
4047 break;
4048 case ISN_LOADWDICT:
4049 smsg("%4d LOAD w:", current);
4050 break;
4051 case ISN_LOADTDICT:
4052 smsg("%4d LOAD t:", current);
4053 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004054 case ISN_LOADOPT:
4055 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4056 break;
4057 case ISN_LOADENV:
4058 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4059 break;
4060 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004061 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004062 break;
4063
4064 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004065 if (iptr->isn_arg.number < 0)
4066 smsg("%4d STORE arg[%lld]", current,
4067 iptr->isn_arg.number + STACK_FRAME_SIZE);
4068 else
4069 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4070 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004071 case ISN_STOREOUTER:
4072 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004073 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004074 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4075 iptr->isn_arg.outer.outer_depth,
4076 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004077 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004078 smsg("%4d STOREOUTER level %d $%d", current,
4079 iptr->isn_arg.outer.outer_depth,
4080 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004081 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004082 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004083 case ISN_STOREV:
4084 smsg("%4d STOREV v:%s", current,
4085 get_vim_var_name(iptr->isn_arg.number));
4086 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004087 case ISN_STOREAUTO:
4088 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4089 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004090 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004091 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4092 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004093 case ISN_STOREB:
4094 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4095 break;
4096 case ISN_STOREW:
4097 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4098 break;
4099 case ISN_STORET:
4100 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4101 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004102 case ISN_STORES:
4103 {
4104 scriptitem_T *si = SCRIPT_ITEM(
4105 iptr->isn_arg.loadstore.ls_sid);
4106
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004107 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004108 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004109 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004110 break;
4111 case ISN_STORESCRIPT:
4112 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004113 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4114 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004115 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004116 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004117
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004118 smsg("%4d STORESCRIPT %s-%d in %s", current,
4119 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004120 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004121 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004122 }
4123 break;
4124 case ISN_STOREOPT:
4125 smsg("%4d STOREOPT &%s", current,
4126 iptr->isn_arg.storeopt.so_name);
4127 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004128 case ISN_STOREENV:
4129 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4130 break;
4131 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004132 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004133 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004134 case ISN_STORENR:
4135 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004136 iptr->isn_arg.storenr.stnr_val,
4137 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004138 break;
4139
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004140 case ISN_STOREINDEX:
4141 switch (iptr->isn_arg.vartype)
4142 {
4143 case VAR_LIST:
4144 smsg("%4d STORELIST", current);
4145 break;
4146 case VAR_DICT:
4147 smsg("%4d STOREDICT", current);
4148 break;
4149 case VAR_ANY:
4150 smsg("%4d STOREINDEX", current);
4151 break;
4152 default: break;
4153 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004154 break;
4155
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004156 // constants
4157 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004158 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004159 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004160 break;
4161 case ISN_PUSHBOOL:
4162 case ISN_PUSHSPEC:
4163 smsg("%4d PUSH %s", current,
4164 get_var_special_name(iptr->isn_arg.number));
4165 break;
4166 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004167#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004168 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004169#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004170 break;
4171 case ISN_PUSHS:
4172 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4173 break;
4174 case ISN_PUSHBLOB:
4175 {
4176 char_u *r;
4177 char_u numbuf[NUMBUFLEN];
4178 char_u *tofree;
4179
4180 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004181 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004182 vim_free(tofree);
4183 }
4184 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004185 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004186 {
4187 char *name = (char *)iptr->isn_arg.string;
4188
4189 smsg("%4d PUSHFUNC \"%s\"", current,
4190 name == NULL ? "[none]" : name);
4191 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004192 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004193 case ISN_PUSHCHANNEL:
4194#ifdef FEAT_JOB_CHANNEL
4195 {
4196 channel_T *channel = iptr->isn_arg.channel;
4197
4198 smsg("%4d PUSHCHANNEL %d", current,
4199 channel == NULL ? 0 : channel->ch_id);
4200 }
4201#endif
4202 break;
4203 case ISN_PUSHJOB:
4204#ifdef FEAT_JOB_CHANNEL
4205 {
4206 typval_T tv;
4207 char_u *name;
4208
4209 tv.v_type = VAR_JOB;
4210 tv.vval.v_job = iptr->isn_arg.job;
4211 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004212 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004213 }
4214#endif
4215 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004216 case ISN_PUSHEXC:
4217 smsg("%4d PUSH v:exception", current);
4218 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004219 case ISN_UNLET:
4220 smsg("%4d UNLET%s %s", current,
4221 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4222 iptr->isn_arg.unlet.ul_name);
4223 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004224 case ISN_UNLETENV:
4225 smsg("%4d UNLETENV%s $%s", current,
4226 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4227 iptr->isn_arg.unlet.ul_name);
4228 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004229 case ISN_UNLETINDEX:
4230 smsg("%4d UNLETINDEX", current);
4231 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004232 case ISN_UNLETRANGE:
4233 smsg("%4d UNLETRANGE", current);
4234 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004235 case ISN_LOCKCONST:
4236 smsg("%4d LOCKCONST", current);
4237 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004238 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004239 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004240 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004241 break;
4242 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004243 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004244 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004245 break;
4246
4247 // function call
4248 case ISN_BCALL:
4249 {
4250 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4251
4252 smsg("%4d BCALL %s(argc %d)", current,
4253 internal_func_name(cbfunc->cbf_idx),
4254 cbfunc->cbf_argcount);
4255 }
4256 break;
4257 case ISN_DCALL:
4258 {
4259 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4260 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4261 + cdfunc->cdf_idx;
4262
4263 smsg("%4d DCALL %s(argc %d)", current,
4264 df->df_ufunc->uf_name_exp != NULL
4265 ? df->df_ufunc->uf_name_exp
4266 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4267 }
4268 break;
4269 case ISN_UCALL:
4270 {
4271 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4272
4273 smsg("%4d UCALL %s(argc %d)", current,
4274 cufunc->cuf_name, cufunc->cuf_argcount);
4275 }
4276 break;
4277 case ISN_PCALL:
4278 {
4279 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4280
4281 smsg("%4d PCALL%s (argc %d)", current,
4282 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4283 }
4284 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004285 case ISN_PCALL_END:
4286 smsg("%4d PCALL end", current);
4287 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004288 case ISN_RETURN:
4289 smsg("%4d RETURN", current);
4290 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004291 case ISN_RETURN_ZERO:
4292 smsg("%4d RETURN 0", current);
4293 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004294 case ISN_FUNCREF:
4295 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004296 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004297 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004298 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004299
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004300 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004301 }
4302 break;
4303
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004304 case ISN_NEWFUNC:
4305 {
4306 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4307
4308 smsg("%4d NEWFUNC %s %s", current,
4309 newfunc->nf_lambda, newfunc->nf_global);
4310 }
4311 break;
4312
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004313 case ISN_DEF:
4314 {
4315 char_u *name = iptr->isn_arg.string;
4316
4317 smsg("%4d DEF %s", current,
4318 name == NULL ? (char_u *)"" : name);
4319 }
4320 break;
4321
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004322 case ISN_JUMP:
4323 {
4324 char *when = "?";
4325
4326 switch (iptr->isn_arg.jump.jump_when)
4327 {
4328 case JUMP_ALWAYS:
4329 when = "JUMP";
4330 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004331 case JUMP_AND_KEEP_IF_TRUE:
4332 when = "JUMP_AND_KEEP_IF_TRUE";
4333 break;
4334 case JUMP_IF_FALSE:
4335 when = "JUMP_IF_FALSE";
4336 break;
4337 case JUMP_AND_KEEP_IF_FALSE:
4338 when = "JUMP_AND_KEEP_IF_FALSE";
4339 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004340 case JUMP_IF_COND_FALSE:
4341 when = "JUMP_IF_COND_FALSE";
4342 break;
4343 case JUMP_IF_COND_TRUE:
4344 when = "JUMP_IF_COND_TRUE";
4345 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004346 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004347 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004348 iptr->isn_arg.jump.jump_where);
4349 }
4350 break;
4351
4352 case ISN_FOR:
4353 {
4354 forloop_T *forloop = &iptr->isn_arg.forloop;
4355
4356 smsg("%4d FOR $%d -> %d", current,
4357 forloop->for_idx, forloop->for_end);
4358 }
4359 break;
4360
4361 case ISN_TRY:
4362 {
4363 try_T *try = &iptr->isn_arg.try;
4364
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004365 if (try->try_ref->try_finally == 0)
4366 smsg("%4d TRY catch -> %d, endtry -> %d",
4367 current,
4368 try->try_ref->try_catch,
4369 try->try_ref->try_endtry);
4370 else
4371 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4372 current,
4373 try->try_ref->try_catch,
4374 try->try_ref->try_finally,
4375 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004376 }
4377 break;
4378 case ISN_CATCH:
4379 // TODO
4380 smsg("%4d CATCH", current);
4381 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004382 case ISN_TRYCONT:
4383 {
4384 trycont_T *trycont = &iptr->isn_arg.trycont;
4385
4386 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4387 trycont->tct_levels,
4388 trycont->tct_levels == 1 ? "" : "s",
4389 trycont->tct_where);
4390 }
4391 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004392 case ISN_FINALLY:
4393 smsg("%4d FINALLY", current);
4394 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004395 case ISN_ENDTRY:
4396 smsg("%4d ENDTRY", current);
4397 break;
4398 case ISN_THROW:
4399 smsg("%4d THROW", current);
4400 break;
4401
4402 // expression operations on number
4403 case ISN_OPNR:
4404 case ISN_OPFLOAT:
4405 case ISN_OPANY:
4406 {
4407 char *what;
4408 char *ins;
4409
4410 switch (iptr->isn_arg.op.op_type)
4411 {
4412 case EXPR_MULT: what = "*"; break;
4413 case EXPR_DIV: what = "/"; break;
4414 case EXPR_REM: what = "%"; break;
4415 case EXPR_SUB: what = "-"; break;
4416 case EXPR_ADD: what = "+"; break;
4417 default: what = "???"; break;
4418 }
4419 switch (iptr->isn_type)
4420 {
4421 case ISN_OPNR: ins = "OPNR"; break;
4422 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4423 case ISN_OPANY: ins = "OPANY"; break;
4424 default: ins = "???"; break;
4425 }
4426 smsg("%4d %s %s", current, ins, what);
4427 }
4428 break;
4429
4430 case ISN_COMPAREBOOL:
4431 case ISN_COMPARESPECIAL:
4432 case ISN_COMPARENR:
4433 case ISN_COMPAREFLOAT:
4434 case ISN_COMPARESTRING:
4435 case ISN_COMPAREBLOB:
4436 case ISN_COMPARELIST:
4437 case ISN_COMPAREDICT:
4438 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004439 case ISN_COMPAREANY:
4440 {
4441 char *p;
4442 char buf[10];
4443 char *type;
4444
4445 switch (iptr->isn_arg.op.op_type)
4446 {
4447 case EXPR_EQUAL: p = "=="; break;
4448 case EXPR_NEQUAL: p = "!="; break;
4449 case EXPR_GREATER: p = ">"; break;
4450 case EXPR_GEQUAL: p = ">="; break;
4451 case EXPR_SMALLER: p = "<"; break;
4452 case EXPR_SEQUAL: p = "<="; break;
4453 case EXPR_MATCH: p = "=~"; break;
4454 case EXPR_IS: p = "is"; break;
4455 case EXPR_ISNOT: p = "isnot"; break;
4456 case EXPR_NOMATCH: p = "!~"; break;
4457 default: p = "???"; break;
4458 }
4459 STRCPY(buf, p);
4460 if (iptr->isn_arg.op.op_ic == TRUE)
4461 strcat(buf, "?");
4462 switch(iptr->isn_type)
4463 {
4464 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4465 case ISN_COMPARESPECIAL:
4466 type = "COMPARESPECIAL"; break;
4467 case ISN_COMPARENR: type = "COMPARENR"; break;
4468 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4469 case ISN_COMPARESTRING:
4470 type = "COMPARESTRING"; break;
4471 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4472 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4473 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4474 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004475 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4476 default: type = "???"; break;
4477 }
4478
4479 smsg("%4d %s %s", current, type, buf);
4480 }
4481 break;
4482
4483 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4484 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4485
4486 // expression operations
4487 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004488 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004489 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004490 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004491 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004492 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004493 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004494 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4495 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004496 case ISN_SLICE: smsg("%4d SLICE %lld",
4497 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004498 case ISN_GETITEM: smsg("%4d ITEM %lld",
4499 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004500 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4501 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004502 iptr->isn_arg.string); break;
4503 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4504
4505 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004506 case ISN_CHECKTYPE:
4507 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004508 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004509 char *tofree;
4510
Bram Moolenaare32e5162021-01-21 20:21:29 +01004511 if (ct->ct_arg_idx == 0)
4512 smsg("%4d CHECKTYPE %s stack[%d]", current,
4513 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004514 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004515 else
4516 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4517 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004518 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004519 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004520 vim_free(tofree);
4521 break;
4522 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004523 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4524 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4525 iptr->isn_arg.checklen.cl_min_len);
4526 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004527 case ISN_SETTYPE:
4528 {
4529 char *tofree;
4530
4531 smsg("%4d SETTYPE %s", current,
4532 type_name(iptr->isn_arg.type.ct_type, &tofree));
4533 vim_free(tofree);
4534 break;
4535 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004536 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004537 case ISN_2BOOL: if (iptr->isn_arg.number)
4538 smsg("%4d INVERT (!val)", current);
4539 else
4540 smsg("%4d 2BOOL (!!val)", current);
4541 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004542 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004543 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004544 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004545 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004546 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004547 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004548 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4549 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004550 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004551 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4552 smsg("%4d PUT %c above range",
4553 current, iptr->isn_arg.put.put_regname);
4554 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4555 smsg("%4d PUT %c range",
4556 current, iptr->isn_arg.put.put_regname);
4557 else
4558 smsg("%4d PUT %c %ld", current,
4559 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004560 (long)iptr->isn_arg.put.put_lnum);
4561 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004562
Bram Moolenaar02194d22020-10-24 23:08:38 +02004563 // TODO: summarize modifiers
4564 case ISN_CMDMOD:
4565 {
4566 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004567 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004568 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4569
4570 buf = alloc(len + 1);
4571 if (buf != NULL)
4572 {
4573 (void)produce_cmdmods(
4574 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4575 smsg("%4d CMDMOD %s", current, buf);
4576 vim_free(buf);
4577 }
4578 break;
4579 }
4580 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004581
Bram Moolenaarb2049902021-01-24 12:53:53 +01004582 case ISN_PROF_START:
4583 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4584 break;
4585
4586 case ISN_PROF_END:
4587 smsg("%4d PROFILE END", current);
4588 break;
4589
Bram Moolenaar792f7862020-11-23 08:31:18 +01004590 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4591 iptr->isn_arg.unpack.unp_count,
4592 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4593 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004594 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4595 iptr->isn_arg.shuffle.shfl_item,
4596 iptr->isn_arg.shuffle.shfl_up);
4597 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004598 case ISN_DROP: smsg("%4d DROP", current); break;
4599 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004600
4601 out_flush(); // output one line at a time
4602 ui_breakcheck();
4603 if (got_int)
4604 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004605 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004606}
4607
4608/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004609 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004610 * list, etc. Mostly like what JavaScript does, except that empty list and
4611 * empty dictionary are FALSE.
4612 */
4613 int
4614tv2bool(typval_T *tv)
4615{
4616 switch (tv->v_type)
4617 {
4618 case VAR_NUMBER:
4619 return tv->vval.v_number != 0;
4620 case VAR_FLOAT:
4621#ifdef FEAT_FLOAT
4622 return tv->vval.v_float != 0.0;
4623#else
4624 break;
4625#endif
4626 case VAR_PARTIAL:
4627 return tv->vval.v_partial != NULL;
4628 case VAR_FUNC:
4629 case VAR_STRING:
4630 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4631 case VAR_LIST:
4632 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4633 case VAR_DICT:
4634 return tv->vval.v_dict != NULL
4635 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4636 case VAR_BOOL:
4637 case VAR_SPECIAL:
4638 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4639 case VAR_JOB:
4640#ifdef FEAT_JOB_CHANNEL
4641 return tv->vval.v_job != NULL;
4642#else
4643 break;
4644#endif
4645 case VAR_CHANNEL:
4646#ifdef FEAT_JOB_CHANNEL
4647 return tv->vval.v_channel != NULL;
4648#else
4649 break;
4650#endif
4651 case VAR_BLOB:
4652 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4653 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004654 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004655 case VAR_VOID:
4656 break;
4657 }
4658 return FALSE;
4659}
4660
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004661 void
4662emsg_using_string_as(typval_T *tv, int as_number)
4663{
4664 semsg(_(as_number ? e_using_string_as_number_str
4665 : e_using_string_as_bool_str),
4666 tv->vval.v_string == NULL
4667 ? (char_u *)"" : tv->vval.v_string);
4668}
4669
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004670/*
4671 * If "tv" is a string give an error and return FAIL.
4672 */
4673 int
4674check_not_string(typval_T *tv)
4675{
4676 if (tv->v_type == VAR_STRING)
4677 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004678 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004679 clear_tv(tv);
4680 return FAIL;
4681 }
4682 return OK;
4683}
4684
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004685
4686#endif // FEAT_EVAL