blob: 6b8cd075c10810a48f53f8f51528b27a586378a4 [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 Moolenaarc970e422021-03-17 15:03:04 +0100326 ++ufunc->uf_calls;
327
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100328 // Set execution state to the start of the called function.
329 ectx->ec_dfunc_idx = cdf_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +0100330 ectx->ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarb2049902021-01-24 12:53:53 +0100331 entry = estack_push_ufunc(ufunc, 1);
Bram Moolenaarc620c052020-07-08 15:16:19 +0200332 if (entry != NULL)
333 {
334 // Set the script context to the script where the function was defined.
335 // TODO: save more than the SID?
336 entry->es_save_sid = current_sctx.sc_sid;
337 current_sctx.sc_sid = ufunc->uf_script_ctx.sc_sid;
338 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100339
340 // Decide where to start execution, handles optional arguments.
341 init_instr_idx(ufunc, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100342
343 return OK;
344}
345
346// Get pointer to item in the stack.
347#define STACK_TV(idx) (((typval_T *)ectx->ec_stack.ga_data) + idx)
348
349/*
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200350 * Used when returning from a function: Check if any closure is still
351 * referenced. If so then move the arguments and variables to a separate piece
352 * of stack to be used when the closure is called.
353 * When "free_arguments" is TRUE the arguments are to be freed.
354 * Returns FAIL when out of memory.
355 */
356 static int
357handle_closure_in_use(ectx_T *ectx, int free_arguments)
358{
359 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
360 + ectx->ec_dfunc_idx;
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200361 int argcount;
362 int top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200363 int idx;
364 typval_T *tv;
365 int closure_in_use = FALSE;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200366 garray_T *gap = &ectx->ec_funcrefs;
367 varnumber_T closure_count;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200368
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200369 if (dfunc->df_ufunc == NULL)
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200370 return OK; // function was freed
371 if (dfunc->df_has_closure == 0)
372 return OK; // no closures
373 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + dfunc->df_varcount);
374 closure_count = tv->vval.v_number;
375 if (closure_count == 0)
376 return OK; // no funcrefs created
377
Bram Moolenaarfdeab652020-09-19 15:16:50 +0200378 argcount = ufunc_argcount(dfunc->df_ufunc);
379 top = ectx->ec_frame_idx - argcount;
380
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200381 // Check if any created closure is still in use.
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200382 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200383 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200384 partial_T *pt;
385 int off = gap->ga_len - closure_count + idx;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200386
Bram Moolenaarc70bdab2020-09-26 19:59:38 +0200387 if (off < 0)
388 continue; // count is off or already done
389 pt = ((partial_T **)gap->ga_data)[off];
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200390 if (pt->pt_refcount > 1)
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200391 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200392 int refcount = pt->pt_refcount;
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200393 int i;
394
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200395 // A Reference in a local variables doesn't count, it gets
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200396 // unreferenced on return.
397 for (i = 0; i < dfunc->df_varcount; ++i)
398 {
399 typval_T *stv = STACK_TV(ectx->ec_frame_idx
400 + STACK_FRAME_SIZE + i);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200401 if (stv->v_type == VAR_PARTIAL && pt == stv->vval.v_partial)
Bram Moolenaar221fcc72020-05-05 19:46:20 +0200402 --refcount;
403 }
404 if (refcount > 1)
405 {
406 closure_in_use = TRUE;
407 break;
408 }
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200409 }
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200410 }
411
412 if (closure_in_use)
413 {
414 funcstack_T *funcstack = ALLOC_CLEAR_ONE(funcstack_T);
415 typval_T *stack;
416
417 // A closure is using the arguments and/or local variables.
418 // Move them to the called function.
419 if (funcstack == NULL)
420 return FAIL;
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200421 funcstack->fs_var_offset = argcount + STACK_FRAME_SIZE;
422 funcstack->fs_ga.ga_len = funcstack->fs_var_offset + dfunc->df_varcount;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200423 stack = ALLOC_CLEAR_MULT(typval_T, funcstack->fs_ga.ga_len);
424 funcstack->fs_ga.ga_data = stack;
425 if (stack == NULL)
426 {
427 vim_free(funcstack);
428 return FAIL;
429 }
430
431 // Move or copy the arguments.
432 for (idx = 0; idx < argcount; ++idx)
433 {
434 tv = STACK_TV(top + idx);
435 if (free_arguments)
436 {
437 *(stack + idx) = *tv;
438 tv->v_type = VAR_UNKNOWN;
439 }
440 else
441 copy_tv(tv, stack + idx);
442 }
443 // Move the local variables.
444 for (idx = 0; idx < dfunc->df_varcount; ++idx)
445 {
446 tv = STACK_TV(ectx->ec_frame_idx + STACK_FRAME_SIZE + idx);
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200447
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200448 // A partial created for a local function, that is also used as a
449 // local variable, has a reference count for the variable, thus
450 // will never go down to zero. When all these refcounts are one
451 // then the funcstack is unused. We need to count how many we have
452 // so we need when to check.
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200453 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL)
454 {
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200455 int i;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200456
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200457 for (i = 0; i < closure_count; ++i)
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200458 if (tv->vval.v_partial == ((partial_T **)gap->ga_data)[
459 gap->ga_len - closure_count + i])
460 ++funcstack->fs_min_refcount;
Bram Moolenaarf821dda2020-05-06 22:18:17 +0200461 }
462
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200463 *(stack + funcstack->fs_var_offset + idx) = *tv;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200464 tv->v_type = VAR_UNKNOWN;
465 }
466
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200467 for (idx = 0; idx < closure_count; ++idx)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200468 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200469 partial_T *pt = ((partial_T **)gap->ga_data)[gap->ga_len
470 - closure_count + idx];
471 if (pt->pt_refcount > 1)
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200472 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200473 ++funcstack->fs_refcount;
474 pt->pt_funcstack = funcstack;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100475 pt->pt_outer.out_stack = &funcstack->fs_ga;
476 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx - top;
477 pt->pt_outer.out_up = ectx->ec_outer;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200478 }
479 }
480 }
481
Bram Moolenaar148ce7a2020-09-23 21:57:23 +0200482 for (idx = 0; idx < closure_count; ++idx)
483 partial_unref(((partial_T **)gap->ga_data)[gap->ga_len
484 - closure_count + idx]);
485 gap->ga_len -= closure_count;
486 if (gap->ga_len == 0)
487 ga_clear(gap);
488
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200489 return OK;
490}
491
492/*
Bram Moolenaar85d5e2b2020-10-10 14:13:01 +0200493 * Called when a partial is freed or its reference count goes down to one. The
494 * funcstack may be the only reference to the partials in the local variables.
495 * Go over all of them, the funcref and can be freed if all partials
496 * referencing the funcstack have a reference count of one.
497 */
498 void
499funcstack_check_refcount(funcstack_T *funcstack)
500{
501 int i;
502 garray_T *gap = &funcstack->fs_ga;
503 int done = 0;
504
505 if (funcstack->fs_refcount > funcstack->fs_min_refcount)
506 return;
507 for (i = funcstack->fs_var_offset; i < gap->ga_len; ++i)
508 {
509 typval_T *tv = ((typval_T *)gap->ga_data) + i;
510
511 if (tv->v_type == VAR_PARTIAL && tv->vval.v_partial != NULL
512 && tv->vval.v_partial->pt_funcstack == funcstack
513 && tv->vval.v_partial->pt_refcount == 1)
514 ++done;
515 }
516 if (done == funcstack->fs_min_refcount)
517 {
518 typval_T *stack = gap->ga_data;
519
520 // All partials referencing the funcstack have a reference count of
521 // one, thus the funcstack is no longer of use.
522 for (i = 0; i < gap->ga_len; ++i)
523 clear_tv(stack + i);
524 vim_free(stack);
525 vim_free(funcstack);
526 }
527}
528
529/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100530 * Return from the current function.
531 */
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200532 static int
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100533func_return(ectx_T *ectx)
534{
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100535 int idx;
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100536 int ret_idx;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200537 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
538 + ectx->ec_dfunc_idx;
539 int argcount = ufunc_argcount(dfunc->df_ufunc);
540 int top = ectx->ec_frame_idx - argcount;
Bram Moolenaarc620c052020-07-08 15:16:19 +0200541 estack_T *entry;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100542 int prev_dfunc_idx = STACK_TV(ectx->ec_frame_idx
543 + STACK_FRAME_FUNC_OFF)->vval.v_number;
544 dfunc_T *prev_dfunc = ((dfunc_T *)def_functions.ga_data)
545 + prev_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100546
Bram Moolenaar12d26532021-02-19 19:13:21 +0100547#ifdef FEAT_PROFILE
548 if (do_profiling == PROF_YES)
549 {
550 ufunc_T *caller = prev_dfunc->df_ufunc;
551
552 if (dfunc->df_ufunc->uf_profiling
553 || (caller != NULL && caller->uf_profiling))
554 {
555 profile_may_end_func(((profinfo_T *)profile_info_ga.ga_data)
556 + profile_info_ga.ga_len - 1, dfunc->df_ufunc, caller);
557 --profile_info_ga.ga_len;
558 }
559 }
560#endif
Bram Moolenaarc970e422021-03-17 15:03:04 +0100561 // TODO: when is it safe to delete the function when it is no longer used?
562 --dfunc->df_ufunc->uf_calls;
563
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100564 // execution context goes one level up
Bram Moolenaarc620c052020-07-08 15:16:19 +0200565 entry = estack_pop();
566 if (entry != NULL)
567 current_sctx.sc_sid = entry->es_save_sid;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100568
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200569 if (handle_closure_in_use(ectx, TRUE) == FAIL)
570 return FAIL;
571
572 // Clear the arguments.
573 for (idx = top; idx < ectx->ec_frame_idx; ++idx)
574 clear_tv(STACK_TV(idx));
575
576 // Clear local variables and temp values, but not the return value.
577 for (idx = ectx->ec_frame_idx + STACK_FRAME_SIZE;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100578 idx < ectx->ec_stack.ga_len - 1; ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100579 clear_tv(STACK_TV(idx));
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100580
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100581 // The return value should be on top of the stack. However, when aborting
582 // it may not be there and ec_frame_idx is the top of the stack.
583 ret_idx = ectx->ec_stack.ga_len - 1;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100584 if (ret_idx == ectx->ec_frame_idx + STACK_FRAME_IDX_OFF)
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100585 ret_idx = 0;
586
Bram Moolenaar0186e582021-01-10 18:33:11 +0100587 vim_free(ectx->ec_outer);
588
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100589 // Restore the previous frame.
Bram Moolenaar12d26532021-02-19 19:13:21 +0100590 ectx->ec_dfunc_idx = prev_dfunc_idx;
Bram Moolenaar0186e582021-01-10 18:33:11 +0100591 ectx->ec_iidx = STACK_TV(ectx->ec_frame_idx
592 + STACK_FRAME_IIDX_OFF)->vval.v_number;
593 ectx->ec_outer = (void *)STACK_TV(ectx->ec_frame_idx
594 + STACK_FRAME_OUTER_OFF)->vval.v_string;
Bram Moolenaar5366e1a2020-10-01 13:01:34 +0200595 // restoring ec_frame_idx must be last
Bram Moolenaar0186e582021-01-10 18:33:11 +0100596 ectx->ec_frame_idx = STACK_TV(ectx->ec_frame_idx
597 + STACK_FRAME_IDX_OFF)->vval.v_number;
Bram Moolenaar12d26532021-02-19 19:13:21 +0100598 ectx->ec_instr = INSTRUCTIONS(prev_dfunc);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +0100599
Bram Moolenaar34c54eb2020-11-25 19:15:19 +0100600 if (ret_idx > 0)
601 {
602 // Reset the stack to the position before the call, with a spot for the
603 // return value, moved there from above the frame.
604 ectx->ec_stack.ga_len = top + 1;
605 *STACK_TV_BOT(-1) = *STACK_TV(ret_idx);
606 }
607 else
608 // Reset the stack to the position before the call.
609 ectx->ec_stack.ga_len = top;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200610
Bram Moolenaar0ba48e82020-11-17 18:23:19 +0100611 funcdepth_decrement();
Bram Moolenaarbf67ea12020-05-02 17:52:42 +0200612 return OK;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100613}
614
615#undef STACK_TV
616
617/*
618 * Prepare arguments and rettv for calling a builtin or user function.
619 */
620 static int
621call_prepare(int argcount, typval_T *argvars, ectx_T *ectx)
622{
623 int idx;
624 typval_T *tv;
625
626 // Move arguments from bottom of the stack to argvars[] and add terminator.
627 for (idx = 0; idx < argcount; ++idx)
628 argvars[idx] = *STACK_TV_BOT(idx - argcount);
629 argvars[argcount].v_type = VAR_UNKNOWN;
630
631 // Result replaces the arguments on the stack.
632 if (argcount > 0)
633 ectx->ec_stack.ga_len -= argcount - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +0200634 else if (GA_GROW(&ectx->ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100635 return FAIL;
636 else
637 ++ectx->ec_stack.ga_len;
638
639 // Default return value is zero.
640 tv = STACK_TV_BOT(-1);
641 tv->v_type = VAR_NUMBER;
642 tv->vval.v_number = 0;
643
644 return OK;
645}
646
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200647// Ugly global to avoid passing the execution context around through many
648// layers.
649static ectx_T *current_ectx = NULL;
650
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100651/*
652 * Call a builtin function by index.
653 */
654 static int
655call_bfunc(int func_idx, int argcount, ectx_T *ectx)
656{
657 typval_T argvars[MAX_FUNC_ARGS];
658 int idx;
Bram Moolenaar171fb922020-10-28 16:54:47 +0100659 int did_emsg_before = did_emsg;
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200660 ectx_T *prev_ectx = current_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100661
662 if (call_prepare(argcount, argvars, ectx) == FAIL)
663 return FAIL;
664
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200665 // Call the builtin function. Set "current_ectx" so that when it
666 // recursively invokes call_def_function() a closure context can be set.
667 current_ectx = ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100668 call_internal_func_by_idx(func_idx, argvars, STACK_TV_BOT(-1));
Bram Moolenaar08f7a412020-07-13 20:41:08 +0200669 current_ectx = prev_ectx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100670
671 // Clear the arguments.
672 for (idx = 0; idx < argcount; ++idx)
673 clear_tv(&argvars[idx]);
Bram Moolenaar015f4262020-05-05 21:25:22 +0200674
Bram Moolenaar57f799e2020-12-12 20:42:19 +0100675 if (did_emsg > did_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200676 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100677 return OK;
678}
679
680/*
681 * Execute a user defined function.
Bram Moolenaarab360522021-01-10 14:02:28 +0100682 * If the function is compiled this will add a stack frame and set the
683 * instruction pointer at the start of the function.
684 * Otherwise the function is called here.
Bram Moolenaar0186e582021-01-10 18:33:11 +0100685 * If "pt" is not null use "pt->pt_outer" for ec_outer.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100686 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100687 */
688 static int
Bram Moolenaar0186e582021-01-10 18:33:11 +0100689call_ufunc(
690 ufunc_T *ufunc,
691 partial_T *pt,
692 int argcount,
693 ectx_T *ectx,
694 isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100695{
696 typval_T argvars[MAX_FUNC_ARGS];
697 funcexe_T funcexe;
698 int error;
699 int idx;
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100700 int did_emsg_before = did_emsg;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100701#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +0100702 int profiling = do_profiling == PROF_YES && ufunc->uf_profiling;
Bram Moolenaarf002a412021-01-24 13:34:18 +0100703#else
704# define profiling FALSE
705#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100706
Bram Moolenaarb2049902021-01-24 12:53:53 +0100707 if (func_needs_compiling(ufunc, profiling)
708 && compile_def_function(ufunc, FALSE, profiling, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +0200709 return FAIL;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +0200710 if (ufunc->uf_def_status == UF_COMPILED)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100711 {
Bram Moolenaar52c124d2020-12-20 21:43:35 +0100712 error = check_user_func_argcount(ufunc, argcount);
Bram Moolenaar50824712020-12-20 21:10:17 +0100713 if (error != FCERR_UNKNOWN)
714 {
715 if (error == FCERR_TOOMANY)
716 semsg(_(e_toomanyarg), ufunc->uf_name);
717 else
718 semsg(_(e_toofewarg), ufunc->uf_name);
719 return FAIL;
720 }
721
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100722 // The function has been compiled, can call it quickly. For a function
723 // that was defined later: we can call it directly next time.
Bram Moolenaarcd45ed02020-12-22 17:35:54 +0100724 // TODO: what if the function was deleted and then defined again?
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100725 if (iptr != NULL)
726 {
Bram Moolenaar20431c92020-03-20 18:39:46 +0100727 delete_instr(iptr);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100728 iptr->isn_type = ISN_DCALL;
729 iptr->isn_arg.dfunc.cdf_idx = ufunc->uf_dfunc_idx;
730 iptr->isn_arg.dfunc.cdf_argcount = argcount;
731 }
Bram Moolenaar0186e582021-01-10 18:33:11 +0100732 return call_dfunc(ufunc->uf_dfunc_idx, pt, argcount, ectx);
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100733 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100734
735 if (call_prepare(argcount, argvars, ectx) == FAIL)
736 return FAIL;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200737 CLEAR_FIELD(funcexe);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100738 funcexe.evaluate = TRUE;
739
740 // Call the user function. Result goes in last position on the stack.
741 // TODO: add selfdict if there is one
742 error = call_user_func_check(ufunc, argcount, argvars,
743 STACK_TV_BOT(-1), &funcexe, NULL);
744
745 // Clear the arguments.
746 for (idx = 0; idx < argcount; ++idx)
747 clear_tv(&argvars[idx]);
748
749 if (error != FCERR_NONE)
750 {
751 user_func_error(error, ufunc->uf_name);
752 return FAIL;
753 }
Bram Moolenaar28ee8922020-10-28 20:20:00 +0100754 if (did_emsg > did_emsg_before)
Bram Moolenaared677f52020-08-12 16:38:10 +0200755 // Error other than from calling the function itself.
756 return FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100757 return OK;
758}
759
760/*
Bram Moolenaara1773442020-08-12 15:21:22 +0200761 * Return TRUE if an error was given or CTRL-C was pressed.
762 */
763 static int
764vim9_aborting(int prev_called_emsg)
765{
766 return called_emsg > prev_called_emsg || got_int || did_throw;
767}
768
769/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100770 * Execute a function by "name".
771 * This can be a builtin function or a user function.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100772 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100773 * Returns FAIL if not found without an error message.
774 */
775 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +0100776call_by_name(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100777{
778 ufunc_T *ufunc;
779
780 if (builtin_function(name, -1))
781 {
782 int func_idx = find_internal_func(name);
783
784 if (func_idx < 0)
785 return FAIL;
Bram Moolenaar389df252020-07-09 21:20:47 +0200786 if (check_internal_func(func_idx, argcount) < 0)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100787 return FAIL;
788 return call_bfunc(func_idx, argcount, ectx);
789 }
790
Bram Moolenaar4c17ad92020-04-27 22:47:51 +0200791 ufunc = find_func(name, FALSE, NULL);
Bram Moolenaara1773442020-08-12 15:21:22 +0200792
793 if (ufunc == NULL)
794 {
795 int called_emsg_before = called_emsg;
796
797 if (script_autoload(name, TRUE))
798 // loaded a package, search for the function again
799 ufunc = find_func(name, FALSE, NULL);
800 if (vim9_aborting(called_emsg_before))
801 return FAIL; // bail out if loading the script caused an error
802 }
803
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100804 if (ufunc != NULL)
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100805 {
806 if (ufunc->uf_arg_types != NULL)
807 {
808 int i;
809 typval_T *argv = STACK_TV_BOT(0) - argcount;
810
811 // The function can change at runtime, check that the argument
812 // types are correct.
813 for (i = 0; i < argcount; ++i)
814 {
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100815 type_T *type = NULL;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100816
Bram Moolenaare3ffcd92021-03-08 21:47:13 +0100817 if (i < ufunc->uf_args.ga_len)
818 type = ufunc->uf_arg_types[i];
819 else if (ufunc->uf_va_type != NULL)
820 type = ufunc->uf_va_type->tt_member;
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100821 if (type != NULL && check_typval_arg_type(type,
822 &argv[i], i + 1) == FAIL)
823 return FAIL;
824 }
825 }
826
Bram Moolenaar0186e582021-01-10 18:33:11 +0100827 return call_ufunc(ufunc, NULL, argcount, ectx, iptr);
Bram Moolenaar04947cc2021-03-06 19:26:46 +0100828 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100829
830 return FAIL;
831}
832
833 static int
Bram Moolenaara90afb92020-07-15 22:38:56 +0200834call_partial(typval_T *tv, int argcount_arg, ectx_T *ectx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100835{
Bram Moolenaara90afb92020-07-15 22:38:56 +0200836 int argcount = argcount_arg;
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200837 char_u *name = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100838 int called_emsg_before = called_emsg;
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100839 int res = FAIL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100840
841 if (tv->v_type == VAR_PARTIAL)
842 {
Bram Moolenaara90afb92020-07-15 22:38:56 +0200843 partial_T *pt = tv->vval.v_partial;
844 int i;
845
846 if (pt->pt_argc > 0)
847 {
848 // Make space for arguments from the partial, shift the "argcount"
849 // arguments up.
850 if (ga_grow(&ectx->ec_stack, pt->pt_argc) == FAIL)
851 return FAIL;
852 for (i = 1; i <= argcount; ++i)
853 *STACK_TV_BOT(-i + pt->pt_argc) = *STACK_TV_BOT(-i);
854 ectx->ec_stack.ga_len += pt->pt_argc;
855 argcount += pt->pt_argc;
856
857 // copy the arguments from the partial onto the stack
858 for (i = 0; i < pt->pt_argc; ++i)
859 copy_tv(&pt->pt_argv[i], STACK_TV_BOT(-argcount + i));
860 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100861
862 if (pt->pt_func != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +0100863 return call_ufunc(pt->pt_func, pt, argcount, ectx, NULL);
Bram Moolenaarf7779c62020-05-03 15:38:16 +0200864
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100865 name = pt->pt_name;
866 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +0200867 else if (tv->v_type == VAR_FUNC)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100868 name = tv->vval.v_string;
Bram Moolenaar95006e32020-08-29 17:47:08 +0200869 if (name != NULL)
870 {
871 char_u fname_buf[FLEN_FIXED + 1];
872 char_u *tofree = NULL;
873 int error = FCERR_NONE;
874 char_u *fname;
875
876 // May need to translate <SNR>123_ to K_SNR.
877 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
878 if (error != FCERR_NONE)
879 res = FAIL;
880 else
881 res = call_by_name(fname, argcount, ectx, NULL);
882 vim_free(tofree);
883 }
884
Bram Moolenaarcb6cbf22021-01-12 17:17:01 +0100885 if (res == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100886 {
887 if (called_emsg == called_emsg_before)
Bram Moolenaar015f4262020-05-05 21:25:22 +0200888 semsg(_(e_unknownfunc),
889 name == NULL ? (char_u *)"[unknown]" : name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100890 return FAIL;
891 }
892 return OK;
893}
894
895/*
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +0200896 * Check if "lock" is VAR_LOCKED or VAR_FIXED. If so give an error and return
897 * TRUE.
898 */
899 static int
900error_if_locked(int lock, char *error)
901{
902 if (lock & (VAR_LOCKED | VAR_FIXED))
903 {
904 emsg(_(error));
905 return TRUE;
906 }
907 return FALSE;
908}
909
910/*
Bram Moolenaar5b5ae292021-02-20 17:04:02 +0100911 * Give an error if "tv" is not a number and return FAIL.
912 */
913 static int
914check_for_number(typval_T *tv)
915{
916 if (tv->v_type != VAR_NUMBER)
917 {
918 semsg(_(e_expected_str_but_got_str),
919 vartype_name(VAR_NUMBER), vartype_name(tv->v_type));
920 return FAIL;
921 }
922 return OK;
923}
924
925/*
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100926 * Store "tv" in variable "name".
927 * This is for s: and g: variables.
928 */
929 static void
930store_var(char_u *name, typval_T *tv)
931{
932 funccal_entry_T entry;
933
934 save_funccal(&entry);
Bram Moolenaarf785aa12021-02-11 21:19:34 +0100935 set_var_const(name, NULL, tv, FALSE, ASSIGN_DECL, 0);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +0100936 restore_funccal();
937}
938
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100939/*
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100940 * Convert "tv" to a string.
941 * Return FAIL if not allowed.
942 */
943 static int
944do_2string(typval_T *tv, int is_2string_any)
945{
946 if (tv->v_type != VAR_STRING)
947 {
948 char_u *str;
949
950 if (is_2string_any)
951 {
952 switch (tv->v_type)
953 {
954 case VAR_SPECIAL:
955 case VAR_BOOL:
956 case VAR_NUMBER:
957 case VAR_FLOAT:
958 case VAR_BLOB: break;
959 default: to_string_error(tv->v_type);
960 return FAIL;
961 }
962 }
Bram Moolenaar34453202021-01-31 13:08:38 +0100963 str = typval_tostring(tv, TRUE);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +0100964 clear_tv(tv);
965 tv->v_type = VAR_STRING;
966 tv->vval.v_string = str;
967 }
968 return OK;
969}
970
971/*
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100972 * When the value of "sv" is a null list of dict, allocate it.
973 */
974 static void
975allocate_if_null(typval_T *tv)
976{
977 switch (tv->v_type)
978 {
979 case VAR_LIST:
980 if (tv->vval.v_list == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100981 (void)rettv_list_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100982 break;
983 case VAR_DICT:
984 if (tv->vval.v_dict == NULL)
Bram Moolenaarfef80642021-02-03 20:01:19 +0100985 (void)rettv_dict_alloc(tv);
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +0100986 break;
987 default:
988 break;
989 }
990}
Bram Moolenaard3aac292020-04-19 14:32:17 +0200991
Bram Moolenaare7525c52021-01-09 13:20:37 +0100992/*
Bram Moolenaar0289a092021-03-14 18:40:19 +0100993 * Return the character "str[index]" where "index" is the character index,
994 * including composing characters.
995 * If "index" is out of range NULL is returned.
Bram Moolenaare7525c52021-01-09 13:20:37 +0100996 */
997 char_u *
998char_from_string(char_u *str, varnumber_T index)
999{
1000 size_t nbyte = 0;
1001 varnumber_T nchar = index;
1002 size_t slen;
1003
1004 if (str == NULL)
1005 return NULL;
1006 slen = STRLEN(str);
1007
1008 // do the same as for a list: a negative index counts from the end
1009 if (index < 0)
1010 {
1011 int clen = 0;
1012
1013 for (nbyte = 0; nbyte < slen; ++clen)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001014 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001015 nchar = clen + index;
1016 if (nchar < 0)
1017 // unlike list: index out of range results in empty string
1018 return NULL;
1019 }
1020
1021 for (nbyte = 0; nchar > 0 && nbyte < slen; --nchar)
Bram Moolenaar0289a092021-03-14 18:40:19 +01001022 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001023 if (nbyte >= slen)
1024 return NULL;
Bram Moolenaar0289a092021-03-14 18:40:19 +01001025 return vim_strnsave(str + nbyte, mb_ptr2len(str + nbyte));
Bram Moolenaare7525c52021-01-09 13:20:37 +01001026}
1027
1028/*
1029 * Get the byte index for character index "idx" in string "str" with length
Bram Moolenaar0289a092021-03-14 18:40:19 +01001030 * "str_len". Composing characters are included.
Bram Moolenaare7525c52021-01-09 13:20:37 +01001031 * If going over the end return "str_len".
1032 * If "idx" is negative count from the end, -1 is the last character.
1033 * When going over the start return -1.
1034 */
1035 static long
1036char_idx2byte(char_u *str, size_t str_len, varnumber_T idx)
1037{
1038 varnumber_T nchar = idx;
1039 size_t nbyte = 0;
1040
1041 if (nchar >= 0)
1042 {
1043 while (nchar > 0 && nbyte < str_len)
1044 {
Bram Moolenaar0289a092021-03-14 18:40:19 +01001045 nbyte += mb_ptr2len(str + nbyte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001046 --nchar;
1047 }
1048 }
1049 else
1050 {
1051 nbyte = str_len;
1052 while (nchar < 0 && nbyte > 0)
1053 {
1054 --nbyte;
1055 nbyte -= mb_head_off(str, str + nbyte);
1056 ++nchar;
1057 }
1058 if (nchar < 0)
1059 return -1;
1060 }
1061 return (long)nbyte;
1062}
1063
1064/*
Bram Moolenaar0289a092021-03-14 18:40:19 +01001065 * Return the slice "str[first : last]" using character indexes. Composing
1066 * characters are included.
Bram Moolenaar6601b622021-01-13 21:47:15 +01001067 * "exclusive" is TRUE for slice().
Bram Moolenaare7525c52021-01-09 13:20:37 +01001068 * Return NULL when the result is empty.
1069 */
1070 char_u *
Bram Moolenaar6601b622021-01-13 21:47:15 +01001071string_slice(char_u *str, varnumber_T first, varnumber_T last, int exclusive)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001072{
1073 long start_byte, end_byte;
1074 size_t slen;
1075
1076 if (str == NULL)
1077 return NULL;
1078 slen = STRLEN(str);
1079 start_byte = char_idx2byte(str, slen, first);
1080 if (start_byte < 0)
1081 start_byte = 0; // first index very negative: use zero
Bram Moolenaar6601b622021-01-13 21:47:15 +01001082 if ((last == -1 && !exclusive) || last == VARNUM_MAX)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001083 end_byte = (long)slen;
1084 else
1085 {
1086 end_byte = char_idx2byte(str, slen, last);
Bram Moolenaar6601b622021-01-13 21:47:15 +01001087 if (!exclusive && end_byte >= 0 && end_byte < (long)slen)
Bram Moolenaare7525c52021-01-09 13:20:37 +01001088 // end index is inclusive
Bram Moolenaar0289a092021-03-14 18:40:19 +01001089 end_byte += mb_ptr2len(str + end_byte);
Bram Moolenaare7525c52021-01-09 13:20:37 +01001090 }
1091
1092 if (start_byte >= (long)slen || end_byte <= start_byte)
1093 return NULL;
1094 return vim_strnsave(str + start_byte, end_byte - start_byte);
1095}
1096
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001097 static svar_T *
1098get_script_svar(scriptref_T *sref, ectx_T *ectx)
1099{
1100 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
1101 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1102 + ectx->ec_dfunc_idx;
1103 svar_T *sv;
1104
1105 if (sref->sref_seq != si->sn_script_seq)
1106 {
1107 // The script was reloaded after the function was
1108 // compiled, the script_idx may not be valid.
1109 semsg(_(e_script_variable_invalid_after_reload_in_function_str),
1110 dfunc->df_ufunc->uf_name_exp);
1111 return NULL;
1112 }
1113 sv = ((svar_T *)si->sn_var_vals.ga_data) + sref->sref_idx;
1114 if (!equal_type(sv->sv_type, sref->sref_type))
1115 {
1116 emsg(_(e_script_variable_type_changed));
1117 return NULL;
1118 }
1119 return sv;
1120}
1121
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001122/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001123 * Execute a function by "name".
1124 * This can be a builtin function, user function or a funcref.
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001125 * "iptr" can be used to replace the instruction with a more efficient one.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001126 */
1127 static int
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01001128call_eval_func(char_u *name, int argcount, ectx_T *ectx, isn_T *iptr)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001129{
Bram Moolenaared677f52020-08-12 16:38:10 +02001130 int called_emsg_before = called_emsg;
1131 int res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001132
Bram Moolenaared677f52020-08-12 16:38:10 +02001133 res = call_by_name(name, argcount, ectx, iptr);
1134 if (res == FAIL && called_emsg == called_emsg_before)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001135 {
Bram Moolenaar1df8b3f2020-04-23 18:13:23 +02001136 dictitem_T *v;
1137
1138 v = find_var(name, NULL, FALSE);
1139 if (v == NULL)
1140 {
1141 semsg(_(e_unknownfunc), name);
1142 return FAIL;
1143 }
1144 if (v->di_tv.v_type != VAR_PARTIAL && v->di_tv.v_type != VAR_FUNC)
1145 {
1146 semsg(_(e_unknownfunc), name);
1147 return FAIL;
1148 }
1149 return call_partial(&v->di_tv, argcount, ectx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001150 }
Bram Moolenaared677f52020-08-12 16:38:10 +02001151 return res;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001152}
1153
1154/*
Bram Moolenaarf112f302020-12-20 17:47:52 +01001155 * When a function reference is used, fill a partial with the information
1156 * needed, especially when it is used as a closure.
1157 */
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01001158 int
Bram Moolenaarf112f302020-12-20 17:47:52 +01001159fill_partial_and_closure(partial_T *pt, ufunc_T *ufunc, ectx_T *ectx)
1160{
1161 pt->pt_func = ufunc;
1162 pt->pt_refcount = 1;
1163
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001164 if (ufunc->uf_flags & FC_CLOSURE)
Bram Moolenaarf112f302020-12-20 17:47:52 +01001165 {
1166 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1167 + ectx->ec_dfunc_idx;
1168
1169 // The closure needs to find arguments and local
1170 // variables in the current stack.
Bram Moolenaar0186e582021-01-10 18:33:11 +01001171 pt->pt_outer.out_stack = &ectx->ec_stack;
1172 pt->pt_outer.out_frame_idx = ectx->ec_frame_idx;
1173 pt->pt_outer.out_up = ectx->ec_outer;
1174 pt->pt_outer.out_up_is_copy = TRUE;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001175
1176 // If this function returns and the closure is still
1177 // being used, we need to make a copy of the context
1178 // (arguments and local variables). Store a reference
1179 // to the partial so we can handle that.
1180 if (ga_grow(&ectx->ec_funcrefs, 1) == FAIL)
1181 {
1182 vim_free(pt);
1183 return FAIL;
1184 }
1185 // Extra variable keeps the count of closures created
1186 // in the current function call.
1187 ++(((typval_T *)ectx->ec_stack.ga_data) + ectx->ec_frame_idx
1188 + STACK_FRAME_SIZE + dfunc->df_varcount)->vval.v_number;
1189
1190 ((partial_T **)ectx->ec_funcrefs.ga_data)
1191 [ectx->ec_funcrefs.ga_len] = pt;
1192 ++pt->pt_refcount;
1193 ++ectx->ec_funcrefs.ga_len;
1194 }
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001195 ++ufunc->uf_refcount;
Bram Moolenaarf112f302020-12-20 17:47:52 +01001196 return OK;
1197}
1198
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001199
Bram Moolenaarf112f302020-12-20 17:47:52 +01001200/*
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001201 * Call a "def" function from old Vim script.
1202 * Return OK or FAIL.
1203 */
1204 int
1205call_def_function(
1206 ufunc_T *ufunc,
Bram Moolenaar23e03252020-04-12 22:22:31 +02001207 int argc_arg, // nr of arguments
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001208 typval_T *argv, // arguments
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001209 partial_T *partial, // optional partial for context
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001210 typval_T *rettv) // return value
1211{
1212 ectx_T ectx; // execution context
Bram Moolenaar23e03252020-04-12 22:22:31 +02001213 int argc = argc_arg;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001214 int initial_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001215 typval_T *tv;
1216 int idx;
1217 int ret = FAIL;
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001218 int defcount = ufunc->uf_args.ga_len - argc;
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001219 sctx_T save_current_sctx = current_sctx;
Bram Moolenaar270d0382020-05-15 21:42:53 +02001220 int breakcheck_count = 0;
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001221 int did_emsg_before = did_emsg_cumul + did_emsg;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001222 int save_suppress_errthrow = suppress_errthrow;
Bram Moolenaar352134b2020-10-17 22:04:08 +02001223 msglist_T **saved_msg_list = NULL;
1224 msglist_T *private_msg_list = NULL;
Bram Moolenaar02194d22020-10-24 23:08:38 +02001225 cmdmod_T save_cmdmod;
1226 int restore_cmdmod = FALSE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01001227 int restore_cmdmod_stacklen = 0;
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001228 int save_emsg_silent_def = emsg_silent_def;
1229 int save_did_emsg_def = did_emsg_def;
Bram Moolenaar171fb922020-10-28 16:54:47 +01001230 int trylevel_at_start = trylevel;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001231 int orig_funcdepth;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001232 where_T where;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001233
1234// Get pointer to item in the stack.
1235#define STACK_TV(idx) (((typval_T *)ectx.ec_stack.ga_data) + idx)
1236
1237// Get pointer to item at the bottom of the stack, -1 is the bottom.
1238#undef STACK_TV_BOT
1239#define STACK_TV_BOT(idx) (((typval_T *)ectx.ec_stack.ga_data) + ectx.ec_stack.ga_len + idx)
1240
Bram Moolenaar1378fbc2020-04-11 20:50:33 +02001241// Get pointer to a local variable on the stack. Negative for arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001242#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 +01001243
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02001244 if (ufunc->uf_def_status == UF_NOT_COMPILED
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001245 || (func_needs_compiling(ufunc, PROFILING(ufunc))
1246 && compile_def_function(ufunc, FALSE, PROFILING(ufunc), NULL)
Bram Moolenaarb2049902021-01-24 12:53:53 +01001247 == FAIL))
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001248 {
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001249 if (did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001250 semsg(_(e_function_is_not_compiled_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001251 printable_func_name(ufunc));
Bram Moolenaar822ba242020-05-24 23:00:18 +02001252 return FAIL;
Bram Moolenaar25e0f582020-05-25 22:36:50 +02001253 }
Bram Moolenaar822ba242020-05-24 23:00:18 +02001254
Bram Moolenaar09689a02020-05-09 22:50:08 +02001255 {
Bram Moolenaar822ba242020-05-24 23:00:18 +02001256 // Check the function was really compiled.
Bram Moolenaar09689a02020-05-09 22:50:08 +02001257 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1258 + ufunc->uf_dfunc_idx;
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001259 if (INSTRUCTIONS(dfunc) == NULL)
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001260 {
1261 iemsg("using call_def_function() on not compiled function");
Bram Moolenaar09689a02020-05-09 22:50:08 +02001262 return FAIL;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02001263 }
Bram Moolenaar09689a02020-05-09 22:50:08 +02001264 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001265
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001266 // If depth of calling is getting too high, don't execute the function.
1267 orig_funcdepth = funcdepth_get();
1268 if (funcdepth_increment() == FAIL)
1269 return FAIL;
1270
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001271 CLEAR_FIELD(ectx);
1272 ectx.ec_dfunc_idx = ufunc->uf_dfunc_idx;
1273 ga_init2(&ectx.ec_stack, sizeof(typval_T), 500);
1274 if (ga_grow(&ectx.ec_stack, 20) == FAIL)
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001275 {
1276 funcdepth_decrement();
Bram Moolenaar3b6a6eb2020-05-09 23:20:20 +02001277 return FAIL;
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01001278 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001279 ga_init2(&ectx.ec_trystack, sizeof(trycmd_T), 10);
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001280 ga_init2(&ectx.ec_funcrefs, sizeof(partial_T *), 10);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001281
Bram Moolenaarfc0e8f52020-12-25 20:24:51 +01001282 // Put arguments on the stack, but no more than what the function expects.
1283 // A lambda can be called with more arguments than it uses.
1284 for (idx = 0; idx < argc
1285 && (ufunc->uf_va_name != NULL || idx < ufunc->uf_args.ga_len);
1286 ++idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001287 {
Bram Moolenaar65b95452020-07-19 14:03:09 +02001288 if (ufunc->uf_arg_types != NULL && idx < ufunc->uf_args.ga_len
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001289 && check_typval_arg_type(ufunc->uf_arg_types[idx], &argv[idx],
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001290 idx + 1) == FAIL)
Bram Moolenaar65b95452020-07-19 14:03:09 +02001291 goto failed_early;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001292 copy_tv(&argv[idx], STACK_TV_BOT(0));
1293 ++ectx.ec_stack.ga_len;
1294 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001295
1296 // Turn varargs into a list. Empty list if no args.
1297 if (ufunc->uf_va_name != NULL)
1298 {
1299 int vararg_count = argc - ufunc->uf_args.ga_len;
1300
1301 if (vararg_count < 0)
1302 vararg_count = 0;
1303 else
1304 argc -= vararg_count;
1305 if (exe_newlist(vararg_count, &ectx) == FAIL)
Bram Moolenaar1a2f4bf2020-04-12 23:09:25 +02001306 goto failed_early;
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001307
1308 // Check the type of the list items.
1309 tv = STACK_TV_BOT(-1);
1310 if (ufunc->uf_va_type != NULL
Bram Moolenaar2f8cbc42020-09-16 17:22:59 +02001311 && ufunc->uf_va_type != &t_any
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001312 && ufunc->uf_va_type->tt_member != &t_any
1313 && tv->vval.v_list != NULL)
1314 {
1315 type_T *expected = ufunc->uf_va_type->tt_member;
1316 listitem_T *li = tv->vval.v_list->lv_first;
1317
1318 for (idx = 0; idx < vararg_count; ++idx)
1319 {
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001320 if (check_typval_arg_type(expected, &li->li_tv,
Bram Moolenaar8b565c22020-08-30 23:24:20 +02001321 argc + idx + 1) == FAIL)
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001322 goto failed_early;
1323 li = li->li_next;
1324 }
1325 }
1326
Bram Moolenaar23e03252020-04-12 22:22:31 +02001327 if (defcount > 0)
1328 // Move varargs list to below missing default arguments.
Bram Moolenaar24aa48b2020-07-25 16:33:02 +02001329 *STACK_TV_BOT(defcount - 1) = *STACK_TV_BOT(-1);
Bram Moolenaar23e03252020-04-12 22:22:31 +02001330 --ectx.ec_stack.ga_len;
1331 }
1332
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001333 // Make space for omitted arguments, will store default value below.
Bram Moolenaar23e03252020-04-12 22:22:31 +02001334 // Any varargs list goes after them.
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001335 if (defcount > 0)
1336 for (idx = 0; idx < defcount; ++idx)
1337 {
1338 STACK_TV_BOT(0)->v_type = VAR_UNKNOWN;
1339 ++ectx.ec_stack.ga_len;
1340 }
Bram Moolenaar23e03252020-04-12 22:22:31 +02001341 if (ufunc->uf_va_name != NULL)
Bram Moolenaarc970e422021-03-17 15:03:04 +01001342 ++ectx.ec_stack.ga_len;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001343
1344 // Frame pointer points to just after arguments.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001345 ectx.ec_frame_idx = ectx.ec_stack.ga_len;
1346 initial_frame_idx = ectx.ec_frame_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001347
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001348 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001349 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1350 + ufunc->uf_dfunc_idx;
1351 ufunc_T *base_ufunc = dfunc->df_ufunc;
1352
1353 // "uf_partial" is on the ufunc that "df_ufunc" points to, as is done
1354 // by copy_func().
1355 if (partial != NULL || base_ufunc->uf_partial != NULL)
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001356 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001357 ectx.ec_outer = ALLOC_CLEAR_ONE(outer_T);
1358 if (ectx.ec_outer == NULL)
1359 goto failed_early;
1360 if (partial != NULL)
Bram Moolenaar0186e582021-01-10 18:33:11 +01001361 {
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001362 if (partial->pt_outer.out_stack == NULL && current_ectx != NULL)
1363 {
1364 if (current_ectx->ec_outer != NULL)
1365 *ectx.ec_outer = *current_ectx->ec_outer;
1366 }
1367 else
1368 *ectx.ec_outer = partial->pt_outer;
Bram Moolenaar0186e582021-01-10 18:33:11 +01001369 }
1370 else
Bram Moolenaar0d3de8c2021-01-22 20:46:27 +01001371 *ectx.ec_outer = base_ufunc->uf_partial->pt_outer;
1372 ectx.ec_outer->out_up_is_copy = TRUE;
Bram Moolenaar08f7a412020-07-13 20:41:08 +02001373 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01001374 }
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02001375
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001376 // dummy frame entries
1377 for (idx = 0; idx < STACK_FRAME_SIZE; ++idx)
1378 {
1379 STACK_TV(ectx.ec_stack.ga_len)->v_type = VAR_UNKNOWN;
1380 ++ectx.ec_stack.ga_len;
1381 }
1382
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001383 {
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001384 // Reserve space for local variables and any closure reference count.
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001385 dfunc_T *dfunc = ((dfunc_T *)def_functions.ga_data)
1386 + ufunc->uf_dfunc_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001387
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001388 for (idx = 0; idx < dfunc->df_varcount; ++idx)
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001389 STACK_TV_VAR(idx)->v_type = VAR_UNKNOWN;
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02001390 ectx.ec_stack.ga_len += dfunc->df_varcount;
1391 if (dfunc->df_has_closure)
1392 {
1393 STACK_TV_VAR(idx)->v_type = VAR_NUMBER;
1394 STACK_TV_VAR(idx)->vval.v_number = 0;
1395 ++ectx.ec_stack.ga_len;
1396 }
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001397
Bram Moolenaare5ea3462021-01-25 21:01:48 +01001398 ectx.ec_instr = INSTRUCTIONS(dfunc);
Bram Moolenaarbd5da372020-03-31 23:13:10 +02001399 }
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001400
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001401 // Following errors are in the function, not the caller.
Bram Moolenaara26b9702020-04-18 19:53:28 +02001402 // Commands behave like vim9script.
Bram Moolenaaree8580e2020-08-28 17:19:07 +02001403 estack_push_ufunc(ufunc, 1);
1404 current_sctx = ufunc->uf_script_ctx;
Bram Moolenaara26b9702020-04-18 19:53:28 +02001405 current_sctx.sc_version = SCRIPT_VERSION_VIM9;
1406
Bram Moolenaar352134b2020-10-17 22:04:08 +02001407 // Use a specific location for storing error messages to be converted to an
1408 // exception.
1409 saved_msg_list = msg_list;
1410 msg_list = &private_msg_list;
1411
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02001412 // Do turn errors into exceptions.
1413 suppress_errthrow = FALSE;
1414
Bram Moolenaarc970e422021-03-17 15:03:04 +01001415 // Do not delete the function while executing it.
1416 ++ufunc->uf_calls;
1417
Bram Moolenaar56602ba2020-12-05 21:22:08 +01001418 // When ":silent!" was used before calling then we still abort the
1419 // function. If ":silent!" is used in the function then we don't.
1420 emsg_silent_def = emsg_silent;
1421 did_emsg_def = 0;
1422
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001423 where.wt_index = 0;
1424 where.wt_variable = FALSE;
1425
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01001426 // Decide where to start execution, handles optional arguments.
1427 init_instr_idx(ufunc, argc, &ectx);
1428
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001429 for (;;)
1430 {
1431 isn_T *iptr;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001432
Bram Moolenaar270d0382020-05-15 21:42:53 +02001433 if (++breakcheck_count >= 100)
1434 {
1435 line_breakcheck();
1436 breakcheck_count = 0;
1437 }
Bram Moolenaar20431c92020-03-20 18:39:46 +01001438 if (got_int)
1439 {
1440 // Turn CTRL-C into an exception.
1441 got_int = FALSE;
Bram Moolenaar97acfc72020-03-22 13:44:28 +01001442 if (throw_exception("Vim:Interrupt", ET_INTERRUPT, NULL) == FAIL)
Bram Moolenaar20431c92020-03-20 18:39:46 +01001443 goto failed;
1444 did_throw = TRUE;
1445 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001446
Bram Moolenaara26b9702020-04-18 19:53:28 +02001447 if (did_emsg && msg_list != NULL && *msg_list != NULL)
1448 {
1449 // Turn an error message into an exception.
1450 did_emsg = FALSE;
1451 if (throw_exception(*msg_list, ET_ERROR, NULL) == FAIL)
1452 goto failed;
1453 did_throw = TRUE;
1454 *msg_list = NULL;
1455 }
1456
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001457 if (did_throw && !ectx.ec_in_catch)
1458 {
1459 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001460 trycmd_T *trycmd = NULL;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001461
1462 // An exception jumps to the first catch, finally, or returns from
1463 // the current function.
1464 if (trystack->ga_len > 0)
1465 trycmd = ((trycmd_T *)trystack->ga_data) + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001466 if (trycmd != NULL && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001467 {
1468 // jump to ":catch" or ":finally"
1469 ectx.ec_in_catch = TRUE;
1470 ectx.ec_iidx = trycmd->tcd_catch_idx;
1471 }
1472 else
1473 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001474 // Not inside try or need to return from current functions.
1475 // Push a dummy return value.
1476 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1477 goto failed;
1478 tv = STACK_TV_BOT(0);
1479 tv->v_type = VAR_NUMBER;
1480 tv->vval.v_number = 0;
1481 ++ectx.ec_stack.ga_len;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001482 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001483 {
Bram Moolenaarcdd70f02020-08-13 21:40:18 +02001484 // At the toplevel we are done.
Bram Moolenaar257cc5e2020-02-19 17:06:11 +01001485 need_rethrow = TRUE;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001486 if (handle_closure_in_use(&ectx, FALSE) == FAIL)
1487 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001488 goto done;
1489 }
1490
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02001491 if (func_return(&ectx) == FAIL)
1492 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001493 }
1494 continue;
1495 }
1496
1497 iptr = &ectx.ec_instr[ectx.ec_iidx++];
1498 switch (iptr->isn_type)
1499 {
1500 // execute Ex command line
1501 case ISN_EXEC:
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001502 {
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001503 source_cookie_T cookie;
1504
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001505 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar9567efa2021-01-11 22:16:30 +01001506 // Pass getsourceline to get an error for a missing ":end"
1507 // command.
1508 CLEAR_FIELD(cookie);
1509 cookie.sourcing_lnum = iptr->isn_lnum - 1;
1510 if (do_cmdline(iptr->isn_arg.string,
1511 getsourceline, &cookie,
1512 DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_KEYTYPED)
1513 == FAIL
1514 || did_emsg)
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001515 goto on_error;
Bram Moolenaar631e8f92020-11-04 15:07:16 +01001516 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001517 break;
1518
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001519 // execute Ex command from pieces on the stack
1520 case ISN_EXECCONCAT:
1521 {
1522 int count = iptr->isn_arg.number;
Bram Moolenaar7f6f56f2020-04-30 20:21:43 +02001523 size_t len = 0;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001524 int pass;
1525 int i;
1526 char_u *cmd = NULL;
1527 char_u *str;
1528
1529 for (pass = 1; pass <= 2; ++pass)
1530 {
1531 for (i = 0; i < count; ++i)
1532 {
1533 tv = STACK_TV_BOT(i - count);
1534 str = tv->vval.v_string;
1535 if (str != NULL && *str != NUL)
1536 {
1537 if (pass == 2)
1538 STRCPY(cmd + len, str);
1539 len += STRLEN(str);
1540 }
1541 if (pass == 2)
1542 clear_tv(tv);
1543 }
1544 if (pass == 1)
1545 {
1546 cmd = alloc(len + 1);
1547 if (cmd == NULL)
1548 goto failed;
1549 len = 0;
1550 }
1551 }
1552
Bram Moolenaar6378c4f2020-04-26 13:50:41 +02001553 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02001554 do_cmdline_cmd(cmd);
1555 vim_free(cmd);
1556 }
1557 break;
1558
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001559 // execute :echo {string} ...
1560 case ISN_ECHO:
1561 {
1562 int count = iptr->isn_arg.echo.echo_count;
1563 int atstart = TRUE;
1564 int needclr = TRUE;
1565
1566 for (idx = 0; idx < count; ++idx)
1567 {
1568 tv = STACK_TV_BOT(idx - count);
1569 echo_one(tv, iptr->isn_arg.echo.echo_with_white,
1570 &atstart, &needclr);
1571 clear_tv(tv);
1572 }
Bram Moolenaare0807ea2020-02-20 22:18:06 +01001573 if (needclr)
1574 msg_clr_eos();
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001575 ectx.ec_stack.ga_len -= count;
1576 }
1577 break;
1578
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001579 // :execute {string} ...
1580 // :echomsg {string} ...
1581 // :echoerr {string} ...
Bram Moolenaarad39c092020-02-26 18:23:43 +01001582 case ISN_EXECUTE:
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001583 case ISN_ECHOMSG:
1584 case ISN_ECHOERR:
Bram Moolenaarad39c092020-02-26 18:23:43 +01001585 {
1586 int count = iptr->isn_arg.number;
1587 garray_T ga;
1588 char_u buf[NUMBUFLEN];
1589 char_u *p;
1590 int len;
1591 int failed = FALSE;
1592
1593 ga_init2(&ga, 1, 80);
1594 for (idx = 0; idx < count; ++idx)
1595 {
1596 tv = STACK_TV_BOT(idx - count);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001597 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaarad39c092020-02-26 18:23:43 +01001598 {
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001599 if (tv->v_type == VAR_CHANNEL
1600 || tv->v_type == VAR_JOB)
1601 {
1602 SOURCING_LNUM = iptr->isn_lnum;
1603 emsg(_(e_inval_string));
1604 break;
1605 }
1606 else
1607 p = tv_get_string_buf(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001608 }
1609 else
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001610 p = tv_stringify(tv, buf);
Bram Moolenaarad39c092020-02-26 18:23:43 +01001611
1612 len = (int)STRLEN(p);
1613 if (ga_grow(&ga, len + 2) == FAIL)
1614 failed = TRUE;
1615 else
1616 {
1617 if (ga.ga_len > 0)
1618 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
1619 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
1620 ga.ga_len += len;
1621 }
1622 clear_tv(tv);
1623 }
1624 ectx.ec_stack.ga_len -= count;
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001625 if (failed)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001626 {
1627 ga_clear(&ga);
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001628 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001629 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001630
Bram Moolenaare5abf7a2020-08-16 18:29:35 +02001631 if (ga.ga_data != NULL)
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001632 {
1633 if (iptr->isn_type == ISN_EXECUTE)
Bram Moolenaar430deb12020-08-23 16:29:11 +02001634 {
1635 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001636 do_cmdline_cmd((char_u *)ga.ga_data);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001637 if (did_emsg)
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001638 {
1639 ga_clear(&ga);
Bram Moolenaareeece9e2020-11-20 19:26:48 +01001640 goto on_error;
Bram Moolenaarc71ee822020-11-21 11:45:50 +01001641 }
Bram Moolenaar430deb12020-08-23 16:29:11 +02001642 }
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001643 else
1644 {
1645 msg_sb_eol();
1646 if (iptr->isn_type == ISN_ECHOMSG)
1647 {
1648 msg_attr(ga.ga_data, echo_attr);
1649 out_flush();
1650 }
1651 else
1652 {
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001653 SOURCING_LNUM = iptr->isn_lnum;
1654 emsg(ga.ga_data);
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02001655 }
1656 }
1657 }
Bram Moolenaarad39c092020-02-26 18:23:43 +01001658 ga_clear(&ga);
1659 }
1660 break;
1661
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001662 // load local variable or argument
1663 case ISN_LOAD:
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(STACK_TV_VAR(iptr->isn_arg.number), STACK_TV_BOT(0));
1667 ++ectx.ec_stack.ga_len;
1668 break;
1669
1670 // load v: variable
1671 case ISN_LOADV:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001672 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001673 goto failed;
1674 copy_tv(get_vim_var_tv(iptr->isn_arg.number), STACK_TV_BOT(0));
1675 ++ectx.ec_stack.ga_len;
1676 break;
1677
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001678 // load s: variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001679 case ISN_LOADSCRIPT:
1680 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001681 scriptref_T *sref = iptr->isn_arg.script.scriptref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001682 svar_T *sv;
1683
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001684 sv = get_script_svar(sref, &ectx);
1685 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001686 goto failed;
Bram Moolenaar3beaf9c2020-12-18 17:23:14 +01001687 allocate_if_null(sv->sv_tv);
Bram Moolenaar270d0382020-05-15 21:42:53 +02001688 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001689 goto failed;
1690 copy_tv(sv->sv_tv, STACK_TV_BOT(0));
1691 ++ectx.ec_stack.ga_len;
1692 }
1693 break;
1694
1695 // load s: variable in old script
1696 case ISN_LOADS:
1697 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001698 hashtab_T *ht = &SCRIPT_VARS(
1699 iptr->isn_arg.loadstore.ls_sid);
1700 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001701 dictitem_T *di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001702
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001703 if (di == NULL)
1704 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001705 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001706 semsg(_(e_undefined_variable_str), name);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001707 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001708 }
1709 else
1710 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001711 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001712 goto failed;
1713 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1714 ++ectx.ec_stack.ga_len;
1715 }
1716 }
1717 break;
1718
Bram Moolenaard3aac292020-04-19 14:32:17 +02001719 // load g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001720 case ISN_LOADG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001721 case ISN_LOADB:
1722 case ISN_LOADW:
1723 case ISN_LOADT:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001724 {
Bram Moolenaard3aac292020-04-19 14:32:17 +02001725 dictitem_T *di = NULL;
1726 hashtab_T *ht = NULL;
1727 char namespace;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001728
Bram Moolenaard3aac292020-04-19 14:32:17 +02001729 switch (iptr->isn_type)
1730 {
1731 case ISN_LOADG:
1732 ht = get_globvar_ht();
1733 namespace = 'g';
1734 break;
1735 case ISN_LOADB:
1736 ht = &curbuf->b_vars->dv_hashtab;
1737 namespace = 'b';
1738 break;
1739 case ISN_LOADW:
1740 ht = &curwin->w_vars->dv_hashtab;
1741 namespace = 'w';
1742 break;
1743 case ISN_LOADT:
1744 ht = &curtab->tp_vars->dv_hashtab;
1745 namespace = 't';
1746 break;
1747 default: // Cannot reach here
1748 goto failed;
1749 }
1750 di = find_var_in_ht(ht, 0, iptr->isn_arg.string, TRUE);
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001751
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001752 if (di == NULL)
1753 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001754 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02001755 semsg(_(e_undefined_variable_char_str),
Bram Moolenaard3aac292020-04-19 14:32:17 +02001756 namespace, iptr->isn_arg.string);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02001757 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001758 }
1759 else
1760 {
Bram Moolenaar270d0382020-05-15 21:42:53 +02001761 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001762 goto failed;
1763 copy_tv(&di->di_tv, STACK_TV_BOT(0));
1764 ++ectx.ec_stack.ga_len;
1765 }
1766 }
1767 break;
1768
Bram Moolenaar03290b82020-12-19 16:30:44 +01001769 // load autoload variable
1770 case ISN_LOADAUTO:
1771 {
1772 char_u *name = iptr->isn_arg.string;
1773
1774 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1775 goto failed;
1776 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar38a434f2021-01-02 12:45:45 +01001777 if (eval_variable(name, (int)STRLEN(name),
Bram Moolenaarcb4e80f2021-03-13 20:57:19 +01001778 STACK_TV_BOT(0), NULL, EVAL_VAR_VERBOSE) == FAIL)
Bram Moolenaar03290b82020-12-19 16:30:44 +01001779 goto on_error;
1780 ++ectx.ec_stack.ga_len;
1781 }
1782 break;
1783
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001784 // load g:/b:/w:/t: namespace
1785 case ISN_LOADGDICT:
1786 case ISN_LOADBDICT:
1787 case ISN_LOADWDICT:
1788 case ISN_LOADTDICT:
1789 {
1790 dict_T *d = NULL;
1791
1792 switch (iptr->isn_type)
1793 {
Bram Moolenaar682d0a12020-07-19 20:48:59 +02001794 case ISN_LOADGDICT: d = get_globvar_dict(); break;
1795 case ISN_LOADBDICT: d = curbuf->b_vars; break;
1796 case ISN_LOADWDICT: d = curwin->w_vars; break;
1797 case ISN_LOADTDICT: d = curtab->tp_vars; break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001798 default: // Cannot reach here
1799 goto failed;
1800 }
1801 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
1802 goto failed;
1803 tv = STACK_TV_BOT(0);
1804 tv->v_type = VAR_DICT;
1805 tv->v_lock = 0;
1806 tv->vval.v_dict = d;
Bram Moolenaar1bd3cb22021-02-24 12:27:31 +01001807 ++d->dv_refcount;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001808 ++ectx.ec_stack.ga_len;
1809 }
1810 break;
1811
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001812 // load &option
1813 case ISN_LOADOPT:
1814 {
1815 typval_T optval;
1816 char_u *name = iptr->isn_arg.string;
1817
Bram Moolenaara8c17702020-04-01 21:17:24 +02001818 // This is not expected to fail, name is checked during
1819 // compilation: don't set SOURCING_LNUM.
Bram Moolenaar270d0382020-05-15 21:42:53 +02001820 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001821 goto failed;
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001822 if (eval_option(&name, &optval, TRUE) == FAIL)
Bram Moolenaar58ceca52020-01-28 22:46:22 +01001823 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001824 *STACK_TV_BOT(0) = optval;
1825 ++ectx.ec_stack.ga_len;
1826 }
1827 break;
1828
1829 // load $ENV
1830 case ISN_LOADENV:
1831 {
1832 typval_T optval;
1833 char_u *name = iptr->isn_arg.string;
1834
Bram Moolenaar270d0382020-05-15 21:42:53 +02001835 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001836 goto failed;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001837 // name is always valid, checked when compiling
Bram Moolenaar9a78e6d2020-07-01 18:29:55 +02001838 (void)eval_env_var(&name, &optval, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001839 *STACK_TV_BOT(0) = optval;
1840 ++ectx.ec_stack.ga_len;
1841 }
1842 break;
1843
1844 // load @register
1845 case ISN_LOADREG:
Bram Moolenaar270d0382020-05-15 21:42:53 +02001846 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001847 goto failed;
1848 tv = STACK_TV_BOT(0);
1849 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02001850 tv->v_lock = 0;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02001851 // This may result in NULL, which should be equivalent to an
1852 // empty string.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001853 tv->vval.v_string = get_reg_contents(
1854 iptr->isn_arg.number, GREG_EXPR_SRC);
1855 ++ectx.ec_stack.ga_len;
1856 break;
1857
1858 // store local variable
1859 case ISN_STORE:
1860 --ectx.ec_stack.ga_len;
1861 tv = STACK_TV_VAR(iptr->isn_arg.number);
1862 clear_tv(tv);
1863 *tv = *STACK_TV_BOT(0);
1864 break;
1865
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001866 // store s: variable in old script
1867 case ISN_STORES:
1868 {
1869 hashtab_T *ht = &SCRIPT_VARS(
1870 iptr->isn_arg.loadstore.ls_sid);
1871 char_u *name = iptr->isn_arg.loadstore.ls_name;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001872 dictitem_T *di = find_var_in_ht(ht, 0, name + 2, TRUE);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001873
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001874 --ectx.ec_stack.ga_len;
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001875 if (di == NULL)
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02001876 store_var(name, STACK_TV_BOT(0));
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001877 else
1878 {
1879 clear_tv(&di->di_tv);
1880 di->di_tv = *STACK_TV_BOT(0);
1881 }
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001882 }
1883 break;
1884
1885 // store script-local variable in Vim9 script
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001886 case ISN_STORESCRIPT:
1887 {
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001888 scriptref_T *sref = iptr->isn_arg.script.scriptref;
1889 svar_T *sv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001890
Bram Moolenaar07a65d22020-12-26 20:09:15 +01001891 sv = get_script_svar(sref, &ectx);
1892 if (sv == NULL)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01001893 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001894 --ectx.ec_stack.ga_len;
1895 clear_tv(sv->sv_tv);
1896 *sv->sv_tv = *STACK_TV_BOT(0);
1897 }
1898 break;
1899
1900 // store option
1901 case ISN_STOREOPT:
1902 {
1903 long n = 0;
1904 char_u *s = NULL;
1905 char *msg;
1906
1907 --ectx.ec_stack.ga_len;
1908 tv = STACK_TV_BOT(0);
1909 if (tv->v_type == VAR_STRING)
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001910 {
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001911 s = tv->vval.v_string;
Bram Moolenaar97a2af32020-01-28 22:52:48 +01001912 if (s == NULL)
1913 s = (char_u *)"";
1914 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001915 else
Bram Moolenaara6e67e42020-05-15 23:36:40 +02001916 // must be VAR_NUMBER, CHECKTYPE makes sure
1917 n = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001918 msg = set_option_value(iptr->isn_arg.storeopt.so_name,
1919 n, s, iptr->isn_arg.storeopt.so_flags);
Bram Moolenaare75ba262020-05-16 15:43:31 +02001920 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001921 if (msg != NULL)
1922 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02001923 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001924 emsg(_(msg));
Bram Moolenaare8593122020-07-18 15:17:02 +02001925 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001926 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001927 }
1928 break;
1929
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001930 // store $ENV
1931 case ISN_STOREENV:
1932 --ectx.ec_stack.ga_len;
Bram Moolenaar20431c92020-03-20 18:39:46 +01001933 tv = STACK_TV_BOT(0);
1934 vim_setenv_ext(iptr->isn_arg.string, tv_get_string(tv));
1935 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001936 break;
1937
1938 // store @r
1939 case ISN_STOREREG:
1940 {
1941 int reg = iptr->isn_arg.number;
1942
1943 --ectx.ec_stack.ga_len;
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001944 tv = STACK_TV_BOT(0);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001945 write_reg_contents(reg == '@' ? '"' : reg,
Bram Moolenaar401d9ff2020-02-19 18:14:44 +01001946 tv_get_string(tv), -1, FALSE);
1947 clear_tv(tv);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001948 }
1949 break;
1950
1951 // store v: variable
1952 case ISN_STOREV:
1953 --ectx.ec_stack.ga_len;
1954 if (set_vim_var_tv(iptr->isn_arg.number, STACK_TV_BOT(0))
1955 == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02001956 // should not happen, type is checked when compiling
1957 goto on_error;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01001958 break;
1959
Bram Moolenaard3aac292020-04-19 14:32:17 +02001960 // store g:/b:/w:/t: variable
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001961 case ISN_STOREG:
Bram Moolenaard3aac292020-04-19 14:32:17 +02001962 case ISN_STOREB:
1963 case ISN_STOREW:
1964 case ISN_STORET:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001965 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001966 dictitem_T *di;
1967 hashtab_T *ht;
1968 char_u *name = iptr->isn_arg.string + 2;
1969
Bram Moolenaard3aac292020-04-19 14:32:17 +02001970 switch (iptr->isn_type)
1971 {
1972 case ISN_STOREG:
1973 ht = get_globvar_ht();
1974 break;
1975 case ISN_STOREB:
1976 ht = &curbuf->b_vars->dv_hashtab;
1977 break;
1978 case ISN_STOREW:
1979 ht = &curwin->w_vars->dv_hashtab;
1980 break;
1981 case ISN_STORET:
1982 ht = &curtab->tp_vars->dv_hashtab;
1983 break;
1984 default: // Cannot reach here
1985 goto failed;
1986 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001987
1988 --ectx.ec_stack.ga_len;
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001989 di = find_var_in_ht(ht, 0, name, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001990 if (di == NULL)
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01001991 store_var(iptr->isn_arg.string, STACK_TV_BOT(0));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001992 else
1993 {
Bram Moolenaar3bdc90b2020-12-22 20:35:40 +01001994 SOURCING_LNUM = iptr->isn_lnum;
1995 if (var_check_permission(di, name) == FAIL)
1996 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01001997 clear_tv(&di->di_tv);
1998 di->di_tv = *STACK_TV_BOT(0);
1999 }
2000 }
2001 break;
2002
Bram Moolenaar03290b82020-12-19 16:30:44 +01002003 // store an autoload variable
2004 case ISN_STOREAUTO:
2005 SOURCING_LNUM = iptr->isn_lnum;
2006 set_var(iptr->isn_arg.string, STACK_TV_BOT(-1), TRUE);
2007 clear_tv(STACK_TV_BOT(-1));
2008 --ectx.ec_stack.ga_len;
2009 break;
2010
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002011 // store number in local variable
2012 case ISN_STORENR:
Bram Moolenaara471eea2020-03-04 22:20:26 +01002013 tv = STACK_TV_VAR(iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002014 clear_tv(tv);
2015 tv->v_type = VAR_NUMBER;
Bram Moolenaara471eea2020-03-04 22:20:26 +01002016 tv->vval.v_number = iptr->isn_arg.storenr.stnr_val;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002017 break;
2018
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002019 // store value in list or dict variable
2020 case ISN_STOREINDEX:
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002021 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002022 vartype_T dest_type = iptr->isn_arg.vartype;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002023 typval_T *tv_idx = STACK_TV_BOT(-2);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002024 typval_T *tv_dest = STACK_TV_BOT(-1);
2025 int status = OK;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002026
Bram Moolenaar752fc692021-01-04 21:57:11 +01002027 // Stack contains:
2028 // -3 value to be stored
2029 // -2 index
2030 // -1 dict or list
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002031 tv = STACK_TV_BOT(-3);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002032 SOURCING_LNUM = iptr->isn_lnum;
2033 if (dest_type == VAR_ANY)
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002034 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002035 dest_type = tv_dest->v_type;
2036 if (dest_type == VAR_DICT)
2037 status = do_2string(tv_idx, TRUE);
2038 else if (dest_type == VAR_LIST
2039 && tv_idx->v_type != VAR_NUMBER)
2040 {
2041 emsg(_(e_number_exp));
2042 status = FAIL;
2043 }
2044 }
2045 else if (dest_type != tv_dest->v_type)
2046 {
2047 // just in case, should be OK
2048 semsg(_(e_expected_str_but_got_str),
2049 vartype_name(dest_type),
2050 vartype_name(tv_dest->v_type));
2051 status = FAIL;
2052 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002053
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002054 if (status == OK && dest_type == VAR_LIST)
2055 {
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002056 long lidx = (long)tv_idx->vval.v_number;
2057 list_T *list = tv_dest->vval.v_list;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002058
2059 if (list == NULL)
2060 {
2061 emsg(_(e_list_not_set));
2062 goto on_error;
2063 }
2064 if (lidx < 0 && list->lv_len + lidx >= 0)
2065 // negative index is relative to the end
2066 lidx = list->lv_len + lidx;
2067 if (lidx < 0 || lidx > list->lv_len)
2068 {
2069 semsg(_(e_listidx), lidx);
2070 goto on_error;
2071 }
2072 if (lidx < list->lv_len)
2073 {
2074 listitem_T *li = list_find(list, lidx);
2075
2076 if (error_if_locked(li->li_tv.v_lock,
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002077 e_cannot_change_list_item))
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002078 goto on_error;
2079 // overwrite existing list item
2080 clear_tv(&li->li_tv);
2081 li->li_tv = *tv;
2082 }
2083 else
2084 {
2085 if (error_if_locked(list->lv_lock,
2086 e_cannot_change_list))
2087 goto on_error;
2088 // append to list, only fails when out of memory
2089 if (list_append_tv(list, tv) == FAIL)
2090 goto failed;
2091 clear_tv(tv);
2092 }
2093 }
2094 else if (status == OK && dest_type == VAR_DICT)
2095 {
2096 char_u *key = tv_idx->vval.v_string;
2097 dict_T *dict = tv_dest->vval.v_dict;
2098 dictitem_T *di;
2099
2100 SOURCING_LNUM = iptr->isn_lnum;
2101 if (dict == NULL)
2102 {
2103 emsg(_(e_dictionary_not_set));
2104 goto on_error;
2105 }
2106 if (key == NULL)
2107 key = (char_u *)"";
2108 di = dict_find(dict, key, -1);
2109 if (di != NULL)
2110 {
2111 if (error_if_locked(di->di_tv.v_lock,
2112 e_cannot_change_dict_item))
2113 goto on_error;
2114 // overwrite existing value
2115 clear_tv(&di->di_tv);
2116 di->di_tv = *tv;
2117 }
2118 else
2119 {
2120 if (error_if_locked(dict->dv_lock,
2121 e_cannot_change_dict))
2122 goto on_error;
2123 // add to dict, only fails when out of memory
2124 if (dict_add_tv(dict, (char *)key, tv) == FAIL)
2125 goto failed;
2126 clear_tv(tv);
2127 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002128 }
2129 else
2130 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002131 status = FAIL;
2132 semsg(_(e_cannot_index_str), vartype_name(dest_type));
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002133 }
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002134
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002135 clear_tv(tv_idx);
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002136 clear_tv(tv_dest);
Bram Moolenaarf163bd52020-05-10 21:47:43 +02002137 ectx.ec_stack.ga_len -= 3;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002138 if (status == FAIL)
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002139 {
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01002140 clear_tv(tv);
Bram Moolenaar8e4c8c82020-08-01 15:38:38 +02002141 goto on_error;
2142 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02002143 }
2144 break;
2145
Bram Moolenaar0186e582021-01-10 18:33:11 +01002146 // load or store variable or argument from outer scope
2147 case ISN_LOADOUTER:
2148 case ISN_STOREOUTER:
2149 {
2150 int depth = iptr->isn_arg.outer.outer_depth;
2151 outer_T *outer = ectx.ec_outer;
2152
2153 while (depth > 1 && outer != NULL)
2154 {
2155 outer = outer->out_up;
2156 --depth;
2157 }
2158 if (outer == NULL)
2159 {
2160 SOURCING_LNUM = iptr->isn_lnum;
2161 iemsg("LOADOUTER depth more than scope levels");
2162 goto failed;
2163 }
2164 tv = ((typval_T *)outer->out_stack->ga_data)
2165 + outer->out_frame_idx + STACK_FRAME_SIZE
2166 + iptr->isn_arg.outer.outer_idx;
2167 if (iptr->isn_type == ISN_LOADOUTER)
2168 {
2169 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2170 goto failed;
2171 copy_tv(tv, STACK_TV_BOT(0));
2172 ++ectx.ec_stack.ga_len;
2173 }
2174 else
2175 {
2176 --ectx.ec_stack.ga_len;
2177 clear_tv(tv);
2178 *tv = *STACK_TV_BOT(0);
2179 }
2180 }
2181 break;
2182
Bram Moolenaar752fc692021-01-04 21:57:11 +01002183 // unlet item in list or dict variable
2184 case ISN_UNLETINDEX:
2185 {
2186 typval_T *tv_idx = STACK_TV_BOT(-2);
2187 typval_T *tv_dest = STACK_TV_BOT(-1);
2188 int status = OK;
2189
2190 // Stack contains:
2191 // -2 index
2192 // -1 dict or list
2193 if (tv_dest->v_type == VAR_DICT)
2194 {
2195 // unlet a dict item, index must be a string
2196 if (tv_idx->v_type != VAR_STRING)
2197 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002198 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002199 semsg(_(e_expected_str_but_got_str),
2200 vartype_name(VAR_STRING),
2201 vartype_name(tv_idx->v_type));
2202 status = FAIL;
2203 }
2204 else
2205 {
2206 dict_T *d = tv_dest->vval.v_dict;
2207 char_u *key = tv_idx->vval.v_string;
2208 dictitem_T *di = NULL;
2209
2210 if (key == NULL)
2211 key = (char_u *)"";
2212 if (d != NULL)
2213 di = dict_find(d, key, (int)STRLEN(key));
2214 if (di == NULL)
2215 {
2216 // NULL dict is equivalent to empty dict
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002217 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002218 semsg(_(e_dictkey), key);
2219 status = FAIL;
2220 }
2221 else
2222 {
2223 // TODO: check for dict or item locked
2224 dictitem_remove(d, di);
2225 }
2226 }
2227 }
2228 else if (tv_dest->v_type == VAR_LIST)
2229 {
2230 // unlet a List item, index must be a number
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002231 SOURCING_LNUM = iptr->isn_lnum;
2232 if (check_for_number(tv_idx) == FAIL)
Bram Moolenaar752fc692021-01-04 21:57:11 +01002233 {
Bram Moolenaar752fc692021-01-04 21:57:11 +01002234 status = FAIL;
2235 }
2236 else
2237 {
2238 list_T *l = tv_dest->vval.v_list;
Bram Moolenaar239f8d92021-01-17 13:21:20 +01002239 long n = (long)tv_idx->vval.v_number;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002240 listitem_T *li = NULL;
2241
2242 li = list_find(l, n);
2243 if (li == NULL)
2244 {
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01002245 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar752fc692021-01-04 21:57:11 +01002246 semsg(_(e_listidx), n);
2247 status = FAIL;
2248 }
2249 else
2250 // TODO: check for list or item locked
2251 listitem_remove(l, li);
2252 }
2253 }
2254 else
2255 {
2256 status = FAIL;
2257 semsg(_(e_cannot_index_str),
2258 vartype_name(tv_dest->v_type));
2259 }
2260
2261 clear_tv(tv_idx);
2262 clear_tv(tv_dest);
2263 ectx.ec_stack.ga_len -= 2;
2264 if (status == FAIL)
2265 goto on_error;
2266 }
2267 break;
2268
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01002269 // unlet range of items in list variable
2270 case ISN_UNLETRANGE:
2271 {
2272 // Stack contains:
2273 // -3 index1
2274 // -2 index2
2275 // -1 dict or list
2276 typval_T *tv_idx1 = STACK_TV_BOT(-3);
2277 typval_T *tv_idx2 = STACK_TV_BOT(-2);
2278 typval_T *tv_dest = STACK_TV_BOT(-1);
2279 int status = OK;
2280
2281 if (tv_dest->v_type == VAR_LIST)
2282 {
2283 // indexes must be a number
2284 SOURCING_LNUM = iptr->isn_lnum;
2285 if (check_for_number(tv_idx1) == FAIL
2286 || check_for_number(tv_idx2) == FAIL)
2287 {
2288 status = FAIL;
2289 }
2290 else
2291 {
2292 list_T *l = tv_dest->vval.v_list;
2293 long n1 = (long)tv_idx1->vval.v_number;
2294 long n2 = (long)tv_idx2->vval.v_number;
2295 listitem_T *li;
2296
2297 li = list_find_index(l, &n1);
2298 if (li == NULL
2299 || list_unlet_range(l, li, NULL, n1,
2300 TRUE, n2) == FAIL)
2301 status = FAIL;
2302 }
2303 }
2304 else
2305 {
2306 status = FAIL;
2307 SOURCING_LNUM = iptr->isn_lnum;
2308 semsg(_(e_cannot_index_str),
2309 vartype_name(tv_dest->v_type));
2310 }
2311
2312 clear_tv(tv_idx1);
2313 clear_tv(tv_idx2);
2314 clear_tv(tv_dest);
2315 ectx.ec_stack.ga_len -= 3;
2316 if (status == FAIL)
2317 goto on_error;
2318 }
2319 break;
2320
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002321 // push constant
2322 case ISN_PUSHNR:
2323 case ISN_PUSHBOOL:
2324 case ISN_PUSHSPEC:
2325 case ISN_PUSHF:
2326 case ISN_PUSHS:
2327 case ISN_PUSHBLOB:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002328 case ISN_PUSHFUNC:
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002329 case ISN_PUSHCHANNEL:
2330 case ISN_PUSHJOB:
Bram Moolenaar270d0382020-05-15 21:42:53 +02002331 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002332 goto failed;
2333 tv = STACK_TV_BOT(0);
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002334 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002335 ++ectx.ec_stack.ga_len;
2336 switch (iptr->isn_type)
2337 {
2338 case ISN_PUSHNR:
2339 tv->v_type = VAR_NUMBER;
2340 tv->vval.v_number = iptr->isn_arg.number;
2341 break;
2342 case ISN_PUSHBOOL:
2343 tv->v_type = VAR_BOOL;
2344 tv->vval.v_number = iptr->isn_arg.number;
2345 break;
2346 case ISN_PUSHSPEC:
2347 tv->v_type = VAR_SPECIAL;
2348 tv->vval.v_number = iptr->isn_arg.number;
2349 break;
2350#ifdef FEAT_FLOAT
2351 case ISN_PUSHF:
2352 tv->v_type = VAR_FLOAT;
2353 tv->vval.v_float = iptr->isn_arg.fnumber;
2354 break;
2355#endif
2356 case ISN_PUSHBLOB:
2357 blob_copy(iptr->isn_arg.blob, tv);
2358 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002359 case ISN_PUSHFUNC:
2360 tv->v_type = VAR_FUNC;
Bram Moolenaar087d2e12020-03-01 15:36:42 +01002361 if (iptr->isn_arg.string == NULL)
2362 tv->vval.v_string = NULL;
2363 else
2364 tv->vval.v_string =
2365 vim_strsave(iptr->isn_arg.string);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002366 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01002367 case ISN_PUSHCHANNEL:
2368#ifdef FEAT_JOB_CHANNEL
2369 tv->v_type = VAR_CHANNEL;
2370 tv->vval.v_channel = iptr->isn_arg.channel;
2371 if (tv->vval.v_channel != NULL)
2372 ++tv->vval.v_channel->ch_refcount;
2373#endif
2374 break;
2375 case ISN_PUSHJOB:
2376#ifdef FEAT_JOB_CHANNEL
2377 tv->v_type = VAR_JOB;
2378 tv->vval.v_job = iptr->isn_arg.job;
2379 if (tv->vval.v_job != NULL)
2380 ++tv->vval.v_job->jv_refcount;
2381#endif
2382 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002383 default:
2384 tv->v_type = VAR_STRING;
Bram Moolenaare69f6d02020-04-01 22:11:01 +02002385 tv->vval.v_string = vim_strsave(
2386 iptr->isn_arg.string == NULL
2387 ? (char_u *)"" : iptr->isn_arg.string);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002388 }
2389 break;
2390
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002391 case ISN_UNLET:
2392 if (do_unlet(iptr->isn_arg.unlet.ul_name,
2393 iptr->isn_arg.unlet.ul_forceit) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002394 goto on_error;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002395 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02002396 case ISN_UNLETENV:
2397 vim_unsetenv(iptr->isn_arg.unlet.ul_name);
2398 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02002399
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02002400 case ISN_LOCKCONST:
2401 item_lock(STACK_TV_BOT(-1), 100, TRUE, TRUE);
2402 break;
2403
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002404 // create a list from items on the stack; uses a single allocation
2405 // for the list header and the items
2406 case ISN_NEWLIST:
Bram Moolenaarfe270812020-04-11 22:31:27 +02002407 if (exe_newlist(iptr->isn_arg.number, &ectx) == FAIL)
2408 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002409 break;
2410
2411 // create a dict from items on the stack
2412 case ISN_NEWDICT:
2413 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002414 int count = iptr->isn_arg.number;
2415 dict_T *dict = dict_alloc();
2416 dictitem_T *item;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002417 char_u *key;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002418
2419 if (dict == NULL)
2420 goto failed;
2421 for (idx = 0; idx < count; ++idx)
2422 {
Bram Moolenaare8593122020-07-18 15:17:02 +02002423 // have already checked key type is VAR_STRING
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002424 tv = STACK_TV_BOT(2 * (idx - count));
Bram Moolenaare8593122020-07-18 15:17:02 +02002425 // check key is unique
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002426 key = tv->vval.v_string == NULL
2427 ? (char_u *)"" : tv->vval.v_string;
2428 item = dict_find(dict, key, -1);
Bram Moolenaare8593122020-07-18 15:17:02 +02002429 if (item != NULL)
2430 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002431 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002432 semsg(_(e_duplicate_key), key);
Bram Moolenaare8593122020-07-18 15:17:02 +02002433 dict_unref(dict);
2434 goto on_error;
2435 }
Bram Moolenaarc7f7f6d2020-11-04 13:38:28 +01002436 item = dictitem_alloc(key);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002437 clear_tv(tv);
2438 if (item == NULL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002439 {
2440 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 item->di_tv = *STACK_TV_BOT(2 * (idx - count) + 1);
2444 item->di_tv.v_lock = 0;
2445 if (dict_add(dict, item) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002446 {
Bram Moolenaard032f342020-07-18 18:13:02 +02002447 // can this ever happen?
Bram Moolenaare8593122020-07-18 15:17:02 +02002448 dict_unref(dict);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002449 goto failed;
Bram Moolenaare8593122020-07-18 15:17:02 +02002450 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002451 }
2452
2453 if (count > 0)
2454 ectx.ec_stack.ga_len -= 2 * count - 1;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002455 else if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002456 goto failed;
2457 else
2458 ++ectx.ec_stack.ga_len;
2459 tv = STACK_TV_BOT(-1);
2460 tv->v_type = VAR_DICT;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002461 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002462 tv->vval.v_dict = dict;
2463 ++dict->dv_refcount;
2464 }
2465 break;
2466
2467 // call a :def function
2468 case ISN_DCALL:
Bram Moolenaardfa3d552020-09-10 22:05:08 +02002469 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar0186e582021-01-10 18:33:11 +01002470 if (call_dfunc(iptr->isn_arg.dfunc.cdf_idx, NULL,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002471 iptr->isn_arg.dfunc.cdf_argcount,
2472 &ectx) == FAIL)
Bram Moolenaare8593122020-07-18 15:17:02 +02002473 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002474 break;
2475
2476 // call a builtin function
2477 case ISN_BCALL:
2478 SOURCING_LNUM = iptr->isn_lnum;
2479 if (call_bfunc(iptr->isn_arg.bfunc.cbf_idx,
2480 iptr->isn_arg.bfunc.cbf_argcount,
2481 &ectx) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002482 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002483 break;
2484
2485 // call a funcref or partial
2486 case ISN_PCALL:
2487 {
2488 cpfunc_T *pfunc = &iptr->isn_arg.pfunc;
2489 int r;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002490 typval_T partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002491
2492 SOURCING_LNUM = iptr->isn_lnum;
2493 if (pfunc->cpf_top)
2494 {
2495 // funcref is above the arguments
2496 tv = STACK_TV_BOT(-pfunc->cpf_argcount - 1);
2497 }
2498 else
2499 {
2500 // Get the funcref from the stack.
2501 --ectx.ec_stack.ga_len;
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002502 partial_tv = *STACK_TV_BOT(0);
2503 tv = &partial_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002504 }
2505 r = call_partial(tv, pfunc->cpf_argcount, &ectx);
Bram Moolenaar6f5b6df2020-05-16 21:20:12 +02002506 if (tv == &partial_tv)
2507 clear_tv(&partial_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002508 if (r == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002509 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002510 }
2511 break;
2512
Bram Moolenaarbd5da372020-03-31 23:13:10 +02002513 case ISN_PCALL_END:
2514 // PCALL finished, arguments have been consumed and replaced by
2515 // the return value. Now clear the funcref from the stack,
2516 // and move the return value in its place.
2517 --ectx.ec_stack.ga_len;
2518 clear_tv(STACK_TV_BOT(-1));
2519 *STACK_TV_BOT(-1) = *STACK_TV_BOT(0);
2520 break;
2521
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002522 // call a user defined function or funcref/partial
2523 case ISN_UCALL:
2524 {
2525 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
2526
2527 SOURCING_LNUM = iptr->isn_lnum;
2528 if (call_eval_func(cufunc->cuf_name,
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01002529 cufunc->cuf_argcount, &ectx, iptr) == FAIL)
Bram Moolenaard032f342020-07-18 18:13:02 +02002530 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002531 }
2532 break;
2533
2534 // return from a :def function call
Bram Moolenaar299f3032021-01-08 20:53:09 +01002535 case ISN_RETURN_ZERO:
2536 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
2537 goto failed;
2538 tv = STACK_TV_BOT(0);
2539 ++ectx.ec_stack.ga_len;
2540 tv->v_type = VAR_NUMBER;
2541 tv->vval.v_number = 0;
2542 tv->v_lock = 0;
2543 // FALLTHROUGH
2544
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002545 case ISN_RETURN:
2546 {
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002547 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002548 trycmd_T *trycmd = NULL;
Bram Moolenaar8cbd6df2020-01-28 22:59:45 +01002549
2550 if (trystack->ga_len > 0)
2551 trycmd = ((trycmd_T *)trystack->ga_data)
2552 + trystack->ga_len - 1;
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002553 if (trycmd != NULL
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002554 && trycmd->tcd_frame_idx == ectx.ec_frame_idx)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002555 {
Bram Moolenaar9cb577a2021-02-22 22:45:10 +01002556 // jump to ":finally" or ":endtry"
2557 if (trycmd->tcd_finally_idx != 0)
2558 ectx.ec_iidx = trycmd->tcd_finally_idx;
2559 else
2560 ectx.ec_iidx = trycmd->tcd_endtry_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002561 trycmd->tcd_return = TRUE;
2562 }
2563 else
Bram Moolenaard032f342020-07-18 18:13:02 +02002564 goto func_return;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002565 }
2566 break;
2567
2568 // push a function reference to a compiled function
2569 case ISN_FUNCREF:
2570 {
Bram Moolenaarf112f302020-12-20 17:47:52 +01002571 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
2572 dfunc_T *pt_dfunc = ((dfunc_T *)def_functions.ga_data)
2573 + iptr->isn_arg.funcref.fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002574
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002575 if (pt == NULL)
2576 goto failed;
Bram Moolenaar270d0382020-05-15 21:42:53 +02002577 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002578 {
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002579 vim_free(pt);
2580 goto failed;
2581 }
Bram Moolenaarf112f302020-12-20 17:47:52 +01002582 if (fill_partial_and_closure(pt, pt_dfunc->df_ufunc,
2583 &ectx) == FAIL)
2584 goto failed;
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02002585
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002586 tv = STACK_TV_BOT(0);
2587 ++ectx.ec_stack.ga_len;
2588 tv->vval.v_partial = pt;
2589 tv->v_type = VAR_PARTIAL;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002590 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002591 }
2592 break;
2593
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002594 // Create a global function from a lambda.
2595 case ISN_NEWFUNC:
2596 {
2597 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
2598
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002599 if (copy_func(newfunc->nf_lambda, newfunc->nf_global,
Bram Moolenaarf112f302020-12-20 17:47:52 +01002600 &ectx) == FAIL)
Bram Moolenaarcd45ed02020-12-22 17:35:54 +01002601 goto failed;
Bram Moolenaar38ddf332020-07-31 22:05:04 +02002602 }
2603 break;
2604
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01002605 // List functions
2606 case ISN_DEF:
2607 if (iptr->isn_arg.string == NULL)
2608 list_functions(NULL);
2609 else
2610 {
2611 exarg_T ea;
2612
2613 CLEAR_FIELD(ea);
2614 ea.cmd = ea.arg = iptr->isn_arg.string;
2615 define_function(&ea, NULL);
2616 }
2617 break;
2618
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002619 // jump if a condition is met
2620 case ISN_JUMP:
2621 {
2622 jumpwhen_T when = iptr->isn_arg.jump.jump_when;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002623 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002624 int jump = TRUE;
2625
2626 if (when != JUMP_ALWAYS)
2627 {
2628 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002629 if (when == JUMP_IF_COND_FALSE
Bram Moolenaar13106602020-10-04 16:06:05 +02002630 || when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002631 || when == JUMP_IF_COND_TRUE)
2632 {
2633 SOURCING_LNUM = iptr->isn_lnum;
2634 jump = tv_get_bool_chk(tv, &error);
2635 if (error)
2636 goto on_error;
2637 }
2638 else
2639 jump = tv2bool(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002640 if (when == JUMP_IF_FALSE
Bram Moolenaar2bb26582020-10-03 22:52:39 +02002641 || when == JUMP_AND_KEEP_IF_FALSE
2642 || when == JUMP_IF_COND_FALSE)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002643 jump = !jump;
Bram Moolenaar777770f2020-02-06 21:27:08 +01002644 if (when == JUMP_IF_FALSE || !jump)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002645 {
2646 // drop the value from the stack
2647 clear_tv(tv);
2648 --ectx.ec_stack.ga_len;
2649 }
2650 }
2651 if (jump)
2652 ectx.ec_iidx = iptr->isn_arg.jump.jump_where;
2653 }
2654 break;
2655
2656 // top of a for loop
2657 case ISN_FOR:
2658 {
2659 list_T *list = STACK_TV_BOT(-1)->vval.v_list;
2660 typval_T *idxtv =
2661 STACK_TV_VAR(iptr->isn_arg.forloop.for_idx);
2662
2663 // push the next item from the list
Bram Moolenaar270d0382020-05-15 21:42:53 +02002664 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002665 goto failed;
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02002666 ++idxtv->vval.v_number;
2667 if (list == NULL || idxtv->vval.v_number >= list->lv_len)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002668 // past the end of the list, jump to "endfor"
2669 ectx.ec_iidx = iptr->isn_arg.forloop.for_end;
2670 else if (list->lv_first == &range_list_item)
2671 {
2672 // non-materialized range() list
2673 tv = STACK_TV_BOT(0);
2674 tv->v_type = VAR_NUMBER;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002675 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002676 tv->vval.v_number = list_find_nr(
2677 list, idxtv->vval.v_number, NULL);
2678 ++ectx.ec_stack.ga_len;
2679 }
2680 else
2681 {
2682 listitem_T *li = list_find(list, idxtv->vval.v_number);
2683
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002684 copy_tv(&li->li_tv, STACK_TV_BOT(0));
2685 ++ectx.ec_stack.ga_len;
2686 }
2687 }
2688 break;
2689
2690 // start of ":try" block
2691 case ISN_TRY:
2692 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002693 trycmd_T *trycmd = NULL;
2694
Bram Moolenaar270d0382020-05-15 21:42:53 +02002695 if (GA_GROW(&ectx.ec_trystack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002696 goto failed;
2697 trycmd = ((trycmd_T *)ectx.ec_trystack.ga_data)
2698 + ectx.ec_trystack.ga_len;
2699 ++ectx.ec_trystack.ga_len;
2700 ++trylevel;
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002701 CLEAR_POINTER(trycmd);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02002702 trycmd->tcd_frame_idx = ectx.ec_frame_idx;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002703 trycmd->tcd_stack_len = ectx.ec_stack.ga_len;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002704 trycmd->tcd_catch_idx = iptr->isn_arg.try.try_ref->try_catch;
2705 trycmd->tcd_finally_idx = iptr->isn_arg.try.try_ref->try_finally;
2706 trycmd->tcd_endtry_idx = iptr->isn_arg.try.try_ref->try_endtry;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002707 }
2708 break;
2709
2710 case ISN_PUSHEXC:
2711 if (current_exception == NULL)
2712 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02002713 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002714 iemsg("Evaluating catch while current_exception is NULL");
2715 goto failed;
2716 }
Bram Moolenaar270d0382020-05-15 21:42:53 +02002717 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002718 goto failed;
2719 tv = STACK_TV_BOT(0);
2720 ++ectx.ec_stack.ga_len;
2721 tv->v_type = VAR_STRING;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02002722 tv->v_lock = 0;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002723 tv->vval.v_string = vim_strsave(
2724 (char_u *)current_exception->value);
2725 break;
2726
2727 case ISN_CATCH:
2728 {
2729 garray_T *trystack = &ectx.ec_trystack;
2730
Bram Moolenaar20a76292020-12-25 19:47:24 +01002731 if (restore_cmdmod)
2732 {
2733 cmdmod.cmod_filter_regmatch.regprog = NULL;
2734 undo_cmdmod(&cmdmod);
2735 cmdmod = save_cmdmod;
2736 restore_cmdmod = FALSE;
2737 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002738 if (trystack->ga_len > 0)
2739 {
Bram Moolenaar20431c92020-03-20 18:39:46 +01002740 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002741 + trystack->ga_len - 1;
2742 trycmd->tcd_caught = TRUE;
2743 }
2744 did_emsg = got_int = did_throw = FALSE;
Bram Moolenaar1430cee2021-01-17 19:20:32 +01002745 force_abort = need_rethrow = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002746 catch_exception(current_exception);
2747 }
2748 break;
2749
Bram Moolenaarc150c092021-02-13 15:02:46 +01002750 case ISN_TRYCONT:
2751 {
2752 garray_T *trystack = &ectx.ec_trystack;
2753 trycont_T *trycont = &iptr->isn_arg.trycont;
2754 int i;
2755 trycmd_T *trycmd;
2756 int iidx = trycont->tct_where;
2757
2758 if (trystack->ga_len < trycont->tct_levels)
2759 {
2760 siemsg("TRYCONT: expected %d levels, found %d",
2761 trycont->tct_levels, trystack->ga_len);
2762 goto failed;
2763 }
2764 // Make :endtry jump to any outer try block and the last
2765 // :endtry inside the loop to the loop start.
2766 for (i = trycont->tct_levels; i > 0; --i)
2767 {
2768 trycmd = ((trycmd_T *)trystack->ga_data)
2769 + trystack->ga_len - i;
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002770 // Add one to tcd_cont to be able to jump to
2771 // instruction with index zero.
2772 trycmd->tcd_cont = iidx + 1;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002773 iidx = trycmd->tcd_finally_idx == 0
2774 ? trycmd->tcd_endtry_idx : trycmd->tcd_finally_idx;
Bram Moolenaarc150c092021-02-13 15:02:46 +01002775 }
2776 // jump to :finally or :endtry of current try statement
2777 ectx.ec_iidx = iidx;
2778 }
2779 break;
2780
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01002781 case ISN_FINALLY:
2782 {
2783 garray_T *trystack = &ectx.ec_trystack;
2784 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2785 + trystack->ga_len - 1;
2786
2787 // Reset the index to avoid a return statement jumps here
2788 // again.
2789 trycmd->tcd_finally_idx = 0;
2790 break;
2791 }
2792
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002793 // end of ":try" block
2794 case ISN_ENDTRY:
2795 {
2796 garray_T *trystack = &ectx.ec_trystack;
2797
2798 if (trystack->ga_len > 0)
2799 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002800 trycmd_T *trycmd;
Bram Moolenaar20431c92020-03-20 18:39:46 +01002801
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002802 --trystack->ga_len;
2803 --trylevel;
Bram Moolenaar68d130c2020-07-17 22:06:44 +02002804 ectx.ec_in_catch = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002805 trycmd = ((trycmd_T *)trystack->ga_data)
2806 + trystack->ga_len;
Bram Moolenaarf575adf2020-02-20 20:41:06 +01002807 if (trycmd->tcd_caught && current_exception != NULL)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002808 {
2809 // discard the exception
2810 if (caught_stack == current_exception)
2811 caught_stack = caught_stack->caught;
2812 discard_current_exception();
2813 }
2814
2815 if (trycmd->tcd_return)
Bram Moolenaard032f342020-07-18 18:13:02 +02002816 goto func_return;
Bram Moolenaard9d77892021-02-12 21:32:47 +01002817
2818 while (ectx.ec_stack.ga_len > trycmd->tcd_stack_len)
2819 {
2820 --ectx.ec_stack.ga_len;
2821 clear_tv(STACK_TV_BOT(0));
2822 }
Bram Moolenaar8d4be892021-02-13 18:33:02 +01002823 if (trycmd->tcd_cont != 0)
Bram Moolenaarc150c092021-02-13 15:02:46 +01002824 // handling :continue: jump to outer try block or
2825 // start of the loop
Bram Moolenaar2e34c342021-03-14 12:13:33 +01002826 ectx.ec_iidx = trycmd->tcd_cont - 1;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002827 }
2828 }
2829 break;
2830
2831 case ISN_THROW:
Bram Moolenaar8f81b222021-01-14 21:47:06 +01002832 {
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002833 garray_T *trystack = &ectx.ec_trystack;
Bram Moolenaar1e021e62020-10-16 20:25:23 +02002834
Bram Moolenaar107e9ce2021-01-24 20:52:00 +01002835 if (trystack->ga_len == 0 && trylevel == 0 && emsg_silent)
2836 {
2837 // throwing an exception while using "silent!" causes
2838 // the function to abort but not display an error.
2839 tv = STACK_TV_BOT(-1);
2840 clear_tv(tv);
2841 tv->v_type = VAR_NUMBER;
2842 tv->vval.v_number = 0;
2843 goto done;
2844 }
2845 --ectx.ec_stack.ga_len;
2846 tv = STACK_TV_BOT(0);
2847 if (tv->vval.v_string == NULL
2848 || *skipwhite(tv->vval.v_string) == NUL)
2849 {
2850 vim_free(tv->vval.v_string);
2851 SOURCING_LNUM = iptr->isn_lnum;
2852 emsg(_(e_throw_with_empty_string));
2853 goto failed;
2854 }
2855
2856 // Inside a "catch" we need to first discard the caught
2857 // exception.
2858 if (trystack->ga_len > 0)
2859 {
2860 trycmd_T *trycmd = ((trycmd_T *)trystack->ga_data)
2861 + trystack->ga_len - 1;
2862 if (trycmd->tcd_caught && current_exception != NULL)
2863 {
2864 // discard the exception
2865 if (caught_stack == current_exception)
2866 caught_stack = caught_stack->caught;
2867 discard_current_exception();
2868 trycmd->tcd_caught = FALSE;
2869 }
2870 }
2871
2872 if (throw_exception(tv->vval.v_string, ET_USER, NULL)
2873 == FAIL)
2874 {
2875 vim_free(tv->vval.v_string);
2876 goto failed;
2877 }
2878 did_throw = TRUE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002879 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002880 break;
2881
2882 // compare with special values
2883 case ISN_COMPAREBOOL:
2884 case ISN_COMPARESPECIAL:
2885 {
2886 typval_T *tv1 = STACK_TV_BOT(-2);
2887 typval_T *tv2 = STACK_TV_BOT(-1);
2888 varnumber_T arg1 = tv1->vval.v_number;
2889 varnumber_T arg2 = tv2->vval.v_number;
2890 int res;
2891
2892 switch (iptr->isn_arg.op.op_type)
2893 {
2894 case EXPR_EQUAL: res = arg1 == arg2; break;
2895 case EXPR_NEQUAL: res = arg1 != arg2; break;
2896 default: res = 0; break;
2897 }
2898
2899 --ectx.ec_stack.ga_len;
2900 tv1->v_type = VAR_BOOL;
2901 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2902 }
2903 break;
2904
2905 // Operation with two number arguments
2906 case ISN_OPNR:
2907 case ISN_COMPARENR:
2908 {
2909 typval_T *tv1 = STACK_TV_BOT(-2);
2910 typval_T *tv2 = STACK_TV_BOT(-1);
2911 varnumber_T arg1 = tv1->vval.v_number;
2912 varnumber_T arg2 = tv2->vval.v_number;
2913 varnumber_T res;
2914
2915 switch (iptr->isn_arg.op.op_type)
2916 {
2917 case EXPR_MULT: res = arg1 * arg2; break;
2918 case EXPR_DIV: res = arg1 / arg2; break;
2919 case EXPR_REM: res = arg1 % arg2; break;
2920 case EXPR_SUB: res = arg1 - arg2; break;
2921 case EXPR_ADD: res = arg1 + arg2; break;
2922
2923 case EXPR_EQUAL: res = arg1 == arg2; break;
2924 case EXPR_NEQUAL: res = arg1 != arg2; break;
2925 case EXPR_GREATER: res = arg1 > arg2; break;
2926 case EXPR_GEQUAL: res = arg1 >= arg2; break;
2927 case EXPR_SMALLER: res = arg1 < arg2; break;
2928 case EXPR_SEQUAL: res = arg1 <= arg2; break;
2929 default: res = 0; break;
2930 }
2931
2932 --ectx.ec_stack.ga_len;
2933 if (iptr->isn_type == ISN_COMPARENR)
2934 {
2935 tv1->v_type = VAR_BOOL;
2936 tv1->vval.v_number = res ? VVAL_TRUE : VVAL_FALSE;
2937 }
2938 else
2939 tv1->vval.v_number = res;
2940 }
2941 break;
2942
2943 // Computation with two float arguments
2944 case ISN_OPFLOAT:
2945 case ISN_COMPAREFLOAT:
Bram Moolenaara5d59532020-01-26 21:42:03 +01002946#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002947 {
2948 typval_T *tv1 = STACK_TV_BOT(-2);
2949 typval_T *tv2 = STACK_TV_BOT(-1);
2950 float_T arg1 = tv1->vval.v_float;
2951 float_T arg2 = tv2->vval.v_float;
2952 float_T res = 0;
2953 int cmp = FALSE;
2954
2955 switch (iptr->isn_arg.op.op_type)
2956 {
2957 case EXPR_MULT: res = arg1 * arg2; break;
2958 case EXPR_DIV: res = arg1 / arg2; break;
2959 case EXPR_SUB: res = arg1 - arg2; break;
2960 case EXPR_ADD: res = arg1 + arg2; break;
2961
2962 case EXPR_EQUAL: cmp = arg1 == arg2; break;
2963 case EXPR_NEQUAL: cmp = arg1 != arg2; break;
2964 case EXPR_GREATER: cmp = arg1 > arg2; break;
2965 case EXPR_GEQUAL: cmp = arg1 >= arg2; break;
2966 case EXPR_SMALLER: cmp = arg1 < arg2; break;
2967 case EXPR_SEQUAL: cmp = arg1 <= arg2; break;
2968 default: cmp = 0; break;
2969 }
2970 --ectx.ec_stack.ga_len;
2971 if (iptr->isn_type == ISN_COMPAREFLOAT)
2972 {
2973 tv1->v_type = VAR_BOOL;
2974 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
2975 }
2976 else
2977 tv1->vval.v_float = res;
2978 }
Bram Moolenaara5d59532020-01-26 21:42:03 +01002979#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01002980 break;
2981
2982 case ISN_COMPARELIST:
2983 {
2984 typval_T *tv1 = STACK_TV_BOT(-2);
2985 typval_T *tv2 = STACK_TV_BOT(-1);
2986 list_T *arg1 = tv1->vval.v_list;
2987 list_T *arg2 = tv2->vval.v_list;
2988 int cmp = FALSE;
2989 int ic = iptr->isn_arg.op.op_ic;
2990
2991 switch (iptr->isn_arg.op.op_type)
2992 {
2993 case EXPR_EQUAL: cmp =
2994 list_equal(arg1, arg2, ic, FALSE); break;
2995 case EXPR_NEQUAL: cmp =
2996 !list_equal(arg1, arg2, ic, FALSE); break;
2997 case EXPR_IS: cmp = arg1 == arg2; break;
2998 case EXPR_ISNOT: cmp = arg1 != arg2; break;
2999 default: cmp = 0; break;
3000 }
3001 --ectx.ec_stack.ga_len;
3002 clear_tv(tv1);
3003 clear_tv(tv2);
3004 tv1->v_type = VAR_BOOL;
3005 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3006 }
3007 break;
3008
3009 case ISN_COMPAREBLOB:
3010 {
3011 typval_T *tv1 = STACK_TV_BOT(-2);
3012 typval_T *tv2 = STACK_TV_BOT(-1);
3013 blob_T *arg1 = tv1->vval.v_blob;
3014 blob_T *arg2 = tv2->vval.v_blob;
3015 int cmp = FALSE;
3016
3017 switch (iptr->isn_arg.op.op_type)
3018 {
3019 case EXPR_EQUAL: cmp = blob_equal(arg1, arg2); break;
3020 case EXPR_NEQUAL: cmp = !blob_equal(arg1, arg2); break;
3021 case EXPR_IS: cmp = arg1 == arg2; break;
3022 case EXPR_ISNOT: cmp = arg1 != arg2; break;
3023 default: cmp = 0; break;
3024 }
3025 --ectx.ec_stack.ga_len;
3026 clear_tv(tv1);
3027 clear_tv(tv2);
3028 tv1->v_type = VAR_BOOL;
3029 tv1->vval.v_number = cmp ? VVAL_TRUE : VVAL_FALSE;
3030 }
3031 break;
3032
3033 // TODO: handle separately
3034 case ISN_COMPARESTRING:
3035 case ISN_COMPAREDICT:
3036 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003037 case ISN_COMPAREANY:
3038 {
3039 typval_T *tv1 = STACK_TV_BOT(-2);
3040 typval_T *tv2 = STACK_TV_BOT(-1);
Bram Moolenaar657137c2021-01-09 15:45:23 +01003041 exprtype_T exprtype = iptr->isn_arg.op.op_type;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003042 int ic = iptr->isn_arg.op.op_ic;
3043
Bram Moolenaareb26f432020-09-14 16:50:05 +02003044 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar657137c2021-01-09 15:45:23 +01003045 typval_compare(tv1, tv2, exprtype, ic);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003046 clear_tv(tv2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003047 --ectx.ec_stack.ga_len;
3048 }
3049 break;
3050
3051 case ISN_ADDLIST:
3052 case ISN_ADDBLOB:
3053 {
3054 typval_T *tv1 = STACK_TV_BOT(-2);
3055 typval_T *tv2 = STACK_TV_BOT(-1);
3056
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003057 // add two lists or blobs
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003058 if (iptr->isn_type == ISN_ADDLIST)
3059 eval_addlist(tv1, tv2);
3060 else
3061 eval_addblob(tv1, tv2);
3062 clear_tv(tv2);
3063 --ectx.ec_stack.ga_len;
3064 }
3065 break;
3066
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003067 case ISN_LISTAPPEND:
3068 {
3069 typval_T *tv1 = STACK_TV_BOT(-2);
3070 typval_T *tv2 = STACK_TV_BOT(-1);
3071 list_T *l = tv1->vval.v_list;
3072
3073 // add an item to a list
3074 if (l == NULL)
3075 {
3076 SOURCING_LNUM = iptr->isn_lnum;
3077 emsg(_(e_cannot_add_to_null_list));
3078 goto on_error;
3079 }
3080 if (list_append_tv(l, tv2) == FAIL)
3081 goto failed;
Bram Moolenaar955347c2020-10-19 23:01:46 +02003082 clear_tv(tv2);
Bram Moolenaar1dcae592020-10-19 19:02:42 +02003083 --ectx.ec_stack.ga_len;
3084 }
3085 break;
3086
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02003087 case ISN_BLOBAPPEND:
3088 {
3089 typval_T *tv1 = STACK_TV_BOT(-2);
3090 typval_T *tv2 = STACK_TV_BOT(-1);
3091 blob_T *b = tv1->vval.v_blob;
3092 int error = FALSE;
3093 varnumber_T n;
3094
3095 // add a number to a blob
3096 if (b == NULL)
3097 {
3098 SOURCING_LNUM = iptr->isn_lnum;
3099 emsg(_(e_cannot_add_to_null_blob));
3100 goto on_error;
3101 }
3102 n = tv_get_number_chk(tv2, &error);
3103 if (error)
3104 goto on_error;
3105 ga_append(&b->bv_ga, (int)n);
3106 --ectx.ec_stack.ga_len;
3107 }
3108 break;
3109
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003110 // Computation with two arguments of unknown type
3111 case ISN_OPANY:
3112 {
3113 typval_T *tv1 = STACK_TV_BOT(-2);
3114 typval_T *tv2 = STACK_TV_BOT(-1);
3115 varnumber_T n1, n2;
3116#ifdef FEAT_FLOAT
3117 float_T f1 = 0, f2 = 0;
3118#endif
3119 int error = FALSE;
3120
3121 if (iptr->isn_arg.op.op_type == EXPR_ADD)
3122 {
3123 if (tv1->v_type == VAR_LIST && tv2->v_type == VAR_LIST)
3124 {
3125 eval_addlist(tv1, tv2);
3126 clear_tv(tv2);
3127 --ectx.ec_stack.ga_len;
3128 break;
3129 }
3130 else if (tv1->v_type == VAR_BLOB
3131 && tv2->v_type == VAR_BLOB)
3132 {
3133 eval_addblob(tv1, tv2);
3134 clear_tv(tv2);
3135 --ectx.ec_stack.ga_len;
3136 break;
3137 }
3138 }
3139#ifdef FEAT_FLOAT
3140 if (tv1->v_type == VAR_FLOAT)
3141 {
3142 f1 = tv1->vval.v_float;
3143 n1 = 0;
3144 }
3145 else
3146#endif
3147 {
Bram Moolenaarf665e972020-12-05 19:17:16 +01003148 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003149 n1 = tv_get_number_chk(tv1, &error);
3150 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003151 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003152#ifdef FEAT_FLOAT
3153 if (tv2->v_type == VAR_FLOAT)
3154 f1 = n1;
3155#endif
3156 }
3157#ifdef FEAT_FLOAT
3158 if (tv2->v_type == VAR_FLOAT)
3159 {
3160 f2 = tv2->vval.v_float;
3161 n2 = 0;
3162 }
3163 else
3164#endif
3165 {
3166 n2 = tv_get_number_chk(tv2, &error);
3167 if (error)
Bram Moolenaard032f342020-07-18 18:13:02 +02003168 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003169#ifdef FEAT_FLOAT
3170 if (tv1->v_type == VAR_FLOAT)
3171 f2 = n2;
3172#endif
3173 }
3174#ifdef FEAT_FLOAT
3175 // if there is a float on either side the result is a float
3176 if (tv1->v_type == VAR_FLOAT || tv2->v_type == VAR_FLOAT)
3177 {
3178 switch (iptr->isn_arg.op.op_type)
3179 {
3180 case EXPR_MULT: f1 = f1 * f2; break;
3181 case EXPR_DIV: f1 = f1 / f2; break;
3182 case EXPR_SUB: f1 = f1 - f2; break;
3183 case EXPR_ADD: f1 = f1 + f2; break;
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003184 default: SOURCING_LNUM = iptr->isn_lnum;
3185 emsg(_(e_modulus));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003186 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003187 }
3188 clear_tv(tv1);
3189 clear_tv(tv2);
3190 tv1->v_type = VAR_FLOAT;
3191 tv1->vval.v_float = f1;
3192 --ectx.ec_stack.ga_len;
3193 }
3194 else
3195#endif
3196 {
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003197 int failed = FALSE;
3198
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003199 switch (iptr->isn_arg.op.op_type)
3200 {
3201 case EXPR_MULT: n1 = n1 * n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003202 case EXPR_DIV: n1 = num_divide(n1, n2, &failed);
3203 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003204 goto on_error;
3205 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003206 case EXPR_SUB: n1 = n1 - n2; break;
3207 case EXPR_ADD: n1 = n1 + n2; break;
Bram Moolenaarb1f28572021-01-21 13:03:20 +01003208 default: n1 = num_modulus(n1, n2, &failed);
3209 if (failed)
Bram Moolenaar99880f92021-01-20 21:23:14 +01003210 goto on_error;
3211 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003212 }
3213 clear_tv(tv1);
3214 clear_tv(tv2);
3215 tv1->v_type = VAR_NUMBER;
3216 tv1->vval.v_number = n1;
3217 --ectx.ec_stack.ga_len;
3218 }
3219 }
3220 break;
3221
3222 case ISN_CONCAT:
3223 {
3224 char_u *str1 = STACK_TV_BOT(-2)->vval.v_string;
3225 char_u *str2 = STACK_TV_BOT(-1)->vval.v_string;
3226 char_u *res;
3227
3228 res = concat_str(str1, str2);
3229 clear_tv(STACK_TV_BOT(-2));
3230 clear_tv(STACK_TV_BOT(-1));
3231 --ectx.ec_stack.ga_len;
3232 STACK_TV_BOT(-1)->vval.v_string = res;
3233 }
3234 break;
3235
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003236 case ISN_STRINDEX:
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003237 case ISN_STRSLICE:
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003238 {
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003239 int is_slice = iptr->isn_type == ISN_STRSLICE;
3240 varnumber_T n1 = 0, n2;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003241 char_u *res;
3242
3243 // string index: string is at stack-2, index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003244 // string slice: string is at stack-3, first index at
3245 // stack-2, second index at stack-1
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003246 if (is_slice)
3247 {
3248 tv = STACK_TV_BOT(-2);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003249 n1 = tv->vval.v_number;
3250 }
3251
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003252 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003253 n2 = tv->vval.v_number;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003254
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003255 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003256 tv = STACK_TV_BOT(-1);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003257 if (is_slice)
3258 // Slice: Select the characters from the string
Bram Moolenaar6601b622021-01-13 21:47:15 +01003259 res = string_slice(tv->vval.v_string, n1, n2, FALSE);
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003260 else
3261 // Index: The resulting variable is a string of a
Bram Moolenaar0289a092021-03-14 18:40:19 +01003262 // single character (including composing characters).
3263 // If the index is too big or negative the result is
3264 // empty.
Bram Moolenaar11107ba2020-08-15 21:10:16 +02003265 res = char_from_string(tv->vval.v_string, n2);
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02003266 vim_free(tv->vval.v_string);
3267 tv->vval.v_string = res;
3268 }
3269 break;
3270
3271 case ISN_LISTINDEX:
Bram Moolenaared591872020-08-15 22:14:53 +02003272 case ISN_LISTSLICE:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003273 {
Bram Moolenaared591872020-08-15 22:14:53 +02003274 int is_slice = iptr->isn_type == ISN_LISTSLICE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003275 list_T *list;
Bram Moolenaared591872020-08-15 22:14:53 +02003276 varnumber_T n1, n2;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003277
3278 // list index: list is at stack-2, index at stack-1
Bram Moolenaared591872020-08-15 22:14:53 +02003279 // list slice: list is at stack-3, indexes at stack-2 and
3280 // stack-1
3281 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003282 list = tv->vval.v_list;
3283
3284 tv = STACK_TV_BOT(-1);
Bram Moolenaared591872020-08-15 22:14:53 +02003285 n1 = n2 = tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003286 clear_tv(tv);
Bram Moolenaared591872020-08-15 22:14:53 +02003287
3288 if (is_slice)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003289 {
Bram Moolenaared591872020-08-15 22:14:53 +02003290 tv = STACK_TV_BOT(-2);
Bram Moolenaared591872020-08-15 22:14:53 +02003291 n1 = tv->vval.v_number;
3292 clear_tv(tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003293 }
Bram Moolenaared591872020-08-15 22:14:53 +02003294
3295 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
Bram Moolenaar435d8972020-07-05 16:42:13 +02003296 tv = STACK_TV_BOT(-1);
Bram Moolenaar1d634542020-08-18 13:41:50 +02003297 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003298 if (list_slice_or_index(list, is_slice, n1, n2, FALSE,
3299 tv, TRUE) == FAIL)
Bram Moolenaared591872020-08-15 22:14:53 +02003300 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003301 }
3302 break;
3303
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003304 case ISN_ANYINDEX:
3305 case ISN_ANYSLICE:
3306 {
3307 int is_slice = iptr->isn_type == ISN_ANYSLICE;
3308 typval_T *var1, *var2;
3309 int res;
3310
3311 // index: composite is at stack-2, index at stack-1
3312 // slice: composite is at stack-3, indexes at stack-2 and
3313 // stack-1
3314 tv = is_slice ? STACK_TV_BOT(-3) : STACK_TV_BOT(-2);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003315 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003316 if (check_can_index(tv, TRUE, TRUE) == FAIL)
3317 goto on_error;
3318 var1 = is_slice ? STACK_TV_BOT(-2) : STACK_TV_BOT(-1);
3319 var2 = is_slice ? STACK_TV_BOT(-1) : NULL;
Bram Moolenaar6601b622021-01-13 21:47:15 +01003320 res = eval_index_inner(tv, is_slice, var1, var2,
3321 FALSE, NULL, -1, TRUE);
Bram Moolenaarcc673e72020-08-16 17:33:35 +02003322 clear_tv(var1);
3323 if (is_slice)
3324 clear_tv(var2);
3325 ectx.ec_stack.ga_len -= is_slice ? 2 : 1;
3326 if (res == FAIL)
3327 goto on_error;
3328 }
3329 break;
3330
Bram Moolenaar9af78762020-06-16 11:34:42 +02003331 case ISN_SLICE:
3332 {
3333 list_T *list;
3334 int count = iptr->isn_arg.number;
3335
Bram Moolenaarc5b1c202020-06-18 22:43:27 +02003336 // type will have been checked to be a list
Bram Moolenaar9af78762020-06-16 11:34:42 +02003337 tv = STACK_TV_BOT(-1);
Bram Moolenaar9af78762020-06-16 11:34:42 +02003338 list = tv->vval.v_list;
3339
3340 // no error for short list, expect it to be checked earlier
3341 if (list != NULL && list->lv_len >= count)
3342 {
3343 list_T *newlist = list_slice(list,
3344 count, list->lv_len - 1);
3345
3346 if (newlist != NULL)
3347 {
3348 list_unref(list);
3349 tv->vval.v_list = newlist;
3350 ++newlist->lv_refcount;
3351 }
3352 }
3353 }
3354 break;
3355
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003356 case ISN_GETITEM:
3357 {
3358 listitem_T *li;
3359 int index = iptr->isn_arg.number;
3360
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003361 // Get list item: list is at stack-1, push item.
3362 // List type and length is checked for when compiling.
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003363 tv = STACK_TV_BOT(-1);
Bram Moolenaarc785b9a2020-06-19 18:34:15 +02003364 li = list_find(tv->vval.v_list, index);
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003365
3366 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3367 goto failed;
3368 ++ectx.ec_stack.ga_len;
3369 copy_tv(&li->li_tv, STACK_TV_BOT(-1));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003370
3371 // Useful when used in unpack assignment. Reset at
3372 // ISN_DROP.
3373 where.wt_index = index + 1;
3374 where.wt_variable = TRUE;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02003375 }
3376 break;
3377
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003378 case ISN_MEMBER:
3379 {
3380 dict_T *dict;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003381 char_u *key;
3382 dictitem_T *di;
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003383 typval_T temp_tv;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003384
3385 // dict member: dict is at stack-2, key at stack-1
3386 tv = STACK_TV_BOT(-2);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003387 // no need to check for VAR_DICT, CHECKTYPE will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003388 dict = tv->vval.v_dict;
3389
3390 tv = STACK_TV_BOT(-1);
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003391 // no need to check for VAR_STRING, 2STRING will check.
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003392 key = tv->vval.v_string;
Bram Moolenaar086fc9a2020-10-30 18:33:02 +01003393 if (key == NULL)
3394 key = (char_u *)"";
Bram Moolenaar4dac32c2020-05-15 21:44:19 +02003395
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003396 if ((di = dict_find(dict, key, -1)) == NULL)
3397 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003398 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003399 semsg(_(e_dictkey), key);
Bram Moolenaar4029cab2020-12-05 18:13:27 +01003400
3401 // If :silent! is used we will continue, make sure the
3402 // stack contents makes sense.
3403 clear_tv(tv);
3404 --ectx.ec_stack.ga_len;
3405 tv = STACK_TV_BOT(-1);
3406 clear_tv(tv);
3407 tv->v_type = VAR_NUMBER;
3408 tv->vval.v_number = 0;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003409 goto on_fatal_error;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003410 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003411 clear_tv(tv);
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003412 --ectx.ec_stack.ga_len;
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003413 // Clear the dict only after getting the item, to avoid
3414 // that it makes the item invalid.
Bram Moolenaar50788ef2020-07-05 16:51:26 +02003415 tv = STACK_TV_BOT(-1);
3416 temp_tv = *tv;
3417 copy_tv(&di->di_tv, tv);
3418 clear_tv(&temp_tv);
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02003419 }
3420 break;
3421
3422 // dict member with string key
3423 case ISN_STRINGMEMBER:
3424 {
3425 dict_T *dict;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003426 dictitem_T *di;
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003427 typval_T temp_tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003428
3429 tv = STACK_TV_BOT(-1);
3430 if (tv->v_type != VAR_DICT || tv->vval.v_dict == NULL)
3431 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003432 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003433 emsg(_(e_dictreq));
Bram Moolenaard032f342020-07-18 18:13:02 +02003434 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003435 }
3436 dict = tv->vval.v_dict;
3437
3438 if ((di = dict_find(dict, iptr->isn_arg.string, -1))
3439 == NULL)
3440 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003441 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003442 semsg(_(e_dictkey), iptr->isn_arg.string);
Bram Moolenaard032f342020-07-18 18:13:02 +02003443 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003444 }
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003445 // Clear the dict after getting the item, to avoid that it
3446 // make the item invalid.
3447 temp_tv = *tv;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003448 copy_tv(&di->di_tv, tv);
Bram Moolenaarfb9d5c52020-07-04 19:19:43 +02003449 clear_tv(&temp_tv);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003450 }
3451 break;
3452
3453 case ISN_NEGATENR:
3454 tv = STACK_TV_BOT(-1);
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003455 if (tv->v_type != VAR_NUMBER
3456#ifdef FEAT_FLOAT
3457 && tv->v_type != VAR_FLOAT
3458#endif
3459 )
3460 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003461 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003462 emsg(_(e_number_exp));
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003463 goto on_error;
Bram Moolenaarc58164c2020-03-29 18:40:30 +02003464 }
3465#ifdef FEAT_FLOAT
3466 if (tv->v_type == VAR_FLOAT)
3467 tv->vval.v_float = -tv->vval.v_float;
3468 else
3469#endif
3470 tv->vval.v_number = -tv->vval.v_number;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003471 break;
3472
3473 case ISN_CHECKNR:
3474 {
3475 int error = FALSE;
3476
3477 tv = STACK_TV_BOT(-1);
Bram Moolenaar3affe7a2020-08-18 20:34:13 +02003478 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003479 if (check_not_string(tv) == FAIL)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003480 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003481 (void)tv_get_number_chk(tv, &error);
3482 if (error)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003483 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003484 }
3485 break;
3486
3487 case ISN_CHECKTYPE:
3488 {
3489 checktype_T *ct = &iptr->isn_arg.type;
3490
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01003491 tv = STACK_TV_BOT((int)ct->ct_off);
Bram Moolenaar5e654232020-09-16 15:22:00 +02003492 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003493 if (!where.wt_variable)
3494 where.wt_index = ct->ct_arg_idx;
3495 if (check_typval_type(ct->ct_type, tv, where) == FAIL)
Bram Moolenaar5e654232020-09-16 15:22:00 +02003496 goto on_error;
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003497 if (!where.wt_variable)
3498 where.wt_index = 0;
Bram Moolenaar5e654232020-09-16 15:22:00 +02003499
3500 // number 0 is FALSE, number 1 is TRUE
3501 if (tv->v_type == VAR_NUMBER
3502 && ct->ct_type->tt_type == VAR_BOOL
3503 && (tv->vval.v_number == 0
3504 || tv->vval.v_number == 1))
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003505 {
Bram Moolenaar5e654232020-09-16 15:22:00 +02003506 tv->v_type = VAR_BOOL;
3507 tv->vval.v_number = tv->vval.v_number
Bram Moolenaardadaddd2020-09-12 19:11:23 +02003508 ? VVAL_TRUE : VVAL_FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003509 }
3510 }
3511 break;
3512
Bram Moolenaar9af78762020-06-16 11:34:42 +02003513 case ISN_CHECKLEN:
3514 {
3515 int min_len = iptr->isn_arg.checklen.cl_min_len;
3516 list_T *list = NULL;
3517
3518 tv = STACK_TV_BOT(-1);
3519 if (tv->v_type == VAR_LIST)
3520 list = tv->vval.v_list;
3521 if (list == NULL || list->lv_len < min_len
3522 || (list->lv_len > min_len
3523 && !iptr->isn_arg.checklen.cl_more_OK))
3524 {
Bram Moolenaar7517ffd2020-08-14 18:35:07 +02003525 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003526 semsg(_(e_expected_nr_items_but_got_nr),
Bram Moolenaar9af78762020-06-16 11:34:42 +02003527 min_len, list == NULL ? 0 : list->lv_len);
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003528 goto on_error;
Bram Moolenaar9af78762020-06-16 11:34:42 +02003529 }
3530 }
3531 break;
3532
Bram Moolenaaraa210a32021-01-02 15:41:03 +01003533 case ISN_SETTYPE:
3534 {
3535 checktype_T *ct = &iptr->isn_arg.type;
3536
3537 tv = STACK_TV_BOT(-1);
3538 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3539 {
3540 free_type(tv->vval.v_dict->dv_type);
3541 tv->vval.v_dict->dv_type = alloc_type(ct->ct_type);
3542 }
3543 else if (tv->v_type == VAR_LIST && tv->vval.v_list != NULL)
3544 {
3545 free_type(tv->vval.v_list->lv_type);
3546 tv->vval.v_list->lv_type = alloc_type(ct->ct_type);
3547 }
3548 }
3549 break;
3550
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003551 case ISN_2BOOL:
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003552 case ISN_COND2BOOL:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003553 {
3554 int n;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003555 int error = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003556
3557 tv = STACK_TV_BOT(-1);
Bram Moolenaar2bb26582020-10-03 22:52:39 +02003558 if (iptr->isn_type == ISN_2BOOL)
3559 {
3560 n = tv2bool(tv);
3561 if (iptr->isn_arg.number) // invert
3562 n = !n;
3563 }
3564 else
3565 {
3566 SOURCING_LNUM = iptr->isn_lnum;
3567 n = tv_get_bool_chk(tv, &error);
3568 if (error)
3569 goto on_error;
3570 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003571 clear_tv(tv);
3572 tv->v_type = VAR_BOOL;
3573 tv->vval.v_number = n ? VVAL_TRUE : VVAL_FALSE;
3574 }
3575 break;
3576
3577 case ISN_2STRING:
Bram Moolenaar418f1df2020-08-12 21:34:49 +02003578 case ISN_2STRING_ANY:
Bram Moolenaar0acbf5a2021-01-05 20:58:25 +01003579 SOURCING_LNUM = iptr->isn_lnum;
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01003580 if (do_2string(STACK_TV_BOT(iptr->isn_arg.number),
3581 iptr->isn_type == ISN_2STRING_ANY) == FAIL)
3582 goto on_error;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003583 break;
3584
Bram Moolenaar08597872020-12-10 19:43:40 +01003585 case ISN_RANGE:
3586 {
3587 exarg_T ea;
3588 char *errormsg;
3589
Bram Moolenaarece0b872021-01-08 20:40:45 +01003590 ea.line2 = 0;
Bram Moolenaard1510ee2021-01-04 16:15:58 +01003591 ea.addr_count = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003592 ea.addr_type = ADDR_LINES;
3593 ea.cmd = iptr->isn_arg.string;
Bram Moolenaarece0b872021-01-08 20:40:45 +01003594 ea.skip = FALSE;
Bram Moolenaar08597872020-12-10 19:43:40 +01003595 if (parse_cmd_address(&ea, &errormsg, FALSE) == FAIL)
Bram Moolenaarece0b872021-01-08 20:40:45 +01003596 goto on_error;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003597
3598 if (GA_GROW(&ectx.ec_stack, 1) == FAIL)
3599 goto failed;
3600 ++ectx.ec_stack.ga_len;
3601 tv = STACK_TV_BOT(-1);
3602 tv->v_type = VAR_NUMBER;
3603 tv->v_lock = 0;
Bram Moolenaar08597872020-12-10 19:43:40 +01003604 if (ea.addr_count == 0)
3605 tv->vval.v_number = curwin->w_cursor.lnum;
3606 else
3607 tv->vval.v_number = ea.line2;
3608 }
3609 break;
3610
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003611 case ISN_PUT:
3612 {
3613 int regname = iptr->isn_arg.put.put_regname;
3614 linenr_T lnum = iptr->isn_arg.put.put_lnum;
3615 char_u *expr = NULL;
3616 int dir = FORWARD;
3617
Bram Moolenaar08597872020-12-10 19:43:40 +01003618 if (lnum < -2)
3619 {
3620 // line number was put on the stack by ISN_RANGE
3621 tv = STACK_TV_BOT(-1);
3622 curwin->w_cursor.lnum = tv->vval.v_number;
3623 if (lnum == LNUM_VARIABLE_RANGE_ABOVE)
3624 dir = BACKWARD;
3625 --ectx.ec_stack.ga_len;
3626 }
3627 else if (lnum == -2)
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003628 // :put! above cursor
3629 dir = BACKWARD;
3630 else if (lnum >= 0)
3631 curwin->w_cursor.lnum = iptr->isn_arg.put.put_lnum;
Bram Moolenaara28639e2021-01-19 22:48:09 +01003632
3633 if (regname == '=')
3634 {
3635 tv = STACK_TV_BOT(-1);
3636 if (tv->v_type == VAR_STRING)
3637 expr = tv->vval.v_string;
3638 else
3639 {
3640 expr = typval2string(tv, TRUE); // allocates value
3641 clear_tv(tv);
3642 }
3643 --ectx.ec_stack.ga_len;
3644 }
Bram Moolenaarc3516f72020-09-08 22:45:35 +02003645 check_cursor();
3646 do_put(regname, expr, dir, 1L, PUT_LINE|PUT_CURSLINE);
3647 vim_free(expr);
3648 }
3649 break;
3650
Bram Moolenaar02194d22020-10-24 23:08:38 +02003651 case ISN_CMDMOD:
3652 save_cmdmod = cmdmod;
3653 restore_cmdmod = TRUE;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003654 restore_cmdmod_stacklen = ectx.ec_stack.ga_len;
Bram Moolenaar02194d22020-10-24 23:08:38 +02003655 cmdmod = *iptr->isn_arg.cmdmod.cf_cmdmod;
3656 apply_cmdmod(&cmdmod);
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003657 break;
3658
Bram Moolenaar02194d22020-10-24 23:08:38 +02003659 case ISN_CMDMOD_REV:
3660 // filter regprog is owned by the instruction, don't free it
3661 cmdmod.cmod_filter_regmatch.regprog = NULL;
3662 undo_cmdmod(&cmdmod);
3663 cmdmod = save_cmdmod;
3664 restore_cmdmod = FALSE;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003665 break;
3666
Bram Moolenaar792f7862020-11-23 08:31:18 +01003667 case ISN_UNPACK:
3668 {
3669 int count = iptr->isn_arg.unpack.unp_count;
3670 int semicolon = iptr->isn_arg.unpack.unp_semicolon;
3671 list_T *l;
3672 listitem_T *li;
3673 int i;
3674
3675 // Check there is a valid list to unpack.
3676 tv = STACK_TV_BOT(-1);
3677 if (tv->v_type != VAR_LIST)
3678 {
3679 SOURCING_LNUM = iptr->isn_lnum;
3680 emsg(_(e_for_argument_must_be_sequence_of_lists));
3681 goto on_error;
3682 }
3683 l = tv->vval.v_list;
3684 if (l == NULL
3685 || l->lv_len < (semicolon ? count - 1 : count))
3686 {
3687 SOURCING_LNUM = iptr->isn_lnum;
3688 emsg(_(e_list_value_does_not_have_enough_items));
3689 goto on_error;
3690 }
3691 else if (!semicolon && l->lv_len > count)
3692 {
3693 SOURCING_LNUM = iptr->isn_lnum;
3694 emsg(_(e_list_value_has_more_items_than_targets));
3695 goto on_error;
3696 }
3697
3698 CHECK_LIST_MATERIALIZE(l);
3699 if (GA_GROW(&ectx.ec_stack, count - 1) == FAIL)
3700 goto failed;
3701 ectx.ec_stack.ga_len += count - 1;
3702
3703 // Variable after semicolon gets a list with the remaining
3704 // items.
3705 if (semicolon)
3706 {
3707 list_T *rem_list =
3708 list_alloc_with_items(l->lv_len - count + 1);
3709
3710 if (rem_list == NULL)
3711 goto failed;
3712 tv = STACK_TV_BOT(-count);
3713 tv->vval.v_list = rem_list;
3714 ++rem_list->lv_refcount;
3715 tv->v_lock = 0;
3716 li = l->lv_first;
3717 for (i = 0; i < count - 1; ++i)
3718 li = li->li_next;
3719 for (i = 0; li != NULL; ++i)
3720 {
3721 list_set_item(rem_list, i, &li->li_tv);
3722 li = li->li_next;
3723 }
3724 --count;
3725 }
3726
3727 // Produce the values in reverse order, first item last.
3728 li = l->lv_first;
3729 for (i = 0; i < count; ++i)
3730 {
3731 tv = STACK_TV_BOT(-i - 1);
3732 copy_tv(&li->li_tv, tv);
3733 li = li->li_next;
3734 }
3735
3736 list_unref(l);
3737 }
3738 break;
3739
Bram Moolenaarb2049902021-01-24 12:53:53 +01003740 case ISN_PROF_START:
3741 case ISN_PROF_END:
3742 {
Bram Moolenaarf002a412021-01-24 13:34:18 +01003743#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003744 funccall_T cookie;
3745 ufunc_T *cur_ufunc =
3746 (((dfunc_T *)def_functions.ga_data)
3747 + ectx.ec_dfunc_idx)->df_ufunc;
3748
3749 cookie.func = cur_ufunc;
3750 if (iptr->isn_type == ISN_PROF_START)
3751 {
3752 func_line_start(&cookie, iptr->isn_lnum);
3753 // if we get here the instruction is executed
3754 func_line_exec(&cookie);
3755 }
3756 else
3757 func_line_end(&cookie);
Bram Moolenaarf002a412021-01-24 13:34:18 +01003758#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003759 }
3760 break;
3761
Bram Moolenaar389df252020-07-09 21:20:47 +02003762 case ISN_SHUFFLE:
3763 {
Bram Moolenaar792f7862020-11-23 08:31:18 +01003764 typval_T tmp_tv;
3765 int item = iptr->isn_arg.shuffle.shfl_item;
3766 int up = iptr->isn_arg.shuffle.shfl_up;
Bram Moolenaar389df252020-07-09 21:20:47 +02003767
3768 tmp_tv = *STACK_TV_BOT(-item);
3769 for ( ; up > 0 && item > 1; --up)
3770 {
3771 *STACK_TV_BOT(-item) = *STACK_TV_BOT(-item + 1);
3772 --item;
3773 }
3774 *STACK_TV_BOT(-item) = tmp_tv;
3775 }
3776 break;
3777
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003778 case ISN_DROP:
3779 --ectx.ec_stack.ga_len;
3780 clear_tv(STACK_TV_BOT(0));
Bram Moolenaarf785aa12021-02-11 21:19:34 +01003781 where.wt_index = 0;
3782 where.wt_variable = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003783 break;
3784 }
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003785 continue;
3786
Bram Moolenaard032f342020-07-18 18:13:02 +02003787func_return:
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003788 // Restore previous function. If the frame pointer is where we started
3789 // then there is none and we are done.
Bram Moolenaard032f342020-07-18 18:13:02 +02003790 if (ectx.ec_frame_idx == initial_frame_idx)
Bram Moolenaard032f342020-07-18 18:13:02 +02003791 goto done;
Bram Moolenaar7cbfaa52020-09-18 21:25:32 +02003792
Bram Moolenaard032f342020-07-18 18:13:02 +02003793 if (func_return(&ectx) == FAIL)
3794 // only fails when out of memory
3795 goto failed;
Bram Moolenaarc7db5772020-07-21 20:55:50 +02003796 continue;
Bram Moolenaard032f342020-07-18 18:13:02 +02003797
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003798on_error:
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003799 // Jump here for an error that does not require aborting execution.
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003800 // If "emsg_silent" is set then ignore the error, unless it was set
3801 // when calling the function.
3802 if (did_emsg_cumul + did_emsg == did_emsg_before
3803 && emsg_silent && did_emsg_def == 0)
Bram Moolenaarf9041332021-01-21 19:41:16 +01003804 {
3805 // If a sequence of instructions causes an error while ":silent!"
3806 // was used, restore the stack length and jump ahead to restoring
3807 // the cmdmod.
3808 if (restore_cmdmod)
3809 {
3810 while (ectx.ec_stack.ga_len > restore_cmdmod_stacklen)
3811 {
3812 --ectx.ec_stack.ga_len;
3813 clear_tv(STACK_TV_BOT(0));
3814 }
3815 while (ectx.ec_instr[ectx.ec_iidx].isn_type != ISN_CMDMOD_REV)
3816 ++ectx.ec_iidx;
3817 }
Bram Moolenaarcd030c42020-10-30 21:49:40 +01003818 continue;
Bram Moolenaarf9041332021-01-21 19:41:16 +01003819 }
Bram Moolenaaraf0df472020-12-02 20:51:22 +01003820on_fatal_error:
3821 // Jump here for an error that messes up the stack.
Bram Moolenaar171fb922020-10-28 16:54:47 +01003822 // If we are not inside a try-catch started here, abort execution.
3823 if (trylevel <= trylevel_at_start)
Bram Moolenaarf0b9f432020-07-17 23:03:17 +02003824 goto failed;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003825 }
3826
3827done:
3828 // function finished, get result from the stack.
3829 tv = STACK_TV_BOT(-1);
3830 *rettv = *tv;
3831 tv->v_type = VAR_UNKNOWN;
3832 ret = OK;
3833
3834failed:
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003835 // When failed need to unwind the call stack.
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003836 while (ectx.ec_frame_idx != initial_frame_idx)
Bram Moolenaar7eeefd42020-02-26 21:24:23 +01003837 func_return(&ectx);
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003838
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003839 // Deal with any remaining closures, they may be in use somewhere.
3840 if (ectx.ec_funcrefs.ga_len > 0)
Bram Moolenaarf112f302020-12-20 17:47:52 +01003841 {
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003842 handle_closure_in_use(&ectx, FALSE);
Bram Moolenaarf112f302020-12-20 17:47:52 +01003843 ga_clear(&ectx.ec_funcrefs); // TODO: should not be needed?
3844 }
Bram Moolenaarc70bdab2020-09-26 19:59:38 +02003845
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003846 estack_pop();
3847 current_sctx = save_current_sctx;
3848
Bram Moolenaarc970e422021-03-17 15:03:04 +01003849 // TODO: when is it safe to delete the function if it is no longer used?
3850 --ufunc->uf_calls;
3851
Bram Moolenaar352134b2020-10-17 22:04:08 +02003852 if (*msg_list != NULL && saved_msg_list != NULL)
3853 {
3854 msglist_T **plist = saved_msg_list;
3855
3856 // Append entries from the current msg_list (uncaught exceptions) to
3857 // the saved msg_list.
3858 while (*plist != NULL)
3859 plist = &(*plist)->next;
3860
3861 *plist = *msg_list;
3862 }
3863 msg_list = saved_msg_list;
3864
Bram Moolenaar02194d22020-10-24 23:08:38 +02003865 if (restore_cmdmod)
3866 {
3867 cmdmod.cmod_filter_regmatch.regprog = NULL;
3868 undo_cmdmod(&cmdmod);
3869 cmdmod = save_cmdmod;
3870 }
Bram Moolenaar56602ba2020-12-05 21:22:08 +01003871 emsg_silent_def = save_emsg_silent_def;
3872 did_emsg_def += save_did_emsg_def;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02003873
Bram Moolenaaree8580e2020-08-28 17:19:07 +02003874failed_early:
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003875 // Free all local variables, but not arguments.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003876 for (idx = 0; idx < ectx.ec_stack.ga_len; ++idx)
3877 clear_tv(STACK_TV(idx));
Bram Moolenaarbf67ea12020-05-02 17:52:42 +02003878
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003879 vim_free(ectx.ec_stack.ga_data);
Bram Moolenaar20431c92020-03-20 18:39:46 +01003880 vim_free(ectx.ec_trystack.ga_data);
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003881
Bram Moolenaar0186e582021-01-10 18:33:11 +01003882 while (ectx.ec_outer != NULL)
3883 {
3884 outer_T *up = ectx.ec_outer->out_up_is_copy
3885 ? NULL : ectx.ec_outer->out_up;
3886
3887 vim_free(ectx.ec_outer);
3888 ectx.ec_outer = up;
3889 }
3890
Bram Moolenaar77e5dcc2020-09-17 21:29:03 +02003891 // Not sure if this is necessary.
3892 suppress_errthrow = save_suppress_errthrow;
3893
Bram Moolenaareeece9e2020-11-20 19:26:48 +01003894 if (ret != OK && did_emsg_cumul + did_emsg == did_emsg_before)
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003895 semsg(_(e_unknown_error_while_executing_str),
Bram Moolenaar682d0a12020-07-19 20:48:59 +02003896 printable_func_name(ufunc));
Bram Moolenaar0ba48e82020-11-17 18:23:19 +01003897 funcdepth_restore(orig_funcdepth);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003898 return ret;
3899}
3900
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003901/*
Bram Moolenaar8e7d6222020-12-18 19:49:56 +01003902 * ":disassemble".
Bram Moolenaar777770f2020-02-06 21:27:08 +01003903 * We don't really need this at runtime, but we do have tests that require it,
3904 * so always include this.
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003905 */
3906 void
3907ex_disassemble(exarg_T *eap)
3908{
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003909 char_u *arg = eap->arg;
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003910 char_u *fname;
3911 ufunc_T *ufunc;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003912 dfunc_T *dfunc;
3913 isn_T *instr;
Bram Moolenaarb2049902021-01-24 12:53:53 +01003914 int instr_count;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003915 int current;
3916 int line_idx = 0;
3917 int prev_current = 0;
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003918 int is_global = FALSE;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003919
Bram Moolenaarbfd65582020-07-13 18:18:00 +02003920 if (STRNCMP(arg, "<lambda>", 8) == 0)
3921 {
3922 arg += 8;
3923 (void)getdigits(&arg);
3924 fname = vim_strnsave(eap->arg, arg - eap->arg);
3925 }
3926 else
3927 fname = trans_function_name(&arg, &is_global, FALSE,
Bram Moolenaar32b3f822021-01-06 21:59:39 +01003928 TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD, NULL, NULL, NULL);
Bram Moolenaar21456cd2020-02-13 21:29:32 +01003929 if (fname == NULL)
3930 {
3931 semsg(_(e_invarg2), eap->arg);
3932 return;
3933 }
3934
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003935 ufunc = find_func(fname, is_global, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003936 if (ufunc == NULL)
3937 {
3938 char_u *p = untrans_function_name(fname);
3939
3940 if (p != NULL)
3941 // Try again without making it script-local.
Bram Moolenaar4c17ad92020-04-27 22:47:51 +02003942 ufunc = find_func(p, FALSE, NULL);
Bram Moolenaara26b9702020-04-18 19:53:28 +02003943 }
Bram Moolenaar0f18b6d2020-02-02 17:22:27 +01003944 vim_free(fname);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003945 if (ufunc == NULL)
3946 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003947 semsg(_(e_cannot_find_function_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003948 return;
3949 }
Bram Moolenaarb2049902021-01-24 12:53:53 +01003950 if (func_needs_compiling(ufunc, eap->forceit)
3951 && compile_def_function(ufunc, FALSE, eap->forceit, NULL) == FAIL)
Bram Moolenaar822ba242020-05-24 23:00:18 +02003952 return;
Bram Moolenaar0cb5bcf2020-06-20 18:19:09 +02003953 if (ufunc->uf_def_status != UF_COMPILED)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003954 {
Bram Moolenaar451c2e32020-08-15 16:33:28 +02003955 semsg(_(e_function_is_not_compiled_str), eap->arg);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003956 return;
3957 }
3958 if (ufunc->uf_name_exp != NULL)
3959 msg((char *)ufunc->uf_name_exp);
3960 else
3961 msg((char *)ufunc->uf_name);
3962
3963 dfunc = ((dfunc_T *)def_functions.ga_data) + ufunc->uf_dfunc_idx;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003964#ifdef FEAT_PROFILE
Bram Moolenaarb2049902021-01-24 12:53:53 +01003965 instr = eap->forceit ? dfunc->df_instr_prof : dfunc->df_instr;
3966 instr_count = eap->forceit ? dfunc->df_instr_prof_count
3967 : dfunc->df_instr_count;
Bram Moolenaarf002a412021-01-24 13:34:18 +01003968#else
3969 instr = dfunc->df_instr;
3970 instr_count = dfunc->df_instr_count;
3971#endif
Bram Moolenaarb2049902021-01-24 12:53:53 +01003972 for (current = 0; current < instr_count; ++current)
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003973 {
3974 isn_T *iptr = &instr[current];
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003975 char *line;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003976
3977 while (line_idx < iptr->isn_lnum && line_idx < ufunc->uf_lines.ga_len)
3978 {
3979 if (current > prev_current)
3980 {
3981 msg_puts("\n\n");
3982 prev_current = current;
3983 }
Bram Moolenaarf2460a32020-02-07 22:09:54 +01003984 line = ((char **)ufunc->uf_lines.ga_data)[line_idx++];
3985 if (line != NULL)
3986 msg(line);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003987 }
3988
3989 switch (iptr->isn_type)
3990 {
3991 case ISN_EXEC:
3992 smsg("%4d EXEC %s", current, iptr->isn_arg.string);
3993 break;
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003994 case ISN_EXECCONCAT:
3995 smsg("%4d EXECCONCAT %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01003996 (varnumber_T)iptr->isn_arg.number);
Bram Moolenaarcfe435d2020-04-25 20:02:55 +02003997 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01003998 case ISN_ECHO:
3999 {
4000 echo_T *echo = &iptr->isn_arg.echo;
4001
4002 smsg("%4d %s %d", current,
4003 echo->echo_with_white ? "ECHO" : "ECHON",
4004 echo->echo_count);
4005 }
4006 break;
Bram Moolenaarad39c092020-02-26 18:23:43 +01004007 case ISN_EXECUTE:
Bram Moolenaar10827722020-03-23 22:53:22 +01004008 smsg("%4d EXECUTE %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004009 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarad39c092020-02-26 18:23:43 +01004010 break;
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004011 case ISN_ECHOMSG:
4012 smsg("%4d ECHOMSG %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004013 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004014 break;
4015 case ISN_ECHOERR:
4016 smsg("%4d ECHOERR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004017 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaarf93c7fe2020-04-23 22:16:53 +02004018 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004019 case ISN_LOAD:
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004020 {
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004021 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004022 smsg("%4d LOAD arg[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004023 (varnumber_T)(iptr->isn_arg.number
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004024 + STACK_FRAME_SIZE));
4025 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004026 smsg("%4d LOAD $%lld", current,
4027 (varnumber_T)(iptr->isn_arg.number));
4028 }
4029 break;
4030 case ISN_LOADOUTER:
4031 {
4032 if (iptr->isn_arg.number < 0)
4033 smsg("%4d LOADOUTER level %d arg[%d]", current,
4034 iptr->isn_arg.outer.outer_depth,
4035 iptr->isn_arg.outer.outer_idx
4036 + STACK_FRAME_SIZE);
4037 else
4038 smsg("%4d LOADOUTER level %d $%d", current,
4039 iptr->isn_arg.outer.outer_depth,
4040 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarc8cd2b32020-05-01 19:29:08 +02004041 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004042 break;
4043 case ISN_LOADV:
4044 smsg("%4d LOADV v:%s", current,
4045 get_vim_var_name(iptr->isn_arg.number));
4046 break;
4047 case ISN_LOADSCRIPT:
4048 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004049 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4050 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004051 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004052 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004053
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004054 smsg("%4d LOADSCRIPT %s-%d from %s", current,
4055 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004056 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004057 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004058 }
4059 break;
4060 case ISN_LOADS:
4061 {
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004062 scriptitem_T *si = SCRIPT_ITEM(
4063 iptr->isn_arg.loadstore.ls_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004064
4065 smsg("%4d LOADS s:%s from %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004066 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004067 }
4068 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004069 case ISN_LOADAUTO:
4070 smsg("%4d LOADAUTO %s", current, iptr->isn_arg.string);
4071 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004072 case ISN_LOADG:
4073 smsg("%4d LOADG g:%s", current, iptr->isn_arg.string);
4074 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004075 case ISN_LOADB:
4076 smsg("%4d LOADB b:%s", current, iptr->isn_arg.string);
4077 break;
4078 case ISN_LOADW:
4079 smsg("%4d LOADW w:%s", current, iptr->isn_arg.string);
4080 break;
4081 case ISN_LOADT:
4082 smsg("%4d LOADT t:%s", current, iptr->isn_arg.string);
4083 break;
Bram Moolenaar2f8ce0a2020-07-19 19:47:35 +02004084 case ISN_LOADGDICT:
4085 smsg("%4d LOAD g:", current);
4086 break;
4087 case ISN_LOADBDICT:
4088 smsg("%4d LOAD b:", current);
4089 break;
4090 case ISN_LOADWDICT:
4091 smsg("%4d LOAD w:", current);
4092 break;
4093 case ISN_LOADTDICT:
4094 smsg("%4d LOAD t:", current);
4095 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004096 case ISN_LOADOPT:
4097 smsg("%4d LOADOPT %s", current, iptr->isn_arg.string);
4098 break;
4099 case ISN_LOADENV:
4100 smsg("%4d LOADENV %s", current, iptr->isn_arg.string);
4101 break;
4102 case ISN_LOADREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004103 smsg("%4d LOADREG @%c", current, (int)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004104 break;
4105
4106 case ISN_STORE:
Bram Moolenaarab360522021-01-10 14:02:28 +01004107 if (iptr->isn_arg.number < 0)
4108 smsg("%4d STORE arg[%lld]", current,
4109 iptr->isn_arg.number + STACK_FRAME_SIZE);
4110 else
4111 smsg("%4d STORE $%lld", current, iptr->isn_arg.number);
4112 break;
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004113 case ISN_STOREOUTER:
4114 {
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004115 if (iptr->isn_arg.number < 0)
Bram Moolenaarab360522021-01-10 14:02:28 +01004116 smsg("%4d STOREOUTEr level %d arg[%d]", current,
4117 iptr->isn_arg.outer.outer_depth,
4118 iptr->isn_arg.outer.outer_idx + STACK_FRAME_SIZE);
Bram Moolenaar170fcfc2020-02-06 17:51:35 +01004119 else
Bram Moolenaarab360522021-01-10 14:02:28 +01004120 smsg("%4d STOREOUTER level %d $%d", current,
4121 iptr->isn_arg.outer.outer_depth,
4122 iptr->isn_arg.outer.outer_idx);
Bram Moolenaarb68b3462020-05-06 21:06:30 +02004123 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004124 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004125 case ISN_STOREV:
4126 smsg("%4d STOREV v:%s", current,
4127 get_vim_var_name(iptr->isn_arg.number));
4128 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01004129 case ISN_STOREAUTO:
4130 smsg("%4d STOREAUTO %s", current, iptr->isn_arg.string);
4131 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004132 case ISN_STOREG:
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004133 smsg("%4d STOREG %s", current, iptr->isn_arg.string);
4134 break;
Bram Moolenaard3aac292020-04-19 14:32:17 +02004135 case ISN_STOREB:
4136 smsg("%4d STOREB %s", current, iptr->isn_arg.string);
4137 break;
4138 case ISN_STOREW:
4139 smsg("%4d STOREW %s", current, iptr->isn_arg.string);
4140 break;
4141 case ISN_STORET:
4142 smsg("%4d STORET %s", current, iptr->isn_arg.string);
4143 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004144 case ISN_STORES:
4145 {
4146 scriptitem_T *si = SCRIPT_ITEM(
4147 iptr->isn_arg.loadstore.ls_sid);
4148
Bram Moolenaar0bbf7222020-02-19 22:31:48 +01004149 smsg("%4d STORES %s in %s", current,
Bram Moolenaar5deeb3f2020-04-05 17:08:17 +02004150 iptr->isn_arg.loadstore.ls_name, si->sn_name);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004151 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004152 break;
4153 case ISN_STORESCRIPT:
4154 {
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004155 scriptref_T *sref = iptr->isn_arg.script.scriptref;
4156 scriptitem_T *si = SCRIPT_ITEM(sref->sref_sid);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004157 svar_T *sv = ((svar_T *)si->sn_var_vals.ga_data)
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004158 + sref->sref_idx;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004159
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004160 smsg("%4d STORESCRIPT %s-%d in %s", current,
4161 sv->sv_name,
Bram Moolenaar4aab88d2020-12-24 21:56:41 +01004162 sref->sref_idx,
Bram Moolenaarfbbcd002020-10-15 12:46:44 +02004163 si->sn_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004164 }
4165 break;
4166 case ISN_STOREOPT:
4167 smsg("%4d STOREOPT &%s", current,
4168 iptr->isn_arg.storeopt.so_name);
4169 break;
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004170 case ISN_STOREENV:
4171 smsg("%4d STOREENV $%s", current, iptr->isn_arg.string);
4172 break;
4173 case ISN_STOREREG:
Bram Moolenaar10827722020-03-23 22:53:22 +01004174 smsg("%4d STOREREG @%c", current, (int)iptr->isn_arg.number);
Bram Moolenaarb283a8a2020-02-02 22:24:04 +01004175 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004176 case ISN_STORENR:
4177 smsg("%4d STORE %lld in $%d", current,
Bram Moolenaara471eea2020-03-04 22:20:26 +01004178 iptr->isn_arg.storenr.stnr_val,
4179 iptr->isn_arg.storenr.stnr_idx);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004180 break;
4181
Bram Moolenaar4f5e3972020-12-21 17:30:50 +01004182 case ISN_STOREINDEX:
4183 switch (iptr->isn_arg.vartype)
4184 {
4185 case VAR_LIST:
4186 smsg("%4d STORELIST", current);
4187 break;
4188 case VAR_DICT:
4189 smsg("%4d STOREDICT", current);
4190 break;
4191 case VAR_ANY:
4192 smsg("%4d STOREINDEX", current);
4193 break;
4194 default: break;
4195 }
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004196 break;
4197
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004198 // constants
4199 case ISN_PUSHNR:
Bram Moolenaar10827722020-03-23 22:53:22 +01004200 smsg("%4d PUSHNR %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004201 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004202 break;
4203 case ISN_PUSHBOOL:
4204 case ISN_PUSHSPEC:
4205 smsg("%4d PUSH %s", current,
4206 get_var_special_name(iptr->isn_arg.number));
4207 break;
4208 case ISN_PUSHF:
Bram Moolenaara5d59532020-01-26 21:42:03 +01004209#ifdef FEAT_FLOAT
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004210 smsg("%4d PUSHF %g", current, iptr->isn_arg.fnumber);
Bram Moolenaara5d59532020-01-26 21:42:03 +01004211#endif
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004212 break;
4213 case ISN_PUSHS:
4214 smsg("%4d PUSHS \"%s\"", current, iptr->isn_arg.string);
4215 break;
4216 case ISN_PUSHBLOB:
4217 {
4218 char_u *r;
4219 char_u numbuf[NUMBUFLEN];
4220 char_u *tofree;
4221
4222 r = blob2string(iptr->isn_arg.blob, &tofree, numbuf);
Bram Moolenaarff80cb62020-02-05 22:10:05 +01004223 smsg("%4d PUSHBLOB %s", current, r);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004224 vim_free(tofree);
4225 }
4226 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004227 case ISN_PUSHFUNC:
Bram Moolenaar087d2e12020-03-01 15:36:42 +01004228 {
4229 char *name = (char *)iptr->isn_arg.string;
4230
4231 smsg("%4d PUSHFUNC \"%s\"", current,
4232 name == NULL ? "[none]" : name);
4233 }
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004234 break;
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004235 case ISN_PUSHCHANNEL:
4236#ifdef FEAT_JOB_CHANNEL
4237 {
4238 channel_T *channel = iptr->isn_arg.channel;
4239
4240 smsg("%4d PUSHCHANNEL %d", current,
4241 channel == NULL ? 0 : channel->ch_id);
4242 }
4243#endif
4244 break;
4245 case ISN_PUSHJOB:
4246#ifdef FEAT_JOB_CHANNEL
4247 {
4248 typval_T tv;
4249 char_u *name;
4250
4251 tv.v_type = VAR_JOB;
4252 tv.vval.v_job = iptr->isn_arg.job;
4253 name = tv_get_string(&tv);
Bram Moolenaarf51cb4e2020-03-01 17:55:14 +01004254 smsg("%4d PUSHJOB \"%s\"", current, name);
Bram Moolenaar42a480b2020-02-29 23:23:47 +01004255 }
4256#endif
4257 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004258 case ISN_PUSHEXC:
4259 smsg("%4d PUSH v:exception", current);
4260 break;
Bram Moolenaard72c1bf2020-04-19 16:28:59 +02004261 case ISN_UNLET:
4262 smsg("%4d UNLET%s %s", current,
4263 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4264 iptr->isn_arg.unlet.ul_name);
4265 break;
Bram Moolenaar7bdaea62020-04-19 18:27:26 +02004266 case ISN_UNLETENV:
4267 smsg("%4d UNLETENV%s $%s", current,
4268 iptr->isn_arg.unlet.ul_forceit ? "!" : "",
4269 iptr->isn_arg.unlet.ul_name);
4270 break;
Bram Moolenaar752fc692021-01-04 21:57:11 +01004271 case ISN_UNLETINDEX:
4272 smsg("%4d UNLETINDEX", current);
4273 break;
Bram Moolenaar5b5ae292021-02-20 17:04:02 +01004274 case ISN_UNLETRANGE:
4275 smsg("%4d UNLETRANGE", current);
4276 break;
Bram Moolenaar0b4c66c2020-09-14 21:39:44 +02004277 case ISN_LOCKCONST:
4278 smsg("%4d LOCKCONST", current);
4279 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004280 case ISN_NEWLIST:
Bram Moolenaar10827722020-03-23 22:53:22 +01004281 smsg("%4d NEWLIST size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004282 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004283 break;
4284 case ISN_NEWDICT:
Bram Moolenaar10827722020-03-23 22:53:22 +01004285 smsg("%4d NEWDICT size %lld", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004286 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004287 break;
4288
4289 // function call
4290 case ISN_BCALL:
4291 {
4292 cbfunc_T *cbfunc = &iptr->isn_arg.bfunc;
4293
4294 smsg("%4d BCALL %s(argc %d)", current,
4295 internal_func_name(cbfunc->cbf_idx),
4296 cbfunc->cbf_argcount);
4297 }
4298 break;
4299 case ISN_DCALL:
4300 {
4301 cdfunc_T *cdfunc = &iptr->isn_arg.dfunc;
4302 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
4303 + cdfunc->cdf_idx;
4304
4305 smsg("%4d DCALL %s(argc %d)", current,
4306 df->df_ufunc->uf_name_exp != NULL
4307 ? df->df_ufunc->uf_name_exp
4308 : df->df_ufunc->uf_name, cdfunc->cdf_argcount);
4309 }
4310 break;
4311 case ISN_UCALL:
4312 {
4313 cufunc_T *cufunc = &iptr->isn_arg.ufunc;
4314
4315 smsg("%4d UCALL %s(argc %d)", current,
4316 cufunc->cuf_name, cufunc->cuf_argcount);
4317 }
4318 break;
4319 case ISN_PCALL:
4320 {
4321 cpfunc_T *cpfunc = &iptr->isn_arg.pfunc;
4322
4323 smsg("%4d PCALL%s (argc %d)", current,
4324 cpfunc->cpf_top ? " top" : "", cpfunc->cpf_argcount);
4325 }
4326 break;
Bram Moolenaarbd5da372020-03-31 23:13:10 +02004327 case ISN_PCALL_END:
4328 smsg("%4d PCALL end", current);
4329 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004330 case ISN_RETURN:
4331 smsg("%4d RETURN", current);
4332 break;
Bram Moolenaar299f3032021-01-08 20:53:09 +01004333 case ISN_RETURN_ZERO:
4334 smsg("%4d RETURN 0", current);
4335 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004336 case ISN_FUNCREF:
4337 {
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004338 funcref_T *funcref = &iptr->isn_arg.funcref;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004339 dfunc_T *df = ((dfunc_T *)def_functions.ga_data)
Bram Moolenaar5adc55c2020-05-02 23:12:58 +02004340 + funcref->fr_func;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004341
Bram Moolenaar148ce7a2020-09-23 21:57:23 +02004342 smsg("%4d FUNCREF %s", current, df->df_ufunc->uf_name);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004343 }
4344 break;
4345
Bram Moolenaar38ddf332020-07-31 22:05:04 +02004346 case ISN_NEWFUNC:
4347 {
4348 newfunc_T *newfunc = &iptr->isn_arg.newfunc;
4349
4350 smsg("%4d NEWFUNC %s %s", current,
4351 newfunc->nf_lambda, newfunc->nf_global);
4352 }
4353 break;
4354
Bram Moolenaar6abdcf82020-11-22 18:15:44 +01004355 case ISN_DEF:
4356 {
4357 char_u *name = iptr->isn_arg.string;
4358
4359 smsg("%4d DEF %s", current,
4360 name == NULL ? (char_u *)"" : name);
4361 }
4362 break;
4363
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004364 case ISN_JUMP:
4365 {
4366 char *when = "?";
4367
4368 switch (iptr->isn_arg.jump.jump_when)
4369 {
4370 case JUMP_ALWAYS:
4371 when = "JUMP";
4372 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004373 case JUMP_AND_KEEP_IF_TRUE:
4374 when = "JUMP_AND_KEEP_IF_TRUE";
4375 break;
4376 case JUMP_IF_FALSE:
4377 when = "JUMP_IF_FALSE";
4378 break;
4379 case JUMP_AND_KEEP_IF_FALSE:
4380 when = "JUMP_AND_KEEP_IF_FALSE";
4381 break;
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004382 case JUMP_IF_COND_FALSE:
4383 when = "JUMP_IF_COND_FALSE";
4384 break;
4385 case JUMP_IF_COND_TRUE:
4386 when = "JUMP_IF_COND_TRUE";
4387 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004388 }
Bram Moolenaar8a677a32020-03-12 19:15:45 +01004389 smsg("%4d %s -> %d", current, when,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004390 iptr->isn_arg.jump.jump_where);
4391 }
4392 break;
4393
4394 case ISN_FOR:
4395 {
4396 forloop_T *forloop = &iptr->isn_arg.forloop;
4397
4398 smsg("%4d FOR $%d -> %d", current,
4399 forloop->for_idx, forloop->for_end);
4400 }
4401 break;
4402
4403 case ISN_TRY:
4404 {
4405 try_T *try = &iptr->isn_arg.try;
4406
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004407 if (try->try_ref->try_finally == 0)
4408 smsg("%4d TRY catch -> %d, endtry -> %d",
4409 current,
4410 try->try_ref->try_catch,
4411 try->try_ref->try_endtry);
4412 else
4413 smsg("%4d TRY catch -> %d, finally -> %d, endtry -> %d",
4414 current,
4415 try->try_ref->try_catch,
4416 try->try_ref->try_finally,
4417 try->try_ref->try_endtry);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004418 }
4419 break;
4420 case ISN_CATCH:
4421 // TODO
4422 smsg("%4d CATCH", current);
4423 break;
Bram Moolenaarc150c092021-02-13 15:02:46 +01004424 case ISN_TRYCONT:
4425 {
4426 trycont_T *trycont = &iptr->isn_arg.trycont;
4427
4428 smsg("%4d TRY-CONTINUE %d level%s -> %d", current,
4429 trycont->tct_levels,
4430 trycont->tct_levels == 1 ? "" : "s",
4431 trycont->tct_where);
4432 }
4433 break;
Bram Moolenaar7e82c5f2021-02-21 21:32:45 +01004434 case ISN_FINALLY:
4435 smsg("%4d FINALLY", current);
4436 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004437 case ISN_ENDTRY:
4438 smsg("%4d ENDTRY", current);
4439 break;
4440 case ISN_THROW:
4441 smsg("%4d THROW", current);
4442 break;
4443
4444 // expression operations on number
4445 case ISN_OPNR:
4446 case ISN_OPFLOAT:
4447 case ISN_OPANY:
4448 {
4449 char *what;
4450 char *ins;
4451
4452 switch (iptr->isn_arg.op.op_type)
4453 {
4454 case EXPR_MULT: what = "*"; break;
4455 case EXPR_DIV: what = "/"; break;
4456 case EXPR_REM: what = "%"; break;
4457 case EXPR_SUB: what = "-"; break;
4458 case EXPR_ADD: what = "+"; break;
4459 default: what = "???"; break;
4460 }
4461 switch (iptr->isn_type)
4462 {
4463 case ISN_OPNR: ins = "OPNR"; break;
4464 case ISN_OPFLOAT: ins = "OPFLOAT"; break;
4465 case ISN_OPANY: ins = "OPANY"; break;
4466 default: ins = "???"; break;
4467 }
4468 smsg("%4d %s %s", current, ins, what);
4469 }
4470 break;
4471
4472 case ISN_COMPAREBOOL:
4473 case ISN_COMPARESPECIAL:
4474 case ISN_COMPARENR:
4475 case ISN_COMPAREFLOAT:
4476 case ISN_COMPARESTRING:
4477 case ISN_COMPAREBLOB:
4478 case ISN_COMPARELIST:
4479 case ISN_COMPAREDICT:
4480 case ISN_COMPAREFUNC:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004481 case ISN_COMPAREANY:
4482 {
4483 char *p;
4484 char buf[10];
4485 char *type;
4486
4487 switch (iptr->isn_arg.op.op_type)
4488 {
4489 case EXPR_EQUAL: p = "=="; break;
4490 case EXPR_NEQUAL: p = "!="; break;
4491 case EXPR_GREATER: p = ">"; break;
4492 case EXPR_GEQUAL: p = ">="; break;
4493 case EXPR_SMALLER: p = "<"; break;
4494 case EXPR_SEQUAL: p = "<="; break;
4495 case EXPR_MATCH: p = "=~"; break;
4496 case EXPR_IS: p = "is"; break;
4497 case EXPR_ISNOT: p = "isnot"; break;
4498 case EXPR_NOMATCH: p = "!~"; break;
4499 default: p = "???"; break;
4500 }
4501 STRCPY(buf, p);
4502 if (iptr->isn_arg.op.op_ic == TRUE)
4503 strcat(buf, "?");
4504 switch(iptr->isn_type)
4505 {
4506 case ISN_COMPAREBOOL: type = "COMPAREBOOL"; break;
4507 case ISN_COMPARESPECIAL:
4508 type = "COMPARESPECIAL"; break;
4509 case ISN_COMPARENR: type = "COMPARENR"; break;
4510 case ISN_COMPAREFLOAT: type = "COMPAREFLOAT"; break;
4511 case ISN_COMPARESTRING:
4512 type = "COMPARESTRING"; break;
4513 case ISN_COMPAREBLOB: type = "COMPAREBLOB"; break;
4514 case ISN_COMPARELIST: type = "COMPARELIST"; break;
4515 case ISN_COMPAREDICT: type = "COMPAREDICT"; break;
4516 case ISN_COMPAREFUNC: type = "COMPAREFUNC"; break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004517 case ISN_COMPAREANY: type = "COMPAREANY"; break;
4518 default: type = "???"; break;
4519 }
4520
4521 smsg("%4d %s %s", current, type, buf);
4522 }
4523 break;
4524
4525 case ISN_ADDLIST: smsg("%4d ADDLIST", current); break;
4526 case ISN_ADDBLOB: smsg("%4d ADDBLOB", current); break;
4527
4528 // expression operations
4529 case ISN_CONCAT: smsg("%4d CONCAT", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004530 case ISN_STRINDEX: smsg("%4d STRINDEX", current); break;
Bram Moolenaar11107ba2020-08-15 21:10:16 +02004531 case ISN_STRSLICE: smsg("%4d STRSLICE", current); break;
Bram Moolenaar1dcae592020-10-19 19:02:42 +02004532 case ISN_LISTAPPEND: smsg("%4d LISTAPPEND", current); break;
Bram Moolenaar80b0e5e2020-10-19 20:45:36 +02004533 case ISN_BLOBAPPEND: smsg("%4d BLOBAPPEND", current); break;
Bram Moolenaarbf9d8c32020-07-19 17:55:44 +02004534 case ISN_LISTINDEX: smsg("%4d LISTINDEX", current); break;
Bram Moolenaared591872020-08-15 22:14:53 +02004535 case ISN_LISTSLICE: smsg("%4d LISTSLICE", current); break;
Bram Moolenaarcc673e72020-08-16 17:33:35 +02004536 case ISN_ANYINDEX: smsg("%4d ANYINDEX", current); break;
4537 case ISN_ANYSLICE: smsg("%4d ANYSLICE", current); break;
Bram Moolenaar9af78762020-06-16 11:34:42 +02004538 case ISN_SLICE: smsg("%4d SLICE %lld",
4539 current, iptr->isn_arg.number); break;
Bram Moolenaar47a519a2020-06-14 23:05:10 +02004540 case ISN_GETITEM: smsg("%4d ITEM %lld",
4541 current, iptr->isn_arg.number); break;
Bram Moolenaar1cc2a942020-05-10 19:10:31 +02004542 case ISN_MEMBER: smsg("%4d MEMBER", current); break;
4543 case ISN_STRINGMEMBER: smsg("%4d MEMBER %s", current,
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004544 iptr->isn_arg.string); break;
4545 case ISN_NEGATENR: smsg("%4d NEGATENR", current); break;
4546
4547 case ISN_CHECKNR: smsg("%4d CHECKNR", current); break;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004548 case ISN_CHECKTYPE:
4549 {
Bram Moolenaare32e5162021-01-21 20:21:29 +01004550 checktype_T *ct = &iptr->isn_arg.type;
Bram Moolenaar5e654232020-09-16 15:22:00 +02004551 char *tofree;
4552
Bram Moolenaare32e5162021-01-21 20:21:29 +01004553 if (ct->ct_arg_idx == 0)
4554 smsg("%4d CHECKTYPE %s stack[%d]", current,
4555 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004556 (int)ct->ct_off);
Bram Moolenaare32e5162021-01-21 20:21:29 +01004557 else
4558 smsg("%4d CHECKTYPE %s stack[%d] arg %d", current,
4559 type_name(ct->ct_type, &tofree),
Bram Moolenaarb3005ce2021-01-22 17:51:06 +01004560 (int)ct->ct_off,
Bram Moolenaare32e5162021-01-21 20:21:29 +01004561 (int)ct->ct_arg_idx);
Bram Moolenaar5e654232020-09-16 15:22:00 +02004562 vim_free(tofree);
4563 break;
4564 }
Bram Moolenaar9af78762020-06-16 11:34:42 +02004565 case ISN_CHECKLEN: smsg("%4d CHECKLEN %s%d", current,
4566 iptr->isn_arg.checklen.cl_more_OK ? ">= " : "",
4567 iptr->isn_arg.checklen.cl_min_len);
4568 break;
Bram Moolenaaraa210a32021-01-02 15:41:03 +01004569 case ISN_SETTYPE:
4570 {
4571 char *tofree;
4572
4573 smsg("%4d SETTYPE %s", current,
4574 type_name(iptr->isn_arg.type.ct_type, &tofree));
4575 vim_free(tofree);
4576 break;
4577 }
Bram Moolenaar2bb26582020-10-03 22:52:39 +02004578 case ISN_COND2BOOL: smsg("%4d COND2BOOL", current); break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004579 case ISN_2BOOL: if (iptr->isn_arg.number)
4580 smsg("%4d INVERT (!val)", current);
4581 else
4582 smsg("%4d 2BOOL (!!val)", current);
4583 break;
Bram Moolenaardb99f9f2020-03-23 22:12:22 +01004584 case ISN_2STRING: smsg("%4d 2STRING stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004585 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar389df252020-07-09 21:20:47 +02004586 break;
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004587 case ISN_2STRING_ANY: smsg("%4d 2STRING_ANY stack[%lld]", current,
Bram Moolenaar82c38fe2021-01-04 10:47:26 +01004588 (varnumber_T)(iptr->isn_arg.number));
Bram Moolenaar418f1df2020-08-12 21:34:49 +02004589 break;
Bram Moolenaar08597872020-12-10 19:43:40 +01004590 case ISN_RANGE: smsg("%4d RANGE %s", current, iptr->isn_arg.string);
4591 break;
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004592 case ISN_PUT:
Bram Moolenaar08597872020-12-10 19:43:40 +01004593 if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE_ABOVE)
4594 smsg("%4d PUT %c above range",
4595 current, iptr->isn_arg.put.put_regname);
4596 else if (iptr->isn_arg.put.put_lnum == LNUM_VARIABLE_RANGE)
4597 smsg("%4d PUT %c range",
4598 current, iptr->isn_arg.put.put_regname);
4599 else
4600 smsg("%4d PUT %c %ld", current,
4601 iptr->isn_arg.put.put_regname,
Bram Moolenaarc3516f72020-09-08 22:45:35 +02004602 (long)iptr->isn_arg.put.put_lnum);
4603 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004604
Bram Moolenaar02194d22020-10-24 23:08:38 +02004605 // TODO: summarize modifiers
4606 case ISN_CMDMOD:
4607 {
4608 char_u *buf;
Bram Moolenaara360dbe2020-10-26 18:46:53 +01004609 size_t len = produce_cmdmods(
Bram Moolenaar02194d22020-10-24 23:08:38 +02004610 NULL, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4611
4612 buf = alloc(len + 1);
4613 if (buf != NULL)
4614 {
4615 (void)produce_cmdmods(
4616 buf, iptr->isn_arg.cmdmod.cf_cmdmod, FALSE);
4617 smsg("%4d CMDMOD %s", current, buf);
4618 vim_free(buf);
4619 }
4620 break;
4621 }
4622 case ISN_CMDMOD_REV: smsg("%4d CMDMOD_REV", current); break;
Bram Moolenaarf4c6e1e2020-10-23 18:02:32 +02004623
Bram Moolenaarb2049902021-01-24 12:53:53 +01004624 case ISN_PROF_START:
4625 smsg("%4d PROFILE START line %d", current, iptr->isn_lnum);
4626 break;
4627
4628 case ISN_PROF_END:
4629 smsg("%4d PROFILE END", current);
4630 break;
4631
Bram Moolenaar792f7862020-11-23 08:31:18 +01004632 case ISN_UNPACK: smsg("%4d UNPACK %d%s", current,
4633 iptr->isn_arg.unpack.unp_count,
4634 iptr->isn_arg.unpack.unp_semicolon ? " semicolon" : "");
4635 break;
Bram Moolenaar389df252020-07-09 21:20:47 +02004636 case ISN_SHUFFLE: smsg("%4d SHUFFLE %d up %d", current,
4637 iptr->isn_arg.shuffle.shfl_item,
4638 iptr->isn_arg.shuffle.shfl_up);
4639 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004640 case ISN_DROP: smsg("%4d DROP", current); break;
4641 }
Bram Moolenaar793dcc52020-08-15 13:49:17 +02004642
4643 out_flush(); // output one line at a time
4644 ui_breakcheck();
4645 if (got_int)
4646 break;
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004647 }
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004648}
4649
4650/*
Bram Moolenaar13106602020-10-04 16:06:05 +02004651 * Return TRUE when "tv" is not falsy: non-zero, non-empty string, non-empty
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004652 * list, etc. Mostly like what JavaScript does, except that empty list and
4653 * empty dictionary are FALSE.
4654 */
4655 int
4656tv2bool(typval_T *tv)
4657{
4658 switch (tv->v_type)
4659 {
4660 case VAR_NUMBER:
4661 return tv->vval.v_number != 0;
4662 case VAR_FLOAT:
4663#ifdef FEAT_FLOAT
4664 return tv->vval.v_float != 0.0;
4665#else
4666 break;
4667#endif
4668 case VAR_PARTIAL:
4669 return tv->vval.v_partial != NULL;
4670 case VAR_FUNC:
4671 case VAR_STRING:
4672 return tv->vval.v_string != NULL && *tv->vval.v_string != NUL;
4673 case VAR_LIST:
4674 return tv->vval.v_list != NULL && tv->vval.v_list->lv_len > 0;
4675 case VAR_DICT:
4676 return tv->vval.v_dict != NULL
4677 && tv->vval.v_dict->dv_hashtab.ht_used > 0;
4678 case VAR_BOOL:
4679 case VAR_SPECIAL:
4680 return tv->vval.v_number == VVAL_TRUE ? TRUE : FALSE;
4681 case VAR_JOB:
4682#ifdef FEAT_JOB_CHANNEL
4683 return tv->vval.v_job != NULL;
4684#else
4685 break;
4686#endif
4687 case VAR_CHANNEL:
4688#ifdef FEAT_JOB_CHANNEL
4689 return tv->vval.v_channel != NULL;
4690#else
4691 break;
4692#endif
4693 case VAR_BLOB:
4694 return tv->vval.v_blob != NULL && tv->vval.v_blob->bv_ga.ga_len > 0;
4695 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +02004696 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004697 case VAR_VOID:
4698 break;
4699 }
4700 return FALSE;
4701}
4702
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004703 void
4704emsg_using_string_as(typval_T *tv, int as_number)
4705{
4706 semsg(_(as_number ? e_using_string_as_number_str
4707 : e_using_string_as_bool_str),
4708 tv->vval.v_string == NULL
4709 ? (char_u *)"" : tv->vval.v_string);
4710}
4711
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004712/*
4713 * If "tv" is a string give an error and return FAIL.
4714 */
4715 int
4716check_not_string(typval_T *tv)
4717{
4718 if (tv->v_type == VAR_STRING)
4719 {
Bram Moolenaarea2d4072020-11-12 12:08:51 +01004720 emsg_using_string_as(tv, TRUE);
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004721 clear_tv(tv);
4722 return FAIL;
4723 }
4724 return OK;
4725}
4726
Bram Moolenaar8a7d6542020-01-26 15:56:19 +01004727
4728#endif // FEAT_EVAL