blob: c0a4874022712ad8c2177fe9424035a4cd03b716 [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 Moolenaar2e34c342021-03-14 12:13:33 +010033 int tcd_cont; // :continue encountered, jump here (minus one)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010034 int tcd_return; // when TRUE return from end of :finally
35} trycmd_T;
36
37
38// A stack is used to store:
39// - arguments passed to a :def function
40// - info about the calling function, to use when returning
41// - local variables
42// - temporary values
43//
44// In detail (FP == Frame Pointer):
45// arg1 first argument from caller (if present)
46// arg2 second argument from caller (if present)
47// extra_arg1 any missing optional argument default value
48// FP -> cur_func calling function
49// current previous instruction pointer
50// frame_ptr previous Frame Pointer
51// var1 space for local variable
52// var2 space for local variable
53// .... fixed space for max. number of local variables
54// temp temporary values
55// .... flexible space for temporary values (can grow big)
56
57/*
58 * Execution context.
59 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010060struct ectx_S {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010061 garray_T ec_stack; // stack of typval_T values
Bram Moolenaarbf67ea12020-05-02 17:52:42 +020062 int ec_frame_idx; // index in ec_stack: context of ec_dfunc_idx
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010063
Bram Moolenaar0186e582021-01-10 18:33:11 +010064 outer_T *ec_outer; // outer scope used for closures, allocated
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +020065
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010066 garray_T ec_trystack; // stack of trycmd_T values
67 int ec_in_catch; // when TRUE in catch or finally block
68
69 int ec_dfunc_idx; // current function index
70 isn_T *ec_instr; // array with instructions
71 int ec_iidx; // index in ec_instr: instruction to execute
Bram Moolenaar148ce7a2020-09-23 21:57:23 +020072
73 garray_T ec_funcrefs; // partials that might be a closure
Bram Moolenaarcd45ed02020-12-22 17:35:54 +010074};
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010075
Bram Moolenaar12d26532021-02-19 19:13:21 +010076#ifdef FEAT_PROFILE
77// stack of profinfo_T used when profiling.
78static garray_T profile_info_ga = {0, 0, sizeof(profinfo_T), 20, NULL};
79#endif
80
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010081// Get pointer to item relative to the bottom of the stack, -1 is the last one.
Bram Moolenaar11107ba2020-08-15 21:10:16 +020082#define STACK_TV_BOT(idx) (((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_stack.ga_len + (idx))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010083
Bram Moolenaar418f1df2020-08-12 21:34:49 +020084 void
85to_string_error(vartype_T vartype)
86{
Bram Moolenaar451c2e32020-08-15 16:33:28 +020087 semsg(_(e_cannot_convert_str_to_string), vartype_name(vartype));
Bram Moolenaar418f1df2020-08-12 21:34:49 +020088}
89
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010090/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +010091 * Return the number of arguments, including optional arguments and any vararg.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +010092 */
93 static int
94ufunc_argcount(ufunc_T *ufunc)
95{
96 return ufunc->uf_args.ga_len + (ufunc->uf_va_name != NULL ? 1 : 0);
97}
98
99/*
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100100 * Set the instruction index, depending on omitted arguments, where the default
101 * values are to be computed. If all optional arguments are present, start
102 * with the function body.
103 * The expression evaluation is at the start of the instructions:
104 * 0 -> EVAL default1
105 * STORE arg[-2]
106 * 1 -> EVAL default2
107 * STORE arg[-1]
108 * 2 -> function body
109 */
110 static void
111init_instr_idx(ufunc_T *ufunc, int argcount, ectx_T *ectx)
112{
113 if (ufunc->uf_def_args.ga_len == 0)
114 ectx->ec_iidx = 0;
115 else
116 {
117 int defcount = ufunc->uf_args.ga_len - argcount;
118
119 // If there is a varargs argument defcount can be negative, no defaults
120 // to evaluate then.
121 if (defcount < 0)
122 defcount = 0;
123 ectx->ec_iidx = ufunc->uf_def_arg_idx[
124 ufunc->uf_def_args.ga_len - defcount];
125 }
126}
127
128/*
Bram Moolenaarfe270812020-04-11 22:31:27 +0200129 * Create a new list from "count" items at the bottom of the stack.
130 * When "count" is zero an empty list is added to the stack.
131 */
132 static int
133exe_newlist(int count, ectx_T *ectx)
134{
135 list_T *list = list_alloc_with_items(count);
136 int idx;
137 typval_T *tv;
138
139 if (list == NULL)
140 return FAIL;
141 for (idx = 0; idx < count; ++idx)
142 list_set_item(list, idx, STACK_TV_BOT(idx - count));
143
144 if (count > 0)
145 ectx->ec_stack.ga_len -= count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200146 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200147 return FAIL;
148 else
149 ++ectx->ec_stack.ga_len;
150 tv = STACK_TV_BOT(-1);
151 tv->v_type = VAR_LIST;
152 tv->vval.v_list = list;
153 ++list->lv_refcount;
154 return OK;
155}
156
157/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100158 * Call compiled function "cdf_idx" from compiled code.
Bram Moolenaarab360522021-01-10 14:02:28 +0100159 * This adds a stack frame and sets the instruction pointer to the start of the
160 * called function.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100161 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100162 *
163 * Stack has:
164 * - current arguments (already there)
165 * - omitted optional argument (default values) added here
166 * - stack frame:
167 * - pointer to calling function
168 * - Index of next instruction in calling function
169 * - previous frame pointer
170 * - reserved space for local variables
171 */
172 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100173call_dfunc(int cdf_idx, partial_T *pt, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100174{
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200175 int argcount = argcount_arg;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100176 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data) + cdf_idx;
177 ufunc_T *ufunc = dfunc->df_ufunc;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200178 int arg_to_add;
179 int vararg_count = 0;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200180 int varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100181 int idx;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200182 estack_T *entry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100183
184 if (dfunc->df_deleted)
185 {
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100186 // don't use ufunc->uf_name, it may have been freed
187 emsg_funcname(e_func_deleted,
188 dfunc->df_name == NULL ? (char_u *)"unknown" : dfunc->df_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100189 return FAIL;
190 }
191
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100192#ifdef FEAT_PROFILE
Bram Moolenaar12d26532021-02-19 19:13:21 +0100193 if (do_profiling == PROF_YES)
194 {
195 if (ga_grow(&profile_info_ga, 1) == OK)
196 {
197 profinfo_T *info = ((profinfo_T *)profile_info_ga.ga_data)
198 + profile_info_ga.ga_len;
199 ++profile_info_ga.ga_len;
200 CLEAR_POINTER(info);
201 profile_may_start_func(info, ufunc,
202 (((dfunc_T *)def_functions.ga_data)
203 + ectx->ec_dfunc_idx)->df_ufunc);
204 }
205
206 // Profiling might be enabled/disabled along the way. This should not
207 // fail, since the function was compiled before and toggling profiling
208 // doesn't change any errors.
209 if (func_needs_compiling(ufunc, PROFILING(ufunc))
210 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100211 == FAIL)
Bram Moolenaar12d26532021-02-19 19:13:21 +0100212 return FAIL;
213 }
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100214#endif
215
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200216 if (ufunc->uf_va_name != NULL)
217 {
Bram Moolenaarfe270812020-04-11 22:31:27 +0200218 // Need to make a list out of the vararg arguments.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200219 // Stack at time of call with 2 varargs:
220 // normal_arg
221 // optional_arg
222 // vararg_1
223 // vararg_2
Bram Moolenaarfe270812020-04-11 22:31:27 +0200224 // After creating the list:
225 // normal_arg
226 // optional_arg
227 // vararg-list
228 // With missing optional arguments we get:
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200229 // normal_arg
Bram Moolenaarfe270812020-04-11 22:31:27 +0200230 // After creating the list
231 // normal_arg
232 // (space for optional_arg)
233 // vararg-list
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200234 vararg_count = argcount - ufunc->uf_args.ga_len;
235 if (vararg_count < 0)
236 vararg_count = 0;
237 else
238 argcount -= vararg_count;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200239 if (exe_newlist(vararg_count, ectx) == FAIL)
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200240 return FAIL;
Bram Moolenaarfe270812020-04-11 22:31:27 +0200241
242 vararg_count = 1;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200243 }
244
Bram Moolenaarfe270812020-04-11 22:31:27 +0200245 arg_to_add = ufunc->uf_args.ga_len - argcount;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200246 if (arg_to_add < 0)
247 {
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200248 if (arg_to_add == -1)
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200249 emsg(_(e_one_argument_too_many));
Bram Moolenaar79e8db92020-08-14 22:16:33 +0200250 else
Bram Moolenaar451c2e32020-08-15 16:33:28 +0200251 semsg(_(e_nr_arguments_too_many), -arg_to_add);
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200252 return FAIL;
253 }
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200254
255 // Reserve space for:
256 // - missing arguments
257 // - stack frame
258 // - local variables
259 // - if needed: a counter for number of closures created in
260 // ectx->ec_funcrefs.
261 varcount = dfunc->df_varcount + dfunc->df_has_closure;
262 if (ga_grow(&ectx->ec_stack, arg_to_add + STACK_FRAME_SIZE + varcount)
263 == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100264 return FAIL;
265
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100266 // If depth of calling is getting too high, don't execute the function.
267 if (funcdepth_increment() == FAIL)
268 return FAIL;
269
Bram Moolenaarfe270812020-04-11 22:31:27 +0200270 // Move the vararg-list to below the missing optional arguments.
271 if (vararg_count > 0 && arg_to_add > 0)
272 *STACK_TV_BOT(arg_to_add - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100273
274 // Reserve space for omitted optional arguments, filled in soon.
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200275 for (idx = 0; idx < arg_to_add; ++idx)
Bram Moolenaarfe270812020-04-11 22:31:27 +0200276 STACK_TV_BOT(idx - vararg_count)->v_type = VAR_UNKNOWN;
Bram Moolenaar1378fbc2020-04-11 20:50:33 +0200277 ectx->ec_stack.ga_len += arg_to_add;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100278
279 // Store current execution state in stack frame for ISN_RETURN.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100280 STACK_TV_BOT(STACK_FRAME_FUNC_OFF)->vval.v_number = ectx->ec_dfunc_idx;
281 STACK_TV_BOT(STACK_FRAME_IIDX_OFF)->vval.v_number = ectx->ec_iidx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100282 STACK_TV_BOT(STACK_FRAME_OUTER_OFF)->vval.v_string = (void *)ectx->ec_outer;
283 STACK_TV_BOT(STACK_FRAME_IDX_OFF)->vval.v_number = ectx->ec_frame_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200284 ectx->ec_frame_idx = ectx->ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100285
286 // Initialize local variables
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200287 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100288 STACK_TV_BOT(STACK_FRAME_SIZE + idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200289 if (dfunc->df_has_closure)
290 {
291 typval_T *tv = STACK_TV_BOT(STACK_FRAME_SIZE + dfunc->df_varcount);
292
293 tv->v_type = VAR_NUMBER;
294 tv->vval.v_number = 0;
295 }
296 ectx->ec_stack.ga_len += STACK_FRAME_SIZE + varcount;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100297
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +0100298 if (pt != NULL || ufunc->uf_partial != NULL
299 || (ufunc->uf_flags & FC_CLOSURE))
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100300 {
Bram Moolenaar0186e582021-01-10 18:33:11 +0100301 outer_T *outer = ALLOC_CLEAR_ONE(outer_T);
302
303 if (outer == NULL)
304 return FAIL;
305 if (pt != NULL)
306 {
307 *outer = pt->pt_outer;
308 outer->out_up_is_copy = TRUE;
309 }
310 else if (ufunc->uf_partial != NULL)
311 {
312 *outer = ufunc->uf_partial->pt_outer;
313 outer->out_up_is_copy = TRUE;
314 }
315 else
316 {
317 outer->out_stack = &ectx->ec_stack;
318 outer->out_frame_idx = ectx->ec_frame_idx;
319 outer->out_up = ectx->ec_outer;
320 }
321 ectx->ec_outer = outer;
Bram Moolenaarab360522021-01-10 14:02:28 +0100322 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100323 else
324 ectx->ec_outer = NULL;
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100325
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100326 // Set execution state to the start of the called function.
327 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100328 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100329 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200330 if (entry != NULL)
331 {
332 // Set the script context to the script where the function was defined.
333 // TODO: save more than the SID?
334 entry->es_save_sid = current_sctx.sc_sid;
335 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
336 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100337
338 // Decide where to start execution, handles optional arguments.
339 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100340
341 return OK;
342}
343
344// Get pointer to item in the stack.
345#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
346
347/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200348 * Used when returning from a function: Check if any closure is still
349 * referenced. If so then move the arguments and variables to a separate piece
350 * of stack to be used when the closure is called.
351 * When "free_arguments" is TRUE the arguments are to be freed.
352 * Returns FAIL when out of memory.
353 */
354 static int
355handle_closure_in_use(ectx_T *ectx, int free_arguments)
356{
357 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
358 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200359 int argcount;
360 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200361 int idx;
362 typval_T *tv;
363 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200364 garray_T *gap = &ectx->ec_funcrefs;
365 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200366
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200367 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200368 return OK; // function was freed
369 if (dfunc->df_has_closure == 0)
370 return OK; // no closures
371 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
372 closure_count = tv->vval.v_number;
373 if (closure_count == 0)
374 return OK; // no funcrefs created
375
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200376 argcount = ufunc_argcount(dfunc->df_ufunc);
377 top = ectx->ec_frame_idx - argcount;
378
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200379 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200380 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200382 partial_T *pt;
383 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200384
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200385 if (off < 0)
386 continue; // count is off or already done
387 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200388 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200389 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200391 int i;
392
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200393 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200394 // unreferenced on return.
395 for (i = 0; i < dfunc->df_varcount; ++i)
396 {
397 typval_T *stv = STACK_TV(ectx->ec_frame_idx
398 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200399 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200400 --refcount;
401 }
402 if (refcount > 1)
403 {
404 closure_in_use = TRUE;
405 break;
406 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200407 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200408 }
409
410 if (closure_in_use)
411 {
412 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
413 typval_T *stack;
414
415 // A closure is using the arguments and/or local variables.
416 // Move them to the called function.
417 if (funcstack == NULL)
418 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200419 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
420 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200421 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
422 funcstack->fs_ga.ga_data = stack;
423 if (stack == NULL)
424 {
425 vim_free(funcstack);
426 return FAIL;
427 }
428
429 // Move or copy the arguments.
430 for (idx = 0; idx < argcount; ++idx)
431 {
432 tv = STACK_TV(top + idx);
433 if (free_arguments)
434 {
435 *(stack + idx) = *tv;
436 tv->v_type = VAR_UNKNOWN;
437 }
438 else
439 copy_tv(tv, stack + idx);
440 }
441 // Move the local variables.
442 for (idx = 0; idx < dfunc->df_varcount; ++idx)
443 {
444 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200445
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200446 // A partial created for a local function, that is also used as a
447 // local variable, has a reference count for the variable, thus
448 // will never go down to zero. When all these refcounts are one
449 // then the funcstack is unused. We need to count how many we have
450 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200451 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
452 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200453 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200454
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200455 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200456 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
457 gap->ga_len - closure_count + i])
458 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200459 }
460
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200461 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200462 tv->v_type = VAR_UNKNOWN;
463 }
464
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200465 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200466 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200467 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
468 - closure_count + idx];
469 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200470 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200471 ++funcstack->fs_refcount;
472 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100473 pt->pt_outer.out_stack = &funcstack->fs_ga;
474 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
475 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200476 }
477 }
478 }
479
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200480 for (idx = 0; idx < closure_count; ++idx)
481 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
482 - closure_count + idx]);
483 gap->ga_len -= closure_count;
484 if (gap->ga_len == 0)
485 ga_clear(gap);
486
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200487 return OK;
488}
489
490/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200491 * Called when a partial is freed or its reference count goes down to one. The
492 * funcstack may be the only reference to the partials in the local variables.
493 * Go over all of them, the funcref and can be freed if all partials
494 * referencing the funcstack have a reference count of one.
495 */
496 void
497funcstack_check_refcount(funcstack_T *funcstack)
498{
499 int i;
500 garray_T *gap = &funcstack->fs_ga;
501 int done = 0;
502
503 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
504 return;
505 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
506 {
507 typval_T *tv = ((typval_T *)gap->ga_data) + i;
508
509 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
510 && tv->vval.v_partial->pt_funcstack == funcstack
511 && tv->vval.v_partial->pt_refcount == 1)
512 ++done;
513 }
514 if (done == funcstack->fs_min_refcount)
515 {
516 typval_T *stack = gap->ga_data;
517
518 // All partials referencing the funcstack have a reference count of
519 // one, thus the funcstack is no longer of use.
520 for (i = 0; i < gap->ga_len; ++i)
521 clear_tv(stack + i);
522 vim_free(stack);
523 vim_free(funcstack);
524 }
525}
526
527/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100528 * Return from the current function.
529 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200530 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100531func_return(ectx_T *ectx)
532{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100534 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200535 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
536 + ectx->ec_dfunc_idx;
537 int argcount = ufunc_argcount(dfunc->df_ufunc);
538 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200539 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100540 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
541 + STACK_FRAME_FUNC_OFF)->vval.v_number;
542 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
543 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100544
Bram Moolenaar12d26532021-02-19 19:13:21 +0100545#ifdef FEAT_PROFILE
546 if (do_profiling == PROF_YES)
547 {
548 ufunc_T *caller = prev_dfunc->df_ufunc;
549
550 if (dfunc->df_ufunc->uf_profiling
551 || (caller != NULL && caller->uf_profiling))
552 {
553 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
554 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
555 --profile_info_ga.ga_len;
556 }
557 }
558#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100559 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200560 entry = estack_pop();
561 if (entry != NULL)
562 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100563
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200564 if (handle_closure_in_use(ectx, TRUE) == FAIL)
565 return FAIL;
566
567 // Clear the arguments.
568 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
569 clear_tv(STACK_TV(idx));
570
571 // Clear local variables and temp values, but not the return value.
572 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100573 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100574 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100575
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100576 // The return value should be on top of the stack. However, when aborting
577 // it may not be there and ec_frame_idx is the top of the stack.
578 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100579 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100580 ret_idx = 0;
581
Bram Moolenaar0186e582021-01-10 18:33:11 +0100582 vim_free(ectx->ec_outer);
583
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100584 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100585 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100586 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
587 + STACK_FRAME_IIDX_OFF)->vval.v_number;
588 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
589 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200590 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100593 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100594
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100595 if (ret_idx > 0)
596 {
597 // Reset the stack to the position before the call, with a spot for the
598 // return value, moved there from above the frame.
599 ectx->ec_stack.ga_len = top + 1;
600 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
601 }
602 else
603 // Reset the stack to the position before the call.
604 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200605
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100606 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200607 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100608}
609
610#undef STACK_TV
611
612/*
613 * Prepare arguments and rettv for calling a builtin or user function.
614 */
615 static int
616call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
617{
618 int idx;
619 typval_T *tv;
620
621 // Move arguments from bottom of the stack to argvars[] and add terminator.
622 for (idx = 0; idx < argcount; ++idx)
623 argvars[idx] = *STACK_TV_BOT(idx - argcount);
624 argvars[argcount].v_type = VAR_UNKNOWN;
625
626 // Result replaces the arguments on the stack.
627 if (argcount > 0)
628 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200629 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100630 return FAIL;
631 else
632 ++ectx->ec_stack.ga_len;
633
634 // Default return value is zero.
635 tv = STACK_TV_BOT(-1);
636 tv->v_type = VAR_NUMBER;
637 tv->vval.v_number = 0;
638
639 return OK;
640}
641
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200642// Ugly global to avoid passing the execution context around through many
643// layers.
644static ectx_T *current_ectx = NULL;
645
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100646/*
647 * Call a builtin function by index.
648 */
649 static int
650call_bfunc(int func_idx, int argcount, ectx_T *ectx)
651{
652 typval_T argvars[MAX_FUNC_ARGS];
653 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100654 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200655 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100656
657 if (call_prepare(argcount, argvars, ectx) == FAIL)
658 return FAIL;
659
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200660 // Call the builtin function. Set "current_ectx" so that when it
661 // recursively invokes call_def_function() a closure context can be set.
662 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100663 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200664 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100665
666 // Clear the arguments.
667 for (idx = 0; idx < argcount; ++idx)
668 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200669
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100670 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200671 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100672 return OK;
673}
674
675/*
676 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100677 * If the function is compiled this will add a stack frame and set the
678 * instruction pointer at the start of the function.
679 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100680 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100681 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100682 */
683 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100684call_ufunc(
685 ufunc_T *ufunc,
686 partial_T *pt,
687 int argcount,
688 ectx_T *ectx,
689 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100690{
691 typval_T argvars[MAX_FUNC_ARGS];
692 funcexe_T funcexe;
693 int error;
694 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100695 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100696#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100697 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100698#else
699# define profiling FALSE
700#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100701
Bram Moolenaarb2049902021-01-24 12:53:53 +0100702 if (func_needs_compiling(ufunc, profiling)
703 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200704 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200705 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100706 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100707 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100708 if (error != FCERR_UNKNOWN)
709 {
710 if (error == FCERR_TOOMANY)
711 semsg(_(e_toomanyarg), ufunc->uf_name);
712 else
713 semsg(_(e_toofewarg), ufunc->uf_name);
714 return FAIL;
715 }
716
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100717 // The function has been compiled, can call it quickly. For a function
718 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100719 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100720 if (iptr != NULL)
721 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100722 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100723 iptr->isn_type = ISN_DCALL;
724 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
725 iptr->isn_arg.dfunc.cdf_argcount = argcount;
726 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100727 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100728 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100729
730 if (call_prepare(argcount, argvars, ectx) == FAIL)
731 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200732 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100733 funcexe.evaluate = TRUE;
734
735 // Call the user function. Result goes in last position on the stack.
736 // TODO: add selfdict if there is one
737 error = call_user_func_check(ufunc, argcount, argvars,
738 STACK_TV_BOT(-1), &funcexe, NULL);
739
740 // Clear the arguments.
741 for (idx = 0; idx < argcount; ++idx)
742 clear_tv(&argvars[idx]);
743
744 if (error != FCERR_NONE)
745 {
746 user_func_error(error, ufunc->uf_name);
747 return FAIL;
748 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100749 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200750 // Error other than from calling the function itself.
751 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100752 return OK;
753}
754
755/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200756 * Return TRUE if an error was given or CTRL-C was pressed.
757 */
758 static int
759vim9_aborting(int prev_called_emsg)
760{
761 return called_emsg > prev_called_emsg || got_int || did_throw;
762}
763
764/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100765 * Execute a function by "name".
766 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100767 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100768 * Returns FAIL if not found without an error message.
769 */
770 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100771call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100772{
773 ufunc_T *ufunc;
774
775 if (builtin_function(name, -1))
776 {
777 int func_idx = find_internal_func(name);
778
779 if (func_idx < 0)
780 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200781 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100782 return FAIL;
783 return call_bfunc(func_idx, argcount, ectx);
784 }
785
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200786 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200787
788 if (ufunc == NULL)
789 {
790 int called_emsg_before = called_emsg;
791
792 if (script_autoload(name, TRUE))
793 // loaded a package, search for the function again
794 ufunc = find_func(name, FALSE, NULL);
795 if (vim9_aborting(called_emsg_before))
796 return FAIL; // bail out if loading the script caused an error
797 }
798
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100799 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100800 {
801 if (ufunc->uf_arg_types != NULL)
802 {
803 int i;
804 typval_T *argv = STACK_TV_BOT(0) - argcount;
805
806 // The function can change at runtime, check that the argument
807 // types are correct.
808 for (i = 0; i < argcount; ++i)
809 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100810 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100811
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100812 if (i < ufunc->uf_args.ga_len)
813 type = ufunc->uf_arg_types[i];
814 else if (ufunc->uf_va_type != NULL)
815 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100816 if (type != NULL && check_typval_arg_type(type,
817 &argv[i], i + 1) == FAIL)
818 return FAIL;
819 }
820 }
821
Bram Moolenaar0186e582021-01-10 18:33:11 +0100822 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100823 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100824
825 return FAIL;
826}
827
828 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200829call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100830{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200831 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200832 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100833 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100834 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835
836 if (tv->v_type == VAR_PARTIAL)
837 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200838 partial_T *pt = tv->vval.v_partial;
839 int i;
840
841 if (pt->pt_argc > 0)
842 {
843 // Make space for arguments from the partial, shift the "argcount"
844 // arguments up.
845 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
846 return FAIL;
847 for (i = 1; i <= argcount; ++i)
848 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
849 ectx->ec_stack.ga_len += pt->pt_argc;
850 argcount += pt->pt_argc;
851
852 // copy the arguments from the partial onto the stack
853 for (i = 0; i < pt->pt_argc; ++i)
854 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
855 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100856
857 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100858 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200859
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100860 name = pt->pt_name;
861 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200862 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100863 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200864 if (name != NULL)
865 {
866 char_u fname_buf[FLEN_FIXED + 1];
867 char_u *tofree = NULL;
868 int error = FCERR_NONE;
869 char_u *fname;
870
871 // May need to translate <SNR>123_ to K_SNR.
872 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
873 if (error != FCERR_NONE)
874 res = FAIL;
875 else
876 res = call_by_name(fname, argcount, ectx, NULL);
877 vim_free(tofree);
878 }
879
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100880 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100881 {
882 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200883 semsg(_(e_unknownfunc),
884 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100885 return FAIL;
886 }
887 return OK;
888}
889
890/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200891 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
892 * TRUE.
893 */
894 static int
895error_if_locked(int lock, char *error)
896{
897 if (lock & (VAR_LOCKED | VAR_FIXED))
898 {
899 emsg(_(error));
900 return TRUE;
901 }
902 return FALSE;
903}
904
905/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100906 * Give an error if "tv" is not a number and return FAIL.
907 */
908 static int
909check_for_number(typval_T *tv)
910{
911 if (tv->v_type != VAR_NUMBER)
912 {
913 semsg(_(e_expected_str_but_got_str),
914 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
915 return FAIL;
916 }
917 return OK;
918}
919
920/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100921 * Store "tv" in variable "name".
922 * This is for s: and g: variables.
923 */
924 static void
925store_var(char_u *name, typval_T *tv)
926{
927 funccal_entry_T entry;
928
929 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100930 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100931 restore_funccal();
932}
933
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100934/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100935 * Convert "tv" to a string.
936 * Return FAIL if not allowed.
937 */
938 static int
939do_2string(typval_T *tv, int is_2string_any)
940{
941 if (tv->v_type != VAR_STRING)
942 {
943 char_u *str;
944
945 if (is_2string_any)
946 {
947 switch (tv->v_type)
948 {
949 case VAR_SPECIAL:
950 case VAR_BOOL:
951 case VAR_NUMBER:
952 case VAR_FLOAT:
953 case VAR_BLOB: break;
954 default: to_string_error(tv->v_type);
955 return FAIL;
956 }
957 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100958 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100959 clear_tv(tv);
960 tv->v_type = VAR_STRING;
961 tv->vval.v_string = str;
962 }
963 return OK;
964}
965
966/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100967 * When the value of "sv" is a null list of dict, allocate it.
968 */
969 static void
970allocate_if_null(typval_T *tv)
971{
972 switch (tv->v_type)
973 {
974 case VAR_LIST:
975 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100976 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100977 break;
978 case VAR_DICT:
979 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100980 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100981 break;
982 default:
983 break;
984 }
985}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200986
Bram Moolenaare7525c52021-01-09 13:20:37 +0100987/*
Bram Moolenaar0289a092021-03-14 18:40:19 +0100988 * Return the character "str[index]" where "index" is the character index,
989 * including composing characters.
990 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +0100991 */
992 char_u *
993char_from_string(char_u *str, varnumber_T index)
994{
995 size_t nbyte = 0;
996 varnumber_T nchar = index;
997 size_t slen;
998
999 if (str == NULL)
1000 return NULL;
1001 slen = STRLEN(str);
1002
1003 // do the same as for a list: a negative index counts from the end
1004 if (index < 0)
1005 {
1006 int clen = 0;
1007
1008 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001009 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001010 nchar = clen + index;
1011 if (nchar < 0)
1012 // unlike list: index out of range results in empty string
1013 return NULL;
1014 }
1015
1016 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001017 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001018 if (nbyte >= slen)
1019 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001020 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001021}
1022
1023/*
1024 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001025 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001026 * If going over the end return "str_len".
1027 * If "idx" is negative count from the end, -1 is the last character.
1028 * When going over the start return -1.
1029 */
1030 static long
1031char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1032{
1033 varnumber_T nchar = idx;
1034 size_t nbyte = 0;
1035
1036 if (nchar >= 0)
1037 {
1038 while (nchar > 0 && nbyte < str_len)
1039 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001040 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001041 --nchar;
1042 }
1043 }
1044 else
1045 {
1046 nbyte = str_len;
1047 while (nchar < 0 && nbyte > 0)
1048 {
1049 --nbyte;
1050 nbyte -= mb_head_off(str, str + nbyte);
1051 ++nchar;
1052 }
1053 if (nchar < 0)
1054 return -1;
1055 }
1056 return (long)nbyte;
1057}
1058
1059/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001060 * Return the slice "str[first : last]" using character indexes. Composing
1061 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001062 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001063 * Return NULL when the result is empty.
1064 */
1065 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001066string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001067{
1068 long start_byte, end_byte;
1069 size_t slen;
1070
1071 if (str == NULL)
1072 return NULL;
1073 slen = STRLEN(str);
1074 start_byte = char_idx2byte(str, slen, first);
1075 if (start_byte < 0)
1076 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001077 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001078 end_byte = (long)slen;
1079 else
1080 {
1081 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001082 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001084 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001085 }
1086
1087 if (start_byte >= (long)slen || end_byte <= start_byte)
1088 return NULL;
1089 return vim_strnsave(str + start_byte, end_byte - start_byte);
1090}
1091
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001092 static svar_T *
1093get_script_svar(scriptref_T *sref, ectx_T *ectx)
1094{
1095 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1096 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1097 + ectx->ec_dfunc_idx;
1098 svar_T *sv;
1099
1100 if (sref->sref_seq != si->sn_script_seq)
1101 {
1102 // The script was reloaded after the function was
1103 // compiled, the script_idx may not be valid.
1104 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1105 dfunc->df_ufunc->uf_name_exp);
1106 return NULL;
1107 }
1108 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1109 if (!equal_type(sv->sv_type, sref->sref_type))
1110 {
1111 emsg(_(e_script_variable_type_changed));
1112 return NULL;
1113 }
1114 return sv;
1115}
1116
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001117/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001118 * Execute a function by "name".
1119 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001120 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001121 */
1122 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001123call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001124{
Bram Moolenaared677f52020-08-12 16:38:10 +02001125 int called_emsg_before = called_emsg;
1126 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001127
Bram Moolenaared677f52020-08-12 16:38:10 +02001128 res = call_by_name(name, argcount, ectx, iptr);
1129 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001130 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001131 dictitem_T *v;
1132
1133 v = find_var(name, NULL, FALSE);
1134 if (v == NULL)
1135 {
1136 semsg(_(e_unknownfunc), name);
1137 return FAIL;
1138 }
1139 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1140 {
1141 semsg(_(e_unknownfunc), name);
1142 return FAIL;
1143 }
1144 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001145 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001146 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001147}
1148
1149/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001150 * When a function reference is used, fill a partial with the information
1151 * needed, especially when it is used as a closure.
1152 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001153 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001154fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1155{
1156 pt->pt_func = ufunc;
1157 pt->pt_refcount = 1;
1158
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001159 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001160 {
1161 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1162 + ectx->ec_dfunc_idx;
1163
1164 // The closure needs to find arguments and local
1165 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001166 pt->pt_outer.out_stack = &ectx->ec_stack;
1167 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1168 pt->pt_outer.out_up = ectx->ec_outer;
1169 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001170
1171 // If this function returns and the closure is still
1172 // being used, we need to make a copy of the context
1173 // (arguments and local variables). Store a reference
1174 // to the partial so we can handle that.
1175 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1176 {
1177 vim_free(pt);
1178 return FAIL;
1179 }
1180 // Extra variable keeps the count of closures created
1181 // in the current function call.
1182 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1183 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1184
1185 ((partial_T **)ectx->ec_funcrefs.ga_data)
1186 [ectx->ec_funcrefs.ga_len] = pt;
1187 ++pt->pt_refcount;
1188 ++ectx->ec_funcrefs.ga_len;
1189 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001190 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001191 return OK;
1192}
1193
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001194
Bram Moolenaarf112f302020-12-20 17:47:52 +01001195/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001196 * Call a "def" function from old Vim script.
1197 * Return OK or FAIL.
1198 */
1199 int
1200call_def_function(
1201 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001202 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001203 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001204 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001205 typval_T *rettv) // return value
1206{
1207 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001208 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001209 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 typval_T *tv;
1211 int idx;
1212 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001213 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001214 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001215 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001216 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001217 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001218 msglist_T **saved_msg_list = NULL;
1219 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001220 cmdmod_T save_cmdmod;
1221 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001222 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001223 int save_emsg_silent_def = emsg_silent_def;
1224 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001225 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001226 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001227 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001228
1229// Get pointer to item in the stack.
1230#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1231
1232// Get pointer to item at the bottom of the stack, -1 is the bottom.
1233#undef STACK_TV_BOT
1234#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1235
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001236// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001237#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 +01001238
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001239 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001240 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1241 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001242 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001243 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001244 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001245 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001246 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001247 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001248 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001249
Bram Moolenaar09689a02020-05-09 22:50:08 +02001250 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001251 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001252 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1253 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001254 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001255 {
1256 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001257 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001258 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001259 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001260
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001261 // If depth of calling is getting too high, don't execute the function.
1262 orig_funcdepth = funcdepth_get();
1263 if (funcdepth_increment() == FAIL)
1264 return FAIL;
1265
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001266 CLEAR_FIELD(ectx);
1267 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1268 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1269 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001270 {
1271 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001272 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001273 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001274 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001275 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001276
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001277 // Put arguments on the stack, but no more than what the function expects.
1278 // A lambda can be called with more arguments than it uses.
1279 for (idx = 0; idx < argc
1280 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1281 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001282 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001283 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001284 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001285 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001286 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 copy_tv(&argv[idx], STACK_TV_BOT(0));
1288 ++ectx.ec_stack.ga_len;
1289 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001290
1291 // Turn varargs into a list. Empty list if no args.
1292 if (ufunc->uf_va_name != NULL)
1293 {
1294 int vararg_count = argc - ufunc->uf_args.ga_len;
1295
1296 if (vararg_count < 0)
1297 vararg_count = 0;
1298 else
1299 argc -= vararg_count;
1300 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001301 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001302
1303 // Check the type of the list items.
1304 tv = STACK_TV_BOT(-1);
1305 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001306 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001307 && ufunc->uf_va_type->tt_member != &t_any
1308 && tv->vval.v_list != NULL)
1309 {
1310 type_T *expected = ufunc->uf_va_type->tt_member;
1311 listitem_T *li = tv->vval.v_list->lv_first;
1312
1313 for (idx = 0; idx < vararg_count; ++idx)
1314 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001315 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001316 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001317 goto failed_early;
1318 li = li->li_next;
1319 }
1320 }
1321
Bram Moolenaar23e03252020-04-12 22:22:31 +02001322 if (defcount > 0)
1323 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001324 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001325 --ectx.ec_stack.ga_len;
1326 }
1327
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001328 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001329 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001330 if (defcount > 0)
1331 for (idx = 0; idx < defcount; ++idx)
1332 {
1333 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1334 ++ectx.ec_stack.ga_len;
1335 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001336 if (ufunc->uf_va_name != NULL)
1337 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001338
1339 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001340 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1341 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001342
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001343 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001344 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1345 + ufunc->uf_dfunc_idx;
1346 ufunc_T *base_ufunc = dfunc->df_ufunc;
1347
1348 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1349 // by copy_func().
1350 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001351 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001352 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1353 if (ectx.ec_outer == NULL)
1354 goto failed_early;
1355 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001356 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001357 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1358 {
1359 if (current_ectx->ec_outer != NULL)
1360 *ectx.ec_outer = *current_ectx->ec_outer;
1361 }
1362 else
1363 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001364 }
1365 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001366 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1367 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001368 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001369 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001370
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001371 // dummy frame entries
1372 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1373 {
1374 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1375 ++ectx.ec_stack.ga_len;
1376 }
1377
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001378 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001379 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001380 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1381 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001382
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001383 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001384 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001385 ectx.ec_stack.ga_len += dfunc->df_varcount;
1386 if (dfunc->df_has_closure)
1387 {
1388 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1389 STACK_TV_VAR(idx)->vval.v_number = 0;
1390 ++ectx.ec_stack.ga_len;
1391 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001392
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001393 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001394 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001395
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001396 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001397 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001398 estack_push_ufunc(ufunc, 1);
1399 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001400 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1401
Bram Moolenaar352134b2020-10-17 22:04:08 +02001402 // Use a specific location for storing error messages to be converted to an
1403 // exception.
1404 saved_msg_list = msg_list;
1405 msg_list = &private_msg_list;
1406
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001407 // Do turn errors into exceptions.
1408 suppress_errthrow = FALSE;
1409
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001410 // When ":silent!" was used before calling then we still abort the
1411 // function. If ":silent!" is used in the function then we don't.
1412 emsg_silent_def = emsg_silent;
1413 did_emsg_def = 0;
1414
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001415 where.wt_index = 0;
1416 where.wt_variable = FALSE;
1417
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001418 // Decide where to start execution, handles optional arguments.
1419 init_instr_idx(ufunc, argc, &ectx);
1420
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001421 for (;;)
1422 {
1423 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001424
Bram Moolenaar270d0382020-05-15 21:42:53 +02001425 if (++breakcheck_count >= 100)
1426 {
1427 line_breakcheck();
1428 breakcheck_count = 0;
1429 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001430 if (got_int)
1431 {
1432 // Turn CTRL-C into an exception.
1433 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001434 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001435 goto failed;
1436 did_throw = TRUE;
1437 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001438
Bram Moolenaara26b9702020-04-18 19:53:28 +02001439 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1440 {
1441 // Turn an error message into an exception.
1442 did_emsg = FALSE;
1443 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1444 goto failed;
1445 did_throw = TRUE;
1446 *msg_list = NULL;
1447 }
1448
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001449 if (did_throw && !ectx.ec_in_catch)
1450 {
1451 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001452 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001453
1454 // An exception jumps to the first catch, finally, or returns from
1455 // the current function.
1456 if (trystack->ga_len > 0)
1457 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001458 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001459 {
1460 // jump to ":catch" or ":finally"
1461 ectx.ec_in_catch = TRUE;
1462 ectx.ec_iidx = trycmd->tcd_catch_idx;
1463 }
1464 else
1465 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001466 // Not inside try or need to return from current functions.
1467 // Push a dummy return value.
1468 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1469 goto failed;
1470 tv = STACK_TV_BOT(0);
1471 tv->v_type = VAR_NUMBER;
1472 tv->vval.v_number = 0;
1473 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001474 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001475 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001476 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001477 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001478 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1479 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001480 goto done;
1481 }
1482
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001483 if (func_return(&ectx) == FAIL)
1484 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001485 }
1486 continue;
1487 }
1488
1489 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1490 switch (iptr->isn_type)
1491 {
1492 // execute Ex command line
1493 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001494 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001495 source_cookie_T cookie;
1496
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001497 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001498 // Pass getsourceline to get an error for a missing ":end"
1499 // command.
1500 CLEAR_FIELD(cookie);
1501 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1502 if (do_cmdline(iptr->isn_arg.string,
1503 getsourceline, &cookie,
1504 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1505 == FAIL
1506 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001507 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001508 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001509 break;
1510
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001511 // execute Ex command from pieces on the stack
1512 case ISN_EXECCONCAT:
1513 {
1514 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001515 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001516 int pass;
1517 int i;
1518 char_u *cmd = NULL;
1519 char_u *str;
1520
1521 for (pass = 1; pass <= 2; ++pass)
1522 {
1523 for (i = 0; i < count; ++i)
1524 {
1525 tv = STACK_TV_BOT(i - count);
1526 str = tv->vval.v_string;
1527 if (str != NULL && *str != NUL)
1528 {
1529 if (pass == 2)
1530 STRCPY(cmd + len, str);
1531 len += STRLEN(str);
1532 }
1533 if (pass == 2)
1534 clear_tv(tv);
1535 }
1536 if (pass == 1)
1537 {
1538 cmd = alloc(len + 1);
1539 if (cmd == NULL)
1540 goto failed;
1541 len = 0;
1542 }
1543 }
1544
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001545 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001546 do_cmdline_cmd(cmd);
1547 vim_free(cmd);
1548 }
1549 break;
1550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001551 // execute :echo {string} ...
1552 case ISN_ECHO:
1553 {
1554 int count = iptr->isn_arg.echo.echo_count;
1555 int atstart = TRUE;
1556 int needclr = TRUE;
1557
1558 for (idx = 0; idx < count; ++idx)
1559 {
1560 tv = STACK_TV_BOT(idx - count);
1561 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1562 &atstart, &needclr);
1563 clear_tv(tv);
1564 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001565 if (needclr)
1566 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001567 ectx.ec_stack.ga_len -= count;
1568 }
1569 break;
1570
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001571 // :execute {string} ...
1572 // :echomsg {string} ...
1573 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001574 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001575 case ISN_ECHOMSG:
1576 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001577 {
1578 int count = iptr->isn_arg.number;
1579 garray_T ga;
1580 char_u buf[NUMBUFLEN];
1581 char_u *p;
1582 int len;
1583 int failed = FALSE;
1584
1585 ga_init2(&ga, 1, 80);
1586 for (idx = 0; idx < count; ++idx)
1587 {
1588 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001589 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001590 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001591 if (tv->v_type == VAR_CHANNEL
1592 || tv->v_type == VAR_JOB)
1593 {
1594 SOURCING_LNUM = iptr->isn_lnum;
1595 emsg(_(e_inval_string));
1596 break;
1597 }
1598 else
1599 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001600 }
1601 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001602 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001603
1604 len = (int)STRLEN(p);
1605 if (ga_grow(&ga, len + 2) == FAIL)
1606 failed = TRUE;
1607 else
1608 {
1609 if (ga.ga_len > 0)
1610 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1611 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1612 ga.ga_len += len;
1613 }
1614 clear_tv(tv);
1615 }
1616 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001617 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001618 {
1619 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001620 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001621 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001622
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001623 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001624 {
1625 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001626 {
1627 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001628 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001629 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001630 {
1631 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001632 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001633 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001634 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001635 else
1636 {
1637 msg_sb_eol();
1638 if (iptr->isn_type == ISN_ECHOMSG)
1639 {
1640 msg_attr(ga.ga_data, echo_attr);
1641 out_flush();
1642 }
1643 else
1644 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001645 SOURCING_LNUM = iptr->isn_lnum;
1646 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001647 }
1648 }
1649 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001650 ga_clear(&ga);
1651 }
1652 break;
1653
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001654 // load local variable or argument
1655 case ISN_LOAD:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001656 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001657 goto failed;
1658 copy_tv(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1659 ++ectx.ec_stack.ga_len;
1660 break;
1661
1662 // load v: variable
1663 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001664 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001665 goto failed;
1666 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1667 ++ectx.ec_stack.ga_len;
1668 break;
1669
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001670 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001671 case ISN_LOADSCRIPT:
1672 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001673 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001674 svar_T *sv;
1675
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001676 sv = get_script_svar(sref, &ectx);
1677 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001678 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001679 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001680 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001681 goto failed;
1682 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1683 ++ectx.ec_stack.ga_len;
1684 }
1685 break;
1686
1687 // load s: variable in old script
1688 case ISN_LOADS:
1689 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001690 hashtab_T *ht = &SCRIPT_VARS(
1691 iptr->isn_arg.loadstore.ls_sid);
1692 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001693 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001694
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001695 if (di == NULL)
1696 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001697 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001698 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001699 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001700 }
1701 else
1702 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001703 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001704 goto failed;
1705 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1706 ++ectx.ec_stack.ga_len;
1707 }
1708 }
1709 break;
1710
Bram Moolenaard3aac292020-04-19 14:32:17 +02001711 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001713 case ISN_LOADB:
1714 case ISN_LOADW:
1715 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001716 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001717 dictitem_T *di = NULL;
1718 hashtab_T *ht = NULL;
1719 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001720
Bram Moolenaard3aac292020-04-19 14:32:17 +02001721 switch (iptr->isn_type)
1722 {
1723 case ISN_LOADG:
1724 ht = get_globvar_ht();
1725 namespace = 'g';
1726 break;
1727 case ISN_LOADB:
1728 ht = &curbuf->b_vars->dv_hashtab;
1729 namespace = 'b';
1730 break;
1731 case ISN_LOADW:
1732 ht = &curwin->w_vars->dv_hashtab;
1733 namespace = 'w';
1734 break;
1735 case ISN_LOADT:
1736 ht = &curtab->tp_vars->dv_hashtab;
1737 namespace = 't';
1738 break;
1739 default: // Cannot reach here
1740 goto failed;
1741 }
1742 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001743
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001744 if (di == NULL)
1745 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001746 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001747 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001748 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001749 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001750 }
1751 else
1752 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001753 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001754 goto failed;
1755 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1756 ++ectx.ec_stack.ga_len;
1757 }
1758 }
1759 break;
1760
Bram Moolenaar03290b82020-12-19 16:30:44 +01001761 // load autoload variable
1762 case ISN_LOADAUTO:
1763 {
1764 char_u *name = iptr->isn_arg.string;
1765
1766 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1767 goto failed;
1768 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001769 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001770 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001771 goto on_error;
1772 ++ectx.ec_stack.ga_len;
1773 }
1774 break;
1775
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001776 // load g:/b:/w:/t: namespace
1777 case ISN_LOADGDICT:
1778 case ISN_LOADBDICT:
1779 case ISN_LOADWDICT:
1780 case ISN_LOADTDICT:
1781 {
1782 dict_T *d = NULL;
1783
1784 switch (iptr->isn_type)
1785 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001786 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1787 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1788 case ISN_LOADWDICT: d = curwin->w_vars; break;
1789 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001790 default: // Cannot reach here
1791 goto failed;
1792 }
1793 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1794 goto failed;
1795 tv = STACK_TV_BOT(0);
1796 tv->v_type = VAR_DICT;
1797 tv->v_lock = 0;
1798 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001799 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001800 ++ectx.ec_stack.ga_len;
1801 }
1802 break;
1803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001804 // load &option
1805 case ISN_LOADOPT:
1806 {
1807 typval_T optval;
1808 char_u *name = iptr->isn_arg.string;
1809
Bram Moolenaara8c17702020-04-01 21:17:24 +02001810 // This is not expected to fail, name is checked during
1811 // compilation: don't set SOURCING_LNUM.
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;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001814 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001815 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001816 *STACK_TV_BOT(0) = optval;
1817 ++ectx.ec_stack.ga_len;
1818 }
1819 break;
1820
1821 // load $ENV
1822 case ISN_LOADENV:
1823 {
1824 typval_T optval;
1825 char_u *name = iptr->isn_arg.string;
1826
Bram Moolenaar270d0382020-05-15 21:42:53 +02001827 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001828 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001829 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001830 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001831 *STACK_TV_BOT(0) = optval;
1832 ++ectx.ec_stack.ga_len;
1833 }
1834 break;
1835
1836 // load @register
1837 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001838 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 goto failed;
1840 tv = STACK_TV_BOT(0);
1841 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001842 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001843 // This may result in NULL, which should be equivalent to an
1844 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001845 tv->vval.v_string = get_reg_contents(
1846 iptr->isn_arg.number, GREG_EXPR_SRC);
1847 ++ectx.ec_stack.ga_len;
1848 break;
1849
1850 // store local variable
1851 case ISN_STORE:
1852 --ectx.ec_stack.ga_len;
1853 tv = STACK_TV_VAR(iptr->isn_arg.number);
1854 clear_tv(tv);
1855 *tv = *STACK_TV_BOT(0);
1856 break;
1857
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001858 // store s: variable in old script
1859 case ISN_STORES:
1860 {
1861 hashtab_T *ht = &SCRIPT_VARS(
1862 iptr->isn_arg.loadstore.ls_sid);
1863 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001864 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001865
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001866 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001867 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001868 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001869 else
1870 {
1871 clear_tv(&di->di_tv);
1872 di->di_tv = *STACK_TV_BOT(0);
1873 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001874 }
1875 break;
1876
1877 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001878 case ISN_STORESCRIPT:
1879 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001880 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1881 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001882
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001883 sv = get_script_svar(sref, &ectx);
1884 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001885 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 --ectx.ec_stack.ga_len;
1887 clear_tv(sv->sv_tv);
1888 *sv->sv_tv = *STACK_TV_BOT(0);
1889 }
1890 break;
1891
1892 // store option
1893 case ISN_STOREOPT:
1894 {
1895 long n = 0;
1896 char_u *s = NULL;
1897 char *msg;
1898
1899 --ectx.ec_stack.ga_len;
1900 tv = STACK_TV_BOT(0);
1901 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001902 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001903 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001904 if (s == NULL)
1905 s = (char_u *)"";
1906 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001907 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001908 // must be VAR_NUMBER, CHECKTYPE makes sure
1909 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001910 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1911 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001912 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001913 if (msg != NULL)
1914 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001915 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001916 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001917 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001919 }
1920 break;
1921
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001922 // store $ENV
1923 case ISN_STOREENV:
1924 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001925 tv = STACK_TV_BOT(0);
1926 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1927 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001928 break;
1929
1930 // store @r
1931 case ISN_STOREREG:
1932 {
1933 int reg = iptr->isn_arg.number;
1934
1935 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001936 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001937 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001938 tv_get_string(tv), -1, FALSE);
1939 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001940 }
1941 break;
1942
1943 // store v: variable
1944 case ISN_STOREV:
1945 --ectx.ec_stack.ga_len;
1946 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1947 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001948 // should not happen, type is checked when compiling
1949 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001950 break;
1951
Bram Moolenaard3aac292020-04-19 14:32:17 +02001952 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001953 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001954 case ISN_STOREB:
1955 case ISN_STOREW:
1956 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001957 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001958 dictitem_T *di;
1959 hashtab_T *ht;
1960 char_u *name = iptr->isn_arg.string + 2;
1961
Bram Moolenaard3aac292020-04-19 14:32:17 +02001962 switch (iptr->isn_type)
1963 {
1964 case ISN_STOREG:
1965 ht = get_globvar_ht();
1966 break;
1967 case ISN_STOREB:
1968 ht = &curbuf->b_vars->dv_hashtab;
1969 break;
1970 case ISN_STOREW:
1971 ht = &curwin->w_vars->dv_hashtab;
1972 break;
1973 case ISN_STORET:
1974 ht = &curtab->tp_vars->dv_hashtab;
1975 break;
1976 default: // Cannot reach here
1977 goto failed;
1978 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001979
1980 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001981 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001982 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001983 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001984 else
1985 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001986 SOURCING_LNUM = iptr->isn_lnum;
1987 if (var_check_permission(di, name) == FAIL)
1988 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001989 clear_tv(&di->di_tv);
1990 di->di_tv = *STACK_TV_BOT(0);
1991 }
1992 }
1993 break;
1994
Bram Moolenaar03290b82020-12-19 16:30:44 +01001995 // store an autoload variable
1996 case ISN_STOREAUTO:
1997 SOURCING_LNUM = iptr->isn_lnum;
1998 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
1999 clear_tv(STACK_TV_BOT(-1));
2000 --ectx.ec_stack.ga_len;
2001 break;
2002
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002003 // store number in local variable
2004 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002005 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002006 clear_tv(tv);
2007 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002008 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002009 break;
2010
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002011 // store value in list or dict variable
2012 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002013 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002014 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002015 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002016 typval_T *tv_dest = STACK_TV_BOT(-1);
2017 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002018
Bram Moolenaar752fc692021-01-04 21:57:11 +01002019 // Stack contains:
2020 // -3 value to be stored
2021 // -2 index
2022 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002023 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002024 SOURCING_LNUM = iptr->isn_lnum;
2025 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002026 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002027 dest_type = tv_dest->v_type;
2028 if (dest_type == VAR_DICT)
2029 status = do_2string(tv_idx, TRUE);
2030 else if (dest_type == VAR_LIST
2031 && tv_idx->v_type != VAR_NUMBER)
2032 {
2033 emsg(_(e_number_exp));
2034 status = FAIL;
2035 }
2036 }
2037 else if (dest_type != tv_dest->v_type)
2038 {
2039 // just in case, should be OK
2040 semsg(_(e_expected_str_but_got_str),
2041 vartype_name(dest_type),
2042 vartype_name(tv_dest->v_type));
2043 status = FAIL;
2044 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002045
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002046 if (status == OK && dest_type == VAR_LIST)
2047 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002048 long lidx = (long)tv_idx->vval.v_number;
2049 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002050
2051 if (list == NULL)
2052 {
2053 emsg(_(e_list_not_set));
2054 goto on_error;
2055 }
2056 if (lidx < 0 && list->lv_len + lidx >= 0)
2057 // negative index is relative to the end
2058 lidx = list->lv_len + lidx;
2059 if (lidx < 0 || lidx > list->lv_len)
2060 {
2061 semsg(_(e_listidx), lidx);
2062 goto on_error;
2063 }
2064 if (lidx < list->lv_len)
2065 {
2066 listitem_T *li = list_find(list, lidx);
2067
2068 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002069 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002070 goto on_error;
2071 // overwrite existing list item
2072 clear_tv(&li->li_tv);
2073 li->li_tv = *tv;
2074 }
2075 else
2076 {
2077 if (error_if_locked(list->lv_lock,
2078 e_cannot_change_list))
2079 goto on_error;
2080 // append to list, only fails when out of memory
2081 if (list_append_tv(list, tv) == FAIL)
2082 goto failed;
2083 clear_tv(tv);
2084 }
2085 }
2086 else if (status == OK && dest_type == VAR_DICT)
2087 {
2088 char_u *key = tv_idx->vval.v_string;
2089 dict_T *dict = tv_dest->vval.v_dict;
2090 dictitem_T *di;
2091
2092 SOURCING_LNUM = iptr->isn_lnum;
2093 if (dict == NULL)
2094 {
2095 emsg(_(e_dictionary_not_set));
2096 goto on_error;
2097 }
2098 if (key == NULL)
2099 key = (char_u *)"";
2100 di = dict_find(dict, key, -1);
2101 if (di != NULL)
2102 {
2103 if (error_if_locked(di->di_tv.v_lock,
2104 e_cannot_change_dict_item))
2105 goto on_error;
2106 // overwrite existing value
2107 clear_tv(&di->di_tv);
2108 di->di_tv = *tv;
2109 }
2110 else
2111 {
2112 if (error_if_locked(dict->dv_lock,
2113 e_cannot_change_dict))
2114 goto on_error;
2115 // add to dict, only fails when out of memory
2116 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2117 goto failed;
2118 clear_tv(tv);
2119 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002120 }
2121 else
2122 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002123 status = FAIL;
2124 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002125 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002126
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002127 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002128 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002129 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002130 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002131 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002132 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002133 goto on_error;
2134 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002135 }
2136 break;
2137
Bram Moolenaar0186e582021-01-10 18:33:11 +01002138 // load or store variable or argument from outer scope
2139 case ISN_LOADOUTER:
2140 case ISN_STOREOUTER:
2141 {
2142 int depth = iptr->isn_arg.outer.outer_depth;
2143 outer_T *outer = ectx.ec_outer;
2144
2145 while (depth > 1 && outer != NULL)
2146 {
2147 outer = outer->out_up;
2148 --depth;
2149 }
2150 if (outer == NULL)
2151 {
2152 SOURCING_LNUM = iptr->isn_lnum;
2153 iemsg("LOADOUTER depth more than scope levels");
2154 goto failed;
2155 }
2156 tv = ((typval_T *)outer->out_stack->ga_data)
2157 + outer->out_frame_idx + STACK_FRAME_SIZE
2158 + iptr->isn_arg.outer.outer_idx;
2159 if (iptr->isn_type == ISN_LOADOUTER)
2160 {
2161 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2162 goto failed;
2163 copy_tv(tv, STACK_TV_BOT(0));
2164 ++ectx.ec_stack.ga_len;
2165 }
2166 else
2167 {
2168 --ectx.ec_stack.ga_len;
2169 clear_tv(tv);
2170 *tv = *STACK_TV_BOT(0);
2171 }
2172 }
2173 break;
2174
Bram Moolenaar752fc692021-01-04 21:57:11 +01002175 // unlet item in list or dict variable
2176 case ISN_UNLETINDEX:
2177 {
2178 typval_T *tv_idx = STACK_TV_BOT(-2);
2179 typval_T *tv_dest = STACK_TV_BOT(-1);
2180 int status = OK;
2181
2182 // Stack contains:
2183 // -2 index
2184 // -1 dict or list
2185 if (tv_dest->v_type == VAR_DICT)
2186 {
2187 // unlet a dict item, index must be a string
2188 if (tv_idx->v_type != VAR_STRING)
2189 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002190 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002191 semsg(_(e_expected_str_but_got_str),
2192 vartype_name(VAR_STRING),
2193 vartype_name(tv_idx->v_type));
2194 status = FAIL;
2195 }
2196 else
2197 {
2198 dict_T *d = tv_dest->vval.v_dict;
2199 char_u *key = tv_idx->vval.v_string;
2200 dictitem_T *di = NULL;
2201
2202 if (key == NULL)
2203 key = (char_u *)"";
2204 if (d != NULL)
2205 di = dict_find(d, key, (int)STRLEN(key));
2206 if (di == NULL)
2207 {
2208 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002209 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002210 semsg(_(e_dictkey), key);
2211 status = FAIL;
2212 }
2213 else
2214 {
2215 // TODO: check for dict or item locked
2216 dictitem_remove(d, di);
2217 }
2218 }
2219 }
2220 else if (tv_dest->v_type == VAR_LIST)
2221 {
2222 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002223 SOURCING_LNUM = iptr->isn_lnum;
2224 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002225 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002226 status = FAIL;
2227 }
2228 else
2229 {
2230 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002231 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002232 listitem_T *li = NULL;
2233
2234 li = list_find(l, n);
2235 if (li == NULL)
2236 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002237 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002238 semsg(_(e_listidx), n);
2239 status = FAIL;
2240 }
2241 else
2242 // TODO: check for list or item locked
2243 listitem_remove(l, li);
2244 }
2245 }
2246 else
2247 {
2248 status = FAIL;
2249 semsg(_(e_cannot_index_str),
2250 vartype_name(tv_dest->v_type));
2251 }
2252
2253 clear_tv(tv_idx);
2254 clear_tv(tv_dest);
2255 ectx.ec_stack.ga_len -= 2;
2256 if (status == FAIL)
2257 goto on_error;
2258 }
2259 break;
2260
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002261 // unlet range of items in list variable
2262 case ISN_UNLETRANGE:
2263 {
2264 // Stack contains:
2265 // -3 index1
2266 // -2 index2
2267 // -1 dict or list
2268 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2269 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2270 typval_T *tv_dest = STACK_TV_BOT(-1);
2271 int status = OK;
2272
2273 if (tv_dest->v_type == VAR_LIST)
2274 {
2275 // indexes must be a number
2276 SOURCING_LNUM = iptr->isn_lnum;
2277 if (check_for_number(tv_idx1) == FAIL
2278 || check_for_number(tv_idx2) == FAIL)
2279 {
2280 status = FAIL;
2281 }
2282 else
2283 {
2284 list_T *l = tv_dest->vval.v_list;
2285 long n1 = (long)tv_idx1->vval.v_number;
2286 long n2 = (long)tv_idx2->vval.v_number;
2287 listitem_T *li;
2288
2289 li = list_find_index(l, &n1);
2290 if (li == NULL
2291 || list_unlet_range(l, li, NULL, n1,
2292 TRUE, n2) == FAIL)
2293 status = FAIL;
2294 }
2295 }
2296 else
2297 {
2298 status = FAIL;
2299 SOURCING_LNUM = iptr->isn_lnum;
2300 semsg(_(e_cannot_index_str),
2301 vartype_name(tv_dest->v_type));
2302 }
2303
2304 clear_tv(tv_idx1);
2305 clear_tv(tv_idx2);
2306 clear_tv(tv_dest);
2307 ectx.ec_stack.ga_len -= 3;
2308 if (status == FAIL)
2309 goto on_error;
2310 }
2311 break;
2312
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002313 // push constant
2314 case ISN_PUSHNR:
2315 case ISN_PUSHBOOL:
2316 case ISN_PUSHSPEC:
2317 case ISN_PUSHF:
2318 case ISN_PUSHS:
2319 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002320 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002321 case ISN_PUSHCHANNEL:
2322 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002323 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002324 goto failed;
2325 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002326 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002327 ++ectx.ec_stack.ga_len;
2328 switch (iptr->isn_type)
2329 {
2330 case ISN_PUSHNR:
2331 tv->v_type = VAR_NUMBER;
2332 tv->vval.v_number = iptr->isn_arg.number;
2333 break;
2334 case ISN_PUSHBOOL:
2335 tv->v_type = VAR_BOOL;
2336 tv->vval.v_number = iptr->isn_arg.number;
2337 break;
2338 case ISN_PUSHSPEC:
2339 tv->v_type = VAR_SPECIAL;
2340 tv->vval.v_number = iptr->isn_arg.number;
2341 break;
2342#ifdef FEAT_FLOAT
2343 case ISN_PUSHF:
2344 tv->v_type = VAR_FLOAT;
2345 tv->vval.v_float = iptr->isn_arg.fnumber;
2346 break;
2347#endif
2348 case ISN_PUSHBLOB:
2349 blob_copy(iptr->isn_arg.blob, tv);
2350 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002351 case ISN_PUSHFUNC:
2352 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002353 if (iptr->isn_arg.string == NULL)
2354 tv->vval.v_string = NULL;
2355 else
2356 tv->vval.v_string =
2357 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002358 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002359 case ISN_PUSHCHANNEL:
2360#ifdef FEAT_JOB_CHANNEL
2361 tv->v_type = VAR_CHANNEL;
2362 tv->vval.v_channel = iptr->isn_arg.channel;
2363 if (tv->vval.v_channel != NULL)
2364 ++tv->vval.v_channel->ch_refcount;
2365#endif
2366 break;
2367 case ISN_PUSHJOB:
2368#ifdef FEAT_JOB_CHANNEL
2369 tv->v_type = VAR_JOB;
2370 tv->vval.v_job = iptr->isn_arg.job;
2371 if (tv->vval.v_job != NULL)
2372 ++tv->vval.v_job->jv_refcount;
2373#endif
2374 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002375 default:
2376 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002377 tv->vval.v_string = vim_strsave(
2378 iptr->isn_arg.string == NULL
2379 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002380 }
2381 break;
2382
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002383 case ISN_UNLET:
2384 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2385 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002386 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002387 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002388 case ISN_UNLETENV:
2389 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2390 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002391
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002392 case ISN_LOCKCONST:
2393 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2394 break;
2395
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002396 // create a list from items on the stack; uses a single allocation
2397 // for the list header and the items
2398 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002399 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2400 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002401 break;
2402
2403 // create a dict from items on the stack
2404 case ISN_NEWDICT:
2405 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002406 int count = iptr->isn_arg.number;
2407 dict_T *dict = dict_alloc();
2408 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002409 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002410
2411 if (dict == NULL)
2412 goto failed;
2413 for (idx = 0; idx < count; ++idx)
2414 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002415 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002416 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002417 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002418 key = tv->vval.v_string == NULL
2419 ? (char_u *)"" : tv->vval.v_string;
2420 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002421 if (item != NULL)
2422 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002423 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002424 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002425 dict_unref(dict);
2426 goto on_error;
2427 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002428 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002429 clear_tv(tv);
2430 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002431 {
2432 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002433 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002434 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002435 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2436 item->di_tv.v_lock = 0;
2437 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002438 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002439 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002440 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002441 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002442 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002443 }
2444
2445 if (count > 0)
2446 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002447 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002448 goto failed;
2449 else
2450 ++ectx.ec_stack.ga_len;
2451 tv = STACK_TV_BOT(-1);
2452 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002453 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002454 tv->vval.v_dict = dict;
2455 ++dict->dv_refcount;
2456 }
2457 break;
2458
2459 // call a :def function
2460 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002461 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002462 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002463 iptr->isn_arg.dfunc.cdf_argcount,
2464 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002465 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002466 break;
2467
2468 // call a builtin function
2469 case ISN_BCALL:
2470 SOURCING_LNUM = iptr->isn_lnum;
2471 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2472 iptr->isn_arg.bfunc.cbf_argcount,
2473 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002474 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002475 break;
2476
2477 // call a funcref or partial
2478 case ISN_PCALL:
2479 {
2480 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2481 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002482 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483
2484 SOURCING_LNUM = iptr->isn_lnum;
2485 if (pfunc->cpf_top)
2486 {
2487 // funcref is above the arguments
2488 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2489 }
2490 else
2491 {
2492 // Get the funcref from the stack.
2493 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002494 partial_tv = *STACK_TV_BOT(0);
2495 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002496 }
2497 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002498 if (tv == &partial_tv)
2499 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002500 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002501 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002502 }
2503 break;
2504
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002505 case ISN_PCALL_END:
2506 // PCALL finished, arguments have been consumed and replaced by
2507 // the return value. Now clear the funcref from the stack,
2508 // and move the return value in its place.
2509 --ectx.ec_stack.ga_len;
2510 clear_tv(STACK_TV_BOT(-1));
2511 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2512 break;
2513
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002514 // call a user defined function or funcref/partial
2515 case ISN_UCALL:
2516 {
2517 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2518
2519 SOURCING_LNUM = iptr->isn_lnum;
2520 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002521 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002522 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002523 }
2524 break;
2525
2526 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002527 case ISN_RETURN_ZERO:
2528 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2529 goto failed;
2530 tv = STACK_TV_BOT(0);
2531 ++ectx.ec_stack.ga_len;
2532 tv->v_type = VAR_NUMBER;
2533 tv->vval.v_number = 0;
2534 tv->v_lock = 0;
2535 // FALLTHROUGH
2536
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002537 case ISN_RETURN:
2538 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002539 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002540 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002541
2542 if (trystack->ga_len > 0)
2543 trycmd = ((trycmd_T *)trystack->ga_data)
2544 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002545 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002546 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002547 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002548 // jump to ":finally" or ":endtry"
2549 if (trycmd->tcd_finally_idx != 0)
2550 ectx.ec_iidx = trycmd->tcd_finally_idx;
2551 else
2552 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002553 trycmd->tcd_return = TRUE;
2554 }
2555 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002556 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002557 }
2558 break;
2559
2560 // push a function reference to a compiled function
2561 case ISN_FUNCREF:
2562 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002563 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2564 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2565 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002566
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002567 if (pt == NULL)
2568 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002569 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002570 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002571 vim_free(pt);
2572 goto failed;
2573 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002574 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2575 &ectx) == FAIL)
2576 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002577
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002578 tv = STACK_TV_BOT(0);
2579 ++ectx.ec_stack.ga_len;
2580 tv->vval.v_partial = pt;
2581 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002582 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002583 }
2584 break;
2585
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002586 // Create a global function from a lambda.
2587 case ISN_NEWFUNC:
2588 {
2589 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2590
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002591 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002592 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002593 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002594 }
2595 break;
2596
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002597 // List functions
2598 case ISN_DEF:
2599 if (iptr->isn_arg.string == NULL)
2600 list_functions(NULL);
2601 else
2602 {
2603 exarg_T ea;
2604
2605 CLEAR_FIELD(ea);
2606 ea.cmd = ea.arg = iptr->isn_arg.string;
2607 define_function(&ea, NULL);
2608 }
2609 break;
2610
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002611 // jump if a condition is met
2612 case ISN_JUMP:
2613 {
2614 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002615 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002616 int jump = TRUE;
2617
2618 if (when != JUMP_ALWAYS)
2619 {
2620 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002621 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002622 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002623 || when == JUMP_IF_COND_TRUE)
2624 {
2625 SOURCING_LNUM = iptr->isn_lnum;
2626 jump = tv_get_bool_chk(tv, &error);
2627 if (error)
2628 goto on_error;
2629 }
2630 else
2631 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002632 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002633 || when == JUMP_AND_KEEP_IF_FALSE
2634 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002635 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002636 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002637 {
2638 // drop the value from the stack
2639 clear_tv(tv);
2640 --ectx.ec_stack.ga_len;
2641 }
2642 }
2643 if (jump)
2644 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2645 }
2646 break;
2647
2648 // top of a for loop
2649 case ISN_FOR:
2650 {
2651 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2652 typval_T *idxtv =
2653 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2654
2655 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002656 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002657 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002658 ++idxtv->vval.v_number;
2659 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002660 // past the end of the list, jump to "endfor"
2661 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2662 else if (list->lv_first == &range_list_item)
2663 {
2664 // non-materialized range() list
2665 tv = STACK_TV_BOT(0);
2666 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002667 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 tv->vval.v_number = list_find_nr(
2669 list, idxtv->vval.v_number, NULL);
2670 ++ectx.ec_stack.ga_len;
2671 }
2672 else
2673 {
2674 listitem_T *li = list_find(list, idxtv->vval.v_number);
2675
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2677 ++ectx.ec_stack.ga_len;
2678 }
2679 }
2680 break;
2681
2682 // start of ":try" block
2683 case ISN_TRY:
2684 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002685 trycmd_T *trycmd = NULL;
2686
Bram Moolenaar270d0382020-05-15 21:42:53 +02002687 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002688 goto failed;
2689 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2690 + ectx.ec_trystack.ga_len;
2691 ++ectx.ec_trystack.ga_len;
2692 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002693 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002694 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002695 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002696 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2697 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2698 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002699 }
2700 break;
2701
2702 case ISN_PUSHEXC:
2703 if (current_exception == NULL)
2704 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002706 iemsg("Evaluating catch while current_exception is NULL");
2707 goto failed;
2708 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002709 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002710 goto failed;
2711 tv = STACK_TV_BOT(0);
2712 ++ectx.ec_stack.ga_len;
2713 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002714 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002715 tv->vval.v_string = vim_strsave(
2716 (char_u *)current_exception->value);
2717 break;
2718
2719 case ISN_CATCH:
2720 {
2721 garray_T *trystack = &ectx.ec_trystack;
2722
Bram Moolenaar20a76292020-12-25 19:47:24 +01002723 if (restore_cmdmod)
2724 {
2725 cmdmod.cmod_filter_regmatch.regprog = NULL;
2726 undo_cmdmod(&cmdmod);
2727 cmdmod = save_cmdmod;
2728 restore_cmdmod = FALSE;
2729 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002730 if (trystack->ga_len > 0)
2731 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002732 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002733 + trystack->ga_len - 1;
2734 trycmd->tcd_caught = TRUE;
2735 }
2736 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002737 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 catch_exception(current_exception);
2739 }
2740 break;
2741
Bram Moolenaarc150c092021-02-13 15:02:46 +01002742 case ISN_TRYCONT:
2743 {
2744 garray_T *trystack = &ectx.ec_trystack;
2745 trycont_T *trycont = &iptr->isn_arg.trycont;
2746 int i;
2747 trycmd_T *trycmd;
2748 int iidx = trycont->tct_where;
2749
2750 if (trystack->ga_len < trycont->tct_levels)
2751 {
2752 siemsg("TRYCONT: expected %d levels, found %d",
2753 trycont->tct_levels, trystack->ga_len);
2754 goto failed;
2755 }
2756 // Make :endtry jump to any outer try block and the last
2757 // :endtry inside the loop to the loop start.
2758 for (i = trycont->tct_levels; i > 0; --i)
2759 {
2760 trycmd = ((trycmd_T *)trystack->ga_data)
2761 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002762 // Add one to tcd_cont to be able to jump to
2763 // instruction with index zero.
2764 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002765 iidx = trycmd->tcd_finally_idx == 0
2766 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002767 }
2768 // jump to :finally or :endtry of current try statement
2769 ectx.ec_iidx = iidx;
2770 }
2771 break;
2772
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002773 case ISN_FINALLY:
2774 {
2775 garray_T *trystack = &ectx.ec_trystack;
2776 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2777 + trystack->ga_len - 1;
2778
2779 // Reset the index to avoid a return statement jumps here
2780 // again.
2781 trycmd->tcd_finally_idx = 0;
2782 break;
2783 }
2784
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002785 // end of ":try" block
2786 case ISN_ENDTRY:
2787 {
2788 garray_T *trystack = &ectx.ec_trystack;
2789
2790 if (trystack->ga_len > 0)
2791 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002792 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002793
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002794 --trystack->ga_len;
2795 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002796 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002797 trycmd = ((trycmd_T *)trystack->ga_data)
2798 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002799 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002800 {
2801 // discard the exception
2802 if (caught_stack == current_exception)
2803 caught_stack = caught_stack->caught;
2804 discard_current_exception();
2805 }
2806
2807 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002808 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002809
2810 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2811 {
2812 --ectx.ec_stack.ga_len;
2813 clear_tv(STACK_TV_BOT(0));
2814 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002815 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002816 // handling :continue: jump to outer try block or
2817 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002818 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002819 }
2820 }
2821 break;
2822
2823 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002824 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002825 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002826
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002827 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2828 {
2829 // throwing an exception while using "silent!" causes
2830 // the function to abort but not display an error.
2831 tv = STACK_TV_BOT(-1);
2832 clear_tv(tv);
2833 tv->v_type = VAR_NUMBER;
2834 tv->vval.v_number = 0;
2835 goto done;
2836 }
2837 --ectx.ec_stack.ga_len;
2838 tv = STACK_TV_BOT(0);
2839 if (tv->vval.v_string == NULL
2840 || *skipwhite(tv->vval.v_string) == NUL)
2841 {
2842 vim_free(tv->vval.v_string);
2843 SOURCING_LNUM = iptr->isn_lnum;
2844 emsg(_(e_throw_with_empty_string));
2845 goto failed;
2846 }
2847
2848 // Inside a "catch" we need to first discard the caught
2849 // exception.
2850 if (trystack->ga_len > 0)
2851 {
2852 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2853 + trystack->ga_len - 1;
2854 if (trycmd->tcd_caught && current_exception != NULL)
2855 {
2856 // discard the exception
2857 if (caught_stack == current_exception)
2858 caught_stack = caught_stack->caught;
2859 discard_current_exception();
2860 trycmd->tcd_caught = FALSE;
2861 }
2862 }
2863
2864 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2865 == FAIL)
2866 {
2867 vim_free(tv->vval.v_string);
2868 goto failed;
2869 }
2870 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002871 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002872 break;
2873
2874 // compare with special values
2875 case ISN_COMPAREBOOL:
2876 case ISN_COMPARESPECIAL:
2877 {
2878 typval_T *tv1 = STACK_TV_BOT(-2);
2879 typval_T *tv2 = STACK_TV_BOT(-1);
2880 varnumber_T arg1 = tv1->vval.v_number;
2881 varnumber_T arg2 = tv2->vval.v_number;
2882 int res;
2883
2884 switch (iptr->isn_arg.op.op_type)
2885 {
2886 case EXPR_EQUAL: res = arg1 == arg2; break;
2887 case EXPR_NEQUAL: res = arg1 != arg2; break;
2888 default: res = 0; break;
2889 }
2890
2891 --ectx.ec_stack.ga_len;
2892 tv1->v_type = VAR_BOOL;
2893 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2894 }
2895 break;
2896
2897 // Operation with two number arguments
2898 case ISN_OPNR:
2899 case ISN_COMPARENR:
2900 {
2901 typval_T *tv1 = STACK_TV_BOT(-2);
2902 typval_T *tv2 = STACK_TV_BOT(-1);
2903 varnumber_T arg1 = tv1->vval.v_number;
2904 varnumber_T arg2 = tv2->vval.v_number;
2905 varnumber_T res;
2906
2907 switch (iptr->isn_arg.op.op_type)
2908 {
2909 case EXPR_MULT: res = arg1 * arg2; break;
2910 case EXPR_DIV: res = arg1 / arg2; break;
2911 case EXPR_REM: res = arg1 % arg2; break;
2912 case EXPR_SUB: res = arg1 - arg2; break;
2913 case EXPR_ADD: res = arg1 + arg2; break;
2914
2915 case EXPR_EQUAL: res = arg1 == arg2; break;
2916 case EXPR_NEQUAL: res = arg1 != arg2; break;
2917 case EXPR_GREATER: res = arg1 > arg2; break;
2918 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2919 case EXPR_SMALLER: res = arg1 < arg2; break;
2920 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2921 default: res = 0; break;
2922 }
2923
2924 --ectx.ec_stack.ga_len;
2925 if (iptr->isn_type == ISN_COMPARENR)
2926 {
2927 tv1->v_type = VAR_BOOL;
2928 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2929 }
2930 else
2931 tv1->vval.v_number = res;
2932 }
2933 break;
2934
2935 // Computation with two float arguments
2936 case ISN_OPFLOAT:
2937 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002938#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002939 {
2940 typval_T *tv1 = STACK_TV_BOT(-2);
2941 typval_T *tv2 = STACK_TV_BOT(-1);
2942 float_T arg1 = tv1->vval.v_float;
2943 float_T arg2 = tv2->vval.v_float;
2944 float_T res = 0;
2945 int cmp = FALSE;
2946
2947 switch (iptr->isn_arg.op.op_type)
2948 {
2949 case EXPR_MULT: res = arg1 * arg2; break;
2950 case EXPR_DIV: res = arg1 / arg2; break;
2951 case EXPR_SUB: res = arg1 - arg2; break;
2952 case EXPR_ADD: res = arg1 + arg2; break;
2953
2954 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2955 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2956 case EXPR_GREATER: cmp = arg1 > arg2; break;
2957 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2958 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2959 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2960 default: cmp = 0; break;
2961 }
2962 --ectx.ec_stack.ga_len;
2963 if (iptr->isn_type == ISN_COMPAREFLOAT)
2964 {
2965 tv1->v_type = VAR_BOOL;
2966 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2967 }
2968 else
2969 tv1->vval.v_float = res;
2970 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002971#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002972 break;
2973
2974 case ISN_COMPARELIST:
2975 {
2976 typval_T *tv1 = STACK_TV_BOT(-2);
2977 typval_T *tv2 = STACK_TV_BOT(-1);
2978 list_T *arg1 = tv1->vval.v_list;
2979 list_T *arg2 = tv2->vval.v_list;
2980 int cmp = FALSE;
2981 int ic = iptr->isn_arg.op.op_ic;
2982
2983 switch (iptr->isn_arg.op.op_type)
2984 {
2985 case EXPR_EQUAL: cmp =
2986 list_equal(arg1, arg2, ic, FALSE); break;
2987 case EXPR_NEQUAL: cmp =
2988 !list_equal(arg1, arg2, ic, FALSE); break;
2989 case EXPR_IS: cmp = arg1 == arg2; break;
2990 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2991 default: cmp = 0; break;
2992 }
2993 --ectx.ec_stack.ga_len;
2994 clear_tv(tv1);
2995 clear_tv(tv2);
2996 tv1->v_type = VAR_BOOL;
2997 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2998 }
2999 break;
3000
3001 case ISN_COMPAREBLOB:
3002 {
3003 typval_T *tv1 = STACK_TV_BOT(-2);
3004 typval_T *tv2 = STACK_TV_BOT(-1);
3005 blob_T *arg1 = tv1->vval.v_blob;
3006 blob_T *arg2 = tv2->vval.v_blob;
3007 int cmp = FALSE;
3008
3009 switch (iptr->isn_arg.op.op_type)
3010 {
3011 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3012 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3013 case EXPR_IS: cmp = arg1 == arg2; break;
3014 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3015 default: cmp = 0; break;
3016 }
3017 --ectx.ec_stack.ga_len;
3018 clear_tv(tv1);
3019 clear_tv(tv2);
3020 tv1->v_type = VAR_BOOL;
3021 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3022 }
3023 break;
3024
3025 // TODO: handle separately
3026 case ISN_COMPARESTRING:
3027 case ISN_COMPAREDICT:
3028 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003029 case ISN_COMPAREANY:
3030 {
3031 typval_T *tv1 = STACK_TV_BOT(-2);
3032 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003033 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003034 int ic = iptr->isn_arg.op.op_ic;
3035
Bram Moolenaareb26f432020-09-14 16:50:05 +02003036 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003037 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003038 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003039 --ectx.ec_stack.ga_len;
3040 }
3041 break;
3042
3043 case ISN_ADDLIST:
3044 case ISN_ADDBLOB:
3045 {
3046 typval_T *tv1 = STACK_TV_BOT(-2);
3047 typval_T *tv2 = STACK_TV_BOT(-1);
3048
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003049 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003050 if (iptr->isn_type == ISN_ADDLIST)
3051 eval_addlist(tv1, tv2);
3052 else
3053 eval_addblob(tv1, tv2);
3054 clear_tv(tv2);
3055 --ectx.ec_stack.ga_len;
3056 }
3057 break;
3058
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003059 case ISN_LISTAPPEND:
3060 {
3061 typval_T *tv1 = STACK_TV_BOT(-2);
3062 typval_T *tv2 = STACK_TV_BOT(-1);
3063 list_T *l = tv1->vval.v_list;
3064
3065 // add an item to a list
3066 if (l == NULL)
3067 {
3068 SOURCING_LNUM = iptr->isn_lnum;
3069 emsg(_(e_cannot_add_to_null_list));
3070 goto on_error;
3071 }
3072 if (list_append_tv(l, tv2) == FAIL)
3073 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003074 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003075 --ectx.ec_stack.ga_len;
3076 }
3077 break;
3078
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003079 case ISN_BLOBAPPEND:
3080 {
3081 typval_T *tv1 = STACK_TV_BOT(-2);
3082 typval_T *tv2 = STACK_TV_BOT(-1);
3083 blob_T *b = tv1->vval.v_blob;
3084 int error = FALSE;
3085 varnumber_T n;
3086
3087 // add a number to a blob
3088 if (b == NULL)
3089 {
3090 SOURCING_LNUM = iptr->isn_lnum;
3091 emsg(_(e_cannot_add_to_null_blob));
3092 goto on_error;
3093 }
3094 n = tv_get_number_chk(tv2, &error);
3095 if (error)
3096 goto on_error;
3097 ga_append(&b->bv_ga, (int)n);
3098 --ectx.ec_stack.ga_len;
3099 }
3100 break;
3101
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003102 // Computation with two arguments of unknown type
3103 case ISN_OPANY:
3104 {
3105 typval_T *tv1 = STACK_TV_BOT(-2);
3106 typval_T *tv2 = STACK_TV_BOT(-1);
3107 varnumber_T n1, n2;
3108#ifdef FEAT_FLOAT
3109 float_T f1 = 0, f2 = 0;
3110#endif
3111 int error = FALSE;
3112
3113 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3114 {
3115 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3116 {
3117 eval_addlist(tv1, tv2);
3118 clear_tv(tv2);
3119 --ectx.ec_stack.ga_len;
3120 break;
3121 }
3122 else if (tv1->v_type == VAR_BLOB
3123 && tv2->v_type == VAR_BLOB)
3124 {
3125 eval_addblob(tv1, tv2);
3126 clear_tv(tv2);
3127 --ectx.ec_stack.ga_len;
3128 break;
3129 }
3130 }
3131#ifdef FEAT_FLOAT
3132 if (tv1->v_type == VAR_FLOAT)
3133 {
3134 f1 = tv1->vval.v_float;
3135 n1 = 0;
3136 }
3137 else
3138#endif
3139 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003140 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003141 n1 = tv_get_number_chk(tv1, &error);
3142 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003143 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003144#ifdef FEAT_FLOAT
3145 if (tv2->v_type == VAR_FLOAT)
3146 f1 = n1;
3147#endif
3148 }
3149#ifdef FEAT_FLOAT
3150 if (tv2->v_type == VAR_FLOAT)
3151 {
3152 f2 = tv2->vval.v_float;
3153 n2 = 0;
3154 }
3155 else
3156#endif
3157 {
3158 n2 = tv_get_number_chk(tv2, &error);
3159 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003160 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003161#ifdef FEAT_FLOAT
3162 if (tv1->v_type == VAR_FLOAT)
3163 f2 = n2;
3164#endif
3165 }
3166#ifdef FEAT_FLOAT
3167 // if there is a float on either side the result is a float
3168 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3169 {
3170 switch (iptr->isn_arg.op.op_type)
3171 {
3172 case EXPR_MULT: f1 = f1 * f2; break;
3173 case EXPR_DIV: f1 = f1 / f2; break;
3174 case EXPR_SUB: f1 = f1 - f2; break;
3175 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003176 default: SOURCING_LNUM = iptr->isn_lnum;
3177 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003178 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003179 }
3180 clear_tv(tv1);
3181 clear_tv(tv2);
3182 tv1->v_type = VAR_FLOAT;
3183 tv1->vval.v_float = f1;
3184 --ectx.ec_stack.ga_len;
3185 }
3186 else
3187#endif
3188 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003189 int failed = FALSE;
3190
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003191 switch (iptr->isn_arg.op.op_type)
3192 {
3193 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003194 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3195 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003196 goto on_error;
3197 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003198 case EXPR_SUB: n1 = n1 - n2; break;
3199 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003200 default: n1 = num_modulus(n1, n2, &failed);
3201 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003202 goto on_error;
3203 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003204 }
3205 clear_tv(tv1);
3206 clear_tv(tv2);
3207 tv1->v_type = VAR_NUMBER;
3208 tv1->vval.v_number = n1;
3209 --ectx.ec_stack.ga_len;
3210 }
3211 }
3212 break;
3213
3214 case ISN_CONCAT:
3215 {
3216 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3217 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3218 char_u *res;
3219
3220 res = concat_str(str1, str2);
3221 clear_tv(STACK_TV_BOT(-2));
3222 clear_tv(STACK_TV_BOT(-1));
3223 --ectx.ec_stack.ga_len;
3224 STACK_TV_BOT(-1)->vval.v_string = res;
3225 }
3226 break;
3227
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003228 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003229 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003230 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003231 int is_slice = iptr->isn_type == ISN_STRSLICE;
3232 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003233 char_u *res;
3234
3235 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003236 // string slice: string is at stack-3, first index at
3237 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003238 if (is_slice)
3239 {
3240 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003241 n1 = tv->vval.v_number;
3242 }
3243
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003244 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003245 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003246
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003247 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003248 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003249 if (is_slice)
3250 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003251 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003252 else
3253 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003254 // single character (including composing characters).
3255 // If the index is too big or negative the result is
3256 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003257 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003258 vim_free(tv->vval.v_string);
3259 tv->vval.v_string = res;
3260 }
3261 break;
3262
3263 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003264 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003265 {
Bram Moolenaared591872020-08-15 22:14:53 +02003266 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003267 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003268 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003269
3270 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003271 // list slice: list is at stack-3, indexes at stack-2 and
3272 // stack-1
3273 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003274 list = tv->vval.v_list;
3275
3276 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003277 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003278 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003279
3280 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003281 {
Bram Moolenaared591872020-08-15 22:14:53 +02003282 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003283 n1 = tv->vval.v_number;
3284 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003285 }
Bram Moolenaared591872020-08-15 22:14:53 +02003286
3287 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003288 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003289 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003290 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3291 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003292 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 }
3294 break;
3295
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003296 case ISN_ANYINDEX:
3297 case ISN_ANYSLICE:
3298 {
3299 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3300 typval_T *var1, *var2;
3301 int res;
3302
3303 // index: composite is at stack-2, index at stack-1
3304 // slice: composite is at stack-3, indexes at stack-2 and
3305 // stack-1
3306 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003307 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003308 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3309 goto on_error;
3310 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3311 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003312 res = eval_index_inner(tv, is_slice, var1, var2,
3313 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003314 clear_tv(var1);
3315 if (is_slice)
3316 clear_tv(var2);
3317 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3318 if (res == FAIL)
3319 goto on_error;
3320 }
3321 break;
3322
Bram Moolenaar9af78762020-06-16 11:34:42 +02003323 case ISN_SLICE:
3324 {
3325 list_T *list;
3326 int count = iptr->isn_arg.number;
3327
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003328 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003329 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003330 list = tv->vval.v_list;
3331
3332 // no error for short list, expect it to be checked earlier
3333 if (list != NULL && list->lv_len >= count)
3334 {
3335 list_T *newlist = list_slice(list,
3336 count, list->lv_len - 1);
3337
3338 if (newlist != NULL)
3339 {
3340 list_unref(list);
3341 tv->vval.v_list = newlist;
3342 ++newlist->lv_refcount;
3343 }
3344 }
3345 }
3346 break;
3347
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003348 case ISN_GETITEM:
3349 {
3350 listitem_T *li;
3351 int index = iptr->isn_arg.number;
3352
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003353 // Get list item: list is at stack-1, push item.
3354 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003355 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003356 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003357
3358 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3359 goto failed;
3360 ++ectx.ec_stack.ga_len;
3361 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003362
3363 // Useful when used in unpack assignment. Reset at
3364 // ISN_DROP.
3365 where.wt_index = index + 1;
3366 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003367 }
3368 break;
3369
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003370 case ISN_MEMBER:
3371 {
3372 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003373 char_u *key;
3374 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003375 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003376
3377 // dict member: dict is at stack-2, key at stack-1
3378 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003379 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003380 dict = tv->vval.v_dict;
3381
3382 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003383 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003384 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003385 if (key == NULL)
3386 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003387
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003388 if ((di = dict_find(dict, key, -1)) == NULL)
3389 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003390 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003391 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003392
3393 // If :silent! is used we will continue, make sure the
3394 // stack contents makes sense.
3395 clear_tv(tv);
3396 --ectx.ec_stack.ga_len;
3397 tv = STACK_TV_BOT(-1);
3398 clear_tv(tv);
3399 tv->v_type = VAR_NUMBER;
3400 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003401 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003402 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003403 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003404 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003405 // Clear the dict only after getting the item, to avoid
3406 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003407 tv = STACK_TV_BOT(-1);
3408 temp_tv = *tv;
3409 copy_tv(&di->di_tv, tv);
3410 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003411 }
3412 break;
3413
3414 // dict member with string key
3415 case ISN_STRINGMEMBER:
3416 {
3417 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003418 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003419 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003420
3421 tv = STACK_TV_BOT(-1);
3422 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3423 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003424 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003425 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003426 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003427 }
3428 dict = tv->vval.v_dict;
3429
3430 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3431 == NULL)
3432 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003433 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003434 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003435 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003436 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003437 // Clear the dict after getting the item, to avoid that it
3438 // make the item invalid.
3439 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003440 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003441 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 }
3443 break;
3444
3445 case ISN_NEGATENR:
3446 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003447 if (tv->v_type != VAR_NUMBER
3448#ifdef FEAT_FLOAT
3449 && tv->v_type != VAR_FLOAT
3450#endif
3451 )
3452 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003453 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003454 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003455 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003456 }
3457#ifdef FEAT_FLOAT
3458 if (tv->v_type == VAR_FLOAT)
3459 tv->vval.v_float = -tv->vval.v_float;
3460 else
3461#endif
3462 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003463 break;
3464
3465 case ISN_CHECKNR:
3466 {
3467 int error = FALSE;
3468
3469 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003470 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003472 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003473 (void)tv_get_number_chk(tv, &error);
3474 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003475 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003476 }
3477 break;
3478
3479 case ISN_CHECKTYPE:
3480 {
3481 checktype_T *ct = &iptr->isn_arg.type;
3482
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003483 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003484 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003485 if (!where.wt_variable)
3486 where.wt_index = ct->ct_arg_idx;
3487 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003488 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003489 if (!where.wt_variable)
3490 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003491
3492 // number 0 is FALSE, number 1 is TRUE
3493 if (tv->v_type == VAR_NUMBER
3494 && ct->ct_type->tt_type == VAR_BOOL
3495 && (tv->vval.v_number == 0
3496 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003497 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003498 tv->v_type = VAR_BOOL;
3499 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003500 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003501 }
3502 }
3503 break;
3504
Bram Moolenaar9af78762020-06-16 11:34:42 +02003505 case ISN_CHECKLEN:
3506 {
3507 int min_len = iptr->isn_arg.checklen.cl_min_len;
3508 list_T *list = NULL;
3509
3510 tv = STACK_TV_BOT(-1);
3511 if (tv->v_type == VAR_LIST)
3512 list = tv->vval.v_list;
3513 if (list == NULL || list->lv_len < min_len
3514 || (list->lv_len > min_len
3515 && !iptr->isn_arg.checklen.cl_more_OK))
3516 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003517 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003518 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003519 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003520 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003521 }
3522 }
3523 break;
3524
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003525 case ISN_SETTYPE:
3526 {
3527 checktype_T *ct = &iptr->isn_arg.type;
3528
3529 tv = STACK_TV_BOT(-1);
3530 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3531 {
3532 free_type(tv->vval.v_dict->dv_type);
3533 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3534 }
3535 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3536 {
3537 free_type(tv->vval.v_list->lv_type);
3538 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3539 }
3540 }
3541 break;
3542
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003543 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003544 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003545 {
3546 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003547 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003548
3549 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003550 if (iptr->isn_type == ISN_2BOOL)
3551 {
3552 n = tv2bool(tv);
3553 if (iptr->isn_arg.number) // invert
3554 n = !n;
3555 }
3556 else
3557 {
3558 SOURCING_LNUM = iptr->isn_lnum;
3559 n = tv_get_bool_chk(tv, &error);
3560 if (error)
3561 goto on_error;
3562 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003563 clear_tv(tv);
3564 tv->v_type = VAR_BOOL;
3565 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3566 }
3567 break;
3568
3569 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003570 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003571 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003572 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3573 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3574 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003575 break;
3576
Bram Moolenaar08597872020-12-10 19:43:40 +01003577 case ISN_RANGE:
3578 {
3579 exarg_T ea;
3580 char *errormsg;
3581
Bram Moolenaarece0b872021-01-08 20:40:45 +01003582 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003583 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003584 ea.addr_type = ADDR_LINES;
3585 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003586 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003587 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003588 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003589
3590 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3591 goto failed;
3592 ++ectx.ec_stack.ga_len;
3593 tv = STACK_TV_BOT(-1);
3594 tv->v_type = VAR_NUMBER;
3595 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003596 if (ea.addr_count == 0)
3597 tv->vval.v_number = curwin->w_cursor.lnum;
3598 else
3599 tv->vval.v_number = ea.line2;
3600 }
3601 break;
3602
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003603 case ISN_PUT:
3604 {
3605 int regname = iptr->isn_arg.put.put_regname;
3606 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3607 char_u *expr = NULL;
3608 int dir = FORWARD;
3609
Bram Moolenaar08597872020-12-10 19:43:40 +01003610 if (lnum < -2)
3611 {
3612 // line number was put on the stack by ISN_RANGE
3613 tv = STACK_TV_BOT(-1);
3614 curwin->w_cursor.lnum = tv->vval.v_number;
3615 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3616 dir = BACKWARD;
3617 --ectx.ec_stack.ga_len;
3618 }
3619 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003620 // :put! above cursor
3621 dir = BACKWARD;
3622 else if (lnum >= 0)
3623 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003624
3625 if (regname == '=')
3626 {
3627 tv = STACK_TV_BOT(-1);
3628 if (tv->v_type == VAR_STRING)
3629 expr = tv->vval.v_string;
3630 else
3631 {
3632 expr = typval2string(tv, TRUE); // allocates value
3633 clear_tv(tv);
3634 }
3635 --ectx.ec_stack.ga_len;
3636 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003637 check_cursor();
3638 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3639 vim_free(expr);
3640 }
3641 break;
3642
Bram Moolenaar02194d22020-10-24 23:08:38 +02003643 case ISN_CMDMOD:
3644 save_cmdmod = cmdmod;
3645 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003646 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003647 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3648 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003649 break;
3650
Bram Moolenaar02194d22020-10-24 23:08:38 +02003651 case ISN_CMDMOD_REV:
3652 // filter regprog is owned by the instruction, don't free it
3653 cmdmod.cmod_filter_regmatch.regprog = NULL;
3654 undo_cmdmod(&cmdmod);
3655 cmdmod = save_cmdmod;
3656 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003657 break;
3658
Bram Moolenaar792f7862020-11-23 08:31:18 +01003659 case ISN_UNPACK:
3660 {
3661 int count = iptr->isn_arg.unpack.unp_count;
3662 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3663 list_T *l;
3664 listitem_T *li;
3665 int i;
3666
3667 // Check there is a valid list to unpack.
3668 tv = STACK_TV_BOT(-1);
3669 if (tv->v_type != VAR_LIST)
3670 {
3671 SOURCING_LNUM = iptr->isn_lnum;
3672 emsg(_(e_for_argument_must_be_sequence_of_lists));
3673 goto on_error;
3674 }
3675 l = tv->vval.v_list;
3676 if (l == NULL
3677 || l->lv_len < (semicolon ? count - 1 : count))
3678 {
3679 SOURCING_LNUM = iptr->isn_lnum;
3680 emsg(_(e_list_value_does_not_have_enough_items));
3681 goto on_error;
3682 }
3683 else if (!semicolon && l->lv_len > count)
3684 {
3685 SOURCING_LNUM = iptr->isn_lnum;
3686 emsg(_(e_list_value_has_more_items_than_targets));
3687 goto on_error;
3688 }
3689
3690 CHECK_LIST_MATERIALIZE(l);
3691 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3692 goto failed;
3693 ectx.ec_stack.ga_len += count - 1;
3694
3695 // Variable after semicolon gets a list with the remaining
3696 // items.
3697 if (semicolon)
3698 {
3699 list_T *rem_list =
3700 list_alloc_with_items(l->lv_len - count + 1);
3701
3702 if (rem_list == NULL)
3703 goto failed;
3704 tv = STACK_TV_BOT(-count);
3705 tv->vval.v_list = rem_list;
3706 ++rem_list->lv_refcount;
3707 tv->v_lock = 0;
3708 li = l->lv_first;
3709 for (i = 0; i < count - 1; ++i)
3710 li = li->li_next;
3711 for (i = 0; li != NULL; ++i)
3712 {
3713 list_set_item(rem_list, i, &li->li_tv);
3714 li = li->li_next;
3715 }
3716 --count;
3717 }
3718
3719 // Produce the values in reverse order, first item last.
3720 li = l->lv_first;
3721 for (i = 0; i < count; ++i)
3722 {
3723 tv = STACK_TV_BOT(-i - 1);
3724 copy_tv(&li->li_tv, tv);
3725 li = li->li_next;
3726 }
3727
3728 list_unref(l);
3729 }
3730 break;
3731
Bram Moolenaarb2049902021-01-24 12:53:53 +01003732 case ISN_PROF_START:
3733 case ISN_PROF_END:
3734 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003735#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003736 funccall_T cookie;
3737 ufunc_T *cur_ufunc =
3738 (((dfunc_T *)def_functions.ga_data)
3739 + ectx.ec_dfunc_idx)->df_ufunc;
3740
3741 cookie.func = cur_ufunc;
3742 if (iptr->isn_type == ISN_PROF_START)
3743 {
3744 func_line_start(&cookie, iptr->isn_lnum);
3745 // if we get here the instruction is executed
3746 func_line_exec(&cookie);
3747 }
3748 else
3749 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003750#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003751 }
3752 break;
3753
Bram Moolenaar389df252020-07-09 21:20:47 +02003754 case ISN_SHUFFLE:
3755 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003756 typval_T tmp_tv;
3757 int item = iptr->isn_arg.shuffle.shfl_item;
3758 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003759
3760 tmp_tv = *STACK_TV_BOT(-item);
3761 for ( ; up > 0 && item > 1; --up)
3762 {
3763 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3764 --item;
3765 }
3766 *STACK_TV_BOT(-item) = tmp_tv;
3767 }
3768 break;
3769
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003770 case ISN_DROP:
3771 --ectx.ec_stack.ga_len;
3772 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003773 where.wt_index = 0;
3774 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003775 break;
3776 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003777 continue;
3778
Bram Moolenaard032f342020-07-18 18:13:02 +02003779func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003780 // Restore previous function. If the frame pointer is where we started
3781 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003782 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003783 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003784
Bram Moolenaard032f342020-07-18 18:13:02 +02003785 if (func_return(&ectx) == FAIL)
3786 // only fails when out of memory
3787 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003788 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003789
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003790on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003791 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003792 // If "emsg_silent" is set then ignore the error, unless it was set
3793 // when calling the function.
3794 if (did_emsg_cumul + did_emsg == did_emsg_before
3795 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003796 {
3797 // If a sequence of instructions causes an error while ":silent!"
3798 // was used, restore the stack length and jump ahead to restoring
3799 // the cmdmod.
3800 if (restore_cmdmod)
3801 {
3802 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3803 {
3804 --ectx.ec_stack.ga_len;
3805 clear_tv(STACK_TV_BOT(0));
3806 }
3807 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3808 ++ectx.ec_iidx;
3809 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003810 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003811 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003812on_fatal_error:
3813 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003814 // If we are not inside a try-catch started here, abort execution.
3815 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003816 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003817 }
3818
3819done:
3820 // function finished, get result from the stack.
3821 tv = STACK_TV_BOT(-1);
3822 *rettv = *tv;
3823 tv->v_type = VAR_UNKNOWN;
3824 ret = OK;
3825
3826failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003827 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003828 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003829 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003830
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003831 // Deal with any remaining closures, they may be in use somewhere.
3832 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003833 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003834 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003835 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3836 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003837
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003838 estack_pop();
3839 current_sctx = save_current_sctx;
3840
Bram Moolenaar352134b2020-10-17 22:04:08 +02003841 if (*msg_list != NULL && saved_msg_list != NULL)
3842 {
3843 msglist_T **plist = saved_msg_list;
3844
3845 // Append entries from the current msg_list (uncaught exceptions) to
3846 // the saved msg_list.
3847 while (*plist != NULL)
3848 plist = &(*plist)->next;
3849
3850 *plist = *msg_list;
3851 }
3852 msg_list = saved_msg_list;
3853
Bram Moolenaar02194d22020-10-24 23:08:38 +02003854 if (restore_cmdmod)
3855 {
3856 cmdmod.cmod_filter_regmatch.regprog = NULL;
3857 undo_cmdmod(&cmdmod);
3858 cmdmod = save_cmdmod;
3859 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003860 emsg_silent_def = save_emsg_silent_def;
3861 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003862
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003863failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003864 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003865 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3866 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003867
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003868 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003869 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003870
Bram Moolenaar0186e582021-01-10 18:33:11 +01003871 while (ectx.ec_outer != NULL)
3872 {
3873 outer_T *up = ectx.ec_outer->out_up_is_copy
3874 ? NULL : ectx.ec_outer->out_up;
3875
3876 vim_free(ectx.ec_outer);
3877 ectx.ec_outer = up;
3878 }
3879
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003880 // Not sure if this is necessary.
3881 suppress_errthrow = save_suppress_errthrow;
3882
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003883 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003884 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003885 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003886 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003887 return ret;
3888}
3889
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003890/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003891 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003892 * We don't really need this at runtime, but we do have tests that require it,
3893 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003894 */
3895 void
3896ex_disassemble(exarg_T *eap)
3897{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003898 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003899 char_u *fname;
3900 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901 dfunc_T *dfunc;
3902 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003903 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003904 int current;
3905 int line_idx = 0;
3906 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003907 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003908
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003909 if (STRNCMP(arg, "<lambda>", 8) == 0)
3910 {
3911 arg += 8;
3912 (void)getdigits(&arg);
3913 fname = vim_strnsave(eap->arg, arg - eap->arg);
3914 }
3915 else
3916 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003917 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003918 if (fname == NULL)
3919 {
3920 semsg(_(e_invarg2), eap->arg);
3921 return;
3922 }
3923
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003924 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003925 if (ufunc == NULL)
3926 {
3927 char_u *p = untrans_function_name(fname);
3928
3929 if (p != NULL)
3930 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003931 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003932 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003933 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003934 if (ufunc == NULL)
3935 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003936 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003937 return;
3938 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003939 if (func_needs_compiling(ufunc, eap->forceit)
3940 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003941 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003942 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003943 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003944 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 return;
3946 }
3947 if (ufunc->uf_name_exp != NULL)
3948 msg((char *)ufunc->uf_name_exp);
3949 else
3950 msg((char *)ufunc->uf_name);
3951
3952 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003953#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003954 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3955 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3956 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003957#else
3958 instr = dfunc->df_instr;
3959 instr_count = dfunc->df_instr_count;
3960#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003961 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003962 {
3963 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003964 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003965
3966 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3967 {
3968 if (current > prev_current)
3969 {
3970 msg_puts("\n\n");
3971 prev_current = current;
3972 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003973 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3974 if (line != NULL)
3975 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976 }
3977
3978 switch (iptr->isn_type)
3979 {
3980 case ISN_EXEC:
3981 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3982 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003983 case ISN_EXECCONCAT:
3984 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003985 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003986 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 case ISN_ECHO:
3988 {
3989 echo_T *echo = &iptr->isn_arg.echo;
3990
3991 smsg("%4d %s %d", current,
3992 echo->echo_with_white ? "ECHO" : "ECHON",
3993 echo->echo_count);
3994 }
3995 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01003996 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01003997 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003998 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01003999 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004000 case ISN_ECHOMSG:
4001 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004002 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004003 break;
4004 case ISN_ECHOERR:
4005 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004006 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004007 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004008 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004009 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004010 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004011 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004012 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004013 + STACK_FRAME_SIZE));
4014 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004015 smsg("%4d LOAD $%lld", current,
4016 (varnumber_T)(iptr->isn_arg.number));
4017 }
4018 break;
4019 case ISN_LOADOUTER:
4020 {
4021 if (iptr->isn_arg.number < 0)
4022 smsg("%4d LOADOUTER level %d arg[%d]", current,
4023 iptr->isn_arg.outer.outer_depth,
4024 iptr->isn_arg.outer.outer_idx
4025 + STACK_FRAME_SIZE);
4026 else
4027 smsg("%4d LOADOUTER level %d $%d", current,
4028 iptr->isn_arg.outer.outer_depth,
4029 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004030 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004031 break;
4032 case ISN_LOADV:
4033 smsg("%4d LOADV v:%s", current,
4034 get_vim_var_name(iptr->isn_arg.number));
4035 break;
4036 case ISN_LOADSCRIPT:
4037 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004038 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4039 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004040 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004041 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004043 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4044 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004045 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004046 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004047 }
4048 break;
4049 case ISN_LOADS:
4050 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004051 scriptitem_T *si = SCRIPT_ITEM(
4052 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
4054 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004055 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004056 }
4057 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004058 case ISN_LOADAUTO:
4059 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4060 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004061 case ISN_LOADG:
4062 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4063 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004064 case ISN_LOADB:
4065 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4066 break;
4067 case ISN_LOADW:
4068 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4069 break;
4070 case ISN_LOADT:
4071 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4072 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004073 case ISN_LOADGDICT:
4074 smsg("%4d LOAD g:", current);
4075 break;
4076 case ISN_LOADBDICT:
4077 smsg("%4d LOAD b:", current);
4078 break;
4079 case ISN_LOADWDICT:
4080 smsg("%4d LOAD w:", current);
4081 break;
4082 case ISN_LOADTDICT:
4083 smsg("%4d LOAD t:", current);
4084 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004085 case ISN_LOADOPT:
4086 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4087 break;
4088 case ISN_LOADENV:
4089 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4090 break;
4091 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004092 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004093 break;
4094
4095 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004096 if (iptr->isn_arg.number < 0)
4097 smsg("%4d STORE arg[%lld]", current,
4098 iptr->isn_arg.number + STACK_FRAME_SIZE);
4099 else
4100 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4101 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004102 case ISN_STOREOUTER:
4103 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004104 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004105 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4106 iptr->isn_arg.outer.outer_depth,
4107 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004108 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004109 smsg("%4d STOREOUTER level %d $%d", current,
4110 iptr->isn_arg.outer.outer_depth,
4111 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004112 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004113 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004114 case ISN_STOREV:
4115 smsg("%4d STOREV v:%s", current,
4116 get_vim_var_name(iptr->isn_arg.number));
4117 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004118 case ISN_STOREAUTO:
4119 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4120 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004121 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004122 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4123 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004124 case ISN_STOREB:
4125 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4126 break;
4127 case ISN_STOREW:
4128 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4129 break;
4130 case ISN_STORET:
4131 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4132 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004133 case ISN_STORES:
4134 {
4135 scriptitem_T *si = SCRIPT_ITEM(
4136 iptr->isn_arg.loadstore.ls_sid);
4137
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004138 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004139 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004140 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004141 break;
4142 case ISN_STORESCRIPT:
4143 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004144 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4145 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004146 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004147 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004148
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004149 smsg("%4d STORESCRIPT %s-%d in %s", current,
4150 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004151 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004152 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004153 }
4154 break;
4155 case ISN_STOREOPT:
4156 smsg("%4d STOREOPT &%s", current,
4157 iptr->isn_arg.storeopt.so_name);
4158 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004159 case ISN_STOREENV:
4160 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4161 break;
4162 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004163 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004164 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004165 case ISN_STORENR:
4166 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004167 iptr->isn_arg.storenr.stnr_val,
4168 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004169 break;
4170
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004171 case ISN_STOREINDEX:
4172 switch (iptr->isn_arg.vartype)
4173 {
4174 case VAR_LIST:
4175 smsg("%4d STORELIST", current);
4176 break;
4177 case VAR_DICT:
4178 smsg("%4d STOREDICT", current);
4179 break;
4180 case VAR_ANY:
4181 smsg("%4d STOREINDEX", current);
4182 break;
4183 default: break;
4184 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004185 break;
4186
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004187 // constants
4188 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004189 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004190 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004191 break;
4192 case ISN_PUSHBOOL:
4193 case ISN_PUSHSPEC:
4194 smsg("%4d PUSH %s", current,
4195 get_var_special_name(iptr->isn_arg.number));
4196 break;
4197 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004198#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004199 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004200#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004201 break;
4202 case ISN_PUSHS:
4203 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4204 break;
4205 case ISN_PUSHBLOB:
4206 {
4207 char_u *r;
4208 char_u numbuf[NUMBUFLEN];
4209 char_u *tofree;
4210
4211 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004212 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004213 vim_free(tofree);
4214 }
4215 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004216 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004217 {
4218 char *name = (char *)iptr->isn_arg.string;
4219
4220 smsg("%4d PUSHFUNC \"%s\"", current,
4221 name == NULL ? "[none]" : name);
4222 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004223 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004224 case ISN_PUSHCHANNEL:
4225#ifdef FEAT_JOB_CHANNEL
4226 {
4227 channel_T *channel = iptr->isn_arg.channel;
4228
4229 smsg("%4d PUSHCHANNEL %d", current,
4230 channel == NULL ? 0 : channel->ch_id);
4231 }
4232#endif
4233 break;
4234 case ISN_PUSHJOB:
4235#ifdef FEAT_JOB_CHANNEL
4236 {
4237 typval_T tv;
4238 char_u *name;
4239
4240 tv.v_type = VAR_JOB;
4241 tv.vval.v_job = iptr->isn_arg.job;
4242 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004243 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004244 }
4245#endif
4246 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004247 case ISN_PUSHEXC:
4248 smsg("%4d PUSH v:exception", current);
4249 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004250 case ISN_UNLET:
4251 smsg("%4d UNLET%s %s", current,
4252 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4253 iptr->isn_arg.unlet.ul_name);
4254 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004255 case ISN_UNLETENV:
4256 smsg("%4d UNLETENV%s $%s", current,
4257 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4258 iptr->isn_arg.unlet.ul_name);
4259 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004260 case ISN_UNLETINDEX:
4261 smsg("%4d UNLETINDEX", current);
4262 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004263 case ISN_UNLETRANGE:
4264 smsg("%4d UNLETRANGE", current);
4265 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004266 case ISN_LOCKCONST:
4267 smsg("%4d LOCKCONST", current);
4268 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004269 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004270 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004271 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004272 break;
4273 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004274 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004275 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004276 break;
4277
4278 // function call
4279 case ISN_BCALL:
4280 {
4281 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4282
4283 smsg("%4d BCALL %s(argc %d)", current,
4284 internal_func_name(cbfunc->cbf_idx),
4285 cbfunc->cbf_argcount);
4286 }
4287 break;
4288 case ISN_DCALL:
4289 {
4290 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4291 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4292 + cdfunc->cdf_idx;
4293
4294 smsg("%4d DCALL %s(argc %d)", current,
4295 df->df_ufunc->uf_name_exp != NULL
4296 ? df->df_ufunc->uf_name_exp
4297 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4298 }
4299 break;
4300 case ISN_UCALL:
4301 {
4302 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4303
4304 smsg("%4d UCALL %s(argc %d)", current,
4305 cufunc->cuf_name, cufunc->cuf_argcount);
4306 }
4307 break;
4308 case ISN_PCALL:
4309 {
4310 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4311
4312 smsg("%4d PCALL%s (argc %d)", current,
4313 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4314 }
4315 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004316 case ISN_PCALL_END:
4317 smsg("%4d PCALL end", current);
4318 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004319 case ISN_RETURN:
4320 smsg("%4d RETURN", current);
4321 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004322 case ISN_RETURN_ZERO:
4323 smsg("%4d RETURN 0", current);
4324 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004325 case ISN_FUNCREF:
4326 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004327 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004328 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004329 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004331 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004332 }
4333 break;
4334
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004335 case ISN_NEWFUNC:
4336 {
4337 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4338
4339 smsg("%4d NEWFUNC %s %s", current,
4340 newfunc->nf_lambda, newfunc->nf_global);
4341 }
4342 break;
4343
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004344 case ISN_DEF:
4345 {
4346 char_u *name = iptr->isn_arg.string;
4347
4348 smsg("%4d DEF %s", current,
4349 name == NULL ? (char_u *)"" : name);
4350 }
4351 break;
4352
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004353 case ISN_JUMP:
4354 {
4355 char *when = "?";
4356
4357 switch (iptr->isn_arg.jump.jump_when)
4358 {
4359 case JUMP_ALWAYS:
4360 when = "JUMP";
4361 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004362 case JUMP_AND_KEEP_IF_TRUE:
4363 when = "JUMP_AND_KEEP_IF_TRUE";
4364 break;
4365 case JUMP_IF_FALSE:
4366 when = "JUMP_IF_FALSE";
4367 break;
4368 case JUMP_AND_KEEP_IF_FALSE:
4369 when = "JUMP_AND_KEEP_IF_FALSE";
4370 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004371 case JUMP_IF_COND_FALSE:
4372 when = "JUMP_IF_COND_FALSE";
4373 break;
4374 case JUMP_IF_COND_TRUE:
4375 when = "JUMP_IF_COND_TRUE";
4376 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004377 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004378 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004379 iptr->isn_arg.jump.jump_where);
4380 }
4381 break;
4382
4383 case ISN_FOR:
4384 {
4385 forloop_T *forloop = &iptr->isn_arg.forloop;
4386
4387 smsg("%4d FOR $%d -> %d", current,
4388 forloop->for_idx, forloop->for_end);
4389 }
4390 break;
4391
4392 case ISN_TRY:
4393 {
4394 try_T *try = &iptr->isn_arg.try;
4395
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004396 if (try->try_ref->try_finally == 0)
4397 smsg("%4d TRY catch -> %d, endtry -> %d",
4398 current,
4399 try->try_ref->try_catch,
4400 try->try_ref->try_endtry);
4401 else
4402 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4403 current,
4404 try->try_ref->try_catch,
4405 try->try_ref->try_finally,
4406 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004407 }
4408 break;
4409 case ISN_CATCH:
4410 // TODO
4411 smsg("%4d CATCH", current);
4412 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004413 case ISN_TRYCONT:
4414 {
4415 trycont_T *trycont = &iptr->isn_arg.trycont;
4416
4417 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4418 trycont->tct_levels,
4419 trycont->tct_levels == 1 ? "" : "s",
4420 trycont->tct_where);
4421 }
4422 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004423 case ISN_FINALLY:
4424 smsg("%4d FINALLY", current);
4425 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004426 case ISN_ENDTRY:
4427 smsg("%4d ENDTRY", current);
4428 break;
4429 case ISN_THROW:
4430 smsg("%4d THROW", current);
4431 break;
4432
4433 // expression operations on number
4434 case ISN_OPNR:
4435 case ISN_OPFLOAT:
4436 case ISN_OPANY:
4437 {
4438 char *what;
4439 char *ins;
4440
4441 switch (iptr->isn_arg.op.op_type)
4442 {
4443 case EXPR_MULT: what = "*"; break;
4444 case EXPR_DIV: what = "/"; break;
4445 case EXPR_REM: what = "%"; break;
4446 case EXPR_SUB: what = "-"; break;
4447 case EXPR_ADD: what = "+"; break;
4448 default: what = "???"; break;
4449 }
4450 switch (iptr->isn_type)
4451 {
4452 case ISN_OPNR: ins = "OPNR"; break;
4453 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4454 case ISN_OPANY: ins = "OPANY"; break;
4455 default: ins = "???"; break;
4456 }
4457 smsg("%4d %s %s", current, ins, what);
4458 }
4459 break;
4460
4461 case ISN_COMPAREBOOL:
4462 case ISN_COMPARESPECIAL:
4463 case ISN_COMPARENR:
4464 case ISN_COMPAREFLOAT:
4465 case ISN_COMPARESTRING:
4466 case ISN_COMPAREBLOB:
4467 case ISN_COMPARELIST:
4468 case ISN_COMPAREDICT:
4469 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004470 case ISN_COMPAREANY:
4471 {
4472 char *p;
4473 char buf[10];
4474 char *type;
4475
4476 switch (iptr->isn_arg.op.op_type)
4477 {
4478 case EXPR_EQUAL: p = "=="; break;
4479 case EXPR_NEQUAL: p = "!="; break;
4480 case EXPR_GREATER: p = ">"; break;
4481 case EXPR_GEQUAL: p = ">="; break;
4482 case EXPR_SMALLER: p = "<"; break;
4483 case EXPR_SEQUAL: p = "<="; break;
4484 case EXPR_MATCH: p = "=~"; break;
4485 case EXPR_IS: p = "is"; break;
4486 case EXPR_ISNOT: p = "isnot"; break;
4487 case EXPR_NOMATCH: p = "!~"; break;
4488 default: p = "???"; break;
4489 }
4490 STRCPY(buf, p);
4491 if (iptr->isn_arg.op.op_ic == TRUE)
4492 strcat(buf, "?");
4493 switch(iptr->isn_type)
4494 {
4495 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4496 case ISN_COMPARESPECIAL:
4497 type = "COMPARESPECIAL"; break;
4498 case ISN_COMPARENR: type = "COMPARENR"; break;
4499 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4500 case ISN_COMPARESTRING:
4501 type = "COMPARESTRING"; break;
4502 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4503 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4504 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4505 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004506 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4507 default: type = "???"; break;
4508 }
4509
4510 smsg("%4d %s %s", current, type, buf);
4511 }
4512 break;
4513
4514 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4515 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4516
4517 // expression operations
4518 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004519 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004520 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004521 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004522 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004523 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004524 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004525 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4526 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004527 case ISN_SLICE: smsg("%4d SLICE %lld",
4528 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004529 case ISN_GETITEM: smsg("%4d ITEM %lld",
4530 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004531 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4532 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004533 iptr->isn_arg.string); break;
4534 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4535
4536 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004537 case ISN_CHECKTYPE:
4538 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004539 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004540 char *tofree;
4541
Bram Moolenaare32e5162021-01-21 20:21:29 +01004542 if (ct->ct_arg_idx == 0)
4543 smsg("%4d CHECKTYPE %s stack[%d]", current,
4544 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004545 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004546 else
4547 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4548 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004549 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004550 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004551 vim_free(tofree);
4552 break;
4553 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004554 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4555 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4556 iptr->isn_arg.checklen.cl_min_len);
4557 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004558 case ISN_SETTYPE:
4559 {
4560 char *tofree;
4561
4562 smsg("%4d SETTYPE %s", current,
4563 type_name(iptr->isn_arg.type.ct_type, &tofree));
4564 vim_free(tofree);
4565 break;
4566 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004567 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004568 case ISN_2BOOL: if (iptr->isn_arg.number)
4569 smsg("%4d INVERT (!val)", current);
4570 else
4571 smsg("%4d 2BOOL (!!val)", current);
4572 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004573 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004574 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004575 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004576 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004577 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004578 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004579 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4580 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004581 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004582 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4583 smsg("%4d PUT %c above range",
4584 current, iptr->isn_arg.put.put_regname);
4585 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4586 smsg("%4d PUT %c range",
4587 current, iptr->isn_arg.put.put_regname);
4588 else
4589 smsg("%4d PUT %c %ld", current,
4590 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004591 (long)iptr->isn_arg.put.put_lnum);
4592 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004593
Bram Moolenaar02194d22020-10-24 23:08:38 +02004594 // TODO: summarize modifiers
4595 case ISN_CMDMOD:
4596 {
4597 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004598 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004599 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4600
4601 buf = alloc(len + 1);
4602 if (buf != NULL)
4603 {
4604 (void)produce_cmdmods(
4605 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4606 smsg("%4d CMDMOD %s", current, buf);
4607 vim_free(buf);
4608 }
4609 break;
4610 }
4611 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004612
Bram Moolenaarb2049902021-01-24 12:53:53 +01004613 case ISN_PROF_START:
4614 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4615 break;
4616
4617 case ISN_PROF_END:
4618 smsg("%4d PROFILE END", current);
4619 break;
4620
Bram Moolenaar792f7862020-11-23 08:31:18 +01004621 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4622 iptr->isn_arg.unpack.unp_count,
4623 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4624 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004625 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4626 iptr->isn_arg.shuffle.shfl_item,
4627 iptr->isn_arg.shuffle.shfl_up);
4628 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004629 case ISN_DROP: smsg("%4d DROP", current); break;
4630 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004631
4632 out_flush(); // output one line at a time
4633 ui_breakcheck();
4634 if (got_int)
4635 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004636 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004637}
4638
4639/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004640 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004641 * list, etc. Mostly like what JavaScript does, except that empty list and
4642 * empty dictionary are FALSE.
4643 */
4644 int
4645tv2bool(typval_T *tv)
4646{
4647 switch (tv->v_type)
4648 {
4649 case VAR_NUMBER:
4650 return tv->vval.v_number != 0;
4651 case VAR_FLOAT:
4652#ifdef FEAT_FLOAT
4653 return tv->vval.v_float != 0.0;
4654#else
4655 break;
4656#endif
4657 case VAR_PARTIAL:
4658 return tv->vval.v_partial != NULL;
4659 case VAR_FUNC:
4660 case VAR_STRING:
4661 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4662 case VAR_LIST:
4663 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4664 case VAR_DICT:
4665 return tv->vval.v_dict != NULL
4666 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4667 case VAR_BOOL:
4668 case VAR_SPECIAL:
4669 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4670 case VAR_JOB:
4671#ifdef FEAT_JOB_CHANNEL
4672 return tv->vval.v_job != NULL;
4673#else
4674 break;
4675#endif
4676 case VAR_CHANNEL:
4677#ifdef FEAT_JOB_CHANNEL
4678 return tv->vval.v_channel != NULL;
4679#else
4680 break;
4681#endif
4682 case VAR_BLOB:
4683 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4684 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004685 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004686 case VAR_VOID:
4687 break;
4688 }
4689 return FALSE;
4690}
4691
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004692 void
4693emsg_using_string_as(typval_T *tv, int as_number)
4694{
4695 semsg(_(as_number ? e_using_string_as_number_str
4696 : e_using_string_as_bool_str),
4697 tv->vval.v_string == NULL
4698 ? (char_u *)"" : tv->vval.v_string);
4699}
4700
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004701/*
4702 * If "tv" is a string give an error and return FAIL.
4703 */
4704 int
4705check_not_string(typval_T *tv)
4706{
4707 if (tv->v_type == VAR_STRING)
4708 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004709 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004710 clear_tv(tv);
4711 return FAIL;
4712 }
4713 return OK;
4714}
4715
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004716
4717#endif // FEAT_EVAL